-
Notifications
You must be signed in to change notification settings - Fork 0
/
stratonread.html
310 lines (254 loc) · 11.6 KB
/
stratonread.html
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<script type="text/html" data-template-name="straton read">
<div class="form-row">
<label for="node-input-address"><i class="fa fa-globe"></i> URL</label>
<input type="text" id="node-input-address" placeholder="Adress">
<a id="node-input-lookup-variables" class="red-ui-button"><i id="node-input-lookup-variables-icon" class="fa fa-search"></i></a>
</div>
<div class="form-tips"><b>Tip:</b> Deploy the node after inserting the node or changing the URL. Hit the <i class="fa fa-search"></i> button to get the list of variables from the straton data-server</div>
<div class="form-row node-input-sub-container-row">
<ol id="node-input-sub-container"></ol>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<script type="text/javascript">
(function () {
function stratonws_oneditprepare() {
var node = this;
node.varlist = []; //Variable list
var timerGetVarList; //timer used to get the satus when retrieving variable list
this.outputs = this.subs.length;
function resizeSub(sub) {
var newWidth = sub.width();
var valueField = sub.find(".node-input-sub-value");
valueField.typedInput("width",(newWidth-60-70));
}
$("#node-input-sub-container").css('min-height', '80px').css('min-width', '450px').editableList({
addItem: function(container,i,opt) {
if (!opt.hasOwnProperty('r')) {
opt.r = ""; //Empty value at creation
}
var sub = opt.r;
if (!opt.hasOwnProperty('i')) {
opt._i = Math.floor((0x99999-0x10000)*Math.random()).toString();
}
container.css({
overflow: 'hidden',
whiteSpace: 'nowrap'
});
var row = $('<div/>').appendTo(container);
function createValueField() {
return $('<input/>', { class: "node-input-sub-value", type: "text", style: "margin-left: 5px;" }).appendTo(row).autocomplete({
source: node.varlist,
minLength: 0,
close: function (event, ui) {
//$("#node-config-input-serialport").autocomplete("destroy");
}
}).autocomplete("search", "");
}
var valueField = null;
var finalspan = $('<span/>',{style:"float: right;margin-top: 6px;"}).appendTo(row);
finalspan.append(' → <span class="node-input-sub-index">' + (i + 1) + '</span> ');
if (!valueField){
valueField = createValueField();
}
valueField.val(sub);
resizeSub(container);
},
removeItem: function(opt) {
},
resizeItem: resizeSub,
sortItems: function(subs) {
},
sortable: false,
removable: true
});
var varName;
for (var i=0;i<this.subs.length;i++) {
varName = this.subs[i];
$("#node-input-sub-container").editableList('addItem', { r: varName, i: i });
}
$("#node-input-sub-container").editableList('height', 40);
//click on the button to get the variable list
$("#node-input-lookup-variables").click(function () {
$('#node-input-lookup-variables-icon').removeClass("fa fa-search"); //Hide the search icon
$('#node-input-lookup-variables-icon').addClass("fa fa-spinner fa-spin"); //Show the spinner icon. the fa-spin after makes the icon spin
$("#node-input-lookup-variables").addClass('disabled');
timerGetVarList = setInterval(function () {
var remoteUrl = $("#node-input-address").val();
var parsedUrl = remoteUrl;
if (remoteUrl.indexOf("ws://") === 0)
parsedUrl = remoteUrl.substring(5);
else if (remoteUrl.indexOf("wss://") === 0)
parsedUrl = remoteUrl.substring(6);
// Get the list of variables from the node itself
$.ajax({
url: "stratonvarlist/" + node.id + "/" + parsedUrl,
type: "POST",
success: function (response) {
if (response.status === -1) { // There is an error
$('#node-input-lookup-variables-icon').removeClass("fa fa-spinner fa-spin");
$('#node-input-lookup-variables-icon').addClass("fa fa-search");
$("#node-input-lookup-variables").removeClass('disabled');
clearInterval(timerGetVarList); //Stop timer
} else if (response.status === 1) { // Request is completed
$('#node-input-lookup-variables-icon').removeClass("fa fa-spinner fa-spin");
$('#node-input-lookup-variables-icon').addClass("fa fa-search");
$("#node-input-lookup-variables").removeClass('disabled');
//Fill the local variable lists with the response
node.varlist = [];
for (var i = 0; i < response.data.length; i++) {
node.varlist.push("app." + response.data[i].toLowerCase());
}
//add sys variables
node.varlist.push("sys.appname");
node.varlist.push("sys.appversion");
node.varlist.push("sys.cycletime");
node.varlist.push("sys.cyclemax");
node.varlist.push("sys.cycleOverflows");
node.varlist.sort();
var subs = $("#node-input-sub-container").editableList('items');
subs.each(function (i) {
var sub = $(this);
sub.find(".node-input-sub-value").autocomplete({
source: node.varlist
});
});
clearInterval(timerGetVarList); //Stop timer
}
}
});
}, 500); //Every 500 ms
});
}
function stratonws_oneditsave() {
var subs = $("#node-input-sub-container").editableList('items');
var node = this;
node.subs = [];
subs.each(function(i) {
var ruleData = $(this).data('data');
var sub = $(this);
var value = sub.find(".node-input-sub-value").val();
if (value !== "") {
node.subs.push(value);
}
});
this.outputs = node.subs.length;
}
function stratonws_oneditcancel() {
var subs = $("#node-input-sub-container").editableList('items');
var node = this;
node.subs = [];
subs.each(function (i) {
var ruleData = $(this).data('data');
var sub = $(this);
var value = sub.find(".node-input-sub-value").val();
if (value !== "") {
node.subs.push(value);
}
});
this.outputs = node.subs.length;
}
function stratonws_oneditresize(size) {
var rows = $("#dialog-form>div:not(.node-input-sub-container-row)");
var height = size.height;
for (var i=0;i<rows.length;i++) {
height -= $(rows[i]).outerHeight(true);
}
var editorRow = $("#dialog-form>div.node-input-sub-container-row");
height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom")));
height += 16;
$("#node-input-sub-container").editableList('height',height);
}
RED.nodes.registerType('straton read', {
color: "#038FC7",
category: 'straton',
defaults: {
name: {value:""},
address: { value: "ws://192.168.1.13:8000", required: true },
subs: { value: "" },
outputs: {value:1}
},
inputs: 0,
outputs: 0,
outputLabels: function (index) {
var label = "";
var sub = this.subs[index];
if (sub) {
label = sub;
}
return label;
},
icon: "white-globe.png",
label: function () {
return this.name || "straton read";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: stratonws_oneditprepare,
oneditsave: stratonws_oneditsave,
oneditresize: stratonws_oneditresize,
oneditcancel: stratonws_oneditcancel
});
})();
</script>
<script type="text/html" data-help-name="straton read">
<p>Connects to a straton WebSocket and subscribes to one or several straton variables.</p>
<h3>Outputs</h3>
<ol class="node-ports">
<p>0 to n (0 to n straton variables to read)</p>
<p><b>For each output:</b></p>
<dl class="message-properties">
<dt>
cmd
<span class="property-type">string</span>
</dt>
<dd> Command of the message received from the WebSocket. </dd>
<dt>
payload
<span class="property-type">string</span>
</dt>
<dd> value received from the WebSocket. </dd>
<dt>
StatusCode
<span class="property-type">number</span>
</dt>
<dd> The status code of the response, or the error code if the request could not be completed. </dd>
</dl>
</ol>
<h3>Details</h3>
<ul>
<li>
<code>URL</code> is the url of the straton websocket to connect to.
</li>
<li>
<code>List of variables</code> to subscribe to.
Each straton variable name has the following format:
<ul>
<li>app.xxxx for straton variables</li>
<li>sys.xxxx for runtime variables (like straton application name)</li>
<li>oem.xxxx for oem variables (like Linux os version)</li>
</ul>
All the variable names are case insensitive
</li>
</ul>
<p>
<b>Note:</b>
If the node is deployed and an URL has been given, it tries to connect to the WebSocket.
</p>
<p>
<b>Note:</b>
If the node is deployed and an URL has been given, you can click on the search button to get the list of variables available on the straton runtime.
</p>
<p>
<b>Note:</b>
An auto-completion will then be available as you type the variable name.
</p>
<p>
<b>Note:</b>
If a variable name is left blank, the corresponding output won't be created.
</p>
</script>