public void SyncMeterConfigurationForInstance(string clientId, int instanceId, int meterId) { RemoteXDAInstance instance = DataContext.Table <RemoteXDAInstance>().QueryRecordWhere("ID = {0}", instanceId); MetersToDataPush meterToDataPush = DataContext.Table <MetersToDataPush>().QueryRecordWhere("ID = {0}", meterId); Meter localMeterRecord = DataContext.Table <Meter>().QueryRecordWhere("ID = {0}", meterToDataPush.LocalXDAMeterID); // get MeterLine table IEnumerable <MeterLine> localMeterLines = DataContext.Table <MeterLine>().QueryRecordsWhere("MeterID = {0}", localMeterRecord.ID); int progressTotal = localMeterLines.Count() + 2; int progressCount = 0; int remoteMeterLocationId = SyncMeterLocations(instance.Address, meterToDataPush, localMeterRecord); int meterGroupId = AddMeterGroup(instance.Address); OnUpdateProgressForMeter(clientId, localMeterRecord.AssetKey, (int)(100 * (progressCount) / progressTotal)); // if meter doesnt exist remotely add it AddMeter(instance.Address, meterToDataPush, localMeterRecord, remoteMeterLocationId); OnUpdateProgressForMeter(clientId, localMeterRecord.AssetKey, (int)(100 * (++progressCount) / progressTotal)); AddMeterMeterGroup(instance.Address, meterGroupId, meterToDataPush.RemoteXDAMeterID); OnUpdateProgressForMeter(clientId, localMeterRecord.AssetKey, (int)(100 * (++progressCount) / progressTotal)); // if there is a line for the meter ensure that its data has been uploaded remotely foreach (MeterLine meterLine in localMeterLines) { LinesToDataPush selectedLine = AddLine(instance.Address, meterLine, meterToDataPush.Obsfucate); // if MeterLine association has not been previously made, make it AddMeterLine(instance.Address, meterToDataPush, selectedLine); // ensure remote and local line impedance matches SyncLineImpedances(instance.Address, selectedLine); // add line to meterlocationline table int meterLocationLineID = SyncMeterLocationLines(instance.Address, selectedLine.RemoteXDALineID, remoteMeterLocationId); // ensure remote and local Source Impedance records match for the current meter line location SyncSourceImpedance(instance.Address, meterLocationLineID); // Sync Channel and channel dependant data SyncChannel(instance.Address, meterToDataPush, selectedLine); OnUpdateProgressForMeter(clientId, localMeterRecord.AssetKey, (int)(100 * (++progressCount) / progressTotal)); } }
public void SyncMeterFilesForInstance(string clientId, int instanceId, int meterId, DateTime?startTime = null, DateTime?endTime = null) { RemoteXDAInstance instance = DataContext.Table <RemoteXDAInstance>().QueryRecordWhere("ID = {0}", instanceId); MetersToDataPush meterToDataPush = DataContext.Table <MetersToDataPush>().QueryRecordWhere("ID = {0}", meterId); IEnumerable <FileGroup> localFileGroups = DataContext.Table <FileGroup>().QueryRecordsWhere("ID IN (SELECT FileGroupID From Event WHERE MeterID = {0})", meterToDataPush.LocalXDAMeterID); int progressTotal = (localFileGroups.Count() > 0 ? localFileGroups.Count() : 1); int progressCount = 0; OnUpdateProgressForMeter(clientId, meterToDataPush.LocalXDAAssetKey, (int)(100 * (progressCount) / progressTotal)); foreach (FileGroup fileGroup in localFileGroups) { FileGroupLocalToRemote fileGroupLocalToRemote = DataContext.Table <FileGroupLocalToRemote>().QueryRecordWhere("LocalFileGroupID = {0}", fileGroup.ID); if (fileGroupLocalToRemote == null) { FileGroup fg = new FileGroup() { ProcessingEndTime = fileGroup.ProcessingEndTime, ProcessingStartTime = fileGroup.ProcessingStartTime, DataEndTime = fileGroup.DataEndTime, DataStartTime = fileGroup.DataStartTime, Error = fileGroup.Error, FileHash = fileGroup.FileHash }; int remoteFileGroupId = WebAPIHub.CreateRecord(instance.Address, "FileGroup", JObject.FromObject(fg)); fileGroupLocalToRemote = new FileGroupLocalToRemote() { LocalFileGroupID = fileGroup.ID, RemoteFileGroupID = remoteFileGroupId }; DataContext.Table <FileGroupLocalToRemote>().AddNewRecord(fileGroupLocalToRemote); } IEnumerable <DataFile> localDataFiles = DataContext.Table <DataFile>().QueryRecordsWhere("FileGroupID = {0}", fileGroupLocalToRemote.LocalFileGroupID); IEnumerable <DataFile> remoteDataFiles = WebAPIHub.GetRecordsWhere(instance.Address, "DataFile", $"FileGroupID = {fileGroupLocalToRemote.RemoteFileGroupID}").Select(x => (DataFile)x); bool process = false; foreach (DataFile localDataFile in localDataFiles) { int remoteDataFileId; if (!remoteDataFiles.Where(x => x.FilePath == localDataFile.FilePath).Any()) { DataFile df = new DataFile() { CreationTime = localDataFile.CreationTime, FileGroupID = fileGroupLocalToRemote.RemoteFileGroupID, FilePath = localDataFile.FilePath, FilePathHash = localDataFile.FilePathHash, FileSize = localDataFile.FileSize, LastAccessTime = localDataFile.LastAccessTime, LastWriteTime = localDataFile.LastWriteTime }; remoteDataFileId = WebAPIHub.CreateRecord(instance.Address, "DataFile", JObject.FromObject(df)); process = true; } else { remoteDataFileId = remoteDataFiles.Where(x => x.FilePath == localDataFile.FilePath).First().ID; } FileBlob remoteFileBlob = (FileBlob)WebAPIHub.GetRecordsWhere(instance.Address, "FileBlob", $"DataFileID = {remoteDataFileId}").FirstOrDefault(); if (remoteFileBlob == null) { FileBlob localFileBlob = DataContext.Table <FileBlob>().QueryRecordWhere("DataFileID = {0}", localDataFile.ID); try { if (localFileBlob == null) { localFileBlob = new FileBlob() { DataFileID = localDataFile.ID, Blob = File.ReadAllBytes(localDataFile.FilePath) }; DataContext.Table <FileBlob>().AddNewRecord(localFileBlob); } } catch (Exception ex) { OnLogExceptionMessage(ex.ToString()); process = false; } localFileBlob.DataFileID = remoteDataFileId; WebAPIHub.CreateRecord(instance.Address, "FileBlob", JObject.FromObject(new FileBlob() { DataFileID = remoteDataFileId, Blob = localFileBlob.Blob })); } } if (process) { Dictionary <string, int> dictionary = new Dictionary <string, int>(); dictionary.Add("FileGroupID", fileGroupLocalToRemote.RemoteFileGroupID); dictionary.Add("MeterID", meterToDataPush.RemoteXDAMeterID); WebAPIHub.ProcessFileGroup(instance.Address, JObject.FromObject(dictionary)); } OnUpdateProgressForMeter(clientId, meterToDataPush.LocalXDAAssetKey, (int)(100 * (++progressCount) / progressTotal)); } }