private void RegisterDevice(DeviceBase device) { Logger.InfoFormat("Registering device {0} ({1})", device.ID, device.Name); try { var cDeviceClass = new DeviceClass(device.ClassName, device.ClassVersion, device.ClassOfflineTimeout, device.ClassData == null ? null : JToken.FromObject(device.ClassData, device.JsonSerializer)); var cDevice = new Device(device.ID, device.Key, device.Name, device.Status, device.Data == null ? null : JToken.FromObject(device.Data, device.JsonSerializer), Network, cDeviceClass); cDevice.Equipment = device.EquipmentInfo.Select(e => new Equipment( e.Name, e.Code, e.Type, e.Data == null ? null : JToken.FromObject(e.Data, device.JsonSerializer))).ToList(); DeviceClient.RegisterDevice(cDevice); } catch (Exception ex) { // critical error - log and fault the service Logger.Error(string.Format("Exception while registering device {0} ({1}), rethrowing exception", device.ID, device.Name), ex); throw; } }
/// <summary> /// Sends a device status update. /// </summary> /// <param name="sender">Sender <see cref="DeviceBase"/> object.</param> /// <param name="status">New device status.</param> public void SendStatusUpdate(DeviceBase sender, string status) { if (sender == null) throw new ArgumentNullException("sender"); if (string.IsNullOrEmpty(status)) throw new ArgumentException("Status is null or empty!", "status"); Logger.InfoFormat("Updating device {1} ({2}) status to '{0}'", status, sender.ID, sender.Name); try { var cDevice = new Device(sender.ID, sender.Key) { Status = status }; DeviceClient.UpdateDevice(cDevice); } catch (Exception ex) { // critical error - log and fault the service Logger.Error(string.Format("Exception while updating device {1} ({2}) status to '{0}'", status, sender.ID, sender.Name), ex); throw; } }