Пример #1
0
        public PacketStateEnum Handle(FunctionHandlerContext context, Packet originalPacket, string response, HubMessageQueue hubMessageQueue)
        {
            var module = originalPacket.Module;

            int periodUnits;

            if (!int.TryParse(response, NumberStyles.Integer, CultureInfo.InvariantCulture, out periodUnits))
            {
                return(LogAndReturn(context, originalPacket,
                                    MethodBase.GetCurrentMethod().GetFullName(),
                                    $"Could not parse text '{originalPacket.Text}' to integer period units."));
            }

            if (periodUnits < 0)
            {
                return(LogAndReturn(context, originalPacket,
                                    MethodBase.GetCurrentMethod().GetFullName(),
                                    $"Period units {periodUnits} cannot be negative."));
            }

            var periodLengthinMs = periodUnits * context.ModuleConfiguration.PeriodUnitLengthInMs;
            var period           = TimeSpan.FromMilliseconds(periodLengthinMs);

            context.DatabaseContext.EnqueueUpdate <TemperatureSensor>(
                i => i.Period == period,
                i => i.ModuleID == module.ID);

            context.DatabaseContext.ExecuteRaw();

            hubMessageQueue.Enqueue(i => i.ChangedTemperatureSensorPeriod(module.ID, period));

            return(PacketStateEnum.Handled);
        }
Пример #2
0
        public override FunctionHandlerResult Handle(FunctionHandlerContext context, Packet packet, ref HubMessageQueue hubMessageQueue)
        {
            var module = packet.Module;

            if (module.State != ModuleStateEnum.New)
            {
                return(PacketStateEnum.Redundant);
            }

            context.DatabaseContext.Messages.Add(new Message(context.FunctionTypes.Forward[FunctionTypeEnum.FunctionList], false)
            {
                Module = module
            });

            context.DatabaseContext.Messages.Add(new Message(context.FunctionTypes.Forward[FunctionTypeEnum.Report], false)
            {
                Module = module
            });

            module.State = ModuleStateEnum.Identified;

            context.DatabaseContext.EnqueueUpdate <Module>(
                i => i.State == ModuleStateEnum.Identified,
                i => i.ID == packet.ModuleID);

            context.DatabaseContext.EnqueueUpdate <Packet>(
                i => i.State == PacketStateEnum.New,
                i => i.ModuleID == packet.ModuleID && i.State == PacketStateEnum.Skipped);

            context.DatabaseContext.SaveChanges();

            hubMessageQueue.Enqueue(i => i.NewModule(module.ID, module.Address));

            return(new FunctionHandlerResult(PacketStateEnum.Handled, true, true));
        }
Пример #3
0
        public override FunctionHandlerResult Handle(FunctionHandlerContext context, Packet packet, ref HubMessageQueue hubMessageQueue)
        {
            var module = packet.Module;

            var currentFunctions = context.DatabaseContext
                                   .Functions
                                   .ToDictionary(i => i.Name, i => i)
                                   .ReadOnly();

            var currentModuleFunctions = context.DatabaseContext
                                         .ModuleFunctions
                                         .AsNoTracking()
                                         .Where(i => i.ModuleID == module.ID)
                                         .Select(i => i.FunctionID)
                                         .ToHashSet();

            var functionNames = packet.Text
                                .ToLowerInvariant()
                                .Split(context.ModuleConfiguration.FunctionResultDelimiter)
                                .Where(i => !string.IsNullOrEmpty(i))
                                .ToList();

            var newModuleFunctions = new List <KeyValuePair <FunctionTypeEnum, string> >(functionNames.Count);

            foreach (var functionName in functionNames)
            {
                var function = currentFunctions.GetValueOrDefault(functionName);
                if (function != null && currentModuleFunctions.Contains(function.ID))
                {
                    continue;
                }

                if (function == null)
                {
                    Log(context, packet,
                        MethodBase.GetCurrentMethod().GetFullName(),
                        $"Unknown function '{functionName}' received in th list.");
                }

                function = function ?? new Function(FunctionTypeEnum.Unknown, false);

                context.DatabaseContext.ModuleFunctions.Add(new ModuleFunction(module, function));

                newModuleFunctions.Add(KeyValuePair.Create(function.FunctionType, function.Name));
            }

            if (newModuleFunctions.Count > 0)
            {
                context.DatabaseContext.SaveChanges();

                hubMessageQueue.Enqueue(i => i.NewModuleFunctions(module.ID, newModuleFunctions));
            }

            return(PacketStateEnum.Handled);
        }
Пример #4
0
        public sealed override FunctionHandlerResult Handle(FunctionHandlerContext context, Packet packet, ref HubMessageQueue hubMessageQueue)
        {
            var items  = GetItems(context.ModuleConfiguration, packet);
            var module = packet.Module;
            var moduleTemperatureSensors = context.DatabaseContext.TemperatureSensors
                                           .AsNoTracking()
                                           .Where(i => i.ModuleID == module.ID)
                                           .ToDictionary(i => i.Address, i => i);

            var newSensors = new List <TemperatureSensor>();

            foreach (var item in items)
            {
                var address = GetAddress(item).ToUpper();
                var isAdded = false;
                TemperatureSensor temperatureSensor;

                if (moduleTemperatureSensors.ContainsKey(address))
                {
                    temperatureSensor = moduleTemperatureSensors[address];
                }
                else
                {
                    temperatureSensor = new TemperatureSensor(module, address);

                    context.DatabaseContext.TemperatureSensors.Add(temperatureSensor);
                    moduleTemperatureSensors.Add(temperatureSensor.Address, temperatureSensor);
                    newSensors.Add(temperatureSensor);

                    isAdded = true;
                    context.DatabaseContext.SaveChanges();

                    hubMessageQueue.Enqueue(i => i.NewTemperatureSensor(temperatureSensor.ModuleID, temperatureSensor.ID, temperatureSensor.Address));
                }

                ItemCallback(context, packet, hubMessageQueue, item, temperatureSensor, isAdded);
            }

            if (newSensors.Count > 0)
            {
                var message = new Message(context.FunctionTypes.Forward[FunctionTypeEnum.OwDS18B20TemperaturePeriodical], true)
                {
                    ModuleID = module.ID,
                };

                context.DatabaseContext.Messages.Add(message);
            }

            context.DatabaseContext.SaveChanges();

            OnHandled(context, module, hubMessageQueue, newSensors);

            return(new FunctionHandlerResult(PacketStateEnum.Handled, newSensors.Count > 0));
        }
Пример #5
0
        protected override void ItemCallback(FunctionHandlerContext context, Packet packet, HubMessageQueue hubMessageQueue, KeyValuePair <string, string> item, TemperatureSensor sensor, bool wasSensorCreated)
        {
            decimal reading;

            if (!decimal.TryParse(item.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out reading))
            {
                Log(context, packet,
                    MethodBase.GetCurrentMethod().GetFullName(),
                    $"Could not parse text '{item.Value}' to decimal temperature.");

                return;
            }

            var temperatureReading = new TemperatureReadout(sensor.ID, reading, packet.Received);

            context.DatabaseContext.TemperatureReadouts.Add(temperatureReading);

            hubMessageQueue.Enqueue(i => i.NewTemperatureReading(packet.Module.ID, temperatureReading.TemperatureSensorID, temperatureReading.Value, temperatureReading.Created, temperatureReading.Received));
        }
Пример #6
0
        public override FunctionHandlerResult Handle(FunctionHandlerContext context, Packet packet, ref HubMessageQueue hubMessageQueue)
        {
            var module = packet.Module;

            decimal reading;

            if (!decimal.TryParse(packet.Text, NumberStyles.Number, CultureInfo.InvariantCulture, out reading))
            {
                return(LogAndReturn(context, packet,
                                    MethodBase.GetCurrentMethod().GetFullName(),
                                    $"Could not parse text '{packet.Text}' to decimal voltage."));
            }

            var voltageReading = new VoltageReadout(module.ID, reading, packet.Received);

            context.DatabaseContext.VoltageReadouts.Add(voltageReading);
            context.DatabaseContext.SaveChanges();

            hubMessageQueue.Enqueue(i => i.NewVoltageReading(voltageReading.ModuleID, voltageReading.Value, voltageReading.Created, voltageReading.Received));

            return(PacketStateEnum.Handled);
        }
Пример #7
0
        protected override void OnHandled(FunctionHandlerContext context, Module module, HubMessageQueue hubMessageQueue, IReadOnlyCollection <TemperatureSensor> newSensors)
        {
            var dictionary = newSensors.ToDictionary(i => i.ID, i => i.Address);

            hubMessageQueue.Enqueue(i => i.NewOneWireDevices(module.ID, dictionary));
        }