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.

NodeHelper.js 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Licensed under the MIT license, see LICENSE file.
  2. // Author: Per Malmberg (https://github.com/PerMalmberg)
  3. var NodeHelper = function( node ) {
  4. var myNode = node;
  5. var self = this;
  6. var decimal = /^\s*[+-]{0,1}\s*([\d]+(\.[\d]*)*)\s*$/
  7. this.ToBoolean = function( value ) {
  8. var res = false;
  9. if (typeof value === 'boolean') {
  10. res = value;
  11. }
  12. else if( typeof value === 'number' || typeof value === 'string' ) {
  13. // Is it formated as a decimal number?
  14. if( decimal.test( value ) ) {
  15. var v = parseFloat( value );
  16. res = v != 0;
  17. }
  18. else {
  19. res = value.toLowerCase() === "true";
  20. }
  21. }
  22. return res;
  23. };
  24. this.DisplayStatus = function( value ) {
  25. myNode.status(
  26. {
  27. fill: value ? "green" : "red",
  28. shape: value ? "dot" : "ring",
  29. text: value ? "true" : "false"
  30. }
  31. );
  32. };
  33. this.DisplayUnkownStatus = function() {
  34. myNode.status(
  35. {
  36. fill: "gray",
  37. shape: "dot",
  38. text: "Unknown"
  39. });
  40. };
  41. this.SetResult = function( value, optionalTopic ) {
  42. self.DisplayStatus( value );
  43. var msg = {
  44. topic: optionalTopic === undefined ? "result" : optionalTopic,
  45. payload: value
  46. };
  47. myNode.send(msg);
  48. };
  49. };
  50. module.exports = NodeHelper;