private void SyncData(PumpConnectionData pumpConnection) { try { var db = new ControlRoomMongoContext().GetDatabase(); DateTime maxdatetime = DateTime.Now.AddDays(-3); if (pumpConnection.PumpModelId == PumpModelResource.Generator) { if (db.GetCollection <PumpStateModel1Mongo>().Find(t => t.PumpId == pumpConnection.PumpId.ToString()).Any()) { maxdatetime = db.GetCollection <PumpStateModel1Mongo>().Find(t => t.PumpId == pumpConnection.PumpId.ToString()).Sort("{DateAndTime: -1}").FirstOrDefault().DateAndTime; } maxdatetime = maxdatetime.ToLocalTime(); var query = $"select * from {pumpConnection.DatabaseNaming} where DateAndTime > @p0 and DateAndTime < (select max(DateAndTime) from {pumpConnection.DatabaseNaming})"; var stateData = SQLHelper.Query(pumpConnection.ConnectionString, query, maxdatetime).Tables[0]; var entities = new List <PumpStateModel1Mongo>(); foreach (var dr in stateData.Select()) { var pumpState = new PumpStateModel1Mongo(); pumpState.PumpId = pumpConnection.PumpId.ToString(); pumpState.DateAndTime = SQLHelper.TryGetValue <DateTime>(dr, "DateAndTime"); pumpState.IndexNo = SQLHelper.TryGetValue <int>(dr, "INDEX"); pumpState.StatusGen = SQLHelper.TryGetValue <bool?>(dr, "Status_Gen"); pumpState.StatusMotor = SQLHelper.TryGetValue <bool?>(dr, "Status_Motor"); pumpState.StatusBarGen = SQLHelper.TryGetValue <int?>(dr, "Status_Bar_Gen"); pumpState.LEDGen = SQLHelper.TryGetValue <bool?>(dr, "LED_Gen"); pumpState.StatusBarMotor = SQLHelper.TryGetValue <int?>(dr, "Status_Bar_Motor"); pumpState.LEDMotor = SQLHelper.TryGetValue <bool?>(dr, "LED_Motor"); pumpState.L1NGen = SQLHelper.TryGetValue <double?>(dr, "L1_N_Gen"); pumpState.L2NGen = SQLHelper.TryGetValue <double?>(dr, "L2_N_Gen"); pumpState.L3NGen = SQLHelper.TryGetValue <double?>(dr, "L3_N_Gen"); pumpState.TempGen = SQLHelper.TryGetValue <double?>(dr, "Temp_Gen"); pumpState.L1L2Gen = SQLHelper.TryGetValue <double?>(dr, "L1_L2_Gen"); pumpState.L2L3Gen = SQLHelper.TryGetValue <double?>(dr, "L2_L3_Gen"); pumpState.L3L1Gen = SQLHelper.TryGetValue <double?>(dr, "L3_L1_Gen"); pumpState.SpeedGen = SQLHelper.TryGetValue <double?>(dr, "Speed_Gen"); pumpState.Fuel = SQLHelper.TryGetValue <double?>(dr, "Fuel"); pumpState.Battery = SQLHelper.TryGetValue <double?>(dr, "Battery"); pumpState.Oil = SQLHelper.TryGetValue <double?>(dr, "Oil"); pumpState.Charge = SQLHelper.TryGetValue <double?>(dr, "Charge"); pumpState.FrequencyGen = SQLHelper.TryGetValue <double?>(dr, "Frequency_Gen"); pumpState.KWHGen = SQLHelper.TryGetValue <double?>(dr, "kWh_Gen"); pumpState.P1NMotor = SQLHelper.TryGetValue <double?>(dr, "P1_N_Motor"); pumpState.P2NMotor = SQLHelper.TryGetValue <double?>(dr, "P2_N_Motor"); pumpState.P3NMotor = SQLHelper.TryGetValue <double?>(dr, "P3_N_Motor"); pumpState.TempMotor = SQLHelper.TryGetValue <double?>(dr, "Temp_Motor"); pumpState.AmpL1 = SQLHelper.TryGetValue <double?>(dr, "Amp_L1"); pumpState.AmpL2 = SQLHelper.TryGetValue <double?>(dr, "Amp_L2"); pumpState.AmpL3 = SQLHelper.TryGetValue <double?>(dr, "Amp_L3"); pumpState.SpeedMotor = SQLHelper.TryGetValue <double?>(dr, "Speed_Motor"); pumpState.Pressure = SQLHelper.TryGetValue <double?>(dr, "Pressure"); pumpState.Vibration = SQLHelper.TryGetValue <double?>(dr, "Vibration"); pumpState.Flow = SQLHelper.TryGetValue <double?>(dr, "Flow"); pumpState.FrequencyMotor = SQLHelper.TryGetValue <double?>(dr, "Frequency_Motor"); pumpState.KWHMotor = SQLHelper.TryGetValue <double?>(dr, "kWh_Motor"); pumpState.TimeMTNHour = SQLHelper.TryGetValue <double?>(dr, "Time_MTN_Hour"); pumpState.TimeMTNMin = SQLHelper.TryGetValue <double?>(dr, "Time_MTN_Min"); pumpState.TimeMTNSec = SQLHelper.TryGetValue <double?>(dr, "Time_MTN_Sec"); pumpState.LEDIDELRate = SQLHelper.TryGetValue <int?>(dr, "LED_IDEL_RATE"); pumpState.KW1 = SQLHelper.TryGetValue <double?>(dr, "kW1"); pumpState.KW2 = SQLHelper.TryGetValue <double?>(dr, "kW2"); pumpState.KW3 = SQLHelper.TryGetValue <double?>(dr, "kW3"); pumpState.TempOver = SQLHelper.TryGetValue <double?>(dr, "Temp_Over"); pumpState.CalibrationPressure = SQLHelper.TryGetValue <double?>(dr, "Calibration_Pressure"); pumpState.CalibrationFlow = SQLHelper.TryGetValue <double?>(dr, "Calibration_Flow"); pumpState.OverLoad = SQLHelper.TryGetValue <double?>(dr, "Over_Load"); pumpState.SetTimeMTN = SQLHelper.TryGetValue <double?>(dr, "Set_Time_MTN"); pumpState.CreatedBy = null; pumpState.CreatedDate = DateTime.Now; entities.Add(pumpState); //db.EngineStateModel1.Add(engineState); } if (entities.Any()) { db.GetCollection <PumpStateModel1Mongo>().InsertMany(entities); } //Console.WriteLine($"{engineConnection.EngineName} {entities.Count()} rows"); } else if (pumpConnection.PumpId == PumpModelResource.Engine) { if (db.GetCollection <PumpStateModel2Mongo>().Find(t => t.PumpId == pumpConnection.PumpId.ToString()).Any()) { maxdatetime = db.GetCollection <PumpStateModel2Mongo>().Find(t => t.PumpId == pumpConnection.PumpId.ToString()).Sort("{DateAndTime: -1}").FirstOrDefault().DateAndTime; } maxdatetime = maxdatetime.ToLocalTime(); var query = $"select * from {pumpConnection.DatabaseNaming} where DateAndTime > @p0 and DateAndTime < (select max(DateAndTime) from {pumpConnection.DatabaseNaming})"; var stateData = SQLHelper.Query(pumpConnection.ConnectionString, query, maxdatetime).Tables[0]; var entities = new List <PumpStateModel2Mongo>(); foreach (var dr in stateData.Select()) { var pumpState = new PumpStateModel2Mongo(); pumpState.PumpId = pumpConnection.PumpId.ToString(); pumpState.DateAndTime = SQLHelper.TryGetValue <DateTime>(dr, "DateAndTime"); pumpState.IndexNo = SQLHelper.TryGetValue <int>(dr, "INDEX"); pumpState.Status = SQLHelper.TryGetValue <double?>(dr, "Status"); pumpState.LEDEngine = SQLHelper.TryGetValue <bool?>(dr, "LED_Engine"); pumpState.StatusBar = SQLHelper.TryGetValue <int?>(dr, "Status_Bar"); pumpState.Speed = SQLHelper.TryGetValue <double?>(dr, "Speed"); pumpState.FuelLevel = SQLHelper.TryGetValue <double?>(dr, "Fuel_Level"); pumpState.Battery = SQLHelper.TryGetValue <double?>(dr, "Battery"); pumpState.OilPressure = SQLHelper.TryGetValue <double?>(dr, "Oil_Pressure"); pumpState.Charge = SQLHelper.TryGetValue <double?>(dr, "Charge"); pumpState.Frequency = SQLHelper.TryGetValue <double?>(dr, "Frequency"); pumpState.KWh = SQLHelper.TryGetValue <double?>(dr, "kWh"); pumpState.Temperature = SQLHelper.TryGetValue <double?>(dr, "Temperature"); pumpState.Pressure = SQLHelper.TryGetValue <double?>(dr, "Pressure"); pumpState.Vibration = SQLHelper.TryGetValue <double?>(dr, "Vibration"); pumpState.FlowMeter = SQLHelper.TryGetValue <double?>(dr, "Flow_Meter"); pumpState.TimeMTNHour = SQLHelper.TryGetValue <double?>(dr, "Time_MTN_Hour"); pumpState.TimeMTNMin = SQLHelper.TryGetValue <double?>(dr, "Time_MTN_Min"); pumpState.TimeMTNSec = SQLHelper.TryGetValue <double?>(dr, "Time_MTN_Sec"); pumpState.LEDOffFuel = SQLHelper.TryGetValue <double?>(dr, "LED_Off_Fuel"); pumpState.CheckFuelLevel = SQLHelper.TryGetValue <double?>(dr, "Check_Fuel_Level"); pumpState.CalibrationPressure = SQLHelper.TryGetValue <double?>(dr, "Calibration_Pressure"); pumpState.CalibrationFlow = SQLHelper.TryGetValue <double?>(dr, "Calibration_Flow"); pumpState.SpeedOver = SQLHelper.TryGetValue <double?>(dr, "Speed_Over"); pumpState.SetTimeMTN = SQLHelper.TryGetValue <double?>(dr, "Set_Time_MTN"); pumpState.CreatedBy = null; pumpState.CreatedDate = DateTime.Now; // db.EngineStateModel2.Add(engineState); entities.Add(pumpState); } if (entities.Any()) { db.GetCollection <PumpStateModel2Mongo>().InsertMany(entities); } //Console.WriteLine($"{engineConnection.EngineName} {entities.Count()} rows"); } } catch (Exception ex) { logger.Error(ex, $"Error while syncing data {pumpConnection.PumpName}."); } }
private async Task SyncDataAsync(PumpConnectionData pumpConnection) { await Task.Run(() => SyncData(pumpConnection)); }