Example #1
0
        private async Task <LoRaDevice> RegisterCoreAsync(IoTHubDeviceInfo deviceInfo, CancellationToken cancellationToken)
        {
            var loRaDevice = CreateDevice(deviceInfo);

            try
            {
                // we always want to register the connection if we have a key.
                // the connection is not opened, unless there is a
                // request made. This allows us to refresh the twins,
                // even though, we don't own it, to detect ownership
                // changes.
                // Ownership is transferred to connection manager.
                this.connectionManager.Register(loRaDevice, CreateDeviceClient(deviceInfo.DevEUI.ToString(), deviceInfo.PrimaryKey));

                loRaDevice.SetRequestHandler(this.dataRequestHandler);

                if (loRaDevice.UpdateIsOurDevice(this.configuration.GatewayID) &&
                    !await loRaDevice.InitializeAsync(this.configuration, cancellationToken))
                {
                    throw new LoRaProcessingException("Failed to initialize device twins.", LoRaProcessingErrorCode.DeviceInitializationFailed);
                }
                this.loRaDeviceCache.Register(loRaDevice);
                return(loRaDevice);
            }
            catch
            {
                this.connectionManager.Release(loRaDevice);
                loRaDevice.Dispose();
                throw;
            }
        }
Example #2
0
 internal JoinDeviceLoader(IoTHubDeviceInfo ioTHubDevice, ILoRaDeviceFactory deviceFactory)
 {
     this.ioTHubDevice  = ioTHubDevice;
     this.deviceFactory = deviceFactory;
     this.canCache      = true;
     this.loading       = this.LoadAsync();
 }
Example #3
0
 // Gets or adds a join device loader to the memory cache
 JoinDeviceLoader GetOrAddJoinDeviceLoader(IoTHubDeviceInfo ioTHubDeviceInfo)
 {
     return(this.cache.GetOrCreate(this.GetJoinDeviceLoaderCacheKey(ioTHubDeviceInfo.DevEUI), (entry) =>
     {
         entry.SlidingExpiration = TimeSpan.FromMinutes(INTERVAL_TO_CACHE_DEVICE_IN_JOIN_PROCESS_IN_MINUTES);
         return new JoinDeviceLoader(ioTHubDeviceInfo, this.deviceFactory);
     }));
 }
Example #4
0
 internal JoinDeviceLoader(IoTHubDeviceInfo ioTHubDevice, ILoRaDeviceFactory deviceFactory, LoRaDeviceCache deviceCache, ILogger <JoinDeviceLoader> logger)
 {
     this.ioTHubDevice  = ioTHubDevice;
     this.deviceFactory = deviceFactory;
     this.deviceCache   = deviceCache;
     this.logger        = logger;
     this.canCache      = true;
 }
        public LoRaDevice Create(IoTHubDeviceInfo deviceInfo)
        {
            var loraDeviceClient = this.CreateDeviceClient(deviceInfo.DevEUI, deviceInfo.PrimaryKey);

            var loRaDevice = new LoRaDevice(
                devAddr: deviceInfo.DevAddr,
                devEUI: deviceInfo.DevEUI,
                loRaDeviceClient: loraDeviceClient);

            return(loRaDevice);
        }
 // Gets or adds a join device loader to the memory cache
 private JoinDeviceLoader GetOrCreateJoinDeviceLoader(IoTHubDeviceInfo ioTHubDeviceInfo)
 {
     // Need to get and ensure it has started since the GetOrAdd can create multiple objects
     // https://github.com/aspnet/Extensions/issues/708
     lock (this.getOrCreateJoinDeviceLoaderLock)
     {
         return(this.cache.GetOrCreate(GetJoinDeviceLoaderCacheKey(ioTHubDeviceInfo.DevEUI), (entry) =>
         {
             entry.SlidingExpiration = TimeSpan.FromMinutes(INTERVAL_TO_CACHE_DEVICE_IN_JOIN_PROCESS_IN_MINUTES);
             return new JoinDeviceLoader(ioTHubDeviceInfo, this.deviceFactory, this.deviceCache, this.loggerFactory.CreateLogger <JoinDeviceLoader>());
         }));
     }
 }
Example #7
0
 protected virtual LoRaDevice CreateDevice(IoTHubDeviceInfo deviceInfo)
 {
     return(deviceInfo == null
             ? throw new ArgumentNullException(nameof(deviceInfo))
             : new LoRaDevice(deviceInfo.DevAddr,
                              deviceInfo.DevEUI,
                              this.connectionManager,
                              this.loggerFactory.CreateLogger <LoRaDevice>(),
                              this.meter)
     {
         GatewayID = deviceInfo.GatewayId,
         NwkSKey = deviceInfo.NwkSKey,
     });
 }
Example #8
0
        public LoRaDevice Create(IoTHubDeviceInfo deviceInfo)
        {
            var loraDeviceClient = this.CreateDeviceClient(deviceInfo.DevEUI, deviceInfo.PrimaryKey);

            var loRaDevice = new LoRaDevice(
                deviceInfo.DevAddr,
                deviceInfo.DevEUI,
                this.connectionManager);

            this.connectionManager.Register(loRaDevice, loraDeviceClient);

            loRaDevice.SetRequestHandler(this.dataRequestHandler);

            return(loRaDevice);
        }
Example #9
0
        public Task <LoRaDevice> CreateAndRegisterAsync(IoTHubDeviceInfo deviceInfo, CancellationToken cancellationToken)
        {
            _ = deviceInfo ?? throw new ArgumentNullException(nameof(deviceInfo));

            if (string.IsNullOrEmpty(deviceInfo.PrimaryKey) || !deviceInfo.DevEUI.IsValid)
            {
                throw new ArgumentException($"Incomplete {nameof(IoTHubDeviceInfo)}", nameof(deviceInfo));
            }

            if (this.loRaDeviceCache.TryGetByDevEui(deviceInfo.DevEUI, out _))
            {
                throw new InvalidOperationException($"Device {deviceInfo.DevEUI} already registered");
            }

            return(RegisterCoreAsync(deviceInfo, cancellationToken));
        }
Example #10
0
        public LoRaDevice Create(IoTHubDeviceInfo deviceInfo)
        {
            var loRaDevice = new LoRaDevice(
                deviceInfo.DevAddr,
                deviceInfo.DevEUI,
                this.connectionManager);

            loRaDevice.GatewayID = deviceInfo.GatewayId;
            loRaDevice.NwkSKey   = deviceInfo.NwkSKey;

            var isOurDevice = string.IsNullOrEmpty(deviceInfo.GatewayId) || string.Equals(deviceInfo.GatewayId, this.configuration.GatewayID, StringComparison.OrdinalIgnoreCase);

            if (isOurDevice)
            {
                this.connectionManager.Register(loRaDevice, this.CreateDeviceClient(deviceInfo.DevEUI, deviceInfo.PrimaryKey));
            }

            loRaDevice.SetRequestHandler(this.dataRequestHandler);

            return(loRaDevice);
        }
Example #11
0
        public static async Task <LoraDeviceInfo> GetLoraDeviceInfoAsync(string DevAddr, string GatewayId)
        {
            var client = GetHttpClient();
            var url    = $"{FacadeServerUrl}GetDevice?code={FacadeAuthCode}&DevAddr={DevAddr}&GatewayId={GatewayId}";
            HttpResponseMessage response = await client.GetAsync(url);

            if (!response.IsSuccessStatusCode)
            {
                Logger.Log(DevAddr, $"error calling façade api: {response.ReasonPhrase} check the azure function log", Logger.LoggingLevel.Error);
                return(null);
            }

            var result = await response.Content.ReadAsStringAsync();

            //TODO enable multi devices with the same devaddr

            List <IoTHubDeviceInfo> iotHubDeviceInfos = ((List <IoTHubDeviceInfo>)JsonConvert.DeserializeObject(result, typeof(List <IoTHubDeviceInfo>)));

            LoraDeviceInfo loraDeviceInfo = new LoraDeviceInfo();

            loraDeviceInfo.DevAddr = DevAddr;


            //we did not find a device with this devaddr so we assume is not ours
            if (iotHubDeviceInfos.Count == 0)
            {
                loraDeviceInfo.IsOurDevice = false;
            }
            else
            {
                IoTHubDeviceInfo iotHubDeviceInfo = iotHubDeviceInfos[0];

                loraDeviceInfo.DevEUI     = iotHubDeviceInfo.DevEUI;
                loraDeviceInfo.PrimaryKey = iotHubDeviceInfo.PrimaryKey;


                loraDeviceInfo.HubSender = new IoTHubConnector(iotHubDeviceInfo.DevEUI, iotHubDeviceInfo.PrimaryKey);



                var twin = await loraDeviceInfo.HubSender.GetTwinAsync();

                if (twin != null)
                {
                    //ABP Case
                    if (twin.Properties.Desired.Contains("AppSKey"))
                    {
                        loraDeviceInfo.AppSKey = twin.Properties.Desired["AppSKey"];
                        loraDeviceInfo.NwkSKey = twin.Properties.Desired["NwkSKey"];
                        loraDeviceInfo.DevAddr = twin.Properties.Desired["DevAddr"];
                    }
                    //OTAA Case
                    else if (twin.Properties.Reported.Contains("AppSKey"))
                    {
                        loraDeviceInfo.AppSKey  = twin.Properties.Reported["AppSKey"];
                        loraDeviceInfo.NwkSKey  = twin.Properties.Reported["NwkSKey"];
                        loraDeviceInfo.DevAddr  = twin.Properties.Reported["DevAddr"];
                        loraDeviceInfo.DevNonce = twin.Properties.Reported["DevNonce"];

                        //todo check if appkey and appeui is needed in the flow
                        loraDeviceInfo.AppEUI = twin.Properties.Desired["AppEUI"];
                        loraDeviceInfo.AppKey = twin.Properties.Desired["AppKey"];
                    }
                    else
                    {
                        Logger.Log(loraDeviceInfo.DevEUI, $"AppSKey not present neither in Desired or in Reported properties", Logger.LoggingLevel.Error);
                    }

                    if (twin.Properties.Desired.Contains("GatewayID"))
                    {
                        loraDeviceInfo.GatewayID = twin.Properties.Desired["GatewayID"];
                    }
                    if (twin.Properties.Desired.Contains("SensorDecoder"))
                    {
                        loraDeviceInfo.SensorDecoder = twin.Properties.Desired["SensorDecoder"];
                    }


                    loraDeviceInfo.IsOurDevice = true;


                    if (twin.Properties.Reported.Contains("FCntUp"))
                    {
                        loraDeviceInfo.FCntUp = twin.Properties.Reported["FCntUp"];
                    }
                    if (twin.Properties.Reported.Contains("FCntDown"))
                    {
                        loraDeviceInfo.FCntDown = twin.Properties.Reported["FCntDown"];
                    }


                    Logger.Log(loraDeviceInfo.DevEUI, $"done getting twins", Logger.LoggingLevel.Info);
                }
            }

            return(loraDeviceInfo);
        }