blob: 67c9d8227a65d491664ec966d4b3a19280074e30 [file] [log] [blame]
Ed Tanouscc5a37f2017-05-11 10:27:23 -07001angular.module('bmcApp').controller('sensorController', [
Ed Tanous4d92cbf2017-06-22 15:41:02 -07002 '$scope', '$http', '$location', 'websocketService',
3 function($scope, $http, $location, websocketService) {
4 $scope.sensor_values = {};
5
6 var host = $location.host();
7 var port = $location.port();
8 var protocol = "ws://";
9 if ($location.protocol() === 'https') {
10 protocol = 'wss://';
11 }
12 websocketService.start(protocol + host + ":" + port + "/sensorws", function (evt) {
13 var obj = JSON.parse(evt.data);
14 $scope.$apply(function () {
15 for (var key in obj) {
16 if (obj.hasOwnProperty(key)) {
17 console.log(key + " -> " + obj[key]);
18 $scope.sensor_values[key] = obj[key];
19 }
20 }
21 });
22 });
23
Ed Tanouscc5a37f2017-05-11 10:27:23 -070024 }
Ed Tanous4d92cbf2017-06-22 15:41:02 -070025]);
26
27app.factory('websocketService', function () {
28 return {
29 start: function (url, callback) {
30 var websocket = new WebSocket(url);
31 websocket.onopen = function () {
32 };
33 websocket.onclose = function () {
34 };
35 websocket.onmessage = function (evt) {
36 callback(evt);
37 };
38 }
39 }
40 }
41);