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()); } }
public MeterSettingsSheet(TableOperations <MeterConfiguration> meterConfigurationTable, MeterConfiguration meterConfiguration) { string sheetText = meterConfigurationTable.Unpatch(meterConfiguration); XDocument sheet = XDocument.Parse(sheetText); Meter = ToMeter(sheet); Sheet = sheet; }