2018年5月22日 星期二

ble MESH:how to delete a provisioned-node in Serial example (Pyaci)

In this April , Nordic Semiconductor release their Mesh SDK version 2.0.1
in Serial example, this example let:
- a nRF52 connect with a Windows or embedded system with UART
- a nRF52 (program serial example)act as provisioner and config client
- the provision and config records store in JSON database in  a Windows or embedded system
- the Windows or embedded system  control the nRF52 with python script
(named  interactive_pyaci.py)

for detail plz check in Nordic website:
serial example 


However the scripts doesn't have function to delete provisioned records.
That means if the node is reset, this serial example can not provision it again.
Or, if a node leave this Mesh network, the provision is not able to delete it.

In this article , I implement a script to delete the record.
name as delete_provisioned_node.py


#power by Agatha Kuan 05/16/2018
import json as JSON
import os
import sys

def check_provisioned_node(file_name):
    with open(file_name) as f:
        load_in = JSON.load(f)
        print(load_in["nodes"])


        for p in load_in["nodes"]:
            print("device name = "+p["name"])


def delete_node(undeleted_node,file_name):
    with open(file_name) as f:
        load_in = JSON.load(f)
        for p in load_in["nodes"]:
            print("list name = "+p["name"])

        for i in range(len(load_in["nodes"])):
            if load_in["nodes"][i]["name"] == undeleted_node:
                load_in["nodes"].pop(i)
                print("success delete ", undeleted_node)
                break

        open(file_name, "w").write(JSON.dumps(load_in, sort_keys=False, indent=4, separators=(',', ':')))


def main():
#sys.argv[0]  是這個腳本的檔名
    if("-c" == sys.argv[1]):
        print("check recent node")
        print("open file:", sys.argv[2])
        check_provisioned_node(sys.argv[2])

    elif("-d" == sys.argv[1]):
        print("delete device :",sys.argv[2])
        delete_node(sys.argv[2],sys.argv[3])

    elif("?" == sys.argv[1]):
        print("Functions:")
        print("1.for check provisioned node:")
        print("     -c FILE_NAME e.g -c example_database.json")
        print("2.after checking nodes, to select which to delete:")
        print("     -d DEVICE_NAME FILE_NAME e.g -d node_01 example_database.json")




if __name__ == "__main__":
    main()


Demostration:
I provision a node "0577"

in database will add a nodes record:

"nodes": [
    {
      "UUID": "0059ffff0000000009f5cf71a0100577",
      "appKeys": [
        0
      ],
      "cid": "0059",
      "configComplete": false,
      "crpl": 32,
      "deviceKey": "dd5baeb65d8ab9eb709c45397a285ab4",
      "elements": [
        {
          "index": 0,
          "location": "0000",
          "models": [
            {
              "modelId": "0000"
            },
            {
              "modelId": "0002"
            },
            {
              "bind": [
                0
              ],
              "modelId": "00590000"
            }
          ]
        }
      ],
      "features": {
        "friend": 2,
        "lowPower": 2,
        "proxy": 2,
        "relay": 0
      },
      "name": "0577",
      "netKeys": [
        0
      ],
      "pid": "0000",
      "security": "low",
      "unicastAddress": 16,
      "vid": "0000"
    }
]

Than put "delete_provisioned_node.py " in :
../script/interactive_pyaci/database


and run it in CMD :

$ database python delete_provisioned_node.py ?

it will show you operation command



So, if I want to delete "0577" , command

$ database python delete_provisioned_node.py -d 0577 example_database.json



reference:
http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.meshsdk.v2.0.1%2Fmd_scripts_interactive_pyaci_doc_demo_configuration.html&cp=4_1_0_1_1_3_2


沒有留言:

張貼留言