Beispiel #1
0
 /**
  * @param Device $thermostat
  *
  * @return \Illuminate\Http\JsonResponse
  */
 public function overview(Device $thermostat)
 {
     $timezone = $thermostat->structure->tz();
     $city = City::findCity($thermostat->structure->postal_code, $thermostat->structure->country_code);
     /** @var \Grumpydictator\Gchart\GChart $chart */
     $chart = App::make('gchart');
     $chart->addColumn('Time', 'timeofday');
     $chart->addColumn('Target temperature', 'number');
     $chart->addColumn('Ambient temperature', 'number');
     $chart->addColumn('Outside temperature', 'number');
     $start = Carbon::now($timezone)->startOfDay();
     $end = Carbon::now($timezone)->endOfDay();
     $start->timezone('utc');
     $end->timezone('utc');
     // get all entries between start and end.
     $ambient = LogEntry::betweenTimes($start, $end)->withLogValue('target_temperature_c')->withLogValue('ambient_temperature_c')->orderBy('date', 'ASC')->where('log_entries.device_id', $thermostat->id)->get([DB::Raw('DATE_FORMAT(`log_entries`.`time`,"%Y-%m-%d %H:%i") as `date`'), DB::Raw('`target_temperature_c`.`value` AS `target_temperature_c`'), DB::Raw('`ambient_temperature_c`.`value` AS `ambient_temperature_c`')]);
     $chartData = [];
     foreach ($ambient as $entry) {
         /** @var Carbon $time */
         $time = $entry->date;
         $chartData[$time] = ['target_temperature_c' => floatval($entry->target_temperature_c), 'ambient_temperature_c' => floatval($entry->ambient_temperature_c), 'main_temp' => null];
     }
     unset($entry, $ambient, $time);
     // get outside temperature between start and end:
     $outside = Report::betweenTimes($start, $end)->withReportValue('main.temp')->orderBy('reports.time', 'ASC')->where('reports.city_id', $city->id)->get([DB::Raw('DATE_FORMAT(`reports`.`time`,"%Y-%m-%d %H:%i") as `date`'), DB::Raw('`main_temp`.`value` AS `main_temp`')]);
     foreach ($outside as $entry) {
         $time = $entry->date;
         $chartData[$time]['main_temp'] = floatval($entry->main_temp);
     }
     ksort($chartData);
     $previous = null;
     foreach ($chartData as $time => $row) {
         $obj = new Carbon($time);
         $obj->timezone($timezone);
         if (is_null($previous)) {
             $previous = clone $obj;
         }
         $target = isset($row['target_temperature_c']) ? $row['target_temperature_c'] : null;
         $ambient = isset($row['ambient_temperature_c']) ? $row['ambient_temperature_c'] : null;
         $main = isset($row['main_temp']) ? $row['main_temp'] : null;
         // only add row when previous is not null and time is not smaller than previous
         if (!is_null($previous) && $obj >= $previous) {
             $chart->addRow($obj, $target, $ambient, $main);
             $previous = clone $obj;
         }
     }
     $chart->generate();
     return Response::json($chart->getData());
 }