Exemple #1
0
 private async Task CheckDeviceAsync(string deviceId)
 {
     if (_whiteList.ContainsKey(deviceId))
         return;
     if (_serviceSettings == null)
         return;
     var connectionString = string.Format("HostName={0}.azure-devices.net;SharedAccessKeyName={1};SharedAccessKey={2}",
                     _serviceSettings.Host, _serviceSettings.KeyName, _serviceSettings.Key);
     var registry = Microsoft.Azure.Devices.RegistryManager.CreateFromConnectionString(connectionString);
     var deviceReg = await registry.GetDeviceAsync(deviceId);
     if (deviceReg == null)
     {
         deviceReg = new Microsoft.Azure.Devices.Device(deviceId);
         deviceReg = await registry.AddDeviceAsync(deviceReg);
     }
     var device = new DeviceModel();
     device.DeviceId = deviceId;
     //device.DeviceRegId = deviceReg.Id;
     device.Key = deviceReg.Authentication.SymmetricKey.PrimaryKey;
     _whiteList.Add(deviceId, device);
     var data = new DeviceMetaData();
     data.Version = "1.0";
     data.IsSimulatedDevice = false;
     data.Properties.DeviceID = deviceId;
     data.Properties.FirmwareVersion = "42";
     data.Properties.HubEnabledState = true;
     data.Properties.Processor = "Foo";
     data.Properties.Platform = "Yep";
     data.Properties.SerialNumber = "Sigfox-" + deviceId;
     data.Properties.InstalledRAM = "1 MB";
     data.Properties.ModelNumber = "007-BOND";
     data.Properties.Manufacturer = "Sigfox";
     //data.Properties.UpdatedTime = DateTime.UtcNow;
     data.Properties.DeviceState = DeviceState.Normal;
     var content = JsonConvert.SerializeObject(data);
     connectionString = string.Format("HostName={0}.azure-devices.net;DeviceId={1};SharedAccessKey={2}",
         "sigfoxmonitoring", device.DeviceId, device.Key);
     _client = DeviceClient.CreateFromConnectionString(connectionString, TransportType.Http1);
     await _client.SendEventAsync(new Message(Encoding.UTF8.GetBytes(content)));
 }
Exemple #2
0
 private static async Task CheckDeviceAsync(string deviceId)
 {
     if (_whiteList.ContainsKey(deviceId))
         return;
     //var connectionString = IotHubConnectionStringBuilder.Create("sigfoxmonitoring",
     //    AuthenticationMethodFactory.CreateAuthenticationWithSharedAccessPolicyKey(deviceId, "registryReadWrite", "weAJyh51pxOk5f/OVZRGXc4JR6AJDg+JYjiK3rlDTzs="));
     var connectionString = string.Format("HostName={0}.azure-devices.net;SharedAccessKeyName={1};SharedAccessKey={2}",
                     "sigfoxmonitoring", "iothubowner", "B2LsypvIEz7bdy0217QYfeUvO1xUjKVujlte4wETrvM=");
     var registry = Microsoft.Azure.Devices.RegistryManager.CreateFromConnectionString(connectionString);
     await registry.RemoveDeviceAsync("Sigfox-FAE88");
     return;
     var deviceReg = await registry.GetDeviceAsync(deviceId);
     if (deviceReg == null)
     {
         deviceReg = new Microsoft.Azure.Devices.Device(deviceId);
         deviceReg = await registry.AddDeviceAsync(deviceReg);
     }
     var device = new DeviceModel();
     device.DeviceId = deviceId;
     //device.DeviceRegId = deviceReg.Id;
     device.Key = deviceReg.Authentication.SymmetricKey.PrimaryKey;
     _whiteList.Add(deviceId, device);
     var data = new DeviceMetaData();
     data.Version = "1.0";
     data.IsSimulatedDevice = false;
     data.Properties.DeviceID = deviceId;
     data.Properties.FirmwareVersion = "42";
     data.Properties.HubEnabledState = true;
     data.Properties.Processor = "Foo";
     data.Properties.Platform = "Yep";
     data.Properties.SerialNumber = "Sigfox-" + deviceId;
     data.Properties.InstalledRAM = "1 MB";
     data.Properties.ModelNumber = "007-BOND";
     data.Properties.Manufacturer = "Sigfox";
     //data.Properties.UpdatedTime = DateTime.UtcNow;
     data.Properties.DeviceState = DeviceState.Normal;
     var content = JsonConvert.SerializeObject(data);
     connectionString = string.Format("HostName={0}.azure-devices.net;DeviceId={1};SharedAccessKey={2}",
         "sigfoxmonitoring", device.DeviceId, device.Key);
     _client = DeviceClient.CreateFromConnectionString(connectionString, TransportType.Http1);
     await _client.SendEventAsync(new Message(Encoding.UTF8.GetBytes(content)));
 }