<?php

class Temperature extends Controller {

	private function GetCurrentTemperatureData() {
		$temps=HomeAutomationService::GetBothTemperatures();
		$vars=new stdClass();
		$vars->target=HomeAutomationService::GetTargetTemperature();
		$vars->mode=HomeAutomationService::GetCurrentMode();
		$vars->temperature=$temps->inside;
		$vars->outsideTemperature=$temps->outside;
		return $vars;
	}

	private function GetLast24HrReadings(ITemperatureReadingRepository $tempRepo) {
		$dateTo=new DateTime();
		$dateFrom=clone $dateTo;
		$dateFrom->sub(new DateInterval("P1D"));
		return $tempRepo->GetRange($dateFrom, $dateTo);
	}
	
	public function Index(ITemperatureReadingRepository $tempRepo) {
		return new View("Temperature/index.view",
			array(
				"current"=>$this->GetCurrentTemperatureData(),
				"readings"=>$this->GetLast24HrReadings($tempRepo)
			)
		);
	}
	
	public function GetTemperatureData() {
		return json_encode($this->GetCurrentTemperatureData());
	}

	public function GetReadings(ITemperatureReadingRepository $tempRepo) {
		return json_encode($this->GetLast24HrReadings($tempRepo));
	}

	public function TakeReading() {
		HomeAutomationService::TakeReading();
		return null;
	}

}
