public function testUpdateSensorData()
 {
     $this->get('/vehicle/fleet/dashboard/1990-01-01/1')->seeJsonContains(["temperature" => "80.00", "pressure" => "10.00"]);
     $updateDatetime = new \DateTime(date("Y-m-d H:i:s"));
     $updateDatetime->modify("-1 second");
     $updateDatetime = $updateDatetime->format("Y-m-d H:i:s");
     TireSensor::forceCreate(['part_id' => 2, 'number' => '123456', 'temperature' => '99', 'pressure' => '23']);
     $this->get('/vehicle/fleet/dashboard/' . $updateDatetime . '/1')->seeJsonContains(["temperature" => "99.00", "pressure" => "23.00"]);
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     DB::table('tire_sensor')->delete();
     TireSensor::forceCreate(['part_id' => 2, 'number' => '123456', 'temperature' => '80', 'pressure' => '10']);
 }
 public function getTireSensorHistoricalData($partsIds, $dateIni, $dateEnd)
 {
     $this->getFleetTireAndSensorParts($partsData, null, $partsIds);
     $tireSensor = TireSensor::select('part_id', 'temperature', 'pressure', 'created_at')->whereIn('part_id', $partsIds);
     if (!empty($dateIni) && $dateIni != '-') {
         $tireSensor = $tireSensor->where('created_at', '>=', $dateIni);
     }
     if (!empty($dateEnd) && $dateEnd != '-') {
         $tireSensor = $tireSensor->where('created_at', '<=', $dateEnd);
     }
     $tireSensor = $tireSensor->get();
     $historicalDataPos = $this->setHistoricalDataPositions($partsData);
     if (!empty($tireSensor)) {
         foreach ($tireSensor as $data) {
             $historicalDataPos[$partsData[$data->part_id]['position']][] = $data;
         }
     }
     return $this->getHistoricalData($historicalDataPos);
 }
 protected function getTiresWarningAndDanger($sensorsIds)
 {
     //         $sensors = \DB::select('part_id', \DB::raw('AVG(temperature) as avg_temperature'))
     //             ->from(
     //                 TireSensor::select('part_id', 'temperature')
     //                 ) as 't'
     //             ->groupBy('part_id')
     //             ->get();
     $sensorsReturn = [];
     $warningPressure = (int) config('app.tires_warning_pressure_percentage') * (int) config('app.tires_ideal_pressure') / 100;
     $sensorsReturn['warningMinPressure'] = (int) config('app.tires_ideal_pressure') - $warningPressure;
     $sensorsReturn['warningMaxPressure'] = (int) config('app.tires_ideal_pressure') + $warningPressure;
     $dangerPressure = (int) config('app.tires_danger_pressure_percentage') * (int) config('app.tires_ideal_pressure') / 100;
     $sensorsReturn['dangerMinPressure'] = (int) config('app.tires_ideal_pressure') - $dangerPressure;
     $sensorsReturn['dangerMaxPressure'] = (int) config('app.tires_ideal_pressure') + $dangerPressure;
     //         $sensors = \DB::select(\DB::raw('
     //             select part_id, avg(temperature) as avg_temperature, avg(pressure) as avg_pressure
     //             from (
     //                 select part_id, temperature, pressure
     //                 from tire_sensor
     //                 where (
     //                     select count(*) from tire_sensor as p
     //                     where p.part_id = tire_sensor.part_id and p.created_at >= tire_sensor.created_at
     //                     and p.part_id in ('.implode(',',$parts).')
     //                     ) <= 3
     //             ) as t
     //             group by part_id'));
     $sensors = TireSensor::select('part_id', 'temperature as avg_temperature', 'pressure as avg_pressure')->whereIn('tire_sensor.id', $sensorsIds)->get();
     if (!empty($sensors)) {
         foreach ($sensors as $sensor) {
             $sensorsReturn['parts'][$sensor->part_id] = $sensor;
         }
     }
     return $sensorsReturn;
 }