コード例 #1
0
ファイル: CallistoDevice.cs プロジェクト: Fike-Rehman/Juno
        public CallistoDevice(CallistoSettings deviceSettings, bool isMetric = false)
        {
            _settings = deviceSettings;

            Id        = _settings.Id;
            Location  = _settings.Location;
            _isMetric = isMetric;
        }
コード例 #2
0
ファイル: CallistoEngine.cs プロジェクト: Fike-Rehman/Juno
        public void Run(CancellationToken cToken)
        {
            _logger.LogInformation("Beginning Callisto Activities...");

            try
            {
                // read callisto devices configuration and build the device list
                foreach (var device in _appSettings.Devicelist.JunoDevices)
                {
                    if (device.Id.StartsWith("Callisto"))
                    {
                        var settings = new CallistoSettings()
                        {
                            Id            = device.Id,
                            Name          = device.Name,
                            SerialNumber  = device.SerialNumber,
                            ProvisionDate = device.ProvisionDate,
                            IpAddress     = device.IpAddress,
                            Location      = device.Location,
                            DeviceKey     = _secureSettings.GetDeviceKey(device.Id)
                        };

                        if (_appSettings.IsMetric)
                        {
                            _callistos.Add(new CallistoDevice(settings, true));
                        }
                        else
                        {
                            _callistos.Add(new CallistoDevice(settings));
                        }
                    }
                }

                if (_callistos.Count > 0)
                {
                    // Initialize each device defined in the config by sending a ping
                    // to see if it is Online. We must wait for this to complete before proceeding

                    InitDevicesAsync(cToken).Wait();

                    // print a list of online devices:
                    _logger.LogInformation($"Found {_callistos.Count} callistos devices online:");

                    foreach (var d in _callistos)
                    {
                        _logger.LogInformation($"Name: {d.Id}");
                        _logger.LogInformation($"Location: {d.Location}");
                    }


                    var callistoTasks = new List <Task>();

                    // Launch ping routines for all the initialized devices:
                    _callistos.ForEach(callisto =>
                    {
                        if (cToken.IsCancellationRequested)
                        {
                            return;
                        }

                        var pt = Task.Run(() => callisto.StartPingRoutineAsync(new Progress <DeviceProgress>(LogProgress), cToken));

                        _logger.LogInformation($"Ping routine for Callisto device: {callisto.Id} started!");

                        callistoTasks.Add(pt);

                        Task.Delay(2000, cToken).Wait();
                    });

                    Task.Delay(2000, cToken);

                    // Launch Monitor Routines for all initialized devices:
                    _callistos.ForEach(callisto =>
                    {
                        if (cToken.IsCancellationRequested)
                        {
                            return;
                        }

                        var mt = Task.Run(() => callisto.StartMonitorRoutineAsync(ProcessMeasurementsAsync,
                                                                                  new Progress <DeviceProgress>(LogProgress),
                                                                                  cToken));

                        _logger.LogInformation($"Monitor routine for Callisto device: {callisto.Id} started!");

                        callistoTasks.Add(mt);

                        Task.Delay(2000, cToken).Wait(); // stagger the device monitoring so the logs are less jumbled
                    });

                    Task.WaitAll(callistoTasks.ToArray());
                }
            }
            catch (Exception x)
            {
                _logger.LogError("Exception while running Callisto Tasks!");
                _logger.LogError(x.Message);
                _logger.LogError(x.InnerException.Message);
            }



            //SendDeviceToCloudMessagesAsync();
        }