public PumpModel2ParameterModel GetPumpModel2Parameter(Guid pumpId)
        {
            var result     = new PumpModel2ParameterModel();
            var mongoDB    = new ControlRoomMongoContext().GetDatabase();
            var collection = mongoDB.GetCollection <PumpStateModel2Mongo>();
            var pumpState  = collection.Find(t => t.PumpId == pumpId.ToString())
                             .SortByDescending(t => t.DateAndTime).FirstOrDefault();

            if (pumpState == null)
            {
                throw new DataValidationException("State data not found");
            }
            ///var engineCommand = db.PumpCommand.Where(t => t.PumpId == engineId).FirstOrDefault();

            if (pumpState != null)
            {
                result.FuelLevel           = pumpState.FuelLevel;
                result.FuelLevel           = pumpState.FuelLevel;
                result.Pressure            = pumpState.Pressure;
                result.FlowMeter           = pumpState.FlowMeter;
                result.TimeMTNHour         = pumpState.TimeMTNHour;
                result.TimeMTNMin          = pumpState.TimeMTNMin;
                result.TimeMTNSec          = pumpState.TimeMTNSec;
                result.OffFuel             = pumpState.LEDOffFuel == 1;
                result.CalibrationPressure = (int?)pumpState.CalibrationPressure;
                result.CalibrationFlow     = (int?)pumpState.CalibrationFlow;
                result.SpeedOver           = (int?)pumpState.SpeedOver;
                result.SetTimeMTN          = (int?)pumpState.SetTimeMTN;
            }

            result.PumpId = pumpId;

            return(result);
        }
        public IActionResult UpdatePumpStateModel2Parameter(PumpModel2ParameterModel data)
        {
            if (!controlPumpService.CheckPermission(data.PumpId, PumpPermissionType.UpdateParameter))
            {
                throw new PermissionDeniedException();
            }
            controlPumpService.UpdatePumpStateModel2Parameter(data);

            return(Ok(new SuccessResult(null)));
        }
        public void UpdatePumpStateModel2Parameter(PumpModel2ParameterModel data)
        {
            var pump = pumpRepository.GetAll().Include(t => t.Connection).Where(t => t.Id == data.PumpId && t.IsActive == true).FirstOrDefault();

            if (pump == null)
            {
                throw new DataValidationException("Pump not found or inactive.");
            }
            if (pump.IsLock)
            {
                throw new DataValidationException("Pump is locked, Please contact administrator.");
            }
            // VerifyResourcePermission(pump.Id, OperationResource.ControlPump_UpdateParameter);
            var connection = pump.Connection;

            if (connection == null || string.IsNullOrEmpty(connection.DatabaseName) || string.IsNullOrEmpty(connection.Password) || string.IsNullOrEmpty(connection.Server) || string.IsNullOrEmpty(connection.Username))
            {
                throw new DataValidationException("Connection data is not complete.");
            }
            if (!pump.PumpModelId.HasValue)
            {
                throw new DataValidationException("Unknown pump model.");
            }
            if (string.IsNullOrEmpty(pump.DatabaseNaming))
            {
                throw new DataValidationException("Database naming is empty");
            }

            var sb = new SqlConnectionStringBuilder();

            sb.DataSource         = connection.Server;
            sb.InitialCatalog     = connection.DatabaseName;
            sb.IntegratedSecurity = false;
            sb.UserID             = connection.Username;
            sb.Password           = CryptoHelper.DecryptText(connection.Password);
            var connectionString = sb.ConnectionString;


            var sql = $"update Button set Button_Push = 1, OFF_Fuel_{pump.DatabaseNaming} = @p0, " +
                      $"Calibration_Pressure_{pump.DatabaseNaming} = @p1, " +
                      $"Calibration_Flow_{pump.DatabaseNaming} = @p2, " +
                      $"Speed_Over_{pump.DatabaseNaming} = @p3, " +
                      $"Set_Time_MTN_{pump.DatabaseNaming} = @p4";

            SQLHelper.ExecuteNonQuery(connectionString, sql,
                                      data.OffFuel,
                                      data.CalibrationPressure,
                                      data.CalibrationFlow,
                                      data.SpeedOver,
                                      data.SetTimeMTN);
        }