Robert Marshall 9 éve
szülő
commit
efd6bd3e05

+ 1 - 1
Controller/Navigation/TemperatureNav.php

@@ -3,7 +3,7 @@ class TemperatureNav implements INavigationController{
 	private $_uri;
 	
 	public function __construct() {
-		$this->_uri=new URI("Temperature Logs","/temperature","/images/status.svg");
+		$this->_uri=new URI("Temperature Logs","/temperature","/images/temperature.svg");
 	}
 	
 	public function GetItems() {

+ 8 - 0
Controller/Temperature.php

@@ -6,6 +6,14 @@ class Temperature extends Controller {
 		return new View("Temperature/index.view");
 	}
 
+	public function GetReadings(ITemperatureReadingRepository $tempRepo) {
+		$dateTo=new DateTime();
+		$dateFrom=clone $dateTo;
+		$dateFrom->sub(new DateInterval("P1D"));
+		$readings=$tempRepo->GetRange($dateFrom, $dateTo);
+		return json_encode($readings);
+	}
+
 	public function TakeReading() {
 		$result=file_get_contents("http://has.robware.uk/gettemperature");
 		$reading=new TemperatureReading();

+ 15 - 2
Database/TemperatureReadingRepository.php

@@ -7,11 +7,24 @@ class TemperatureReadingRepository extends BaseRepository implements ITemperatur
 	}
 
 	public function GetAll() {
-		
+		$readings=array();
+		$ids=self::$PDO->query("SELECT temperature_id FROM temperature_log")->fetchAll(PDO::FETCH_COLUMN);
+		foreach ($ids as $id)
+			$readings[]=new TemperatureReading($id);
+		return $readings;
 	}
 
 	public function GetRange(DateTime $from, DateTime $to) {
-		
+		$readings=array();
+		$prep=self::$PDO->prepare("SELECT temperature_id FROM temperature_log WHERE `timestamp`>=:from AND `timestamp`<=:to");
+		$prep->execute(array(
+			":from"=>$from->format('Y-m-d 00:00:00'),
+			":to"=>$to->format('Y-m-d 23:59:59')
+		));
+		$results=$prep->fetchAll(PDO::FETCH_COLUMN);
+		foreach ($results as $id)
+			$readings[]=new TemperatureReading($id);
+		return $readings;
 	}
 
 }

+ 11 - 1
Model/TemperatureReading.php

@@ -1,6 +1,16 @@
 <?php
-class TemperatureReading extends DBObject {
+
+class TemperatureReading extends DBObject implements JsonSerializable {
+
 	public function __construct($id=0) {
 		parent::__construct("temperature_log", "temperature_id", $id);
 	}
+
+	public function jsonSerialize() {
+		$obj=new stdClass();
+		$obj->Timestamp=strtotime($this->Timestamp);
+		$obj->Reading=$this->Reading;
+		return $obj;
+	}
+
 }

+ 5 - 3
View/Temperature/index.view

@@ -1,8 +1,10 @@
 @Init{
-	//$this->RegisterJSFile("services/statusService.js");
-	//$this->RegisterJSFile("controllers/status.js");
+	$this->RegisterJSFile("services/temperatureService.js");
+	$this->RegisterJSFile("controllers/temperature.js");
 }@
 @Title{Temperature Logs}@
 @Body{
-
+<div ng-controller="temperature">
+	<div id="linechart"></div>
+</div>
 }@

+ 10 - 0
images/temperature.svg

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 15.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
+	 width="24px" height="24px" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve">
+<path fill="none" d="M0,0h24v24H0V0z"/>
+<rect x="9.375" y="13.292" fill="#795548" width="5.333" height="6.708"/>
+<polygon fill="#D7CCC8" points="5,12 5,20 10,20 10,14 14,14 14,20 19,20 19,12 19.021,12 12.01,5.691 "/>
+<polygon fill="#F44336" points="12,3 2,12 5,12 12.01,5.691 19.021,12 22,12 "/>
+</svg>

+ 44 - 0
scripts/controllers/temperature.js

@@ -0,0 +1,44 @@
+app.controller('temperature', function($scope, temperatureService, $interval) {
+	$.getScript("https://www.google.com/jsapi", function() {
+		//google.load('visualization', '1.1', {packages: ['line'], callback:function(){
+		google.load('visualization', '1', {packages: ['corechart'], callback: function() {
+			google.setOnLoadCallback(InitChart);
+		}});
+	});
+
+	var chart;
+
+	var chartOptions = {
+		chart: {
+			title: 'Temperature Logs',
+		},
+		height: 400,
+		backgroundColor: 'transparent', 
+		legend:{position:'none'}
+	};
+
+	function InitChart() {
+		chart = new google.visualization.LineChart(document.getElementById('linechart'));
+		StartGettingData();
+	}
+	
+	function DrawChart(data){
+		var chartData = new google.visualization.DataTable();
+		chartData.addColumn('date', 'Timestamp');
+		chartData.addColumn('number', 'Reading');
+		angular.forEach(data, function(datum){
+			chartData.addRow([new Date(datum.Timestamp*1000), parseFloat(datum.Reading)]);
+		});		
+		chart.draw(chartData, chartOptions);
+	}
+	
+	function StartGettingData(){
+		GetData();
+		$interval(GetData, 60000);
+	}
+	
+	function GetData(){
+		temperatureService.getReadings().then(DrawChart);
+	}
+});
+

+ 7 - 0
scripts/services/temperatureService.js

@@ -0,0 +1,7 @@
+app.service('temperatureService', function($http) {
+	this.getReadings = function() {
+		return $http.get('/temperature/getreadings').then(function(response) {
+			return response.data;
+		});
+	};
+});