forked from njh/node-red-contrib-owfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
owfs.html
190 lines (171 loc) · 8.78 KB
/
owfs.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
<!--
Copyright 2014-2016 Nicholas Humfrey
Copyright 2013 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="owfs">
<div class="form-row">
<label for="node-input-host"><i class="icon-globe"></i> Host</label>
<input class="input-append-left" type="text" id="node-input-host" placeholder="localhost" style="width: 40%;" />
<label for="node-input-port" style="margin-left: 10px; width: 35px; "> Port</span></label>
<input type="text" id="node-input-port" placeholder="4304" style="width:45px" />
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" />
</div>
<div class="form-row" style="margin-top: 25px">
<div class="btn-group">
<a class="btn btn-mini dropdown-toggle" data-toggle="dropdown" href="#"><i class="icon-align-justify"></i> Select <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a id='node-input-select-none' href="#">None</a></li>
<li><a id='node-input-select-all' href="#">All</a></li>
<li><a id='node-input-select-temperature' href="#">All Temperature</a></li>
<li><a id='node-input-select-humidity' href="#">All Humidity</a></li>
</ul>
</div>
<a class="btn btn-mini" id="node-input-refresh-paths" href="#"><i class="icon-refresh"></i> Refresh</a>
<span id="owfs-status"><small></small></span>
</div>
<div class="form-row">
<div id="node-input-paths-container-div" style="border-radius: 5px; height: 310px; padding: 5px; border: 1px solid #ccc; overflow-y:scroll;">
<ol id="node-input-paths-container" style="list-style-type:none; margin: 0;">
</ol>
</div>
</div>
</script>
<script type="text/x-red" data-help-name="owfs">
<p>A node for talking to <a href="http://www.maximintegrated.com/en/products/comms/one-wire.html" target="_new">1-wire</a> devices using an <a href="http://owfs.org/" target="_new">owfs</a> <a href="http://owfs.org/index.php?page=owserver" target="_new">owserver</a> instance.</p>
<p>One or more owfs paths can be selected in the node edit dialog. Upon receiving a message on the input the node, it will request each of the values in turn from owfs and places the reading in <b>msg.payload</b>.</p>
<p>Alternatively a device path can be specified in the <b>msg.topic</b> field, for example <code>28.0080BE21AA00/temperature</code>. This will override any paths selected in the edit dialog.</p>
<p>In addition the owserver port and/or host can be specified in <b>msg.host</b> and <b>msg.port</b>, overriding any settings in the node configuration.</p>
<p>To trigger reading sensors periodically, use an Inject node to send messages every X seconds.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('owfs',{
category: 'function',
defaults: {
name: {value:""},
host: {value:"localhost"},
port: {value:4304,validate:RED.validators.regex(/^[0-9]*$/)},
paths: {value:[]}
},
color:"#C0DEED",
inputs:1,
outputs:1,
icon: "1w.png",
label: function() {
return this.name||"owfs";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function() {
function generateRow(i,path) {
var container = $('<li/>',{style:"margin:0; padding:8px 0px; border-bottom: 1px solid #ccc;"});
var row = $('<div/>',{class:"node-input-path-row"}).appendTo(container);
var row2 = $('<div/>',{style:"padding-top: 5px;"}).appendTo(container);
var checkBox = $('<input/>',{id:"node-input-path-cb"+i,type:"checkbox",style:"margin: 5px; width: auto"}).appendTo(row);
checkBox.data('path', path);
var pathField = $('<label/>',{for:"node-input-path-cb"+i,style:"width:auto"}).text(path).appendTo(row);
$("#node-input-paths-container").append(container);
}
var node = this;
function refreshPaths(currentPaths) {
var params = {
"host": $('#node-input-host').val(),
"port": $('#node-input-port').val()
};
if (!params['host']) {
$('#owfs-status small').html("No host set");
return;
} else if (!params['port']) {
$('#owfs-status small').html("No port set");
return;
} else {
$('#owfs-status small').html("Connecting...");
}
$("#node-input-paths-container").empty();
$.getJSON("owfs/dirall?"+$.param(params)).done(function(data) {
if (data['error']) {
$('#owfs-status small').html("Error: "+data['error']);
} else if (data['deviceCount'] > 0) {
$.each(data["paths"], generateRow);
$('#node-input-paths-container input:checkbox').each( function() {
var checkbox = $(this);
if ($.inArray(checkbox.data('path'), currentPaths) != -1) {
checkbox.prop('checked', true);
}
});
$('#owfs-status small').html("Successfully read "+data["deviceCount"]+" devices.");
} else {
$('#owfs-status small').html("No 1-wire devices found.");
}
}).fail(function(jqxhr) {
if (jqxhr.status == 0) {
$('#owfs-status small').html("Failed to connect to Node-RED");
} else {
$('#owfs-status small').html(jqxhr.status+": "+jqxhr.statusText);
}
});
};
function switchDialogResize(ev,ui) {
$("#node-input-paths-container-div").css("height",(ui.size.height-260)+"px");
};
$( "#dialog" ).on("dialogresize", switchDialogResize);
$( "#dialog" ).one("dialogopen", function(ev) {
var size = $( "#dialog" ).dialog('option','sizeCache-switch');
if (size) {
switchDialogResize(null,{size:size});
}
});
$( "#dialog" ).one("dialogclose", function(ev,ui) {
$( "#dialog" ).off("dialogresize",switchDialogResize);
});
$("#node-input-refresh-paths").click( function() {
var currentPaths = $('#node-input-paths-container input:checked').map( function() {
return $(this).data('path');
});
refreshPaths(currentPaths);
});
$("#node-input-select-none").click( function() {
$('#node-input-paths-container input:checked').each( function() {
$(this).prop('checked', false);
});
});
$("#node-input-select-all").click( function() {
$('#node-input-paths-container input:checkbox').each( function() {
$(this).prop('checked', true);
});
});
function checkPathsMatching(regexp) {
$('#node-input-paths-container input:checkbox').each( function() {
var checkbox = $(this);
if (checkbox.data('path').match(regexp)) {
checkbox.prop('checked', true);
}
});
}
$("#node-input-select-temperature").click( function() {
checkPathsMatching(/\/temperature$/);
});
$("#node-input-select-humidity").click( function() {
checkPathsMatching(/\/humidity$/);
});
refreshPaths(this.paths);
},
oneditsave: function() {
this.paths = $('#node-input-paths-container input:checked').map( function() {
return $(this).data('path');
}).toArray();
}
});
</script>