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.

data.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const data = {}
  2. const config = {
  3. RED: null
  4. }
  5. /**
  6. * Checks if a Client/Socket ID has been assigned to this message,
  7. * and whether the node type is being scoped to a specific client.
  8. * If so, do not store this in our centralised datastore
  9. * @param {*} msg
  10. * @returns
  11. */
  12. function canSaveInStore (base, node, msg) {
  13. // gets a list of node types that allow for client configuration/limits
  14. const constrained = base.acceptsClientConfig
  15. const checks = []
  16. if (constrained.includes(node.type)) {
  17. // core check
  18. if (msg._client?.socketId) {
  19. // we are in a node type that allows for definition of specific clients,
  20. // and a client has been defined
  21. checks.push(false)
  22. }
  23. // plugin checks
  24. // loop over plugins and check if any have defined a custom isValidConnection function
  25. // if so, use that to determine if the connection is valid
  26. for (const plugin of config.RED.plugins.getByType('node-red-dashboard-2')) {
  27. if (plugin.hooks?.onCanSaveInStore) {
  28. checks.push(plugin.hooks.onCanSaveInStore(msg))
  29. }
  30. }
  31. }
  32. return checks.length === 0 || !checks.includes(false)
  33. }
  34. const getters = {
  35. RED () {
  36. return config.RED
  37. },
  38. // given a widget id, return the latest msg received
  39. msg (id) {
  40. return config.RED.util.cloneMessage(data[id])
  41. }
  42. }
  43. const setters = {
  44. // map the instance of Node-RED to this module
  45. setConfig (RED) {
  46. config.RED = RED
  47. },
  48. // remove data associated to a given widget
  49. clear (id) {
  50. delete data[id]
  51. },
  52. /**
  53. *
  54. * @param {*} base - the ui-base node associated with this widget
  55. * @param {*} node - the UI node for which we are storing data
  56. * @param {*} msg - the msg to be stored
  57. */
  58. save (base, node, msg) {
  59. if (Array.isArray(msg)) {
  60. /// need to check msg by msg
  61. const filtered = []
  62. for (const m of msg) {
  63. if (canSaveInStore(base, node, m)) {
  64. filtered.push(config.RED.util.cloneMessage(m))
  65. }
  66. }
  67. data[node.id] = filtered
  68. } else {
  69. if (canSaveInStore(base, node, msg)) {
  70. data[node.id] = config.RED.util.cloneMessage(msg)
  71. }
  72. }
  73. },
  74. // given a widget id, and msg, store in an array of history of values
  75. // useful for charting widgets
  76. append (base, node, msg) {
  77. if (canSaveInStore(base, node, msg)) {
  78. if (!data[node.id]) {
  79. data[node.id] = []
  80. }
  81. data[node.id].push(config.RED.util.cloneMessage(msg))
  82. }
  83. }
  84. }
  85. module.exports = {
  86. get: getters.msg,
  87. RED: getters.RED,
  88. setConfig: setters.setConfig,
  89. save: setters.save,
  90. append: setters.append,
  91. clear: setters.clear
  92. }