/// <summary> /// Called when information on a discovered BLE device has changed /// </summary> /// <param name="sender">Device watcher which discovered the device</param> /// <param name="device_info">BLE device information</param> private void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate device_info) { // Retrieve device information BleDeviceInformation ble_device_info = new BleDeviceInformation(); ble_device_info.Id = device_info.Id; try { ble_device_info.MacAddress = device_info.Properties["System.Devices.Aep.DeviceAddress"].ToString(); } catch (KeyNotFoundException) { ble_device_info.MacAddress = ""; } try { ble_device_info.IsConnected = (bool)device_info.Properties["System.Devices.Aep.IsConnected"]; } catch (KeyNotFoundException) { ble_device_info.IsConnected = false; } try { ble_device_info.IsConnectable = (bool)device_info.Properties["System.Devices.Aep.Bluetooth.Le.IsConnectable"]; } catch (KeyNotFoundException) { ble_device_info.IsConnectable = false; } // Notify device update DeviceUpdated?.Invoke(this, ble_device_info); }
private void HandleMessage(Message message) { if (message == null) { return; } switch (message?.ResponseMessage) { case "newDeviceValue": DeviceUpdated?.Invoke(this, message.Response.ToObject <DeviceValue>()); break; case "newSwitchingSequenceInfo": _logger.LogInformation($"Received Switch Sequence Info: {message.Response["switchingSequenceID"]}, nextActivationTime: {message.Response["nextActivationTime"]}"); break; case "newSwitchingSequenceStatus": _logger.LogInformation($"Received Switch Sequence: {message.Response["switchingSequenceID"]}, status: {message.Response["status"]}"); break; case "newDeviceInfo": _logger.LogInformation($"Received new device: {message.Response}"); break; default: _messageQueue.Enqueue(message); break; } }
public bool UpdateTask(CanMsg msg) { if (CanDb.GetNodeTypeId(msg.ArbId) == CanDb.Instance.Nodes.FirstOrDefault(n => n.Name == NodeCollection.NODE_MCEL).NodeTypeId) { byte node = CanDb.GetNodeAddress(msg.ArbId); byte msgId = CanDb.GetMsgId(msg.ArbId); if (Devices.FirstOrDefault(n => n.Address == node) is IDevice item) { item.Update(msgId, msg.Data); DeviceUpdated?.Invoke(this, item); } else { var newitem = new MCEL181123DeviceItem(node, msgId, msg.Data); Devices.Add(newitem); NewDeviceArrived?.Invoke(this, newitem); } return(true); } else { return(false); } }
private void ClientDeviceUpdated(object sender, DeviceValue value) { var masterId = value.MasterDeviceId == 0 ? value.DeviceId : value.MasterDeviceId; var device = GetDevice(masterId); if (device?.SetValues(value) ?? false) { DeviceUpdated?.Invoke(this, value); } }
public void HandleData(GlobalUpdate eventData) { GlobalUpdated?.Invoke(this, new NestGlobalEventArgs(eventData)); DeviceUpdated?.Invoke(this, new NestDeviceEventArgs(eventData.Devices)); StructureUpdated?.Invoke(this, new NestStructureEventArgs(eventData.Structures)); ThermostatUpdated?.Invoke(this, new NestThermostatEventArgs(eventData.Thermostats)); CameraUpdated?.Invoke(this, new NestCameraEventArgs(eventData.Cameras)); SmokeCOAlarmUpdated?.Invoke(this, new NestSmokeCOAlarmEventArgs(eventData.SmokeCOAlarms)); MetadataUpdated?.Invoke(this, new NestMetadataEventArgs(eventData.Metadata)); }
private void StartScannerThread() { EndPoint localEp = _localEp; _cancellationTokenSource = new CancellationTokenSource(); CancellationToken ct = _cancellationTokenSource.Token; _scannerThread = Task.Run(async() => { while (!ct.IsCancellationRequested) { try { byte[] response = new byte[8000]; int no = _socket.ReceiveFrom(response, ref localEp); string str = Encoding.UTF8.GetString(Decrypt(response.Take(no).ToArray())); JObject obj = ParserHelpers.ParseGetSysInfo(str); if (obj == null) { continue; } DeviceType deviceType = ParserHelpers.GetDeviceType(obj); if (GetDeviceTypeFilter()?.Contains(deviceType) == false) { Debug.WriteLine("Excluded device"); continue; } var requestContext = new RequestContext(obj, (localEp as IPEndPoint)?.Address); DeviceStateInfo state = await _deviceManager.AddOrUpdate(requestContext).ConfigureAwait(false); switch (state.State) { case DeviceState.Added: DeviceDiscovered?.Invoke(this, new DeviceEventArgs(state.Device)); break; case DeviceState.Updated: DeviceUpdated?.Invoke(this, new DeviceEventArgs(state.Device)); break; } } catch (Exception e) { Debug.WriteLine(e); } } }, ct); }
public bool TryUpdateDevice(DeviceType device, DeviceState state, out DeviceStatus status) { if (_devices.TryGetValue(device, out status) && status.State != state) { status = status.Update(state, DateTime.Now); _devices[device] = status; DeviceUpdated?.Invoke(this, status); return(true); } return(false); }
private void Reader(object input) { try { var token = (CancellationToken)input; while (!token.IsCancellationRequested) { var buffer = new byte[2048]; var messageData = new StringBuilder(); int bytes; do { bytes = _stream.ReadAsync(buffer, 0, buffer.Length, token).Result; var decoder = Encoding.UTF8.GetDecoder(); var chars = new char[decoder.GetCharCount(buffer, 0, bytes)]; decoder.GetChars(buffer, 0, bytes, chars, 0); messageData.Append(chars); if (chars.Last() == '\n') { break; } } while (bytes != 0 && !token.IsCancellationRequested); var message = Deserialize <Message>(messageData.ToString()); if (message.ResponseMessage == "newDeviceValue") { _logger.LogInformation($"Received Status: {messageData}"); DeviceUpdated?.Invoke(this, message.Response.ToObject <DeviceValue>()); } else { _messageQueue.Enqueue(message); } } } catch (Exception e) { //Only log if still connected. if (Connected) { _logger.LogError(e, "Connection closed"); Close().ConfigureAwait(false); } } }
public bool Update(Device device) { if (device == null) { return(false); } if (GetDevice(device.DeviceType, device.DeviceIndex) == null) { Logger.Info(string.Format("update device failed, no this device[{0}] in device group", device.GetDeviceName())); return(false); } if (DeviceUpdated != null) { DeviceUpdated.Invoke(device, null); } return(true); }
protected virtual void OnDeviceUpdated(Events.DeviceUpdatedEventArgs e) { DeviceUpdated?.Invoke(this, e); }
internal Task InternalDeviceUpdatedAsync(Device before, Device after) { return(DeviceUpdated is null ? Task.CompletedTask : DeviceUpdated.Invoke(before, after)); }
protected virtual void OnDeviceUpdated(object source, EventArgs e) { DeviceUpdated?.Invoke(this, EventArgs.Empty); }
private void OnDeviceUpdated(DeviceWatcher sender, DeviceInformationUpdate args) { DeviceUpdated.Invoke(this, null); RaisePropertyChanged(new PropertyChangedEventArgs(nameof(RemoteDevices))); }