Example #1
0
        public async Task StartDeviceStatus()
        {
            while (true)
            {
                try
                {
                    await  UpdateLastCheckedLog(InterfaceType.deviceheartbeat);
                    await RefreshDeviceStatusList();

                    var lst = new List <(string, string, object)>();
                    foreach (var device in Devices)
                    {
                        //if (device.Status != "Active")
                        //    continue;
                        //Add Heartbeat of device and send
                        var _iotObject = new IotObject();
                        //_iotObject.Device = device;
                        _iotObject.DeviceName = device.DeviceName;
                        _iotObject.DeviceType = "DEVICE";
                        _iotObject.ObjectType = "HEARTBEAT";
                        _iotObject.Object     = device.Status == "Active" ? 1 : 0;
                        lst.Add(_iotObject.ToString());
                        //Send list to Iot Cloud Platform
                    }
                    await SendNotification(lst);
                    await UpdateLastUpdatedLog(InterfaceType.deviceheartbeat);
                }
                catch (Exception ex)
                {
                    LoggerX.WriteErrorLog(ex);
                    _logger.LogError(ex, $"Sending notification for gateway {IotGateway.GatewayId}");
                }
                await Task.Delay(IotGateway.DeviceHeatbeatInterval * 1000);
            }
        }
        public async Task StartAsync()
        {
            try
            {
                var _iotObject = new IotObject();
                _iotObject.Group      = "RFID";
                _iotObject.DeviceType = "RNC";
                _iotObject.DeviceName = IotGateway.GatewayId;
                _iotObject.ObjectType = "HEARTBEAT";

                while (true)
                {
                    await  UpdateLastCheckedLog(InterfaceType.rncheartbeat);

                    var lst = new List <(string, string, object)>();
                    Console.WriteLine("Check RNC Heartbeat");
                    //Check if the RNC Server is online -RNC IP Address is configurable in app.config

                    if (await IsRNCAlive(IotGateway.RNCIpAddress))
                    {
                        _iotObject.Object    = 1;
                        IotGateway.Heartbeat = true;
                        LoggerX.WriteEventLog($"RNC Heartbeat is alive");
                    }
                    else
                    {
                        _iotObject.Object    = 0;
                        IotGateway.Heartbeat = false;
                        LoggerX.WriteEventLog($"RNC Heartbeat is dead");
                    }
                    lst.Add(_iotObject.ToString());
                    Console.WriteLine("Attempting to send " + DateTime.Now.ToString());
                    await SendNotification(lst);
                    await  UpdateLastUpdatedLog(InterfaceType.rncheartbeat);

                    LoggerX.WriteEventLog($"RNC Heartbeat Notification Sent ");

                    if (IotGateway.RNCCheckInterval > 0)
                    {
                        await Task.Delay((int)(IotGateway.RNCCheckInterval * 1000));
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                LoggerX.WriteErrorLog(ex);
                _logger.LogError(ex, $"Sending notification for gateway {IotGateway.GatewayId}");
            }
        }
        /// <summary>
        /// Loop through all devices and send current tag list to IoT Cloud Platform
        /// </summary>
        /// <returns></returns>
        public async Task StartKeyFrameMonitor()
        {
            while (DeviceList.Count == 0)
            {
                DeviceList = await new Device().ReadAsync();
            }
            while (true)
            {
                try
                {
                    await UpdateLastCheckedLog(InterfaceType.keyframe);

                    var lst = new List <(string, string, object)>();
                    foreach (var device in DeviceList)
                    {
                        var _iotObject = new IotObject();
                        _iotObject.Group = "RFID";
                        await _iotObject.SetDevice(device);

                        _iotObject.ObjectType = "TAGS";
                        //Add all added tag of device
                        await Task.Delay(100);

                        Dictionary <string, JObject> Alltags = null;
                        while (Alltags == null)
                        {
                            Alltags = await GetCurrentDeviceTagList(device);
                        }
                        //if (Alltags.Count == 0)
                        //    continue;

                        LoggerX.WriteEventLog($"{Alltags.Count} tags found at device {device.DeviceName}");
                        var value = new JObject
                        {
                            ["reset"] = JObject.FromObject(Alltags)
                        };
                        _iotObject.Object = value;
                        lst.Add(_iotObject.ToString());
                        //Console.WriteLine(JArray.FromObject(lst).ToString());
                    }
                    _logger.LogTrace("Logging: Key frame attempt");
                    Console.WriteLine("Key frame try..");
                    LoggerX.WriteEventLog("Reset Notification Try");
                    await SendNotification(lst);
                    await UpdateLastUpdatedLog(InterfaceType.keyframe);

                    LoggerX.WriteEventLog("Reset Notification Sent");

                    //Delay For Total Key Frame Interval Seconds
                    if (IotGateway.KeyframeInterval > 0)
                    {
                        await Task.Delay((int)(IotGateway.KeyframeInterval * 1000));
                    }
                }
                catch (Exception ex)
                {
                    LoggerX.WriteErrorLog(ex);
                    _logger.LogError(ex, $"Sending notification for gateway {IotGateway.GatewayId}");
                }
            }
        }
        public async Task StartTagReads()
        {
            //  await Gateway.InitAsync();

            while (true)
            {
                LoggerX.WriteDebugLog("1. Check for un processed tags");
                //Get all un seen tags
                //while(TagList == null)
                TagList = await CheckForUnprocessedTags();

                //Loop through all unprocessed tags
                LoggerX.WriteDebugLog($"2. {TagList.Count} Tags found to process");
                foreach (var read in TagList)
                {
                    try
                    {
                        var lst        = new List <(string, string, object)>();
                        var _iotObject = new IotObject("RFID");//Group name
                        await UpdateLastCheckedLog(InterfaceType.tagreads);

                        //Add new tag read of device
                        LoggerX.WriteDebugLog($"3. Get all tags of device {read.Device.DeviceName}");
                        Dictionary <string, RfidTag> addedUpdatedTags = null;
                        while (addedUpdatedTags == null)
                        {
                            addedUpdatedTags = await GetAddedTags(read.Device.DeviceName);
                        }
                        var value = new JObject
                        {
                            ["addOrUpdate"] = JObject.FromObject(addedUpdatedTags)
                        };
                        LoggerX.WriteDebugLog($"4. Add device taglist to iot object");
                        //AddTagRead(read.Device.DeviceName, read.EPC, read.DateTime.ToString());
                        //Remove all tags from last device and add to

                        LoggerX.WriteDebugLog($"5. Get remove list");
                        await AddRemovedTagsToList(lst);

                        LoggerX.WriteDebugLog($"6. Remove list added to iot object");
                        // await Task.Delay(10);
                        LoggerX.WriteEventLog($"7. {read.EPC} tags read at device: {read.Device.DeviceName}");

                        _iotObject.Object = value;
                        _iotObject.Group  = "RFID";
                        // _iotObject.DeviceType = "ZONE";
                        await _iotObject.SetDevice((Innotrack.DeviceManager.Entities.Device) read.Device);

                        //_iotObject.DeviceName = read.Device.DeviceName;
                        _iotObject.ObjectType = "TAGS";
                        lst.Add(_iotObject.ToString());
                        LoggerX.WriteEventLog($"8. Fill iot object");
                        // lst.Add(("RFID|1:ZONE|" + read.Device.DeviceName, "TAGS", value));
                        if (lst.Count > 0)
                        {
                            LoggerX.WriteEventLog(JArray.FromObject(lst).ToString());
                            LoggerX.WriteEventLog("Tag Reads Notification Try");
                            await SendNotification(lst);

                            LoggerX.WriteEventLog("Tag Reads Notification Sent");
                            UpdateTagReadsHostSeen();
                            //read.HostSeen = true;
                            //read.Update();
                            await  UpdateLastUpdatedLog(InterfaceType.tagreads);

                            lst.Clear();
                        }
                    }
                    catch (Exception ex)
                    {
                        LoggerX.WriteErrorLog(ex);
                        _logger.LogError(ex, $"Sending notification for gateway {IotGateway.GatewayId}");
                        // await Task.Delay((int)(Math.Max(1, sim.IntervalSeconds) * 1000));
                    }
                    break;
                }
                await Task.Delay(50);
            }
            // ReSharper disable once FunctionNeverReturns
        }
        public async Task StartUnSeenMonitor()
        {
            while (true)
            {
                try
                {
                    await UpdateLastCheckedLog(InterfaceType.unreadtaglist);

                    Devices = await new Device().ReadAsync();
                    var lst = new List <(string, string, object)>();
                    foreach (var device in Devices)
                    {
                        var _iotObject = new IotObject();
                        await _iotObject.SetDevice(device);

                        _iotObject.ObjectType = "TAGS";
                        DateTime removedatetime = DateTime.Now;
                        removedatetime = removedatetime.AddSeconds(IotGateway.RemoveTimeout);
                        Dictionary <string, JObject> removeList = null;
                        while (removeList == null)
                        {
                            removeList = await GetRemovedTags(device.ID.ToString(), removedatetime);
                        }
                        if (removeList.Count == 0)
                        {
                            continue;
                        }
                        LoggerX.WriteEventLog($"{removeList.Count} unread tags at device: {device.DeviceName}");
                        var value = new JObject
                        {
                            ["remove"] = JToken.FromObject(removeList)
                        };
                        _iotObject.Object = value;
                        lst.Add(_iotObject.ToString());
                    }
                    if (lst.Count > 0)
                    {
                        LoggerX.WriteEventLog("Remove List try...");
                        await SendNotification(lst);

                        UpdateAllTagSeen();
                        await  UpdateLastUpdatedLog(InterfaceType.unreadtaglist);

                        LoggerX.WriteEventLog($"Remove list notifcation sent");
                        await Task.Delay(50);
                    }

                    if (IotGateway.UnSeenListInterval > 0)
                    {
                        await Task.Delay((int)(IotGateway.UnSeenListInterval * 1000));
                    }
                }
                catch (Exception ex)
                {
                    LoggerX.WriteErrorLog(ex);
                    if (ex.Message == "Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.")
                    {
                        continue;
                    }
                    _logger.LogError(ex, $"Sending notification for gateway {IotGateway.GatewayId}");
                }
            }
        }