Ejemplo n.º 1
0
        private Meter LoadMeterFromDatabase(MeterDataSet meterDataSet)
        {
            using (AdoDataConnection connection = meterDataSet.CreateDbConnection())
            {
                Meter LoadCurrentMeter()
                {
                    TableOperations <Meter> meterTable = new TableOperations <Meter>(connection);
                    Meter parsedMeter = meterDataSet.Meter;
                    Meter dbMeter     = meterTable.QueryRecordWhere("AssetKey = {0}", parsedMeter.AssetKey);

                    dbMeter.ConnectionFactory = meterDataSet.CreateDbConnection;
                    return(dbMeter);
                }

                Meter LoadHistoricMeter()
                {
                    const string ConfigKey = "openXDA";

                    TableOperations <MeterConfiguration> meterConfigurationTable = new TableOperations <MeterConfiguration>(connection);

                    RecordRestriction recordRestriction =
                        new RecordRestriction("ConfigKey = {0}", ConfigKey) &
                        new RecordRestriction("{0} IN (SELECT FileGroupID FROM FileGroupMeterConfiguration WHERE MeterConfigurationID = MeterConfiguration.ID)", meterDataSet.FileGroup.ID);

                    MeterConfiguration meterConfiguration = meterConfigurationTable.QueryRecord("ID DESC", recordRestriction);

                    if (meterConfiguration == null)
                    {
                        // Need to find the oldest configuration record for this meter
                        Meter dbMeter = LoadCurrentMeter();
                        int?  meterID = dbMeter?.ID;

                        recordRestriction =
                            new RecordRestriction("MeterID = {0}", meterID) &
                            new RecordRestriction("ConfigKey = {0}", ConfigKey) &
                            new RecordRestriction("ID NOT IN (SELECT DiffID FROM MeterConfiguration WHERE DiffID IS NOT NULL)");

                        meterConfiguration = meterConfigurationTable.QueryRecord("ID", recordRestriction);
                    }

                    if (meterConfiguration == null)
                    {
                        return(null);
                    }

                    MeterSettingsSheet settingsSheet = new MeterSettingsSheet(meterConfigurationTable, meterConfiguration);

                    return(settingsSheet.Meter);
                }

                Log.Info("Locating meter in database...");

                if (meterDataSet.LoadHistoricConfiguration)
                {
                    return(LoadHistoricMeter());
                }

                return(LoadCurrentMeter());
            }
        }
Ejemplo n.º 2
0
        public static Event GetEvent(this TableOperations <Event> eventTable, FileGroup fileGroup, DataGroup dataGroup)
        {
            int      fileGroupID = fileGroup.ID;
            int      lineID      = dataGroup.Line.ID;
            DateTime startTime   = dataGroup.StartTime;
            DateTime endTime     = dataGroup.EndTime;
            int      samples     = dataGroup.Samples;

            IDbDataParameter startTimeParameter = new SqlParameter()
            {
                ParameterName = nameof(dataGroup.StartTime),
                DbType        = DbType.DateTime2,
                Value         = startTime
            };

            IDbDataParameter endTimeParameter = new SqlParameter()
            {
                ParameterName = nameof(dataGroup.EndTime),
                DbType        = DbType.DateTime2,
                Value         = endTime
            };

            RecordRestriction recordRestriction =
                new RecordRestriction("FileGroupID = {0}", fileGroupID) &
                new RecordRestriction("LineID = {0}", lineID) &
                new RecordRestriction("StartTime = {0}", startTimeParameter) &
                new RecordRestriction("EndTime = {0}", endTimeParameter) &
                new RecordRestriction("Samples = {0}", samples);

            return(eventTable.QueryRecord(recordRestriction));
        }
Ejemplo n.º 3
0
            public void Initialize(AdoDataConnection connection, VICycleDataGroup viCycleDataGroup, double systemFrequency)
            {
                int samplesPerCycle = Transform.CalculateSamplesPerCycle(viCycleDataGroup.VA.RMS, systemFrequency);

                TableOperations <FaultSegment> faultSegmentTable = new TableOperations <FaultSegment>(connection);

                RecordRestriction recordRestriction =
                    new RecordRestriction("EventID = {0}", Fault.EventID) &
                    new RecordRestriction("StartTime = {0}", ToDateTime2(connection, Fault.Inception)) &
                    new RecordRestriction("(SELECT Name FROM SegmentType WHERE ID = SegmentTypeID) = 'Fault'");

                FaultSegment faultSegment = faultSegmentTable.QueryRecord(recordRestriction);

                if ((object)faultSegment == null)
                {
                    throw new InvalidOperationException($"Unable to find fault segment that matches fault summary for event {Fault.EventID}.");
                }

                StartSample    = faultSegment.StartSample;
                EndSample      = faultSegment.EndSample - samplesPerCycle + 1;
                CycleDataGroup = Rotate(viCycleDataGroup.ToSubSet(StartSample, EndSample));

                DistanceCurve.StartIndex = StartSample;
                AngleCurve.StartIndex    = StartSample;
            }
Ejemplo n.º 4
0
        private static void GenerateEmail(LineEvent lineEvent)
        {
            using (AdoDataConnection connection = s_connectionFactory())
            {
                TableOperations <Event> eventTable = new TableOperations <Event>(connection);

                object startTime2 = ToDateTime2(connection, lineEvent.TimeRange.Start);
                object endTime2   = ToDateTime2(connection, lineEvent.TimeRange.End);

                RecordRestriction recordRestriction =
                    new RecordRestriction("LineID = {0}", lineEvent.LineID) &
                    new RecordRestriction("StartTime >= {0}", startTime2) &
                    new RecordRestriction("EndTime <= {0}", endTime2);

                Event evt = eventTable.QueryRecord(recordRestriction);

                GenerateEmail(connection, evt.ID);
            }
        }
Ejemplo n.º 5
0
        public static void PatchLatestConfiguration(this TableOperations <MeterConfiguration> meterConfigurationTable, Meter meter, string configKey, string newConfigText)
        {
            RecordRestriction latestConfigurationQueryRestriction =
                new RecordRestriction("MeterID = {0}", meter.ID) &
                new RecordRestriction("ConfigKey = {0}", configKey) &
                new RecordRestriction("DiffID IS NULL");

            MeterConfiguration latestConfiguration = meterConfigurationTable.QueryRecord("RevisionMajor DESC, RevisionMinor DESC", latestConfigurationQueryRestriction);

            if (latestConfiguration == null)
            {
                MeterConfiguration newConfiguration = new MeterConfiguration();
                newConfiguration.MeterID       = meter.ID;
                newConfiguration.ConfigKey     = configKey;
                newConfiguration.ConfigText    = newConfigText;
                newConfiguration.RevisionMajor = 0;
                newConfiguration.RevisionMinor = 0;
                meterConfigurationTable.AddNewRecord(newConfiguration);
                return;
            }

            meterConfigurationTable.Patch(latestConfiguration, newConfigText);
        }