Beispiel #1
0
        private MeasurementData GetCustomMeasurementValue(SensorDevice sensorDevice, List <MeasurementData> measurements, CustomMeasurementRule rule)
        {
            var sensorMeasurement = measurements.FirstOrDefault(m => m.SensorId.Equals(rule.SensorId, StringComparison.OrdinalIgnoreCase) &&
                                                                m.SensorDeviceId.Equals(sensorDevice.Id, StringComparison.OrdinalIgnoreCase));

            if (sensorMeasurement == null)
            {
                _logger.LogError($"No sensor '{rule.SensorId}' found from measurements of device type '{sensorDevice.SensorDeviceTypeId}'");
                return(null);
            }

            var data = new MeasurementData()
            {
                SensorDeviceId = sensorDevice.Id, SensorId = rule.Id, Timestamp = DateTime.Now
            };
            var comparisonResult = LogicHelper.Compare(sensorMeasurement.Value, rule.Value, rule.Operator);

            switch (rule.Type)
            {
            case Shared.Enum.ValueType.Default:
                data.Value = comparisonResult ? "true" : "false";
                break;

            case Shared.Enum.ValueType.WasLast:
                var keyName = $"{sensorDevice.Id}-{sensorMeasurement.SensorId}-{rule.Id}";
                if (comparisonResult)
                {
                    if (!_wasLastList.ContainsKey(keyName))
                    {
                        _wasLastList.Add(keyName, DateTime.Now);
                    }
                    else
                    {
                        _wasLastList[keyName] = DateTime.Now;
                    }
                    data.Value = "0";
                }
                else
                {
                    if (_wasLastList.ContainsKey(keyName))
                    {
                        var ts = DateTime.Now - _wasLastList[keyName];
                        data.Value = ((int)ts.TotalSeconds).ToString();
                    }
                    else
                    {
                        data.Value = "-1";
                    }
                }
                break;

            default:
                break;
            }
            return(data);
        }
Beispiel #2
0
        public async Task <bool> Send(List <LocationData> locations)
        {
            if (!_config.AlertConfiguration.Enabled)
            {
                return(false);
            }

            foreach (var alert in _config.AlertConfiguration.Alerts)
            {
                bool alertNeedsToBeSent = false;
                foreach (var rule in alert.RuleSet.Rules)
                {
                    var location    = locations.FirstOrDefault(l => l.Id.Equals(rule.Location, StringComparison.OrdinalIgnoreCase));
                    var measurement = location?.Measurements.FirstOrDefault(m => m.SensorDeviceId.Equals(rule.SensorDeviceId, StringComparison.OrdinalIgnoreCase) &&
                                                                            m.SensorId.Equals(rule.SensorId, StringComparison.OrdinalIgnoreCase));

                    if (measurement == null)
                    {
                        continue;
                    }

                    var comparisonResult = LogicHelper.Compare(measurement.Value, rule.Value, rule.Operator);

                    if (!comparisonResult && alert.RuleSet.Type == MeasurementRuleSetType.AND)
                    {
                        alertNeedsToBeSent = false;
                        break;
                    }
                    else if (!comparisonResult && alert.RuleSet.Type == MeasurementRuleSetType.OR)
                    {
                        continue;
                    }
                    else if (comparisonResult && alert.RuleSet.Type == MeasurementRuleSetType.AND)
                    {
                        alertNeedsToBeSent = true;
                        continue;
                    }
                    else if (comparisonResult && alert.RuleSet.Type == MeasurementRuleSetType.OR)
                    {
                        alertNeedsToBeSent = true;
                        break;
                    }
                }
                if (alertNeedsToBeSent)
                {
                    var hasNotificationBeenSent = await _noticationDataProvider.HasNotificationBeenSent(alert.Id);

                    if (!hasNotificationBeenSent)
                    {
                        await _notificationService.Send(alert.Title, alert.Message);

                        await _noticationDataProvider.SetNotificationStatus(alert.Id, true);

                        _logger.LogInformation($"{DateTime.Now}: Sent notification of '{alert.Description}' ({alert.Id})");
                    }
                }
                else
                {
                    await _noticationDataProvider.SetNotificationStatus(alert.Id, false);
                }
            }
            return(true);
        }