forked from Adorkable/node-red-contrib-ui-led
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled.js
108 lines (92 loc) · 3.21 KB
/
led.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
module.exports = function(RED) {
function HTML(config, ledStyle) {
return String.raw`
<style>
div.led {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
height: 100%;
}
span.led {
width: 24px;
height: 24px;
border-radius: 50%;
margin: 8px;
}
</style>
<div class="led">
<span class="name">` + config.label + `</span>
<span class="led" id="led_{{$id}}" style="` + ledStyle + `">
</span>
</div>`;
};
/**
* Check for that we have a config instance and that our config instance has a group selected, otherwise report an error
* @param {object} config - The config instance
* @param {object} node - The node to report the error on
* @returns {boolean} `false` if we encounter an error, otherwise `true`
*/
function checkConfig(config, node) {
if (!config) {
// TODO: have to think further if it makes sense to separate these out, it isn't clear what the user can do if they encounter this besides use the explicit error to more clearly debug the code
node.error(RED._("ui_led.error.no-config"));
return false;
}
if (!config.hasOwnProperty("group")) {
node.error(RED._("ui_led.error.no-group"));
return false;
}
return true;
}
var ui = undefined;
function LEDNode(config) {
try {
if(ui === undefined) {
ui = RED.require("node-red-dashboard")(RED);
}
RED.nodes.createNode(this, config);
var node = this;
var beforeEmit = function(msg, value) {
return { msg: msg };
};
var initController = ($scope) => {
$scope.flag = true;
var update = (msg) => {
if (!msg) {
return;
}
var value = msg.payload === true;
var ledStyleTemplate = (color) => {
return `background-color: ` + color + `; box-shadow: inset #ffffff8c 0px 1px 2px, inset #00000033 0 -1px 1px 1px, inset ` + color + ` 0 -1px 4px, ` + color + ` 0 0px 16px, ` + color + ` 0 0px 16px;`
};
var ptr = document.getElementById("led_" + $scope.$eval('$id'));
$(ptr).attr('style', ledStyleTemplate(
value ? 'green' : 'red'
)
);
};
$scope.$watch('msg', update);
};
var ledStyleTemplate = (color) => {
return `background-color: ` + color + `; box-shadow: inset #ffffff8c 0px 1px 2px, inset #00000033 0 -1px 1px 1px, inset ` + color + ` 0 -1px 4px;`
};
if (checkConfig(config, node)) {
var done = ui.addWidget({
node: node,
format: HTML(config, ledStyleTemplate('gray')),
group: config.group,
templateScope: "local",
order: 0,
beforeEmit: beforeEmit,
initController: initController
});
}
} catch(error) {
console.log(error);
}
node.on("close", done);
}
RED.nodes.registerType("ui_led", LEDNode);
}