digidotcom / xbee-python

Python library to interact with Digi International's XBee radio frequency modules.
Mozilla Public License 2.0
185 stars 93 forks source link

added json support with the network export functionality. #228

Open duysqubix opened 3 years ago

duysqubix commented 3 years ago

To generate a network overview, the XBeeNetwork.export only supports xml.

Simply, I needed json functionality and figured I would contribute to codebase while I am it.

This mimics the exact structure found in the generated XML, but in JSON. They map 1:1 very nicely.

duysqubix commented 3 years ago

Here is an example output from the latest commit:

{
  "xbee_network": {
    "description": "",
    "date": "2021-02-16 14:34:24.729143",
    "map_type": "dynamic",
    "protocol": "3",
    "devices": [
      {
        "addr": "0013A20041BD3346",
        "nwk_address": "FFFE",
        "node_id": " ",
        "role": "Router",
        "hw_version": "23",
        "fw_version": "8075",
        "serial_config": {
          "port": "/dev/ttyUSB0",
          "baud_rate": "9600",
          "data_bits": "8",
          "stop_bits": "1",
          "parity": "0",
          "flow_control": "0"
        },
        "connections": [
          {
            "addr": "0013A20041B625B5",
            "strength": "?",
            "status": "0"
          },
          {
            "addr": "0013A20041BD2A7A",
            "strength": "?",
            "status": "0"
          }
        ]
      },
      {
        "addr": "0013A20041B625B5",
        "nwk_address": "FFFE",
        "node_id": "MYNODE",
        "role": "Router",
        "hw_version": "???",
        "fw_version": "???",
        "connections": [
          {
            "addr": "0013A20041BD3346",
            "strength": "?",
            "status": "0"
          }
        ]
      },
      {
        "addr": "0013A20041BD2A7A",
        "nwk_address": "FFFE",
        "node_id": "SLAVE2",
        "role": "Router",
        "hw_version": "???",
        "fw_version": "???",
        "connections": [
          {
            "addr": "0013A20041BD3346",
            "strength": "?",
            "status": "0"
          }
        ]
      }
    ]
  }
}

XML Equivalent

<network name="%s_network">
    <description />
    <date>2/16/21 2:50:4 PM</date>
    <protocol>3</protocol>
    <map_type>dynamic</map_type>
    <devices>
        <device address="0013A20041BD3346">
            <nwk_address>FFFE</nwk_address>
            <node_id> </node_id>
            <role>Router</role>
            <hw_version>0x23</hw_version>
            <fw_version>8075</fw_version>
            <serial_config>
                <port>/dev/ttyUSB0</port>
                <baud_rate>9600</baud_rate>
                <data_bits>8</data_bits>
                <stop_bits>1</stop_bits>
                <parity>0</parity>
                <flow_control>0</flow_control>
            </serial_config>
            <connections>
                <connection address="0013A20041B625B5">
                    <strength>?</strength>
                    <status>0</status>
                </connection>
                <connection address="0013A20041BD2A7A">
                    <strength>?</strength>
                    <status>0</status>
                </connection>
            </connections>
        </device>
        <device address="0013A20041B625B5">
            <nwk_address>FFFE</nwk_address>
            <node_id>MYNODE</node_id>
            <role>Router</role>
            <hw_version />
            <fw_version />
            <connections>
                <connection address="0013A20041BD3346">
                    <strength>?</strength>
                    <status>0</status>
                </connection>
            </connections>
        </device>
        <device address="0013A20041BD2A7A">
            <nwk_address>FFFE</nwk_address>
            <node_id>SLAVE2</node_id>
            <role>Router</role>
            <hw_version />
            <fw_version />
            <connections>
                <connection address="0013A20041BD3346">
                    <strength>?</strength>
                    <status>0</status>
                </connection>
            </connections>
        </device>
    </devices>
</network>