Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ui_switch.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. module.exports = function(RED) {
  2. var ui = require('../ui')(RED);
  3. function validateSwitchValue(node,property,type,payload) {
  4. if (payloadType === 'flow' || payloadType === 'global') {
  5. try {
  6. var parts = RED.util.normalisePropertyExpression(payload);
  7. if (parts.length === '') {
  8. throw new Error();
  9. }
  10. } catch(err) {
  11. node.warn("Invalid payload property expression - defaulting to node id")
  12. payload = node.id;
  13. payloadType = 'str';
  14. }
  15. }
  16. else {
  17. payload = payload || node.id;
  18. }
  19. }
  20. function SwitchNode(config) {
  21. RED.nodes.createNode(this, config);
  22. this.pt = config.passthru;
  23. this.state = ["off"," "];
  24. this.decouple = (config.decouple === "true") ? false : true;
  25. var node = this;
  26. node.status({});
  27. var group = RED.nodes.getNode(config.group);
  28. if (!group) { return; }
  29. var tab = RED.nodes.getNode(group.config.tab);
  30. if (!tab) { return; }
  31. var parts;
  32. var onvalue = config.onvalue;
  33. var onvalueType = config.onvalueType;
  34. if (onvalueType === 'flow' || onvalueType === 'global') {
  35. try {
  36. parts = RED.util.normalisePropertyExpression(onvalue);
  37. if (parts.length === 0) {
  38. throw new Error();
  39. }
  40. } catch(err) {
  41. node.warn("Invalid onvalue property expression - defaulting to true")
  42. onvalue = true;
  43. onvalueType = 'bool';
  44. }
  45. }
  46. var offvalue = config.offvalue;
  47. var offvalueType = config.offvalueType;
  48. if (offvalueType === 'flow' || offvalueType === 'global') {
  49. try {
  50. parts = RED.util.normalisePropertyExpression(offvalue);
  51. if (parts.length === 0) {
  52. throw new Error();
  53. }
  54. } catch(err) {
  55. node.warn("Invalid offvalue property expression - defaulting to false")
  56. offvalue = false;
  57. offvalueType = 'bool';
  58. }
  59. }
  60. node.on("input", function(msg) {
  61. node.topi = msg.topic;
  62. });
  63. var done = ui.add({
  64. node: node,
  65. tab: tab,
  66. group: group,
  67. emitOnlyNewValues: false,
  68. forwardInputMessages: config.passthru,
  69. storeFrontEndInputAsState: (config.decouple === "true") ? false : true, //config.passthru,
  70. state: false,
  71. control: {
  72. type: 'switch' + (config.style ? '-' + config.style : ''),
  73. label: config.label,
  74. tooltip: config.tooltip,
  75. order: config.order,
  76. value: false,
  77. onicon: config.onicon,
  78. officon: config.officon,
  79. oncolor: config.oncolor,
  80. offcolor: config.offcolor,
  81. animate: config.animate?"flip-icon":"",
  82. width: config.width || group.config.width || 6,
  83. height: config.height || 1,
  84. className: config.className || '',
  85. },
  86. convert: function (payload, oldval, msg) {
  87. var myOnValue,myOffValue;
  88. if (onvalueType === "date") { myOnValue = Date.now(); }
  89. else { myOnValue = RED.util.evaluateNodeProperty(onvalue,onvalueType,node); }
  90. if (offvalueType === "date") { myOffValue = Date.now(); }
  91. else { myOffValue = RED.util.evaluateNodeProperty(offvalue,offvalueType,node); }
  92. if (!this.forwardInputMessages && this.storeFrontEndInputAsState) {
  93. if (myOnValue === oldval) { return true; }
  94. if (oldval === true) { return true; }
  95. else { return false; }
  96. }
  97. if (RED.util.compareObjects(myOnValue,msg.payload)) { node.state[0] = "on"; return true; }
  98. else if (RED.util.compareObjects(myOffValue,msg.payload)) { node.state[0] = "off"; return false; }
  99. else { return oldval; }
  100. },
  101. convertBack: function (value) {
  102. node.state[1] = value?"on":"off";
  103. if (node.pt) {
  104. node.status({fill:(value?"green":"red"),shape:(value?"dot":"ring"),text:value?"on":"off"});
  105. }
  106. else {
  107. var col = (node.decouple) ? ((node.state[1]=="on")?"green":"red") : ((node.state[0]=="on")?"green":"red");
  108. var shp = (node.decouple) ? ((node.state[1]=="on")?"dot":"ring") : ((node.state[0]=="on")?"dot":"ring");
  109. var txt = (node.decouple) ? (node.state[0] +" | "+node.state[1].toUpperCase()) : (node.state[0].toUpperCase() +" | "+node.state[1])
  110. node.status({fill:col, shape:shp, text:txt});
  111. }
  112. var payload = value ? onvalue : offvalue;
  113. var payloadType = value ? onvalueType : offvalueType;
  114. if (payloadType === "date") { value = Date.now(); }
  115. else {
  116. try {
  117. value = RED.util.evaluateNodeProperty(payload,payloadType,node);
  118. }
  119. catch(e) {
  120. if (payloadType === "bin") { node.error("Badly formatted buffer"); }
  121. else { node.error(e,payload); }
  122. }
  123. }
  124. return value;
  125. },
  126. beforeSend: function (msg) {
  127. var t = RED.util.evaluateNodeProperty(config.topic,config.topicType || "str",node,msg) || node.topi;
  128. if (t) { msg.topic = t; }
  129. }
  130. });
  131. if (!node.pt) {
  132. node.on("input", function() {
  133. var col = (node.state[0]=="on") ? "green" : "red";
  134. var shp = (node.state[0]=="on") ? "dot" : "ring";
  135. var txt = (node.decouple) ? (node.state[0] +" | "+node.state[1].toUpperCase()) : (node.state[0].toUpperCase() +" | "+node.state[1])
  136. node.status({fill:col, shape:shp, text:txt});
  137. });
  138. }
  139. node.on("close", done);
  140. }
  141. RED.nodes.registerType("ui_switch", SwitchNode);
  142. };