private void HandleNotification(ushort intent, byte[] data) { if (_notificationMapping == null) // device isn't registered yet throw new InvalidOperationException("Device sent notification without registration"); NotificationMetadata notificationMetadata; if (!_notificationMapping.TryGetValue(intent, out notificationMetadata)) throw new InvalidOperationException("Unsupported intent: " + intent); JToken parameters; using (var stream = new MemoryStream(data)) using (var reader = new BinaryReader(stream)) parameters = ReadParameterValue(reader, notificationMetadata.Parameters); var notification = new Notification(notificationMetadata.Name, parameters); HandleHotification(notification); }
/// <summary> /// Sends a notification on behalf of the specified device. /// </summary> /// <param name="sender">Sender <see cref="DeviceBase"/> object.</param> /// <param name="notification"><see cref="DeviceNotification"/> object to send.</param> public void SendNotification(DeviceBase sender, DeviceNotification notification) { if (sender == null) throw new ArgumentNullException("sender"); if (notification == null) throw new ArgumentNullException("notification"); Logger.InfoFormat("Sending notification '{0}' from device {1} ({2})", notification.Name, sender.ID, sender.Name); try { var cNotification = new Notification(notification.Name.Trim(), notification.Parameters == null ? null : notification.Parameters.DeepClone()); DeviceClient.SendNotification(sender.ID, sender.Key, cNotification); } catch (Exception ex) { // critical error - log and fault the service Logger.Error(string.Format("Exception while sending notification '{0}' from device {1} ({2})", notification.Name, sender.ID, sender.Name), ex); throw; } }
/// <summary> /// Override it to handle device custom notification in the service specific way /// </summary> protected abstract void HandleHotification(Notification notification);
protected override void HandleHotification(DeviceHive.Device.Notification notification) { throw new NotImplementedException(); }