Esempio n. 1
0
        public override async Task OnConnectedAsync()
        {
            var deviceStates = externalWorld.GetHashFields("heartbeat").Select(h => new
            {
                name       = h.Key.Substring(0, 1).ToUpper(),
                latestDate = h.Value,
                isDead     = (DateTime.Now - DateTime.ParseExact(h.Value, "dd.MM.yy HH:mm:ss", CultureInfo.InvariantCulture)).Minutes > 10
            }).ToList();

            await Clients.Caller.SendAsync("heartbeats", new
            {
                deviceStates,
                pendingDevices = externalWorld.ReceivedDevices.Count
            });

            await base.OnConnectedAsync();
        }
Esempio n. 2
0
        public IActionResult Get()
        {
            SystemOverview devicesOverview = new SystemOverview(supportedTypes, supportedLocations);

            bool hasValidationErrors = false;

            List <string> databaseKeys = externalWorld.GetAllKeys();

            if (!databaseKeys.Any())
            {
                devicesOverview.Messages.Add("Database has no keys.");
            }

            Dictionary <string, DeviceDetails> devices = new Dictionary <string, DeviceDetails>(databaseKeys.Count);

            foreach (var key in databaseKeys)
            {
                var type = externalWorld.GetType(key);

                switch (type)
                {
                case RedisType.Set:
                    var deviceIds = externalWorld.GetInstanceDeviceIds(key).Select(deviceId =>
                    {
                        DeviceDetails device;
                        if (!devices.ContainsKey(deviceId))
                        {
                            device = new DeviceDetails(deviceId);
                            devices.Add(deviceId, device);
                        }
                        else
                        {
                            device = devices[deviceId];
                        }

                        device.MappedWith.Add(key);
                        return(device);
                    }).ToList();

                    devicesOverview.Instances.Add(new InstanceDetails(key, deviceIds));
                    break;

                case RedisType.Hash:
                    if (!devices.ContainsKey(key))
                    {
                        devices.Add(key, new DeviceDetails(key));
                    }

                    devices[key].Fields = externalWorld.GetHashFields(key);
                    break;

                default:
                    devicesOverview.Messages.Add($"I don't know what to do with this type: {type.ToString()}.");
                    break;
                }
            }

            foreach (var device in devices)
            {
                if (!device.Value.MappedWith.Any())
                {
                    devicesOverview.Unmapped.Add(device.Value);
                    continue;
                }

                if (device.Value.Fields == null)
                {
                    device.Value.Messages.Add("This has no fields.");
                    device.Value.Fields = new KeyValuePair <string, string> [0];
                    hasValidationErrors = true;
                }

                if (device.Value.MappedWith.Count > 1)
                {
                    device.Value.Messages.Add("There is more than one instance that has this device.");
                    hasValidationErrors = true;
                }
            }

            foreach (var instance in devicesOverview.Instances)
            {
                List <int> gpios = new List <int>();

                foreach (var device in instance.Devices)
                {
                    foreach (var field in device.Fields)
                    {
                        int gpio;
                        if (field.Key.ToLower().Contains("gpio"))
                        {
                            if (Int32.TryParse(field.Value, out gpio))
                            {
                                if (gpios.Contains(gpio))
                                {
                                    device.Messages.Add($"GPIO {field.Value} int field {field.Key} is duplicated.");
                                    hasValidationErrors = true;
                                }
                                gpios.Add(gpio);
                            }
                            else
                            {
                                device.Messages.Add($"GPIO {field.Value} from field {field.Key} cannot be converted in int.");
                                hasValidationErrors = true;
                            }
                        }

                        if (String.Compare(field.Key, "type", true) == 0 && !supportedTypes.Contains(field.Value))
                        {
                            device.Messages.Add($"type {field.Value} is not supported.");
                            hasValidationErrors = true;
                        }

                        if (String.Compare(field.Key, "location", true) == 0 && !supportedLocations.Contains(field.Value))
                        {
                            device.Messages.Add($"location {field.Value} is not supported.");
                            hasValidationErrors = true;
                        }
                    }
                }
            }

            if (hasValidationErrors)
            {
                devicesOverview.Messages.Add("See the validation errors bellow.");
            }

            return(Json(devicesOverview));
        }