/** * This is the same routine as the default month chart and is designed to give some debug information to the * user. * * @return \Illuminate\Http\JsonResponse|string */ public function debugSummary() { echo "<pre>\n"; /** @var \Marauder\Helper\Chart\ChartHelperInterface $helper */ $helper = App::make('Marauder\\Helper\\Chart\\ChartHelperInterface'); $start = $helper->getStartDateForPeriod('month'); $end = $helper->getEndDateForPeriod('month'); $compareDateFormat = 'Y-m-d'; $thermostats = $helper->getAllThermostats(); /** @var Device $thermostat */ foreach ($thermostats as $thermostat) { echo 'Thermostat #' . $thermostat->id . ' (' . $thermostat->name . '):' . "\n"; // leafs and is_heating_on $city = City::findCity($thermostat->structure->postal_code, $thermostat->structure->country_code); $leafs = $helper->getLogEntriesInRange('has_leaf', $thermostat, $start, $end, 'month'); $heating = $helper->getLogEntriesInRange('is_heating_on', $thermostat, $start, $end, 'month'); $temperatures = $helper->getTemperaturesForCity($city, $start, $end, 'month'); // also get summaries: $summarizedLeafs = $helper->getSummarizedValuesInRange('has_leaf', $thermostat, $start, $end, 'month'); $summarizedHeating = $helper->getSummarizedValuesInRange('is_heating_on', $thermostat, $start, $end, 'month'); while ($start <= $end) { $format = $start->format($compareDateFormat); // filter leafs: $leaf = $leafs->filter(function (LogEntry $entry) use($format) { if ($entry->date == $format) { return $entry; } }); // filter summarized leafs: $summarizedLeaf = $summarizedLeafs->filter(function (SummaryEntry $entry) use($format) { if ($entry->date == $format) { return $entry; } }); // filter heating: $heat = $heating->filter(function (LogEntry $entry) use($format) { if ($entry->date == $format) { return $entry; } }); // filter summarized heating: $summarizedHeat = $summarizedHeating->filter(function (SummaryEntry $entry) use($format) { if ($entry->date == $format) { return $entry; } }); // filter temperature: $temp = $temperatures->filter(function (Report $entry) use($format) { if ($entry->date == $format) { return $entry; } }); $leafCount = 0; $heatingCount = 0; $temperature = null; $foundLeafInSummary = false; $foundHeatInSummary = false; if ($leaf->count() == 1) { $leafCount = intval($leaf->first()->singleValue) / 12; } else { if ($summarizedLeaf->count() == 1) { $leafCount = intval($summarizedLeaf->first()->singleValue) / 60; $foundLeafInSummary = true; } } if ($heat->count() == 1) { $heatingCount = intval($heat->first()->singleValue) / 12; } else { if ($summarizedHeat->count() == 1) { $heatingCount = intval($summarizedHeat->first()->singleValue) / 60; $foundHeatInSummary = true; } } if ($temp->count() == 1) { $temperature = floatval($temp->first()->main_temp); } echo 'Now at date ' . $format . ': [leaf: ' . round($leafCount, 1) . '], [' . round($heatingCount, 1) . '], [' . round($temperature, 1) . 'C]'; if ($foundLeafInSummary) { echo ' [leaf-summarized]'; } if ($foundHeatInSummary) { echo ' [heat-summarized]'; } echo "\n"; $start->addDay(); } echo "\n"; } }
/** * @return \Illuminate\View\View * @throws Exception * */ public function rawWeatherTable() { $title = 'Raw weather'; $fields = Config::get('marauder.rawWeatherEntries'); // all of the users log entries: $get = ['reports.id', 'reports.time']; /* * get the users cities: */ $structures = Auth::user()->structures()->get(); $cities = []; /** @var Structure $structure */ foreach ($structures as $structure) { $city = City::findCity($structure->postal_code, $structure->country_code); $cities[] = $city->id; } $cities = array_unique($cities); $query = Report::whereIn('reports.city_id', $cities)->take(Config::get('marauder.weatherTableLimit'))->orderBy('time', 'DESC')->orderBy('reports.city_id', 'ASC'); foreach ($fields as $field) { $query->withReportValue($field); $fieldName = str_replace('.', '_', $field); $get[] = $fieldName . '.value as ' . $fieldName; } $result = $query->get($get); if ($result->count() > 0) { $result = $result->toArray(); } foreach ($result as $key => $entry) { foreach ($entry as $name => $value) { $result[$key][$name] = Format::format($name, $value); } } $ignore = []; return View::make('raw.table', compact('result', 'title', 'ignore')); }
/** * @param Device $device * * @return \Illuminate\Http\JsonResponse */ public function energyUseYear(Device $device) { $chart = App::make('gchart'); $chart->addColumn('Date', 'date'); $chart->addColumn('Leaf time', 'number'); $chart->addColumn('Heating', 'number'); $chart->addColumn('Outside temperature', 'number'); $city = City::findCity($device->structure->postal_code, $device->structure->country_code); $today = new Carbon(); $today->endOfMonth()->addDay(); $lastMonth = clone $today; $lastMonth->subYear(); while ($lastMonth < $today) { // get is_heating_on and has_leaf for this day: $entry = $device->logEntries()->inMonth($lastMonth)->withLogValue('has_leaf')->withLogValue('is_heating_on')->first([DB::Raw('sum(has_leaf.value) / 12 as has_leaf'), DB::Raw('sum(is_heating_on.value) / 12 as is_heating_on')]); $heating = null; $leaftime = null; $outside = null; // if both are null, get data from summaries instead: if (!is_null($entry->has_leaf) && !is_null($entry->is_heating_on)) { $heating = floatval($entry->is_heating_on); $leaftime = floatval($entry->has_leaf); } else { // get summaries (or try at least): $entry = $device->summaryEntries()->inMonth($lastMonth)->withSummaryValue('has_leaf')->withSummaryValue('is_heating_on')->first([DB::Raw('sum(has_leaf.value) / 60 as has_leaf'), DB::Raw('sum(is_heating_on.value) / 60 as is_heating_on')]); $heating = is_null($entry->is_heating_on) ? null : floatval($entry->is_heating_on); $leaftime = is_null($entry->has_leaf) ? null : floatval($entry->has_leaf); } // get weather for city: $report = $city->reports()->inMonth($lastMonth)->withReportValue('main.temp')->first([DB::Raw('avg(main_temp.value) as main_temp')]); if (!is_null($report)) { $outside = !is_null($report->main_temp) ? floatval($report->main_temp) : null; } $chart->addRow(clone $lastMonth, $leaftime, $heating, $outside); $lastMonth->endOfMonth()->addDay(); } $chart->generate(); return Response::json($chart->getData()); }