-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathactuator.js
169 lines (147 loc) · 6.55 KB
/
actuator.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// ┌────────────────────────────────────────────────────────────────────┐ \\
// │ freeboard-actuator-plugin │ \\
// ├────────────────────────────────────────────────────────────────────┤ \\
// │ http://blog.onlinux.fr/?tag=freeboard │ \\
// ├────────────────────────────────────────────────────────────────────┤ \\
// │ Licensed under the MIT license. │ \\
// ├────────────────────────────────────────────────────────────────────┤ \\
// │ Freeboard widget plugin. │ \\
// └────────────────────────────────────────────────────────────────────┘ \\
(function () {
//
// DECLARATIONS
//
var LOADING_INDICATOR_DELAY = 1000;
//
freeboard.loadWidgetPlugin({
type_name: "actuator",
display_name: "Actuator",
description: "Actuator which can send a value as well as receive one",
settings: [
{
name: "title",
display_name: "Title",
type: "text"
},
{
name: "value",
display_name: "Value",
type: "calculated"
},
{
name: "urlOn",
display_name: "url On ",
type: "calculated"
},
{
name: "urlOff",
display_name: "url Off ",
type: "calculated"
},
{
name: "on_text",
display_name: "On Text",
type: "calculated"
},
{
name: "off_text",
display_name: "Off Text",
type: "calculated"
}
],
newInstance: function (settings, newInstanceCallback) {
newInstanceCallback(new actuator(settings));
}
});
freeboard.addStyle('.indicator-light.interactive:hover', "box-shadow: 0px 0px 15px #FF9900; cursor: pointer;");
var actuator = function (settings) {
var self = this;
var titleElement = $('<h2 class="section-title"></h2>');
var stateElement = $('<div class="indicator-text"></div>');
var indicatorElement = $('<div class="indicator-light interactive"></div>');
var currentSettings = settings;
var isOn = false;
var onText;
var offText;
var url;
function updateState() {
indicatorElement.toggleClass("on", isOn);
if (isOn) {
stateElement.text((_.isUndefined(onText) ? (_.isUndefined(currentSettings.on_text) ? "" : currentSettings.on_text) : onText));
}
else {
stateElement.text((_.isUndefined(offText) ? (_.isUndefined(currentSettings.off_text) ? "" : currentSettings.off_text) : offText));
}
}
this.onClick = function(e) {
e.preventDefault()
var new_val = !isOn
this.onCalculatedValueChanged('value', new_val);
url = (new_val) ? currentSettings.urlOn : currentSettings.urlOff;
if (_.isUndefined(url))
freeboard.showDialog($("<div align='center'>url undefined</div>"), "Error!", "OK", null, function () {
});
else {
this.sendValue(url, new_val);
}
}
this.render = function (element) {
$(element).append(titleElement).append(indicatorElement).append(stateElement);
$(indicatorElement).click(this.onClick.bind(this));
}
this.onSettingsChanged = function (newSettings) {
currentSettings = newSettings;
titleElement.html((_.isUndefined(newSettings.title) ? "" : newSettings.title));
updateState();
}
this.onCalculatedValueChanged = function (settingName, newValue) {
if (settingName == "value") {
isOn = Boolean(newValue);
}
if (settingName == "on_text") {
onText = newValue;
}
if (settingName == "off_text") {
offText = newValue;
}
updateState();
}
var request;
this.sendValue = function (url, options) {
console.log(url, options);
request = new XMLHttpRequest();
if (!request) {
console.log('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
request.onreadystatechange = this.alertContents;
request.open('GET', url, true);
freeboard.showLoadingIndicator(true);
request.send();
}
this.alertContents = function () {
if (request.readyState === XMLHttpRequest.DONE) {
if (request.status === 200) {
console.log(request.responseText);
setTimeout(function () {
freeboard.showLoadingIndicator(false);
//freeboard.showDialog($("<div align='center'>Request response 200</div>"),"Success!","OK",null,function(){});
}, LOADING_INDICATOR_DELAY);
} else {
console.log('There was a problem with the request.');
setTimeout(function () {
freeboard.showLoadingIndicator(false);
freeboard.showDialog($("<div align='center'>There was a problem with the request. Code " + request.status + request.responseText + " </div>"), "Error!", "OK", null, function () {
});
}, LOADING_INDICATOR_DELAY);
}
}
}
this.onDispose = function () {
}
this.getHeight = function () {
return 1;
}
this.onSettingsChanged(settings);
};
}());