【问题标题】:update a file in node-red through context-storage通过 context-storage 更新 node-red 中的文件
【发布时间】:2018-01-23 15:51:58
【问题描述】:

我已经编写了代码来更新 node-red 中的文件,该文件可以按我的意愿工作。我不太确定它是否可行。当按下注入按钮时,当前值通过上下文存储,读取文件并将存储的数据添加到其中。 (a-)同步性怎么样?如果存储的数据变大了怎么办?也许数据还没有被存储,但旧数据被写入文件?这是可能的吗?我可以通过某种方式使用某种回调函数来解决这个问题吗?

这是节点红色代码:

[{"id":"ff428878.2bf0d8","type":"inject","z":"33a0c3f0.5ad66c","name":"","topic":"","payload" :"","payloadType":"date","re​​peat":"","crontab":"","once":false,"x":140,"y":80,"wires":[[ "d97f4731.48f508"]]},{"id":"aff66bbc.653048","type":"file in","z":"33a0c3f0.5ad66c","name":"","filename": "examplefile.txt","format":"utf8","chunk":false,"sendError":false,"x":380,"y":240,"wires":[["82b889ff.4e5f18"] ]},{"id":"216dc166.5c79be","type":"file","z":"33a0c3f0.5ad66c","name":"","filename":"examplefile.txt"," appendNewline":true,"createDir":false,"overwriteFile":"true","x":900,"y":80,"wires":[]},{"id":"9238c649.dac2e8", "type":"function","z":"33a0c3f0.5ad66c","name":"merge","func":"if(msg.topic == \"new\"){\n context.set ('shortterm', msg.payload);\n return [null,msg];\n}\n\nif(msg.topic == \"old\"){\n msg.payload.push(context.get ('shortterm'));\n return [msg,null];\n}\n\n","outputs":"2","noerr":0,"x":611,"y":80 ,"电线":[["216dc166.5c79be"],["aff66bbc.653048"]]},{"id":"d97f4731.48f508","t ype":"function","z":"33a0c3f0.5ad66c","name":"mark as new","func":"msg.topic = \"new\";\nreturn msg;","outputs ":1,"noerr":0,"x":400,"y":80,"wires":[["9238c649.dac2e8"]]},{"id":"10b8938f.1cddfc","type ":"function","z":"33a0c3f0.5ad66c","name":"标记为旧","func":"msg.topic = \"old\";\nreturn msg;","outputs" :1,"noerr":0,"x":810,"y":240,"wires":[["9238c649.dac2e8"]]},{"id":"82b889ff.4e5f18","type" :"function","z":"33a0c3f0.5ad66c","name":"JSON 解析","func":"if(msg.payload){\n msg.payload = JSON.parse(msg.payload) ;\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":610,"y":240,"wires":[["10b8938f.1cddfc"]]} ,{"id":"7572637c.0a25ac","type":"file","z":"33a0c3f0.5ad66c","name":"","filename":"examplefile.txt","appendNewline" :true,"createDir":false,"overwriteFile":"true","x":510,"y":480,"wires":[]},{"id":"1f6b9043.ff153","type ":"inject","z":"33a0c3f0.5ad66c","name":"","topic":"","payload":"[]","payloadType":"json","re​​peat" :"","crontab":"25 22 * * *","once":false,"x":210,"y":460,"wires":[["7572637c .0a25ac"]]}]

【问题讨论】:

    标签: node-red


    【解决方案1】:

    如果您使用单个流程来读取文件,则流程中的下一步将在文件被读取之前不会发生。

    所以你的流程太复杂了。

    inject->read file->json node (if the file content is json)->
      function node->write file
    

    函数节点所在位置:

    1. 获取上下文变量(如果不存在则创建一个空变量)
    2. 将文件数据与上下文合并
    3. 写入上下文变量
    4. 将msg.payload 更改为更新后的内容
    5. return msg

    【讨论】:

      【解决方案2】:

      Node-Red 19.0+ 持久文件存储的另一个较新选项是内置的“上下文存储”。

      这里有一篇关于如何使用上下文存储滚动的很好的文章:

      https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115

      基础是使用 contextStorage 设置更新您的 ~/.node-red/settings.js 文件:

      contextStorage: { default : { module: "memory" }, fileStore: { module: "localfilesystem"} },

      从那里您可以在流程中引用:

      flow.set("jsObj", "Hello there!", "fileStore");
      

      要取回数据:

      flow.get("jsObj", "fileStore")
      

      此时您可以在 ~/.node-red/context/ 文件夹中看到您保存的文件。

      上面的链接文章应该可以回答任何其他问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多