blob: 394319224ccd8df90cd8b61c50e3d951593f5151 [file] [log] [blame]
Ed Tanousb61d5892017-06-28 11:36:51 -07001angular.module('bmcApp')
2 .controller(
3 'sensorController',
4 [
5 '$scope', '$http', '$location', 'websocketService',
6 function($scope, $http, $location, websocketService) {
7 $scope.smartTablePageSize = 10;
8 $scope.next_id = 0;
Ed Tanous3dac7492017-08-02 13:46:20 -07009 websocketService.start('/dbus_monitor', function(evt) {
Ed Tanousb61d5892017-06-28 11:36:51 -070010 var obj = JSON.parse(evt.data);
Ed Tanous4d92cbf2017-06-22 15:41:02 -070011
Ed Tanousb61d5892017-06-28 11:36:51 -070012 $scope.$apply(function() {
13 for (var sensor_name in obj) {
14 var found = false;
15 for (var sensor_index in $scope.rowCollection) {
16 var sensor_object = $scope.rowCollection[sensor_index];
17 if (sensor_object.name === sensor_name) {
18 sensor_object.value = obj[sensor_name];
19 found = true;
20 break;
21 }
22 }
23 if (!found) {
24 console.log(sensor_name + ' -> ' + obj[sensor_name]);
25 $scope.next_id = $scope.next_id + 1;
Ed Tanous5fceeb42017-06-28 09:43:09 -070026
Ed Tanousb61d5892017-06-28 11:36:51 -070027 $scope.rowCollection.push({
28 id : $scope.next_id,
29 name : sensor_name,
30 value : obj[sensor_name],
31 });
32 }
33 };
34 });
Ed Tanous5fceeb42017-06-28 09:43:09 -070035 });
Ed Tanousb61d5892017-06-28 11:36:51 -070036
37 $scope.rowCollection = [];
38
Ed Tanous5fceeb42017-06-28 09:43:09 -070039 }
Ed Tanousb61d5892017-06-28 11:36:51 -070040 ])
41 .factory('websocketService', [
42 '$location',
43 function($location) {
44 return {
45 start: function(url, callback) {
46 var host = $location.host();
47 var port = 18080;
48 var protocol = 'wss://';
49 if ($location.protocol() === 'http') {
50 protocol = 'ws://';
51 }
52 var websocket = new WebSocket(protocol + host + ':' + port + url);
53 websocket.onopen = function() {};
54 websocket.onclose = function() {};
55 websocket.onmessage = function(evt) { callback(evt); };
56 }
Ed Tanous4d92cbf2017-06-22 15:41:02 -070057 }
Ed Tanousb61d5892017-06-28 11:36:51 -070058 }
59 ]);