Esempio n. 1
0
        private void PushDataToRemoteInstances(AdoDataConnection connection, int fileGroupId, int meterId)
        {
            // If file group has already been pushed to a remote instance, return
            TableOperations <FileGroupLocalToRemote> fileGroupLocalToRemoteTable = new TableOperations <FileGroupLocalToRemote>(connection);
            FileGroupLocalToRemote fileGroup = fileGroupLocalToRemoteTable.QueryRecordWhere("LocalFileGroupID = {0}", fileGroupId);

            if (fileGroup != null)
            {
                Log.Info("File has already been pushed previously.");
                return;
            }

            TableOperations <RemoteXDAInstance> instanceTable = new TableOperations <RemoteXDAInstance>(connection);
            TableOperations <MetersToDataPush>  meterTable    = new TableOperations <MetersToDataPush>(connection);
            IEnumerable <RemoteXDAInstance>     instances     = instanceTable.QueryRecordsWhere("Frequency ='*'");
            DataPusherEngine engine = new DataPusherEngine();

            foreach (RemoteXDAInstance instance in instances)
            {
                IEnumerable <MetersToDataPush> meters = meterTable.QueryRecordsWhere("LocalXDAMeterID = {0} AND ID IN (SELECT MetersToDataPushID From RemoteXDAInstanceMeter WHERE RemoteXDAInstanceID = {1})", meterId, instance.ID);
                foreach (MetersToDataPush meter in meters)
                {
                    Log.Info($"Sending data to intance: {instance.Name} for FileGroup: {fileGroupId}...");
                    engine.SyncMeterFileForInstance(instance, meter, fileGroupId);
                }
            }
            Log.Info("Sync complete...");
        }
Esempio n. 2
0
        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));
            }
        }