prettyprint

2021年10月25日 星期一

Node-RED 進階使用筆記

一、Node-RED的設定檔存在.node-red/settings.js中。 

A、將網站改成https:

找到如下圖位置,需製作一組 public/private key,以openssl來製作,再將製作好的key放倒適當位置後更改privkey.pem與cert.pem路徑。

製作步驟如同前一篇文章(MQTT 簡介:使用Mosquitto Broker)介紹,採用self-signed方式產server private key與server CERT步驟如下:
  1. root CA使用已產生的ca.crt,不用再製作,以後對自己的建置的seft-signed系統可使用同一組root CA。
  2. 產生server private key:(取名叫做nodered.key)
    openssl genrsa -out nodered.key 2048
  3. 產生簽署憑證需求檔案(certificate request):((取名叫做nodered.crs)
     openssl req -new -out nodered.csr -key nodered.key
  4. 使用root CA與ca.key簽署nodered.crs產生CERT檔案:
    openssl x509 -req -in nodered.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out nodered.crt -days 360
  5. 假設nodered.key 與 nodered.crt放在/path下,則將https:...改成如下:
    https: {
          key: require("fs").readFileSync('/path/nodered.key'),
          cert: require("fs").readFileSync('/path/nodered.crt')
        },
  6. 執行 sudo systemctl restart nodered。
  7. Node-RED Dashboard ui網址也需改成https

B、dashboard ui驗證密碼:

  1. 以下列指令產生hash密碼
    node-red admin hash-pw
    再將產生的hash密碼複製至下列httpNodeAuth與httpStaticAuth的pass欄位,user可任意指定,但兩個httpNodeAuth與httpStaticAuth最好是一樣。
    httpNodeAuth: {user:"admin",pass:"..................."},
    httpStaticAuth: {user:"admin",pass:"............. ....."},

二、Node-RED Dashboard ui_template node探討:

在ui_template說明中(The template widget can contain any valid html and Angular/Angular-Material directives.),因此單以ui_template就可以完成很多UI功能。
ui_template與其他node msg input/output以下圖說明:

範例一,原始inject -> </>template->debug:


  1. inject 送出msg.payload 為timestamp,
  2. </>template內容
  3. <div ng-bind-html="msg.payload"></div>,所以把timestamp秀在
  4. UI網頁上,
  5. 有勾選Pass through messages from input,所以又把
  6. timestamp的 msg payload送到debug node。
範例二:message payload含有HTML markup tag:

inject輸出字串<font color="RED" size=10>測試</font>,UI網頁如上圖。

範例三:Dashboard </>template內容為HTML 含markup tag


{{msg.payload}}是以雙大括號(mustache template格式)表示,內容會則依照輸入顯示。


範例五:使用<script>以watch function 接收incoming message:


<script>
(function(scope) {
  scope.$watch('msg', function(msg) {
    if (msg) {
      // Do something when msg arrives
     
    }
  });
})(scope);
</script>
    如下影片所示,inject topic為TEST1,TEST2與TEST3,TEST1,TEST2分別更改網頁內容,TEST3 send message給下一個node。

script中變數為scope.variable在 html markup tag則為{{varialbe}}。

沒有留言:

張貼留言