Esempio n. 1
0
        private async void AskPersistDeviceAlarm(object state)
        {
            IOptionsSnapshot <CoreOptions> coreOptions =
                this.serviceProvider.GetRequiredService <IOptionsSnapshot <CoreOptions> >();
            IOptionsSnapshot <DeviceOptions> deviceOptions =
                this.serviceProvider.GetRequiredService <IOptionsSnapshot <DeviceOptions> >();

            using BjdireContext db = this.serviceProvider.GetRequiredService <BjdireContext>();

            foreach (DeviceOptionsEntry d in deviceOptions.Value.Devices)
            {
                try
                {
                    byte[] id       = d.ComputeIdBinary();
                    string deviceId = BitConverter.ToString(id);
                    if (this.plcManager.PlcDictionary.TryGetValue(ByteString.CopyFrom(id), out PlcClient client))
                    {
                        this.logger.LogInformation("Ask alarm for {0}({1})", d.Id, d.Name);

                        GrpcAlarm alarm = await client
                                          .GetAlarmAsync(
                            new GetAlarmRequest(),
                            DateTime.UtcNow.AddMilliseconds(
                                coreOptions.Value.DefaultReadTimeoutMillis))
                                          .ConfigureAwait(false);

                        var m = new ModelAlarm
                        {
                            DeviceId = deviceId,
                        };
                        alarm.AssignTo(m);

                        ModelAlarm lastKnownAlarmInfo = (
                            from mm in db.Alarms
                            where mm.DeviceId == deviceId
                            orderby mm.Timestamp descending
                            select mm)
                                                        .FirstOrDefault();

                        if (lastKnownAlarmInfo == null)
                        {
                            lastKnownAlarmInfo = new ModelAlarm();
                        }

                        db.AlarmChanges.AddRange(ComputeAlarmChanges(deviceId, lastKnownAlarmInfo, m));

                        db.Alarms.Add(m);
                    }
                    else
                    {
                        this.logger.LogWarning(
                            "Failed to ask alarm for {0}({1}), currently offline.",
                            d.Id,
                            d.Name);
                    }
                }
                catch (RpcException e)
                {
                    this.logger.LogWarning(e, "Failed to ask alarm for {0}({1})", d.Id, d.Name);
                }
            }

            db.SaveChanges();
        }
Esempio n. 2
0
        private static IEnumerable <ModelAlarmChange> ComputeAlarmChanges(
            string deviceId,
            ModelAlarm lastKnownAlarm,
            ModelAlarm currentAlarm)
        {
            if (lastKnownAlarm.LowFlowRate != currentAlarm.LowFlowRate)
            {
                yield return(new ModelAlarmChange
                {
                    DeviceId = deviceId,
                    Timestamp = currentAlarm.Timestamp,
                    Type = AlarmType.LowFlowRate,
                    Direction = currentAlarm.LowFlowRate
                        ? AlarmChangeDirection.Appearance
                        : AlarmChangeDirection.Disappearance,
                });
            }

            if (lastKnownAlarm.HighHeaterPressure != currentAlarm.HighHeaterPressure)
            {
                yield return(new ModelAlarmChange
                {
                    DeviceId = deviceId,
                    Timestamp = currentAlarm.Timestamp,
                    Type = AlarmType.HighHeaterPressure,
                    Direction = currentAlarm.HighHeaterPressure
                        ? AlarmChangeDirection.Appearance
                        : AlarmChangeDirection.Disappearance,
                });
            }

            if (lastKnownAlarm.LowHeaterPressure != currentAlarm.LowHeaterPressure)
            {
                yield return(new ModelAlarmChange
                {
                    DeviceId = deviceId,
                    Timestamp = currentAlarm.Timestamp,
                    Type = AlarmType.LowHeaterPressure,
                    Direction = currentAlarm.LowHeaterPressure
                        ? AlarmChangeDirection.Appearance
                        : AlarmChangeDirection.Disappearance,
                });
            }

            if (lastKnownAlarm.NoPower != currentAlarm.NoPower)
            {
                yield return(new ModelAlarmChange
                {
                    DeviceId = deviceId,
                    Timestamp = currentAlarm.Timestamp,
                    Type = AlarmType.NoPower,
                    Direction = currentAlarm.NoPower
                        ? AlarmChangeDirection.Appearance
                        : AlarmChangeDirection.Disappearance,
                });
            }

            if (lastKnownAlarm.HeaterOverloadedBroken != currentAlarm.HeaterOverloadedBroken)
            {
                yield return(new ModelAlarmChange
                {
                    DeviceId = deviceId,
                    Timestamp = currentAlarm.Timestamp,
                    Type = AlarmType.HeaterOverloadedBroken,
                    Direction = currentAlarm.HeaterOverloadedBroken
                        ? AlarmChangeDirection.Appearance
                        : AlarmChangeDirection.Disappearance,
                });
            }

            if (lastKnownAlarm.ElectricalHeaterBroken != currentAlarm.ElectricalHeaterBroken)
            {
                yield return(new ModelAlarmChange
                {
                    DeviceId = deviceId,
                    Timestamp = currentAlarm.Timestamp,
                    Type = AlarmType.ElectricalHeaterBroken,
                    Direction = currentAlarm.ElectricalHeaterBroken
                        ? AlarmChangeDirection.Appearance
                        : AlarmChangeDirection.Disappearance,
                });
            }
        }