private async void UpdateDevicesAsync() { for (; ;) { var updatedZWayDevices = await GetDeviceDataAsync(_lastDeviceUpdate); var updatedZWaveDevices = new List <IDevice>(); foreach (var device in updatedZWayDevices.devices) { if (!_zWayDeviceDictonary.ContainsKey(device.id)) { _logger.LogWarning($"Cannot update ZWayDevice with id '{device.id}' since it is not found in device dictionary"); } var storedDevice = _zWayDeviceDictonary[device.id]; // Only trigger update if device has a parent device if (storedDevice.ParentDevice != null && storedDevice.UpdateMetrics(device.metrics) && !updatedZWaveDevices.Contains(storedDevice.ParentDevice)) { storedDevice.ParentDevice.LastChanged = DateTime.Now; updatedZWaveDevices.Add(storedDevice.ParentDevice); } } if (updatedZWaveDevices.Count > 0 && OnDevicesUpdated != null) { OnDevicesUpdated.Invoke(this, new DevicesUpdatedEventArgs(updatedZWaveDevices.ToArray())); } await Task.Delay(1000); } }
private IntPtr HwndHandler(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled) { // Only checking changed event since it gets called when devices are added and removed // while remove notifications don't always get called. if (msg == WmDevicechange) { if ((int)wparam == DbtDeviceArrival || (int)wparam == DbtDeviceRemoveComplete) { if (lparam != IntPtr.Zero) { //Get Device name DevBroadcastDeviceInterface deviceInterface = (DevBroadcastDeviceInterface)Marshal.PtrToStructure(lparam, typeof(DevBroadcastDeviceInterface)); string deviceName = ""; deviceName = System.Text.Encoding.Unicode.GetString(deviceInterface.Name); //convert byte array into unicode deviceName = deviceName.Replace("\0", string.Empty); //removes all unicode null "\0" charecters if (deviceName.Contains("057e")) // nintendo VID = 057e { OnDevicesUpdated?.Invoke(); } } } } handled = false; return(IntPtr.Zero); }
private IntPtr HwndHandler(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled) { // Only checking changed event since it gets called when devices are added and removed // while remove notifications don't always get called. if (msg == WmDevicechange && (int)wparam == DbtDevNodesChanged) { OnDevicesUpdated?.Invoke(); } handled = false; return(IntPtr.Zero); }
public void SetAutomated(string deviceId, bool automated) { if (!_devices.ContainsKey(deviceId)) { throw new ArgumentException($"No device with id '{deviceId}'"); } var device = _devices[deviceId]; device.Automated = automated; device.LastChanged = DateTime.Now; OnDevicesUpdated?.Invoke(this, new DevicesUpdatedEventArgs(new IDevice[] { device })); }
/// <summary> /// When a new device is added to the watcher, add it to the collection /// </summary> /// <param name="sender"></param> /// <param name="deviceInfo"></param> private static async void Watcher_Added(DeviceWatcher sender, DeviceInformation deviceInfo) { // We must update the collection on the UI thread await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Protect against race condition if the task runs after the app stopped the deviceWatcher. if (sender == watcher) { // if the entry doesnt already exist in the dictionary, if (!InformationCollection.ContainsKey(deviceInfo.Id)) { InformationCollection.Add(deviceInfo.Id, new GattInformation(deviceInfo)); } } }); OnDevicesUpdated?.Invoke(InformationCollection, DeviceUpdate.Added); }
public void SetAutomated(string deviceId, bool automated) { if (!_zWaveDeviceDictonary.ContainsKey(deviceId)) { throw new ArgumentException($"No device found with id '{deviceId}'"); } var device = _zWaveDeviceDictonary[deviceId] as ZWavePowerPlugDevice; if (device == null) { throw new ArgumentException($"Device must be of type IToggleDevice"); } device.Automated = automated; device.LastChanged = DateTime.Now; OnDevicesUpdated?.Invoke(this, new DevicesUpdatedEventArgs(new IDevice[] { device })); }
private void OnChildDeviceChanged(DeviceGroup deviceGroup) { var aggregatedToggleState = 0; // Calculate if the devices in the group is primarly toggled or untoggled foreach (var device in deviceGroup.Devices) { aggregatedToggleState += device.Toggled ? 1 : -1; } var newToggleState = aggregatedToggleState >= 0; if (newToggleState != deviceGroup.Toggled) { deviceGroup.Toggled = newToggleState; deviceGroup.LastChanged = DateTime.Now; OnDevicesUpdated?.Invoke(this, new DevicesUpdatedEventArgs(new IDevice[] { deviceGroup })); } }
private IntPtr HwndHandler(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled) { // Only checking changed event since it gets called when devices are added and removed // while remove notifications don't always get called. if (msg == WmDevicechange) { if (lparam != IntPtr.Zero) { DEV_BROADCAST_HANDLE dbh = (DEV_BROADCAST_HANDLE)Marshal.PtrToStructure(lparam, typeof(DEV_BROADCAST_HANDLE)); if (dbh.dbch_eventguid == GUID_BLUETOOTH_HCI_EVENT) { // BTH_HCI_EVENT_INFO OnDevicesUpdated?.Invoke(); } else if (dbh.dbch_eventguid == GUID_BLUETOOTH_L2CAP_EVENT) { // BTH_L2CAP_EVENT_INFO (not needed as controller connection triggers both HCI & L2CAP EVENTS) } } } handled = false; return(IntPtr.Zero); }