예제 #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());
 }
예제 #2
0
 /**
  * Set the value of the "updated at" attribute.
  *
  * @param  mixed  $value
  * @return void
  */
 public function setUpdatedAt($value)
 {
     $this->{static::UPDATED_AT} = $value;
     if (!$value instanceof Carbon) {
         $value = new Carbon($value);
     }
     $this->{static::UPDATED_AT . '_gmt'} = $value->timezone('GMT');
 }
예제 #3
0
 public function publish($date, Carbon $startAt, Carbon $finishAt, $serviceId, $capacity = 1)
 {
     $vacancyKeys = ['business_id' => $this->business->id, 'service_id' => $serviceId, 'date' => $date];
     $vacancyValues = ['capacity' => intval($capacity), 'start_at' => $startAt->timezone('UTC')->toDateTimeString(), 'finish_at' => $finishAt->timezone('UTC')->toDateTimeString()];
     return Vacancy::updateOrCreate($vacancyKeys, $vacancyValues);
 }
예제 #4
0
 /**
  * Between Dates.
  *
  * @param Illuminate\Database\Query $query
  * @param Carbon                    $startAt
  * @param Carbon                    $finishAt
  *
  * @return Illuminate\Database\Query
  */
 public function scopeAffectingInterval($query, Carbon $startAt, Carbon $finishAt)
 {
     return $query->where(function ($query) use($startAt, $finishAt) {
         $query->where(function ($query) use($startAt, $finishAt) {
             $query->where('finish_at', '>=', $finishAt->timezone('UTC'))->where('start_at', '<=', $startAt->timezone('UTC'));
         })->orWhere(function ($query) use($startAt, $finishAt) {
             $query->where('finish_at', '<', $finishAt->timezone('UTC'))->where('finish_at', '>', $startAt->timezone('UTC'));
         })->orWhere(function ($query) use($startAt, $finishAt) {
             $query->where('start_at', '>', $startAt->timezone('UTC'))->where('start_at', '<', $finishAt->timezone('UTC'));
         })->orWhere(function ($query) use($startAt, $finishAt) {
             $query->where('start_at', '>', $startAt->timezone('UTC'))->where('finish_at', '<', $finishAt->timezone('UTC'));
         });
     });
 }
예제 #5
0
 /**
  * Scope only till date.
  *
  * @param Illuminate\Database\Query $query
  * @param Carbon                    $date  Inquired range end date
  *
  * @return Illuminate\Database\Query Scoped appointments up to the inquired date
  */
 public function scopeTillDate($query, Carbon $date)
 {
     return $query->where('start_at', '<=', $date->timezone('UTC'));
 }
 /**
  * 日本時間で「今日の同時刻」を得る
  *
  * @param Carbon $time
  * @return Carbon
  */
 private function todayAtSameTime(Carbon $time)
 {
     $timezone = $time->timezone;
     // 日本人の感覚の「今日」にするため、日本時間で設定
     $sameTime = Carbon::now()->timezone('Asia/Tokyo');
     $time->timezone('Asia/Tokyo');
     $sameTime->hour = $time->hour;
     $sameTime->minute = $time->minute;
     $sameTime->second = $time->second;
     return $sameTime->timezone($timezone);
 }
 public function __toString()
 {
     // 日付はタイムゾーンがバラバラなので、明示的に日本時間にする
     return 'URL:' . $this->source . "\n" . '日付:' . $this->date->timezone('Asia/Tokyo')->toDateTimeString() . "\n" . 'コンテンツ:' . "\n" . $this->content . "\n";
 }