-
Notifications
You must be signed in to change notification settings - Fork 1
/
Device.js
94 lines (78 loc) · 2.68 KB
/
Device.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(function($){
var methods = {
init: function(element){
var $element = $(element);
return $(this).each(function(){
var $this = $(this);
var id = $element.attr('id');
var nameOrId = $element.attr('name') || id;
$this
.attr('data-role', 'Device')
.attr('data-device-id', id)
.append($('<h1></h1>').text(nameOrId))
.append($('<div data-role="program"></div>'))
.append($('<div data-role="block"></div>'))
.append($('<ul></ul>'))
;
data[id] = {element: $element, items:{}}
$element.find('DataItem').each(function(){
var $this = $(this);
if(
$this.attr('type') === 'EXECUTION'
|| $this.attr('type') === 'AVAILABILITY'
|| $this.attr('type') === 'PROGRAM'
|| $this.attr('type') === 'BLOCK'
){
data[id].items[$this.attr('id')] = $this;
}
});
});
},
update: function(stream){
var $device = $(this);
var deviceId = $device.attr('data-device-id');
var $stream= $(stream);
//var lag = (new Date() - new Date($stream.find('Header').attr('creationTime'))); //TODO: remove debug code. this will not work on some browsers.
//if(lag>9200) console.log('Lag: ' + lag + ' ms');
return $stream.find('Samples,Events,Condition').children().each(function(){
var item = this;
var $item = $(this);
var itemId = $item.attr('dataItemId');
if(data[deviceId].items.hasOwnProperty(itemId)){
if($item.is('Execution')){
$device.removeClass('active ready stopped interrupted unavailable').addClass($item.text().toLowerCase());
console.log('Execution: ' + $item.text());
}
else if($item.is('Availability') && $item.text() === 'UNAVAILABLE'){
$device.removeClass('active ready stopped interrupted unavailable').addClass('unavailable');
console.log('Availability: ' + $item.text());
}
else if($item.is('Program')){
$device.find('[data-role="program"]').text($item.text());
console.log('Program: ' + $item.text());
}
else if($item.is('Block')){
$device.find('[data-role="block"]').text($item.text());
}
}
});
}
};
var data = {};
var notifications = {};
var pushNotification = function(device, item){
if( !notifications.hasOwnProperty(device) ) notifications[device] = {};
notifications[device][item.attr('dataItemId')] = item;
};
$.fn.Device = function(method){
if(methods[method]){
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}
else if(typeof method === 'object' || !method){
return methods.init.apply(this, arguments);
}
else{
$.error('Method ' + method + ' does not exist on jQuery.Device');
}
}
})(jQuery);