Пример #1
0
        private static void LoadChannelAttributes(Meter meter, Line line, MeterLocation remoteMeterLocation, Channel channel, string channelKey, LookupTables lookupTables)
        {
            if ((object)remoteMeterLocation != null)
            {
                channel.Name = string.Format("{0}({1}) {2}", remoteMeterLocation.Name, line.AssetKey, channelKey);
            }
            else
            {
                channel.Name = string.Format("({0}) {1}", line.AssetKey, channelKey);
            }

            channel.HarmonicGroup   = 0;
            channel.MeasurementType = lookupTables.MeasurementTypeLookup.GetOrAdd(GetMeasurementTypeName(channelKey), name => new MeasurementType()
            {
                Name = name, Description = name
            });
            channel.MeasurementCharacteristic = lookupTables.MeasurementCharacteristicLookup.GetOrAdd("Instantaneous", name => new MeasurementCharacteristic()
            {
                Name = name, Description = name
            });
            channel.Phase = lookupTables.PhaseLookup.GetOrAdd(GetPhaseName(channelKey), name => new Phase()
            {
                Name = name, Description = name
            });

            channel.Meter = meter;
            channel.Line  = line;
        }
Пример #2
0
        private static Meter ParseDataSource(DataSourceRecord dataSource)
        {
            Meter         meter;
            MeterLocation meterLocation;

            string name        = dataSource.DataSourceName;
            Guid   vendorID    = dataSource.VendorID;
            Guid   equipmentID = dataSource.EquipmentID;

            meter           = new Meter();
            meter.Name      = name;
            meter.AssetKey  = name;
            meter.ShortName = name.Substring(0, Math.Min(name.Length, 50));

            meterLocation             = new MeterLocation();
            meterLocation.AssetKey    = meter.Name;
            meterLocation.Name        = string.Format("{0} location", meter.Name);
            meterLocation.ShortName   = meterLocation.Name.Substring(0, Math.Min(meterLocation.Name.Length, 50));
            meterLocation.Description = meterLocation.Name;

            if (vendorID != Vendor.None)
            {
                meter.Make = Vendor.ToString(vendorID);
            }

            if (equipmentID != Guid.Empty)
            {
                meter.Model = Equipment.ToString(equipmentID);
            }

            return(meter);
        }
Пример #3
0
        private static MeterLocationLine Link(MeterLocation meterLocation, Line line, Dictionary <Tuple <string, string>, MeterLocationLine> meterLocationLineLookup)
        {
            Tuple <string, string> key = Tuple.Create(meterLocation.AssetKey, line.AssetKey);
            MeterLocationLine      meterLocationLine;

            if (!meterLocationLineLookup.TryGetValue(key, out meterLocationLine))
            {
                meterLocationLine = new MeterLocationLine()
                {
                    MeterLocation = meterLocation,
                    Line          = line
                };

                meterLocationLineLookup.Add(key, meterLocationLine);
            }

            return(meterLocationLine);
        }
Пример #4
0
        private int SyncMeterLocations(string address, MetersToDataPush meterToDataPush, Meter localMeterRecord)
        {
            List <MeterLocation> remoteMeterLocations = WebAPIHub.GetRecords(address, "MeterLocation", "all").Select(x => (MeterLocation)x).ToList();

            if (meterToDataPush.Obsfucate)
            {
                // if the company meter location does not exist, create it
                if (!remoteMeterLocations.Where(x => x.AssetKey == WebAPIHub.CompanyName).Any())
                {
                    MeterLocation record = new MeterLocation()
                    {
                        AssetKey    = WebAPIHub.CompanyName,
                        Name        = WebAPIHub.CompanyName,
                        Alias       = WebAPIHub.CompanyName,
                        ShortName   = "",
                        Description = "",
                        Latitude    = 0.0F,
                        Longitude   = 0.0F
                    };

                    return(WebAPIHub.CreateRecord(address, "MeterLocation", JObject.FromObject(record)));
                }
                else
                {
                    return(remoteMeterLocations.Where(x => x.AssetKey == WebAPIHub.CompanyName).First().ID);
                }
            }
            else
            {
                MeterLocation meterLocation = DataContext.Table <MeterLocation>().QueryRecordWhere("ID = {0}", localMeterRecord.MeterLocationID);

                if (!remoteMeterLocations.Where(x => x.AssetKey == meterLocation.AssetKey).Any())
                {
                    return(WebAPIHub.CreateRecord(address, "MeterLocation", JObject.FromObject(meterLocation)));
                }
                else
                {
                    return(remoteMeterLocations.Where(x => x.AssetKey == meterLocation.AssetKey).First().ID);
                }
            }
        }
Пример #5
0
        private static void LoadRemoteMeterLocationAttributes(MeterLocation meterLocation, XElement lineElement)
        {
            string meterLocationName = (string)lineElement.Element("endStationName");
            string latitude          = (string)lineElement.Element("endStationLatitude");
            string longitude         = (string)lineElement.Element("endStationLongitude");

            if (meterLocation.Name != meterLocationName)
            {
                meterLocation.Name      = meterLocationName;
                meterLocation.ShortName = new string(meterLocationName.Take(50).ToArray());
            }

            if ((object)latitude != null)
            {
                meterLocation.Latitude = Convert.ToDouble(latitude);
            }

            if ((object)longitude != null)
            {
                meterLocation.Longitude = Convert.ToDouble(longitude);
            }
        }
Пример #6
0
        private static MeterLocation LoadRemoteMeterLocationAttributes(LookupTables lookupTables, XElement lineElement, AdoDataConnection connection)
        {
            MeterLocation meterLocation = new MeterLocation {
                Name      = (string)lineElement.Element("endStationName"),
                ShortName = new string(((string)lineElement.Element("endStationName")).Take(50).ToArray()),
                AssetKey  = (string)lineElement.Element("endStationID"),
                Latitude  = double.Parse((string)lineElement.Element("endStationLatitude") ?? "0"),
                Longitude = double.Parse((string)lineElement.Element("endStationLongitude") ?? "0")
            };

            if (lookupTables.MeterLocationLookup.ContainsKey(meterLocation.AssetKey))
            {
                meterLocation.ID = lookupTables.MeterLocationLookup[meterLocation.AssetKey].ID;
                (new TableOperations <MeterLocation>(connection)).UpdateRecord(meterLocation);
            }
            else
            {
                (new TableOperations <MeterLocation>(connection)).AddNewRecord(meterLocation);
                meterLocation.ID = connection.ExecuteScalar <int>("SELECT @@IDENTITY");
                lookupTables.MeterLocationLookup.Add(meterLocation.AssetKey, meterLocation);
            }

            return(meterLocation);
        }
Пример #7
0
        /// <summary>
        /// Parses the file into a meter data set per meter contained in the file.
        /// </summary>
        /// <param name="filePath">The path to the file to be parsed.</param>
        /// <returns>List of meter data sets, one per meter.</returns>
        public void Parse(string filePath)
        {
            if ((object)m_eventFile == null)
            {
                m_eventFile = EventFile.Parse(filePath, SystemFrequency, MaxFileDuration);
            }

            if (!m_eventFile.EventReports.Any() && !m_eventFile.CommaSeparatedEventReports.Any())
            {
                return;
            }

            Header header = m_eventFile.EventReports.FirstOrDefault()?.Header
                            ?? m_eventFile.CommaSeparatedEventReports[0].Header;

            Meter meter = new Meter();

            meter.MeterLocation = new MeterLocation();
            meter.Channels      = new List <Channel>();
            meter.AssetKey      = header.RelayID;
            meter.Name          = header.RelayID;
            meter.ShortName     = new string(header.RelayID.ToNonNullString().Take(50).ToArray());

            MeterLocation meterLocation = meter.MeterLocation;

            meterLocation.Meters = new List <Meter>()
            {
                meter
            };
            meterLocation.AssetKey    = header.StationID;
            meterLocation.Name        = header.StationID;
            meterLocation.ShortName   = new string(header.StationID.ToNonNullString().Take(50).ToArray());
            meterLocation.Description = header.StationID;

            foreach (EventReport report in m_eventFile.EventReports)
            {
                for (int i = 0; i < report.AnalogSection.AnalogChannels.Count; i++)
                {
                    Channel channel = MakeParsedAnalog(report, i);
                    channel.Meter = meter;
                    meter.Channels.Add(channel);

                    List <DateTime> timeSamples  = report.AnalogSection.TimeChannel.Samples;
                    List <double>   valueSamples = report.AnalogSection.AnalogChannels[i].Samples;
                    DataSeries      dataSeries   = new DataSeries();

                    dataSeries.DataPoints = timeSamples
                                            .Zip(valueSamples, (time, value) => new DataPoint()
                    {
                        Time = time, Value = value
                    })
                                            .ToList();

                    if (new string[] { "VA", "VB", "VC", "VS" }.Contains(report.AnalogSection.AnalogChannels[i].Name))
                    {
                        dataSeries = dataSeries.Multiply(1000.0D);
                    }

                    dataSeries.SeriesInfo = channel.Series[0];
                    m_meterDataSet.DataSeries.Add(dataSeries);
                }

                for (int i = 0; i < report.AnalogSection.DigitalChannels.Count; i++)
                {
                    Channel channel = MakeParsedDigital(report, i);

                    if (channel.Name == "*")
                    {
                        continue;
                    }

                    channel.Meter = meter;
                    meter.Channels.Add(channel);

                    List <DateTime> timeSamples    = report.AnalogSection.TimeChannel.Samples;
                    List <bool?>    digitalSamples = report.AnalogSection.DigitalChannels[i].Samples;
                    DataSeries      dataSeries     = new DataSeries();

                    dataSeries.SeriesInfo = channel.Series[0];

                    dataSeries.DataPoints = timeSamples
                                            .Zip(digitalSamples, (time, value) => new { Time = time, Value = value })
                                            .Where(x => x.Value != null)
                                            .Select(x => new DataPoint {
                        Time = x.Time, Value = Convert.ToDouble(x.Value)
                    })
                                            .ToList();

                    m_meterDataSet.Digitals.Add(dataSeries);
                }

                ComplexNumber z1 = new ComplexNumber(0.0D, 0.0D);
                ComplexNumber z0 = new ComplexNumber(0.0D, 0.0D);
                double        groupSetting;

                if (double.TryParse(report.GetGroupSettings("Z1MAG"), out groupSetting))
                {
                    z1.Magnitude = groupSetting;
                }

                if (double.TryParse(report.GetGroupSettings("Z1ANG"), out groupSetting))
                {
                    z1.Angle = Angle.FromDegrees(groupSetting);
                }

                if (double.TryParse(report.GetGroupSettings("Z0MAG"), out groupSetting))
                {
                    z0.Magnitude = groupSetting;
                }

                if (double.TryParse(report.GetGroupSettings("Z0ANG"), out groupSetting))
                {
                    z0.Angle = Angle.FromDegrees(groupSetting);
                }

                if (z1 != z0)
                {
                    m_meterDataSet.Configuration.R1 = z1.Real;
                    m_meterDataSet.Configuration.X1 = z1.Imaginary;
                    m_meterDataSet.Configuration.R0 = z0.Real;
                    m_meterDataSet.Configuration.X0 = z0.Imaginary;

                    if (double.TryParse(report.GetGroupSettings("LL"), out groupSetting))
                    {
                        m_meterDataSet.Configuration.LineLength = groupSetting;
                    }
                }
            }

            foreach (CommaSeparatedEventReport report in m_eventFile.CommaSeparatedEventReports)
            {
                for (int i = 0; i < report.AnalogSection.AnalogChannels.Count; i++)
                {
                    Channel channel = MakeParsedAnalog(report, i);
                    channel.Meter = meter;
                    meter.Channels.Add(channel);

                    List <DateTime> timeSamples  = report.AnalogSection.TimeChannel.Samples;
                    List <double>   valueSamples = report.AnalogSection.AnalogChannels[i].Samples;
                    DataSeries      dataSeries   = new DataSeries();

                    dataSeries.DataPoints = timeSamples
                                            .Zip(valueSamples, (time, value) => new DataPoint()
                    {
                        Time = time, Value = value
                    })
                                            .ToList();

                    dataSeries.SeriesInfo = channel.Series[0];
                    m_meterDataSet.DataSeries.Add(dataSeries);
                }

                for (int i = 0; i < report.AnalogSection.DigitalChannels.Count; i++)
                {
                    Channel channel = MakeParsedDigital(report, i);

                    if (channel.Name == "*")
                    {
                        continue;
                    }

                    channel.Meter = meter;
                    meter.Channels.Add(channel);

                    List <DateTime> timeSamples    = report.AnalogSection.TimeChannel.Samples;
                    List <bool?>    digitalSamples = report.AnalogSection.DigitalChannels[i].Samples;
                    DataSeries      dataSeries     = new DataSeries();

                    dataSeries.SeriesInfo = channel.Series[0];

                    dataSeries.DataPoints = timeSamples
                                            .Zip(digitalSamples, (time, value) => new { Time = time, Value = value })
                                            .Where(x => x.Value != null)
                                            .Select(x => new DataPoint {
                        Time = x.Time, Value = Convert.ToDouble(x.Value)
                    })
                                            .ToList();

                    m_meterDataSet.Digitals.Add(dataSeries);
                }
            }

            m_meterDataSet.Meter = meter;
        }
Пример #8
0
        public override void Execute(MeterDataSet meterDataSet)
        {
            Log.Info("Executing operation to load trending summary data into the openHistorian...");

            ImportedMeasurementsTable importedMeasurementsTable = null;

            if ((m_historianSettings.SecurePassword?.Length ?? 0) > 0)
            {
                string url      = m_historianSettings.URL;
                string username = m_historianSettings.InstanceName;
                string password = m_historianSettings.Password;
                importedMeasurementsTable = new ImportedMeasurementsTable(url, username, password);
            }

            using (Historian historian = new Historian(m_historianSettings.Server, m_historianSettings.InstanceName))
            {
                Dictionary <Channel, List <TrendingDataSummaryResource.TrendingDataSummary> > trendingDataSummaries = meterDataSet.GetResource <TrendingDataSummaryResource>().TrendingDataSummaries;

                // Import measurement metadata into the historian metadata container
                if ((object)importedMeasurementsTable != null)
                {
                    List <ImportedMeasurement> measurements = new List <ImportedMeasurement>();

                    foreach (Channel channel in trendingDataSummaries.Keys)
                    {
                        int channelID = channel.ID;

                        foreach (SeriesID seriesID in Enum.GetValues(typeof(SeriesID)))
                        {
                            long pointID = ToPointID(channelID, seriesID);
                            ImportedMeasurement measurement = importedMeasurementsTable.FindByID(pointID).FirstOrDefault();

                            if ((object)measurement == null)
                            {
                                Func <string, int, string> truncate = (str, len) => (str.Length > len) ? str.Substring(0, len) : str;

                                Meter         meter         = meterDataSet.Meter;
                                MeterLocation meterLocation = meter.MeterLocation;

                                string measurementType           = channel.MeasurementType.Name;
                                string measurementCharacteristic = channel.MeasurementCharacteristic.Name;
                                string phase      = channel.Phase.Name;
                                string seriesType = seriesID.ToString();

                                string pointTag          = $"{meter.AssetKey}:{measurementCharacteristic}";
                                string alternateTag      = channel.Name;
                                string signalTypeAcronym = measurementCharacteristic.ToUpper();
                                string signalReference   = $"{meter.AssetKey}-{measurementCharacteristic}";

                                measurement = new ImportedMeasurement()
                                {
                                    SignalID          = Guid.NewGuid(),
                                    PointID           = pointID,
                                    PointTag          = truncate($"{meter.AssetKey}:{measurementCharacteristic}", 200),
                                    AlternateTag      = truncate(channel.Name, 200),
                                    SignalTypeAcronym = truncate(measurementCharacteristic, 4).ToUpper(),
                                    SignalReference   = truncate($"{meter.AssetKey}-{measurementCharacteristic}", 200),
                                    ProtocolAcronym   = "TREND",
                                    ProtocolType      = "Trending",
                                    Phase             = phase[0],
                                    Description       = $"{seriesType} {measurementCharacteristic} ({phase} {measurementType})",
                                    Enabled           = true
                                };

                                if (meterLocation.Latitude != 0.0D && meterLocation.Longitude != 0.0D)
                                {
                                    measurement.Longitude = meterLocation.Longitude;
                                    measurement.Latitude  = meterLocation.Latitude;
                                }

                                measurements.Add(measurement);
                            }
                        }
                    }

                    importedMeasurementsTable.ImportMeasurements(measurements);
                }

                foreach (KeyValuePair <Channel, List <TrendingDataSummaryResource.TrendingDataSummary> > channelSummaries in trendingDataSummaries)
                {
                    // Reduce data set to valid summaries
                    IEnumerable <TrendingDataSummaryResource.TrendingDataSummary> validSummaries = channelSummaries.Value.Where(summary => summary.IsValid);
                    int channelID = channelSummaries.Key.ID;

                    foreach (TrendingDataSummaryResource.TrendingDataSummary summary in validSummaries)
                    {
                        // Write minimum series value
                        if (!summary.IsDuplicate)
                        {
                            historian.Write(channelID, SeriesID.Minimum, summary.Time, summary.Minimum);
                        }

                        // Write maximum series value
                        if (!summary.IsDuplicate)
                        {
                            historian.Write(channelID, SeriesID.Maximum, summary.Time, summary.Maximum);
                        }

                        // Write average series value
                        if (!summary.IsDuplicate)
                        {
                            historian.Write(channelID, SeriesID.Average, summary.Time, summary.Average);
                        }
                    }
                }

                // Wait for queue
                // to be processed
                historian.Flush();
            }
        }
        public void Parse(string filePath)
        {
            Meter meter = new Meter();

            meter.MeterLocation = new MeterLocation();
            meter.Channels      = new List <Channel>();

            MeterLocation meterLocation = meter.MeterLocation;

            meterLocation.Meters = new List <Meter>()
            {
                meter
            };

            try
            {
                // open file and read first line
                StreamReader file = new StreamReader(filePath);
                string       line = file.ReadLine();

                // if first line is not header columns move down until you read the header columns
                while (!line.Contains(","))
                {
                    line = file.ReadLine();
                }

                // build channels and dataseries from header columns
                string[] headers = CSVDecode(line);
                for (int i = 1; i < headers.Length; ++i)
                {
                    if (headers[i].ToLower() == "flag" || headers[i].ToLower() == "nothing")
                    {
                        continue;
                    }

                    Channel channel = ParseSeries(headers[i]);
                    channel.Meter = meter;

                    DataSeries dataSeries = new DataSeries();
                    dataSeries.SeriesInfo = channel.Series[0];
                    meter.Channels.Add(channel);
                    m_meterDataSet.DataSeries.Add(dataSeries);
                    Channels.Add(headers[i], m_meterDataSet.DataSeries.Count - 1);
                }

                while ((line = file.ReadLine()) != null)
                {
                    string[] data      = CSVDecode(line);
                    string   timeStamp = data[0];

                    for (int i = 1; i < headers.Length; i++)
                    {
                        if (headers[i].ToLower() == "flag" || headers[i].ToLower() == "nothing")
                        {
                            continue;
                        }
                        m_meterDataSet.DataSeries[Channels[headers[i]]].DataPoints.Add(new DataPoint()
                        {
                            Time = DateTime.Parse(timeStamp), Value = double.Parse(data[i])
                        });
                    }
                }
            }
            catch (InvalidOperationException ex)
            {
                Log.Warn(ex.Message, ex);
            }

            m_meterDataSet.Meter = meter;
        }
Пример #10
0
        public void Parse(string filePath)
        {
            Schema schema = Parser.Schema;

            Meter meter = new Meter();

            meter.MeterLocation = new MeterLocation();
            meter.Channels      = new List <Channel>();
            meter.AssetKey      = schema.DeviceID;
            meter.Name          = schema.DeviceID;
            meter.ShortName     = schema.DeviceID.Substring(0, Math.Min(schema.DeviceID.Length, 50));

            MeterLocation meterLocation = meter.MeterLocation;

            meterLocation.Meters = new List <Meter>()
            {
                meter
            };
            meterLocation.AssetKey    = schema.StationName;
            meterLocation.Name        = schema.StationName;
            meterLocation.ShortName   = schema.StationName.Substring(0, Math.Min(schema.StationName.Length, 50));
            meterLocation.Description = schema.StationName;

            foreach (AnalogChannel analogChannel in schema.AnalogChannels)
            {
                Channel channel = ParseSeries(analogChannel);
                channel.Meter = meter;

                DataSeries dataSeries = new DataSeries();
                dataSeries.SeriesInfo = channel.Series[0];

                meter.Channels.Add(channel);

                while (MeterDataSet.DataSeries.Count <= analogChannel.Index)
                {
                    MeterDataSet.DataSeries.Add(new DataSeries());
                }

                MeterDataSet.DataSeries[analogChannel.Index] = dataSeries;
            }

            foreach (DigitalChannel digitalChannel in schema.DigitalChannels)
            {
                Channel channel = ParseSeries(digitalChannel);
                channel.Meter = meter;

                DataSeries dataSeries = new DataSeries();
                dataSeries.SeriesInfo = channel.Series[0];

                meter.Channels.Add(channel);

                while (MeterDataSet.Digitals.Count <= digitalChannel.Index)
                {
                    MeterDataSet.Digitals.Add(new DataSeries());
                }

                MeterDataSet.Digitals[digitalChannel.Index] = dataSeries;
            }

            try
            {
                while (Parser.ReadNext())
                {
                    for (int i = 0; i < schema.AnalogChannels.Length; i++)
                    {
                        int    seriesIndex = schema.AnalogChannels[i].Index;
                        string units       = schema.AnalogChannels[i].Units.ToUpper();
                        double multiplier  = (units.Contains("KA") || units.Contains("KV")) ? 1000.0D : 1.0D;
                        MeterDataSet.DataSeries[seriesIndex].DataPoints.Add(new DataPoint()
                        {
                            Time = Parser.Timestamp, Value = multiplier * Parser.PrimaryValues[i]
                        });
                    }

                    for (int i = 0; i < schema.DigitalChannels.Length; i++)
                    {
                        int valuesIndex = schema.TotalAnalogChannels + i;
                        int seriesIndex = schema.DigitalChannels[i].Index;
                        MeterDataSet.Digitals[seriesIndex].DataPoints.Add(new DataPoint()
                        {
                            Time = Parser.Timestamp, Value = Parser.Values[valuesIndex]
                        });
                    }
                }
            }
            catch (InvalidOperationException ex)
            {
                Log.Warn(ex.Message, ex);
            }

            MeterDataSet.Meter = meter;

            string infFilePath = Path.ChangeExtension(filePath, "inf");

            if (File.Exists(infFilePath))
            {
                IniFile    infFile    = new IniFile(infFilePath);
                INFDataSet infDataSet = new INFDataSet(infFile);
                MeterDataSet.GetResource(() => new BreakerRestrikeResource(infDataSet));
            }
        }
Пример #11
0
        public void Parse(string filePath)
        {
            Meter meter = new Meter();

            meter.MeterLocation = new MeterLocation();
            meter.Channels      = new List <Channel>();
            meter.AssetKey      = "UNKNOWN";
            meter.Name          = "Unknown";
            meter.Description   = "Unknown";
            meter.Make          = "Eaton";
            meter.Model         = "EMCB";

            MeterLocation meterLocation = meter.MeterLocation;

            meterLocation.AssetKey    = "UNKNOWN";
            meterLocation.Latitude    = 0.0D;
            meterLocation.Longitude   = 0.0D;
            meterLocation.Name        = "Unknown";
            meterLocation.Description = "Unknown";

            DataSeries CreateDataSeries(string channelName, string type, string characteristic, string phase, DateTime[] timeSeries, JArray valueSeries)
            {
                if (valueSeries == null)
                {
                    return(null);
                }

                const string SeriesType = "Values";

                DataSeries dataSeries = new DataSeries();

                dataSeries.DataPoints = timeSeries
                                        .Zip(valueSeries, (time, token) => new DataPoint()
                {
                    Time = time, Value = token.Value <double>()
                })
                                        .ToList();

                // Convert from mV/mA to Volts/Amps
                dataSeries = dataSeries.Multiply(1.0D / 1000.0D);

                Series series = dataSeries.SeriesInfo = new Series();

                series.Channel    = new Channel();
                series.SeriesType = new SeriesType()
                {
                    Name = SeriesType
                };

                Channel channel = series.Channel;

                channel.Series = new List <Series>()
                {
                    series
                };
                channel.MeasurementType = new MeasurementType()
                {
                    Name = type
                };
                channel.MeasurementCharacteristic = new MeasurementCharacteristic()
                {
                    Name = characteristic
                };
                channel.Phase = new Phase()
                {
                    Name = phase
                };
                channel.Name          = channelName;
                channel.Description   = channelName;
                channel.HarmonicGroup = 0;

                if (type == "Voltage")
                {
                    channel.PerUnitValue = 120.0D;
                }

                return(dataSeries);
            }

            void Parse(byte[] fileData, string characteristic)
            {
                using (MemoryStream fileStream = new MemoryStream(fileData))
                    using (GZipStream decompressor = new GZipStream(fileStream, CompressionMode.Decompress))
                        using (MemoryStream jsonStream = new MemoryStream())
                            using (TextReader textReader = new StreamReader(jsonStream))
                                using (JsonReader jsonReader = new JsonTextReader(textReader))
                                {
                                    decompressor.CopyTo(jsonStream);
                                    decompressor.Dispose();
                                    jsonStream.Position = 0;

                                    dynamic obj      = JObject.Load(jsonReader);
                                    string  meterKey = obj.idAgent;

                                    meter.AssetKey            = meterKey;
                                    meter.Name                = meterKey;
                                    meter.Description         = meterKey;
                                    meterLocation.AssetKey    = meterKey;
                                    meterLocation.Name        = meterKey;
                                    meterLocation.Description = meterKey;

                                    double startMilliseconds = obj.startTime;
                                    double deltaTime         = obj.deltaTime;
                                    int    samples           = obj.numSamples;

                                    DateTime epoch     = new DateTime(1970, 1, 1);
                                    DateTime startTime = epoch.AddMilliseconds(startMilliseconds);

                                    DateTime[] timeSeries = Enumerable.Range(0, samples)
                                                            .Select(i => i * deltaTime)
                                                            .Select(startTime.AddMilliseconds)
                                                            .ToArray();

                                    DataSeries[] allSeries =
                                    {
                                        CreateDataSeries("VAN", "Voltage", characteristic, "AN", timeSeries, obj.mVpA),
                                        CreateDataSeries("VBN", "Voltage", characteristic, "BN", timeSeries, obj.mVpB),
                                        CreateDataSeries("VCN", "Voltage", characteristic, "CN", timeSeries, obj.mVpC),
                                        CreateDataSeries("IAN", "Current", characteristic, "AN", timeSeries, obj.mApA),
                                        CreateDataSeries("IBN", "Current", characteristic, "BN", timeSeries, obj.mApB),
                                        CreateDataSeries("ICN", "Current", characteristic, "CN", timeSeries, obj.mApC)
                                    };

                                    List <DataSeries> allValidSeries = allSeries
                                                                       .Where(dataSeries => dataSeries != null)
                                                                       .ToList();

                                    MeterDataSet.Meter = meter;
                                    MeterDataSet.DataSeries.AddRange(allValidSeries);

                                    IEnumerable <Channel> allChannels = allValidSeries.Select(dataSeries => dataSeries.SeriesInfo.Channel);
                                    meter.Channels.AddRange(allChannels);
                                }
            }

            foreach (byte[] waveformData in WaveformData)
            {
                Parse(waveformData, "Instantaneous");
            }

            foreach (byte[] waveformData in FastRMSData)
            {
                Parse(waveformData, "RMS");
            }
        }
Пример #12
0
        public void Parse(string filePath)
        {
            Meter meter = new Meter();

            meter.MeterLocation = new MeterLocation();
            meter.Channels      = new List <Channel>();
            meter.AssetKey      = "UNKNOWN";
            meter.Name          = "Unknown";
            meter.Description   = "Unknown EMCB";
            meter.Make          = "Eaton";
            meter.Model         = "EMCB";

            MeterLocation meterLocation = meter.MeterLocation;

            meterLocation.AssetKey    = "UNKNOWN";
            meterLocation.Latitude    = 0.0D;
            meterLocation.Longitude   = 0.0D;
            meterLocation.Name        = "Unknown";
            meterLocation.Description = "Unknown";

            Dictionary <string, DataSeries> dataSeriesLookup = new Dictionary <string, DataSeries>();

            void AddChannel(string channelName, string type, string characteristic, string phase, Dictionary <string, string> seriesMappings)
            {
                Channel channel = new Channel();

                channel.Series          = new List <Series>();
                channel.Meter           = meter;
                channel.Name            = channelName;
                channel.MeasurementType = new MeasurementType()
                {
                    Name = type
                };
                channel.MeasurementCharacteristic = new MeasurementCharacteristic()
                {
                    Name = characteristic
                };
                channel.Phase = new Phase()
                {
                    Name = phase
                };
                channel.HarmonicGroup = 0;

                if (type == "Voltage")
                {
                    channel.PerUnitValue = 120.0D;
                }

                foreach (KeyValuePair <string, string> mapping in seriesMappings)
                {
                    string path       = mapping.Key;
                    string seriesType = mapping.Value;

                    Series series = new Series();
                    series.Channel    = channel;
                    series.SeriesType = new SeriesType()
                    {
                        Name = seriesType
                    };
                    channel.Series.Add(series);

                    DataSeries dataSeries = new DataSeries();
                    dataSeries.SeriesInfo = series;
                    dataSeriesLookup.Add(path, dataSeries);
                }

                meter.Channels.Add(channel);
            }

            AddChannel("VAN", "Voltage", "RMS", "AN", new Dictionary <string, string>()
            {
                { "statistics.voltage.LN.AN.avg", "Average" },
                { "statistics.voltage.LN.AN.min", "Minimum" },
                { "statistics.voltage.LN.AN.max", "Maxmimum" },
                { "voltage.LN.AN", "Values" }
            });

            AddChannel("VBN", "Voltage", "RMS", "BN", new Dictionary <string, string>()
            {
                { "statistics.voltage.LN.BN.avg", "Average" },
                { "statistics.voltage.LN.BN.min", "Minimum" },
                { "statistics.voltage.LN.BN.max", "Maxmimum" },
                { "voltage.LN.BN", "Values" }
            });

            AddChannel("VCN", "Voltage", "RMS", "CN", new Dictionary <string, string>()
            {
                { "statistics.voltage.LN.CN.avg", "Average" },
                { "statistics.voltage.LN.CN.min", "Minimum" },
                { "statistics.voltage.LN.CN.max", "Maxmimum" },
                { "voltage.LN.CN", "Values" }
            });

            AddChannel("VAB", "Voltage", "RMS", "AB", new Dictionary <string, string>()
            {
                { "statistics.voltage.LL.AB.avg", "Average" },
                { "statistics.voltage.LL.AB.min", "Minimum" },
                { "statistics.voltage.LL.AB.max", "Maxmimum" },
                { "voltage.LL.AB", "Values" }
            });

            AddChannel("VBC", "Voltage", "RMS", "BC", new Dictionary <string, string>()
            {
                { "statistics.voltage.LL.BC.avg", "Average" },
                { "statistics.voltage.LL.BC.min", "Minimum" },
                { "statistics.voltage.LL.BC.max", "Maxmimum" },
                { "voltage.LL.BC", "Values" }
            });

            AddChannel("VCA", "Voltage", "RMS", "CA", new Dictionary <string, string>()
            {
                { "statistics.voltage.LL.CA.avg", "Average" },
                { "statistics.voltage.LL.CA.min", "Minimum" },
                { "statistics.voltage.LL.CA.max", "Maxmimum" },
                { "voltage.LL.CA", "Values" }
            });

            AddChannel("IAN", "Current", "RMS", "AN", new Dictionary <string, string>()
            {
                { "statistics.current.A.avg", "Average" },
                { "statistics.current.A.min", "Minimum" },
                { "statistics.current.A.max", "Maxmimum" },
                { "current.A", "Values" }
            });

            AddChannel("IBN", "Current", "RMS", "BN", new Dictionary <string, string>()
            {
                { "statistics.current.B.avg", "Average" },
                { "statistics.current.B.min", "Minimum" },
                { "statistics.current.B.max", "Maxmimum" },
                { "current.B", "Values" }
            });

            AddChannel("ICN", "Current", "RMS", "CN", new Dictionary <string, string>()
            {
                { "statistics.current.C.avg", "Average" },
                { "statistics.current.C.min", "Minimum" },
                { "statistics.current.C.max", "Maxmimum" },
                { "current.C", "Values" }
            });

            AddChannel("FREQ", "Voltage", "Frequency", "AN", new Dictionary <string, string>()
            {
                { "statistics.frequency.avg", "Average" },
                { "statistics.frequency.min", "Minimum" },
                { "statistics.frequency.max", "Maxmimum" },
                { "frequency", "Values" }
            });

            AddChannel("Q1 Active Energy AN", "Energy", "PIntgPos", "AN", new Dictionary <string, string>()
            {
                { "energy.q1mJpA", "Total" }
            });
            AddChannel("Q2 Active Energy AN", "Energy", "PIntgNeg", "AN", new Dictionary <string, string>()
            {
                { "energy.q2mJpA", "Total" }
            });
            AddChannel("Q3 Active Energy AN", "Energy", "PIntgNeg", "AN", new Dictionary <string, string>()
            {
                { "energy.q3mJpA", "Total" }
            });
            AddChannel("Q4 Active Energy AN", "Energy", "PIntgPos", "AN", new Dictionary <string, string>()
            {
                { "energy.q4mJpA", "Total" }
            });
            AddChannel("Q1 Active Energy BN", "Energy", "PIntgPos", "BN", new Dictionary <string, string>()
            {
                { "energy.q1mJpB", "Total" }
            });
            AddChannel("Q2 Active Energy BN", "Energy", "PIntgNeg", "BN", new Dictionary <string, string>()
            {
                { "energy.q2mJpB", "Total" }
            });
            AddChannel("Q3 Active Energy BN", "Energy", "PIntgNeg", "BN", new Dictionary <string, string>()
            {
                { "energy.q3mJpB", "Total" }
            });
            AddChannel("Q4 Active Energy BN", "Energy", "PIntgPos", "BN", new Dictionary <string, string>()
            {
                { "energy.q4mJpB", "Total" }
            });
            AddChannel("Q1 Active Energy CN", "Energy", "PIntgPos", "CN", new Dictionary <string, string>()
            {
                { "energy.q1mJpC", "Total" }
            });
            AddChannel("Q2 Active Energy CN", "Energy", "PIntgNeg", "CN", new Dictionary <string, string>()
            {
                { "energy.q2mJpC", "Total" }
            });
            AddChannel("Q3 Active Energy CN", "Energy", "PIntgNeg", "CN", new Dictionary <string, string>()
            {
                { "energy.q3mJpC", "Total" }
            });
            AddChannel("Q4 Active Energy CN", "Energy", "PIntgPos", "CN", new Dictionary <string, string>()
            {
                { "energy.q4mJpC", "Total" }
            });

            AddChannel("Q1 Reactive Energy AN", "Energy", "QIntgPos", "AN", new Dictionary <string, string>()
            {
                { "energy.q1mVARspA", "Total" }
            });
            AddChannel("Q2 Reactive Energy AN", "Energy", "QIntgPos", "AN", new Dictionary <string, string>()
            {
                { "energy.q2mVARspA", "Total" }
            });
            AddChannel("Q3 Reactive Energy AN", "Energy", "QIntgNeg", "AN", new Dictionary <string, string>()
            {
                { "energy.q3mVARspA", "Total" }
            });
            AddChannel("Q4 Reactive Energy AN", "Energy", "QIntgNeg", "AN", new Dictionary <string, string>()
            {
                { "energy.q4mVARspA", "Total" }
            });
            AddChannel("Q1 Reactive Energy BN", "Energy", "QIntgPos", "BN", new Dictionary <string, string>()
            {
                { "energy.q1mVARspB", "Total" }
            });
            AddChannel("Q2 Reactive Energy BN", "Energy", "QIntgPos", "BN", new Dictionary <string, string>()
            {
                { "energy.q2mVARspB", "Total" }
            });
            AddChannel("Q3 Reactive Energy BN", "Energy", "QIntgNeg", "BN", new Dictionary <string, string>()
            {
                { "energy.q3mVARspB", "Total" }
            });
            AddChannel("Q4 Reactive Energy BN", "Energy", "QIntgNeg", "BN", new Dictionary <string, string>()
            {
                { "energy.q4mVARspB", "Total" }
            });
            AddChannel("Q1 Reactive Energy CN", "Energy", "QIntgPos", "CN", new Dictionary <string, string>()
            {
                { "energy.q1mVARspC", "Total" }
            });
            AddChannel("Q2 Reactive Energy CN", "Energy", "QIntgPos", "CN", new Dictionary <string, string>()
            {
                { "energy.q2mVARspC", "Total" }
            });
            AddChannel("Q3 Reactive Energy CN", "Energy", "QIntgNeg", "CN", new Dictionary <string, string>()
            {
                { "energy.q3mVARspC", "Total" }
            });
            AddChannel("Q4 Reactive Energy CN", "Energy", "QIntgNeg", "CN", new Dictionary <string, string>()
            {
                { "energy.q4mVARspC", "Total" }
            });

            JToken Get(JToken obj, string path) =>
            path.Split('.').Aggregate(obj, (token, identifier) => token?[identifier]);

            foreach (byte[] fileData in FileData)
            {
                using (MemoryStream fileStream = new MemoryStream(fileData))
                    using (GZipStream decompressor = new GZipStream(fileStream, CompressionMode.Decompress))
                        using (MemoryStream jsonStream = new MemoryStream())
                            using (TextReader textReader = new StreamReader(jsonStream))
                                using (JsonReader jsonReader = new JsonTextReader(textReader))
                                {
                                    decompressor.CopyTo(jsonStream);
                                    decompressor.Dispose();
                                    jsonStream.Position = 0;

                                    JObject obj = JObject.Load(jsonReader);

                                    DateTime epoch     = new DateTime(1970, 1, 1);
                                    double   ts        = obj["ts"].Value <double>();
                                    DateTime timestamp = epoch.AddSeconds(ts);

                                    foreach (KeyValuePair <string, DataSeries> kvp in dataSeriesLookup)
                                    {
                                        string     path       = kvp.Key;
                                        DataSeries dataSeries = kvp.Value;
                                        JToken     token      = Get(obj, path);

                                        if (token != null)
                                        {
                                            double    value     = token.Value <double>();
                                            DataPoint dataPoint = new DataPoint()
                                            {
                                                Time = timestamp, Value = value
                                            };
                                            dataSeries.DataPoints.Add(dataPoint);
                                        }
                                    }
                                }
            }

            foreach (KeyValuePair <string, DataSeries> kvp in dataSeriesLookup)
            {
                DataSeries dataSeries = kvp.Value;

                if (dataSeries.DataPoints.Count > 0)
                {
                    string path = kvp.Key;

                    double adjustment = path.StartsWith("energy")
                        ? (1.0D / 1000.0D / 3600.0D)  // mJ -> Watt-hours || mVAR*s -> VAR-hours
                        : (1.0D / 1000.0D);           // mV -> Volts || mA -> Amps || mHz -> Hz

                    DataSeries adjustedData = dataSeries.Multiply(adjustment);
                    adjustedData.SeriesInfo = dataSeries.SeriesInfo;
                    MeterDataSet.DataSeries.Add(adjustedData);
                }
            }

            MeterDataSet.Meter = meter;
        }
Пример #13
0
 public void UpdateMeterLocation(MeterLocation record)
 {
     DataContext.Table <MeterLocation>().UpdateRecord(record);
 }
Пример #14
0
 public void AddNewMeterLocation(MeterLocation record)
 {
     DataContext.Table <MeterLocation>().AddNewRecord(record);
 }
Пример #15
0
        public void Parse(string filePath)
        {
            ControlFile controlFile    = m_parser.ControlFile;
            string      identityString = controlFile.IdentityString.value;
            string      deviceName     = identityString.Substring(0, IndexOf(identityString, "\r\n", "\n", "\r"));

            Meter meter = new Meter();

            meter.MeterLocation = new MeterLocation();
            meter.Channels      = new List <Channel>();
            meter.AssetKey      = deviceName;
            meter.Name          = deviceName;
            meter.ShortName     = deviceName.Substring(0, Math.Min(deviceName.Length, 50));

            MeterLocation meterLocation = meter.MeterLocation;

            meterLocation.Meters = new List <Meter>()
            {
                meter
            };
            meterLocation.AssetKey    = deviceName;
            meterLocation.Name        = deviceName;
            meterLocation.ShortName   = meter.ShortName;
            meterLocation.Description = deviceName;

            List <ANLG_CHNL_NEW> analogChannels = controlFile.AnalogChannelSettings
                                                  .OrderBy(kvp => kvp.Key)
                                                  .Select(kvp => kvp.Value)
                                                  .ToList();

            List <EVNT_CHNL_NEW> digitalChannels = controlFile.EventChannelSettings
                                                   .OrderBy(kvp => kvp.Key)
                                                   .Select(kvp => kvp.Value)
                                                   .ToList();

            // Add an empty data series for 1-based indexing
            m_meterDataSet.DataSeries.Add(new DataSeries());
            m_meterDataSet.Digitals.Add(new DataSeries());

            foreach (ANLG_CHNL_NEW analogChannel in analogChannels)
            {
                Channel channel = ParseSeries(analogChannel);
                channel.Meter = meter;
                channel.Series.Single().SourceIndexes = m_meterDataSet.DataSeries.Count.ToString();

                DataSeries dataSeries = new DataSeries();
                dataSeries.SeriesInfo = channel.Series[0];

                meter.Channels.Add(channel);

                m_meterDataSet.DataSeries.Add(dataSeries);
            }

            foreach (EVNT_CHNL_NEW digitalChannel in digitalChannels)
            {
                Channel channel = ParseSeries(digitalChannel);
                channel.Series.Single().SourceIndexes = m_meterDataSet.Digitals.Count.ToString();

                DataSeries series = new DataSeries();
                series.SeriesInfo = channel.Series[0];

                meter.Channels.Add(channel);

                m_meterDataSet.Digitals.Add(series);
            }

            while (m_parser.ReadNext())
            {
                DateTime timestamp = m_emaxSettings.ApplyTimestampCorrection ? m_parser.CalculatedTimestamp : m_parser.ParsedTimestamp;

                for (int i = 0; i < analogChannels.Count; i++)
                {
                    double value = m_emaxSettings.ApplyValueCorrection ? m_parser.CorrectedValues[i] : m_parser.Values[i];
                    m_meterDataSet.DataSeries[i + 1].DataPoints.Add(new DataPoint()
                    {
                        Time = timestamp, Value = value
                    });
                }

                for (int i = 0; i < digitalChannels.Count; i++)
                {
                    int    bitCount   = sizeof(ushort) * 8;
                    int    groupIndex = i / bitCount;
                    int    bitIndex   = i % bitCount;
                    ushort mask       = (ushort)~(0x8000u >> bitIndex);
                    double value      = m_parser.EventGroups[groupIndex] & mask;
                    m_meterDataSet.Digitals[i + 1].DataPoints.Add(new DataPoint()
                    {
                        Time = timestamp, Value = value
                    });
                }
            }

            if (!string.IsNullOrEmpty(m_emaxSettings.COMTRADEExportDirectory))
            {
                TryExportToCOMTRADE(filePath, controlFile, identityString, analogChannels, digitalChannels);
            }

            m_meterDataSet.Meter = meter;
        }