Пример #1
0
        private static async Task RecordDeviceValue(InfluxDBMeasurementsCollector collector, IHSApplication HS, DeviceClass device)
        {
            if (device != null)
            {
                int  deviceRefId = device.get_Ref(HS);
                bool notValid    = HS.get_DeviceInvalidValue(deviceRefId);
                if (!notValid)
                {
                    double deviceValue  = device.get_devValue(HS);
                    string deviceString = HS.DeviceString(deviceRefId);
                    if (string.IsNullOrWhiteSpace(deviceString))
                    {
                        deviceString = HS.DeviceVSP_GetStatus(deviceRefId, deviceValue, ePairStatusControl.Status);
                    }
                    Trace.WriteLine(Invariant($"Recording Device Ref Id: {deviceRefId} with [{deviceValue}] & [{deviceString}]"));

                    DateTime lastChange = device.get_Last_Change(HS);

                    RecordData recordData = new RecordData(deviceRefId,
                                                           deviceValue,
                                                           deviceString,
                                                           device.get_Name(HS),
                                                           device.get_Location(HS),
                                                           device.get_Location2(HS),
                                                           lastChange);

                    await collector.Record(recordData).ConfigureAwait(false);
                }
                else
                {
                    Trace.TraceWarning(Invariant($"Not recording Device Ref Id: {deviceRefId} as it has invalid value."));
                }
            }
        }
Пример #2
0
        public async Task <bool> Record(RecordData data)
        {
            if (peristenceDataMap == null)
            {
                throw new HspiException("Collection not started");
            }
            if (peristenceDataMap.TryGetValue(data.DeviceRefId, out var peristenceData))
            {
                foreach (var value in peristenceData)
                {
                    var influxDatapoint = new InfluxDatapoint <InfluxValueField>()
                    {
                        MeasurementName = value.Measurement,
                        Precision       = TimePrecision.Seconds,
                        UtcTimestamp    = data.TimeStamp.ToUniversalTime(),
                    };

                    if (!string.IsNullOrWhiteSpace(value.Field))
                    {
                        double deviceValue = data.DeviceValue;

                        if (IsValidRange(value, deviceValue))
                        {
                            influxDatapoint.Fields.Add(value.Field, new InfluxValueField(deviceValue));
                        }
                        else
                        {
                            Trace.TraceInformation(Invariant($"Not Recording Value for {data.Name} as there is no it does not have valid ranged value at {deviceValue}"));
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(value.FieldString))
                    {
                        influxDatapoint.Fields.Add(value.FieldString, new InfluxValueField(data.DeviceString));
                    }

                    if (influxDatapoint.Fields.Count == 0)
                    {
                        Trace.TraceInformation(Invariant($"Not Recording Value for {data.Name} as there is no valid value to record."));
                        continue;
                    }

                    influxDatapoint.Tags.Add(PluginConfig.DeviceNameTag, data.Name);
                    influxDatapoint.Tags.Add(PluginConfig.DeviceRefIdTag, Convert.ToString(data.DeviceRefId, CultureInfo.InvariantCulture));

                    AddIfNotEmpty(influxDatapoint.Tags, PluginConfig.DeviceLocation1Tag, data.Location1);
                    AddIfNotEmpty(influxDatapoint.Tags, PluginConfig.DeviceLocation2Tag, data.Location2);

                    foreach (var tag in value.Tags)
                    {
                        AddIfNotEmpty(influxDatapoint.Tags, tag.Key, tag.Value);
                    }

                    await queue.EnqueueAsync(influxDatapoint, tokenSource.Token).ConfigureAwait(false);
                }
            }

            return(false);
        }
Пример #3
0
        public async Task <bool> Record(RecordData data)
        {
            Interlocked.MemoryBarrier();
            if (peristenceDataMap == null)
            {
                throw new HspiException("Collection not started");
            }
            if (peristenceDataMap.TryGetValue(data.DeviceRefId, out var peristenceData))
            {
                foreach (var value in peristenceData)
                {
                    var fields = new Dictionary <string, object>();

                    if (!string.IsNullOrWhiteSpace(value.Field))
                    {
                        double deviceValue = data.DeviceValue;

                        if (IsValidRange(value, deviceValue))
                        {
                            fields.Add(value.Field, data.DeviceValue);
                        }
                        else
                        {
                            Trace.TraceInformation(Invariant($"Not Recording Value for {data.Name} as there is no it does not have valid ranged value at {deviceValue}"));
                        }
                    }

                    AddIfNotEmpty(fields, value.FieldString, data.DeviceString);

                    if (fields.Count == 0)
                    {
                        Trace.TraceInformation(Invariant($"Not Recording Value for {data.Name} as there is no valid value to record."));
                        continue;
                    }

                    var tags = new Dictionary <string, object>()
                    {
                        { PluginConfig.DeviceNameTag, data.Name },
                    };

                    tags.Add(PluginConfig.DeviceRefIdTag, data.DeviceRefId);
                    AddIfNotEmpty(tags, PluginConfig.DeviceLocation1Tag, data.Location1);
                    AddIfNotEmpty(tags, PluginConfig.DeviceLocation2Tag, data.Location2);

                    foreach (var tag in value.Tags)
                    {
                        AddIfNotEmpty(tags, tag.Key, tag.Value);
                    }

                    var point = new Point()
                    {
                        Name      = value.Measurement,
                        Fields    = fields,
                        Tags      = tags,
                        Timestamp = data.TimeStamp.ToUniversalTime(),
                    };

                    await queue.EnqueueAsync(point, cancellationToken).ConfigureAwait(false);
                }
            }

            return(false);
        }