old-silvrgit/app/js/temp.js
2016-02-18 10:56:59 +00:00

109 lines
2.4 KiB
JavaScript

var app = angular.module( "Temp", []);
app.controller(
"TempController",
function( $scope, tempService) {
$scope.tempData = [];
loadRemoteData();
function applyRemoteData(newData)
{
$scope.tempData = newData;
}
function loadRemoteData() {
tempService.getTemp()
.then(
function(tempData) {
applyRemoteData(tempData);
}
)
}
}
);
app.service(
"tempService",
function($http, $q) {
return({getTemp:getTemp});
function getTemp() {
var request = $http({
method: "get",
url:"http://api.silvrtree.co.uk/temp/all",
params: {
/* action:"get"*/
}
});
return (request.then(handleSuccess, handleError));
}
function handleError(response) {
if ( !angular.isObject(response.data) || !response.data.message) {
return( $q.reject("An unknown error occured"));
}
return($q.reject(response.data.message));
}
function handleSuccess(response)
{
return(response.data);
}
}
);
(function () {
console.log('Starting socket?');
var url = "ws://api.silvrtree.co.uk:8039";
var wsCtor = window['MozWebSocket'] ? MozWebSocket : WebSocket;
this.socket = new wsCtor(url, 'stream');
this.handleData = function(d) {
switch(d.id) {
case 'LightingDataReceived':
// this.updateLighting(d.sensorData.d);
break;
case 'ProjectorDataReceived':
// this.updateProj(d.sensorData.d);
break;
case 'HeatingDataReceived':
break;
default:
}
};
this.handleWebsocketMessage = function (message) {
try {
var command = JSON.parse(message.data);
}
catch (e) { /* do nothing */
}
if (command) {
//this.dispatchCommand(command);
this.handleData(command);
}
};
this.handleWebsocketClose = function () {
alert("WebSocket Connection Closed.");
};
this.socket.onmessage = this.handleWebsocketMessage.bind(this);
this.socket.onclose = this.handleWebsocketClose.bind(this);
})();