/// <summary> /// We only support update twin /// </summary> /// <param name="device"></param> /// <returns></returns> public async Task <DeviceServiceModel> CreateOrUpdateAsync(DeviceServiceModel device) { // validate device module var azureDevice = await this.registry.GetDeviceAsync(device.Id); if (azureDevice == null) { azureDevice = await this.registry.AddDeviceAsync(device.ToAzureModel()); } Twin azureTwin; if (device.Twin == null) { azureTwin = await this.registry.GetTwinAsync(device.Id); } else { azureTwin = await this.registry.UpdateTwinAsync(device.Id, device.Twin.ToAzureModel(), device.Twin.ETag); // Update the deviceGroupFilter cache, no need to wait var unused = this.configService.UpdateDeviceGroupFiltersAsync(device.Twin); } return(new DeviceServiceModel(azureDevice, azureTwin, this.ioTHubHostName)); }
public async Task <DeviceServiceModel> CreateAsync(DeviceServiceModel device) { if (device.IsEdgeDevice && device.Authentication != null && !device.Authentication.AuthenticationType.Equals(AuthenticationType.Sas)) { throw new InvalidInputException("Edge devices only support symmetric key authentication."); } // auto generate DeviceId, if missing if (string.IsNullOrEmpty(device.Id)) { device.Id = Guid.NewGuid().ToString(); } var azureDevice = await this.registry.AddDeviceAsync(device.ToAzureModel()); Twin azureTwin; if (device.Twin == null) { azureTwin = await this.registry.GetTwinAsync(device.Id); } else { azureTwin = await this.registry.UpdateTwinAsync(device.Id, device.Twin.ToAzureModel(), "*"); } return(new DeviceServiceModel(azureDevice, azureTwin, this.ioTHubHostName)); }
public async Task <DeviceServiceModel> CreateAsync(DeviceServiceModel device) { if (device.IsEdgeDevice && device.Authentication != null && !device.Authentication.AuthenticationType.Equals(AuthenticationType.Sas)) { throw new InvalidInputException("Edge devices only support symmetric key authentication."); } // auto generate DeviceId, if missing if (string.IsNullOrEmpty(device.Id)) { device.Id = Guid.NewGuid().ToString(); } var azureDevice = await this.tenantConnectionHelper.GetRegistry().AddDeviceAsync(device.ToAzureModel()); Twin azureTwin; if (device.Twin == null) { azureTwin = await this.tenantConnectionHelper.GetRegistry().GetTwinAsync(device.Id); } else { azureTwin = await this.tenantConnectionHelper.GetRegistry().UpdateTwinAsync(device.Id, device.Twin.ToAzureModel(), "*"); } await this.asaManager.BeginDeviceGroupsConversionAsync(); return(new DeviceServiceModel(azureDevice, azureTwin, this.tenantConnectionHelper.GetIotHubName())); }
private DeviceServiceModel CreateDeviceTwinServiceModel(ValueApiModel input, DeviceServiceModel data) { TwinServiceModel output = JsonConvert.DeserializeObject <TwinServiceModel>(input.Data); data.Twin = output; return(data); }
public async Task InvalidAuthenticationTypeForEdgeDevice(string authTypeString) { // Arrange var authType = Enum.Parse <AuthenticationType>(authTypeString); var auth = new AuthenticationMechanismServiceModel() { AuthenticationType = authType, }; DeviceServiceModel model = new DeviceServiceModel( etag: "etag", id: "deviceId", c2DMessageCount: 0, lastActivity: DateTime.Now, connected: true, enabled: true, isEdgeDevice: true, lastStatusUpdated: DateTime.Now, twin: null, ioTHubHostName: this.ioTHubHostName, authentication: auth); // Act & Assert await Assert.ThrowsAsync <InvalidInputException>(async() => await this.devices.CreateAsync(model)); }
public DeviceRegistryApiModel(DeviceServiceModel device) { if (device == null) { return; } this.Id = device.Id; this.ETag = device.Etag; this.C2DMessageCount = device.C2DMessageCount; this.LastActivity = device.LastActivity; this.Connected = device.Connected; this.Enabled = device.Enabled; this.IsEdgeDevice = device.IsEdgeDevice; this.LastStatusUpdated = device.LastStatusUpdated; this.IoTHubHostName = device.IoTHubHostName; this.Authentication = new AuthenticationMechanismApiModel( device.Authentication ?? new AuthenticationMechanismServiceModel()); if (device.Twin != null) { this.ETag = $"{this.ETag}|{device.Twin.ETag}"; this.Properties = new TwinPropertiesApiModel(device.Twin.DesiredProperties, device.Twin.ReportedProperties); this.Tags = device.Twin.Tags; this.IsSimulated = device.Twin.IsSimulated; } }
private DeviceServiceModel CreateDeviceServiceModel(ValueApiModel input) { DeviceServiceModel output = JsonConvert.DeserializeObject <DeviceServiceModel>(input.Data); output.Id = input.objectid; output.Etag = input.ETag; output.IoTHubHostName = "iCuboid"; return(output); }
public async Task UpdateDeviceThrowsResourceNotFound() { this.registryMock .Setup(x => x.GetDeviceAsync( It.IsAny <string>())) .ReturnsAsync((Device)null); var model = new DeviceServiceModel( CreateTestDevice("deviceId", false), CreateTestTwin(0, false), "iotHubHostName", false); await Assert.ThrowsAsync <ResourceNotFoundException>(async() => await this.devices.UpdateAsync(model, null)); }
public async Task <DeviceServiceModel> CreateWithDesiredAsync(DeviceServiceModel device, string twinPatch) { // auto generate DeviceId, if missing if (string.IsNullOrEmpty(device.Id)) { device.Id = Guid.NewGuid().ToString(); } Device azureDevice = await registry.AddDeviceAsync(device.ToAzureModel()); Twin azureTwin; azureTwin = await registry.UpdateTwinAsync(device.Id, twinPatch, "*"); return(new DeviceServiceModel(azureDevice, azureTwin, ioTHubHostName)); }
static void Main(string[] args) { if (args.Length > 2) { Device newAzureDevice = new Device(); Twin newAzureTwin = new Twin(); DeviceServiceModel newDevice = new DeviceServiceModel(newAzureDevice, newAzureTwin, args[0]); RegistryManager newRegistry = RegistryManager.CreateFromConnectionString(args[0]); Devices newDevices = new Devices(newRegistry, args[1]); newDevices.CreateAsync(newDevice).Wait(); } }
public async Task <DeviceServiceModel> CreateOrUpdateAsync(DeviceServiceModel device, DevicePropertyDelegate devicePropertyDelegate) { // validate device module var azureDevice = await this.tenantConnectionHelper.GetRegistry().GetDeviceAsync(device.Id); if (azureDevice == null) { azureDevice = await this.tenantConnectionHelper.GetRegistry().AddDeviceAsync(device.ToAzureModel()); } Twin azureTwin; if (device.Twin == null) { azureTwin = await this.tenantConnectionHelper.GetRegistry().GetTwinAsync(device.Id); } else { azureTwin = await this.tenantConnectionHelper.GetRegistry().UpdateTwinAsync(device.Id, device.Twin.ToAzureModel(), device.Twin.ETag); // Update the deviceGroupFilter cache, no need to wait var model = new DevicePropertyServiceModel(); var tagRoot = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(device.Twin.Tags)) as JToken; if (tagRoot != null) { model.Tags = new HashSet <string>(tagRoot.GetAllLeavesPath()); } var reportedRoot = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(device.Twin.ReportedProperties)) as JToken; if (reportedRoot != null) { model.Reported = new HashSet <string>(reportedRoot.GetAllLeavesPath()); } var unused = devicePropertyDelegate(model); } return(new DeviceServiceModel(azureDevice, azureTwin, this.tenantConnectionHelper.GetIotHubName())); }
public async Task <DeviceServiceModel> UpdateAsync(DeviceServiceModel device, DevicePropertyDelegate devicePropertyDelegate) { // validate device module var azureDevice = await this.tenantConnectionHelper.GetRegistry().GetDeviceAsync(device.Id); if (azureDevice == null) { throw new ResourceNotFoundException($"Device {device.Id} could not be found on this tenant's IoT Hub. You must create the device first before calling the update method."); } Twin azureTwin; if (device.Twin == null) { azureTwin = await this.tenantConnectionHelper.GetRegistry().GetTwinAsync(device.Id); } else { azureTwin = await this.tenantConnectionHelper.GetRegistry().UpdateTwinAsync(device.Id, device.Twin.ToAzureModel(), device.Twin.ETag); // Update the deviceGroupFilter cache, no need to wait var model = new DevicePropertyServiceModel(); if (JsonConvert.DeserializeObject(JsonConvert.SerializeObject(device.Twin.Tags)) is JToken tagRoot) { model.Tags = new HashSet <string>(tagRoot.GetAllLeavesPath()); } if (JsonConvert.DeserializeObject(JsonConvert.SerializeObject(device.Twin.ReportedProperties)) is JToken reportedRoot) { model.Reported = new HashSet <string>(reportedRoot.GetAllLeavesPath()); } _ = devicePropertyDelegate(model); } await this.asaManager.BeginDeviceGroupsConversionAsync(); return(new DeviceServiceModel(azureDevice, azureTwin, this.tenantConnectionHelper.GetIotHubName())); }
/// <summary> /// We only support update twin /// </summary> /// <param name="device"></param> /// <param name="devicePropertyDelegate"></param> /// <returns></returns> public async Task <DeviceServiceModel> CreateOrUpdateAsync(DeviceServiceModel device, DevicePropertyDelegate devicePropertyDelegate) { // validate device module Device azureDevice = await registry.GetDeviceAsync(device.Id); if (azureDevice == null) { azureDevice = await registry.AddDeviceAsync(device.ToAzureModel()); } Twin azureTwin; if (device.Twin == null) { azureTwin = await registry.GetTwinAsync(device.Id); } else { azureTwin = await registry.UpdateTwinAsync(device.Id, device.Twin.ToAzureModel(), device.Twin.ETag); // Update the deviceGroupFilter cache, no need to wait DevicePropertyServiceModel model = new DevicePropertyServiceModel(); JToken tagRoot = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(device.Twin.Tags)) as JToken; if (tagRoot != null) { model.Tags = new HashSet <string>(tagRoot.GetAllLeavesPath()); } JToken reportedRoot = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(device.Twin.ReportedProperties)) as JToken; if (reportedRoot != null) { model.Reported = new HashSet <string>(reportedRoot.GetAllLeavesPath()); } Task <DevicePropertyServiceModel> unused = devicePropertyDelegate(model); } return(new DeviceServiceModel(azureDevice, azureTwin, ioTHubHostName)); }
public async Task <DeviceServiceModel> CreateAsync(DeviceServiceModel device) { // auto generate DeviceId, if missing if (string.IsNullOrEmpty(device.Id)) { device.Id = Guid.NewGuid().ToString(); } var azureDevice = await this.registry.AddDeviceAsync(device.ToAzureModel()); Twin azureTwin; if (device.Twin == null) { azureTwin = await this.registry.GetTwinAsync(device.Id); } else { azureTwin = await this.registry.UpdateTwinAsync(device.Id, device.Twin.ToAzureModel(), "*"); } return(new DeviceServiceModel(azureDevice, azureTwin, this.ioTHubHostName)); }
private DeviceServiceModel CreateDeviceTwinListServiceModel(TwinServiceModel input, DeviceServiceModel data) { data.Twin = input; return(data); }
public void AddNewDevice(DeviceServiceModel device, int userId) { throw new NotImplementedException(); }
public async Task <DeviceServiceModel> CreateAsync(DeviceServiceModel device) { try { if (device.IsEdgeDevice && device.Authentication != null && !device.Authentication.AuthenticationType.Equals(AuthenticationType.Sas)) { throw new InvalidInputException("Edge devices only support symmetric key authentication."); } // auto generate DeviceId, if missing if (string.IsNullOrEmpty(device.Id)) { device.Id = Guid.NewGuid().ToString(); } var value = JsonConvert.SerializeObject(device, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); var CreateDeviceResponse = await this.client.CreateAsync(DEVICE_COLLECTION_ID, value); var iotDevice = this.CreateDeviceServiceModel(CreateDeviceResponse); JObject jsonObject = new JObject { { "DeviceId", iotDevice.Id }, { "ModuleId", iotDevice.Id }, { "Tags", new JObject { { "Location", "chennai" }, { "Floor", 2 }, { "Campus", "office" }, } }, { "ReportedProperties", new JObject { { "SupportedMethods", "Reboot,FirmwareUpdate,EmergencyValveRelease,IncreasePressure" }, { "telemetry", new JObject { { "MessageTemplate", "{\"floor\":${floor},\"vibration\":${vibration},\"vibration_unit\":\"${vibration_unit}\",\"temperature\":${temperature},\"temperature_unit\":\"${temperature_unit}\"}" }, { "MessageSchema", new JObject { { "Name", "JOBS" }, { "Format", DateTime.UtcNow }, { "Fields", new JObject { { "floor", "integer" }, { "vibration", "integer" }, { "vibration_unit", "integer" }, } }, } }, } }, { "type", "JOBS" }, { "firmware", new JObject { { "currentFwVersion", "JOBS" }, { "lastFwUpdateStartTime", DateTime.UtcNow }, { "lastFwUpdateEndTime", DateTime.UtcNow }, } } } }, { "DesiredProperties", new JObject { { "telemetry", "JOBS" }, { "type", "JOBS" }, { "firmware", new JObject { { "currentFwVersion", "JOBS" }, { "lastFwUpdateStartTime", DateTime.UtcNow }, { "lastFwUpdateEndTime", DateTime.UtcNow }, } } } } }; var valuetwin = JsonConvert.SerializeObject(jsonObject, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); var deviceTwin = await this.client.CreateAsync(DEVICE_TWIN_COLLECTION_ID, valuetwin); var TwinDevice = this.CreateDeviceTwinServiceModel(deviceTwin, iotDevice); return(TwinDevice); } catch (Exception ex) { throw ex; } }
public async Task <bool> UpdateDeviceTwinAsync(DeviceServiceModel device) { Twin azureTwin = await registry.UpdateTwinAsync(device.Id, device.Twin.ToAzureModel(), "*"); return(azureTwin != null); }