Ejemplo n.º 1
0
        public override Task <InputReply> ReportInputs(InputValue value, ServerCallContext context)
        {
            logger.LogDebug($"Received {value.Value:X8} from {value.Sender}");

            using (DomoCoreInMemDbContext dbcontext = contextFactory.CreateContext())
            {
                foreach (var input in dbcontext.Inputs.Include(x => x.Device)
                         .Where(x => x.Device.Name == value.Sender))
                {
                    if (input.Changed == false)
                    {
                        input.PreviousState = input.CurrentState;
                        if ((value.Value != 0) && ((value.Value & (uint)input.HWValue) == value.Value))
                        {
                            logger.LogDebug($"{input.Id} Pressed");
                            input.CurrentState = Models.InputState.Pressed;
                        }
                        else
                        {
                            input.CurrentState = Models.InputState.Released;
                            if (input.PreviousState == Models.InputState.Pressed)
                            {
                                logger.LogDebug($"{input.Id} Released");
                            }
                        }
                        if (input.PreviousState != input.CurrentState)
                        {
                            input.Changed = true;
                        }
                    }
                }
                dbcontext.SaveChanges();
            }

            return(Task.FromResult(new InputReply {
                Done = true
            }));
        }
Ejemplo n.º 2
0
        // Methods

        private async Task <bool> Initialize()
        {
            // Copy data from database in memory
            using (DomoCoreDbContext context = new DomoCoreDbContext())
            {
                using (DomoCoreInMemDbContext memContext = contextFactory.CreateContext())
                {
                    foreach (var input in context.Inputs)
                    {
                        await memContext.Inputs.AddAsync(
                            new Models.Input
                        {
                            Id            = input.Id,
                            HWValue       = input.HWValue,
                            CurrentState  = input.CurrentState,
                            PreviousState = input.PreviousState,
                            DeviceId      = input.DeviceId,
                            Changed       = input.Changed
                        });
                    }

                    foreach (var output in context.Outputs)
                    {
                        await memContext.Outputs.AddAsync(
                            new Models.Output
                        {
                            Id       = output.Id,
                            State    = output.State,
                            HWValue  = output.HWValue,
                            DeviceId = output.DeviceId
                        });
                    }

                    foreach (var simpleOutput in context.SimpleOutputs)
                    {
                        await memContext.SimpleOutputs.AddAsync(
                            new Models.SimpleOutput
                        {
                            Id              = simpleOutput.Id,
                            Name            = simpleOutput.Name,
                            InputId         = simpleOutput.InputId,
                            OutputId        = simpleOutput.OutputId,
                            AutoOff         = simpleOutput.AutoOff,
                            AutoOffTimeSecs = simpleOutput.AutoOffTimeSecs,
                            State           = simpleOutput.State
                        });
                    }

                    foreach (var device in context.Devices)
                    {
                        await memContext.Devices.AddAsync(
                            new Models.Device
                        {
                            Id      = device.Id,
                            Address = device.Address,
                            Name    = device.Name
                        }
                            );
                    }

                    await memContext.SaveChangesAsync();
                }
            }

            logger.LogDebug("Database copied to memory");

            await client.SetOutputsAsync(new OutputValue { Value = 0 });

            logger.LogDebug("Outputs switched off");

            return(true);
        }