您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. module.exports = function (RED) {
  2. function InjectUltimate(config) {
  3. RED.nodes.createNode(this, config);
  4. this.config = config;
  5. var node = this;
  6. node.curVal = true;
  7. node.topic = config.topic || "Inject";
  8. setNodeStatus({ fill: "grey", shape: "dot", text: "Waiting" });
  9. RED.httpAdmin.post("/InjectUltimate/:id", RED.auth.needsPermission("InjectUltimate.write"), function (req, res) {
  10. var node = RED.nodes.getNode(req.params.id);
  11. if (node != null) {
  12. try {
  13. node.buttonpressed();
  14. res.sendStatus(200);
  15. } catch (err) {
  16. res.sendStatus(500);
  17. node.error(RED._("InjectUltimate.failed", { error: err.toString() }));
  18. }
  19. } else {
  20. res.sendStatus(404);
  21. }
  22. });
  23. // 29/08/2020 triggered by button press
  24. node.buttonpressed = () => {
  25. setNodeStatus({ fill: "green", shape: "dot", text: "Sent true/false/" + node.curVal.toString() + " (next " + (!node.curVal).toString() + ")" });
  26. var msgTrue = { payload: true, topic: node.topic };
  27. var msgFalse = { payload: false, topic: node.topic };
  28. var msgToggled = { payload: node.curVal, topic: node.topic };
  29. node.curVal = !node.curVal;
  30. node.send([msgTrue, msgFalse, msgToggled]);
  31. }
  32. function setNodeStatus({ fill, shape, text }) {
  33. var dDate = new Date();
  34. node.status({ fill: fill, shape: shape, text: text + " (" + dDate.getDate() + ", " + dDate.toLocaleTimeString() + ")" })
  35. }
  36. }
  37. RED.nodes.registerType("InjectUltimate", InjectUltimate);
  38. }