From a65572183c59cfb2b68cc490a234b558cdca6b9f Mon Sep 17 00:00:00 2001 From: HwangKC Date: Tue, 11 Mar 2025 14:54:07 +0800 Subject: [PATCH] initialized version --- .gitignore | 3 + config.json | 55 ++++++ config2.json | 55 ++++++ config3.json | 492 +++++++++++++++++++++++++++++++++++++++++++++++++++ config4.json | 492 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 231 ++++++++++++++++++++++++ 6 files changed, 1328 insertions(+) create mode 100644 .gitignore create mode 100644 config.json create mode 100644 config2.json create mode 100644 config3.json create mode 100644 config4.json create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e45074c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/*.log +.idea +venv diff --git a/config.json b/config.json new file mode 100644 index 0000000..9bc4294 --- /dev/null +++ b/config.json @@ -0,0 +1,55 @@ +{ + "client_id": "gw_simulator_888", + "broker_ip": "116.236.50.106", + "port": 8764, + "username": "hjemq-c2", + "passwd": "Hj57471000", + "publish_topic": "GateWayPublicTopic_Server", + "publish_interval": 5000, + "max_publish_count": 0, + "publish_qos": 0, + "subscribe_qos": 0, + "subcriber_topic": "ServerPublicTopic_GateWay", + "publish_data": { + "FsuCode": "11010110100888", + "type": "gateway-data", + "IdCodeContent": [ + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.1.1.0", + "Value": "112", + "Factor": "10", + "type": "random_number", + "RND": [ + 110, + 120 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.1.3.0", + "Value": "130200", + "Factor": "100", + "type": "random_number", + "RND": [ + 130200, + 130400 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.1.22.0", + "Value": "1234", + "Factor": "10", + "type": "random_number", + "RND": [ + 1234, + 2234 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.1.23.0", + "Value": "TestString", + "Factor": "", + "type": "string" + } + ] + } +} \ No newline at end of file diff --git a/config2.json b/config2.json new file mode 100644 index 0000000..234116e --- /dev/null +++ b/config2.json @@ -0,0 +1,55 @@ +{ + "client_id": "gw_simulator_999", + "broker_ip": "116.236.50.106", + "port": 8764, + "username": "hjemq-c2", + "passwd": "Hj57471000", + "publish_topic": "GateWayPublicTopic_Server", + "publish_interval": 5000, + "max_publish_count": 0, + "publish_qos": 0, + "subscribe_qos": 0, + "subcriber_topic": "ServerPublicTopic_GateWay", + "publish_data": { + "FsuCode": "11010110100999", + "type": "gateway-data", + "IdCodeContent": [ + { + "IDCode": ".1.3.6.1.4.1.41475.1.1.4.1.1.0", + "Value": "112", + "Factor": "10", + "type": "random_number", + "RND": [ + 110, + 120 + ] + }, + { + "IDCode": ".1.3.6.1.4.1.41475.1.1.4.1.3.0", + "Value": "130200", + "Factor": "100", + "type": "random_number", + "RND": [ + 130200, + 130400 + ] + }, + { + "IDCode": ".1.3.6.1.4.1.41475.1.1.4.1.22.0", + "Value": "1234", + "Factor": "0.1", + "type": "random_number", + "RND": [ + 1234, + 2234 + ] + }, + { + "IDCode": ".1.3.6.1.4.1.41475.1.1.4.1.23.0", + "Value": "TestString", + "Factor": "1", + "type": "string" + } + ] + } +} \ No newline at end of file diff --git a/config3.json b/config3.json new file mode 100644 index 0000000..96c0403 --- /dev/null +++ b/config3.json @@ -0,0 +1,492 @@ +{ + "client_id": "gw_simulator_888", + "broker_ip": "172.16.100.23", + "port": 8764, + "username": "hjemq-c2", + "passwd": "Hj57471000", + "publish_topic": "GateWayPublicTopic_Server", + "publish_interval": 5000, + "max_publish_count": 0, + "publish_qos": 0, + "subscribe_qos": 0, + "subcriber_topic": "ServerPublicTopic_GateWay", + "publish_data": { + "FsuCode": "11010110100999", + "type": "gateway-data", + "IdCodeContent": [ + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.4.1.0", + "Value": "1", + "Factor": "1", + "type": "int", + "RND": [ + 1, + 1 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.4.7.0", + "Value": "2860", + "Factor": "100", + "type": "random_number", + "RND": [ + 2501, + 3600 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.4.8.0", + "Value": "6300", + "Factor": "100", + "type": "random_number", + "RND": [ + 3500, + 6600 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.1.1.0", + "Value": "1", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.1.2.0", + "Value": "1", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.1.10.0", + "Value": "1", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.1.0", + "Value": "1", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.8.0", + "Value": "0", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.9.0", + "Value": "0", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.2.0", + "Value": "0", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.3.0", + "Value": "330", + "Factor": "10", + "type": "random_number", + "RND": [ + 210, + 460 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.4.0", + "Value": "0", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.5.0", + "Value": "0", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.6.0", + "Value": "0", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.7.0", + "Value": "0", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.1.0", + "Value": "1", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.10.0", + "Value": "23212", + "Factor": "100", + "type": "random_number", + "RND": [ + 23000, + 25100 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.11.0", + "Value": "23212", + "Factor": "100", + "type": "random_number", + "RND": [ + 23000, + 25100 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.12.0", + "Value": "23212", + "Factor": "100", + "type": "random_number", + "RND": [ + 23000, + 25100 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.13.0", + "Value": "5004", + "Factor": "100", + "type": "random_number", + "RND": [ + 4980, + 5210 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.15.0", + "Value": "3300", + "Factor": "100", + "type": "random_number", + "RND": [ + 3000, + 4600 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.16.0", + "Value": "3330", + "Factor": "100", + "type": "random_number", + "RND": [ + 3100, + 6600 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.20.0", + "Value": "80", + "Factor": "100", + "type": "random_number", + "RND": [ + 80, + 85 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.21.0", + "Value": "330000", + "Factor": "100", + "type": "random_number", + "RND": [ + 300000, + 356000 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.22.0", + "Value": "23030", + "Factor": "100", + "type": "random_number", + "RND": [ + 23000, + 25000 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.23.0", + "Value": "330000", + "Factor": "100", + "type": "random_number", + "RND": [ + 300000, + 356000 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.24.0", + "Value": "230000", + "Factor": "100", + "type": "random_number", + "RND": [ + 200000, + 296000 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.5.1.0", + "Value": "1", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.5.2.0", + "Value": "350", + "Factor": "100", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.5.3.0", + "Value": "2", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.5.4.0", + "Value": "1", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.5.8.0", + "Value": "4", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.5.9.0", + "Value": "19", + "Factor": "1", + "type": "random_number", + "RND": [ + 10, + 35 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.5.10.0", + "Value": "30", + "Factor": "1", + "type": "random_number", + "RND": [ + 20, + 56 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.1.0", + "Value": "1", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.2.0", + "Value": "800", + "Factor": "10", + "type": "random_number", + "RND": [ + 800, + 990 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.3.0", + "Value": "536", + "Factor": "10", + "type": "random_number", + "RND": [ + 520, + 550 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.4.0", + "Value": "40", + "Factor": "10", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.5.0", + "Value": "3350", + "Factor": "1000", + "type": "random_number", + "RND": [ + 3000, + 3478 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.6.0", + "Value": "3350", + "Factor": "1000", + "type": "random_number", + "RND": [ + 3000, + 3478 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.7.0", + "Value": "3350", + "Factor": "1000", + "type": "random_number", + "RND": [ + 3000, + 3478 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.8.0", + "Value": "19", + "Factor": "1", + "type": "random_number", + "RND": [ + 10, + 35 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.29.0", + "Value": "270", + "Factor": "10", + "type": "random_number", + "RND": [ + 220, + 350 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.30.0", + "Value": "270", + "Factor": "10", + "type": "random_number", + "RND": [ + 220, + 350 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.31.0", + "Value": "370", + "Factor": "10", + "type": "random_number", + "RND": [ + 320, + 450 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.32.0", + "Value": "40", + "Factor": "10", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.33.0", + "Value": "270", + "Factor": "10", + "type": "random_number", + "RND": [ + 220, + 350 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.34.0", + "Value": "270", + "Factor": "10", + "type": "random_number", + "RND": [ + 220, + 350 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.35.0", + "Value": "270", + "Factor": "10", + "type": "random_number", + "RND": [ + 220, + 350 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.36.0", + "Value": "270", + "Factor": "10", + "type": "random_number", + "RND": [ + 220, + 350 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.45.0", + "Value": "19", + "Factor": "10", + "type": "random_number", + "RND": [ + 10, + 35 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.46.0", + "Value": "33410", + "Factor": "10", + "type": "random_number", + "RND": [ + 33400, + 33910 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.47.0", + "Value": "1000", + "Factor": "10", + "type": "random_number", + "RND": [ + 999, + 1000 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.49.0", + "Value": "1000", + "Factor": "10", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.50.0", + "Value": "1000", + "Factor": "10", + "type": "random_number", + "RND": [ + 890, + 990 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.51.0", + "Value": "50", + "Factor": "10", + "type": "int" + } + + ] + } +} \ No newline at end of file diff --git a/config4.json b/config4.json new file mode 100644 index 0000000..96c0403 --- /dev/null +++ b/config4.json @@ -0,0 +1,492 @@ +{ + "client_id": "gw_simulator_888", + "broker_ip": "172.16.100.23", + "port": 8764, + "username": "hjemq-c2", + "passwd": "Hj57471000", + "publish_topic": "GateWayPublicTopic_Server", + "publish_interval": 5000, + "max_publish_count": 0, + "publish_qos": 0, + "subscribe_qos": 0, + "subcriber_topic": "ServerPublicTopic_GateWay", + "publish_data": { + "FsuCode": "11010110100999", + "type": "gateway-data", + "IdCodeContent": [ + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.4.1.0", + "Value": "1", + "Factor": "1", + "type": "int", + "RND": [ + 1, + 1 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.4.7.0", + "Value": "2860", + "Factor": "100", + "type": "random_number", + "RND": [ + 2501, + 3600 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.4.8.0", + "Value": "6300", + "Factor": "100", + "type": "random_number", + "RND": [ + 3500, + 6600 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.1.1.0", + "Value": "1", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.1.2.0", + "Value": "1", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.1.10.0", + "Value": "1", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.1.0", + "Value": "1", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.8.0", + "Value": "0", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.9.0", + "Value": "0", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.2.0", + "Value": "0", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.3.0", + "Value": "330", + "Factor": "10", + "type": "random_number", + "RND": [ + 210, + 460 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.4.0", + "Value": "0", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.5.0", + "Value": "0", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.6.0", + "Value": "0", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.2.7.0", + "Value": "0", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.1.0", + "Value": "1", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.10.0", + "Value": "23212", + "Factor": "100", + "type": "random_number", + "RND": [ + 23000, + 25100 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.11.0", + "Value": "23212", + "Factor": "100", + "type": "random_number", + "RND": [ + 23000, + 25100 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.12.0", + "Value": "23212", + "Factor": "100", + "type": "random_number", + "RND": [ + 23000, + 25100 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.13.0", + "Value": "5004", + "Factor": "100", + "type": "random_number", + "RND": [ + 4980, + 5210 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.15.0", + "Value": "3300", + "Factor": "100", + "type": "random_number", + "RND": [ + 3000, + 4600 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.16.0", + "Value": "3330", + "Factor": "100", + "type": "random_number", + "RND": [ + 3100, + 6600 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.20.0", + "Value": "80", + "Factor": "100", + "type": "random_number", + "RND": [ + 80, + 85 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.21.0", + "Value": "330000", + "Factor": "100", + "type": "random_number", + "RND": [ + 300000, + 356000 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.22.0", + "Value": "23030", + "Factor": "100", + "type": "random_number", + "RND": [ + 23000, + 25000 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.23.0", + "Value": "330000", + "Factor": "100", + "type": "random_number", + "RND": [ + 300000, + 356000 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.3.24.0", + "Value": "230000", + "Factor": "100", + "type": "random_number", + "RND": [ + 200000, + 296000 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.5.1.0", + "Value": "1", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.5.2.0", + "Value": "350", + "Factor": "100", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.5.3.0", + "Value": "2", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.5.4.0", + "Value": "1", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.5.8.0", + "Value": "4", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.5.9.0", + "Value": "19", + "Factor": "1", + "type": "random_number", + "RND": [ + 10, + 35 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.5.10.0", + "Value": "30", + "Factor": "1", + "type": "random_number", + "RND": [ + 20, + 56 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.1.0", + "Value": "1", + "Factor": "1", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.2.0", + "Value": "800", + "Factor": "10", + "type": "random_number", + "RND": [ + 800, + 990 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.3.0", + "Value": "536", + "Factor": "10", + "type": "random_number", + "RND": [ + 520, + 550 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.4.0", + "Value": "40", + "Factor": "10", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.5.0", + "Value": "3350", + "Factor": "1000", + "type": "random_number", + "RND": [ + 3000, + 3478 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.6.0", + "Value": "3350", + "Factor": "1000", + "type": "random_number", + "RND": [ + 3000, + 3478 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.7.0", + "Value": "3350", + "Factor": "1000", + "type": "random_number", + "RND": [ + 3000, + 3478 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.8.0", + "Value": "19", + "Factor": "1", + "type": "random_number", + "RND": [ + 10, + 35 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.29.0", + "Value": "270", + "Factor": "10", + "type": "random_number", + "RND": [ + 220, + 350 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.30.0", + "Value": "270", + "Factor": "10", + "type": "random_number", + "RND": [ + 220, + 350 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.31.0", + "Value": "370", + "Factor": "10", + "type": "random_number", + "RND": [ + 320, + 450 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.32.0", + "Value": "40", + "Factor": "10", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.33.0", + "Value": "270", + "Factor": "10", + "type": "random_number", + "RND": [ + 220, + 350 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.34.0", + "Value": "270", + "Factor": "10", + "type": "random_number", + "RND": [ + 220, + 350 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.35.0", + "Value": "270", + "Factor": "10", + "type": "random_number", + "RND": [ + 220, + 350 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.36.0", + "Value": "270", + "Factor": "10", + "type": "random_number", + "RND": [ + 220, + 350 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.45.0", + "Value": "19", + "Factor": "10", + "type": "random_number", + "RND": [ + 10, + 35 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.46.0", + "Value": "33410", + "Factor": "10", + "type": "random_number", + "RND": [ + 33400, + 33910 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.47.0", + "Value": "1000", + "Factor": "10", + "type": "random_number", + "RND": [ + 999, + 1000 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.49.0", + "Value": "1000", + "Factor": "10", + "type": "int" + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.50.0", + "Value": "1000", + "Factor": "10", + "type": "random_number", + "RND": [ + 890, + 990 + ] + }, + { + "OID": ".1.3.6.1.4.1.61626.1.1.4.6.51.0", + "Value": "50", + "Factor": "10", + "type": "int" + } + + ] + } +} \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..c3c6318 --- /dev/null +++ b/main.py @@ -0,0 +1,231 @@ +#!/usr/bin/python +# -*- coding: UTF-8 -*- +# __author__ = 'HKC' +import io +import logging +from logging.handlers import RotatingFileHandler +import json +import datetime +import time +import sys +import os +from paho.mqtt import client as mqtt_client +import random +import getopt + + +# 日志文件路径 +LOG_FILE_PATH = "./gwsimulator.log" +CFG_FILE_PATH = "./config.json" + +# create logger +logger = logging.getLogger(__name__) + +DAEMON_MODE = 0 + + +def log_setup(): + if len(logger.handlers) == 0: + level = logging.DEBUG + if os.path.exists(LOG_FILE_PATH): + file_size = os.path.getsize(LOG_FILE_PATH) + if file_size > 15000000: + timestamp = time.mktime(datetime.datetime.now().timetuple()) + time_fmt = datetime.datetime.fromtimestamp(timestamp).strftime("%Y%m%d_%H%M") + new_name = LOG_FILE_PATH + "." + time_fmt + os.rename(LOG_FILE_PATH, new_name) + time.sleep(1) + + handler = RotatingFileHandler(LOG_FILE_PATH, mode='a', maxBytes=30 * 1024 * 1024, backupCount=10, encoding=None, + delay=False) + formatter = logging.Formatter( + "[%(asctime)s] [%(module)s.%(funcName)s][%(lineno)s] [%(levelname)s] : %(message)s") + handler.setFormatter(formatter) + + if DAEMON_MODE == 0: + console_handler = logging.StreamHandler() + formatter = logging.Formatter("[%(asctime)s] [%(levelname)s] : %(message)s", "%Y-%m-%d %H:%M:%S") + console_handler.setFormatter(formatter) + logger.addHandler(console_handler) + + logger.addHandler(handler) + logger.setLevel(level) + + logger.info("===== log start, Welcome to the Earth ====") + + +# 保存异常信息 +def dump_exception_msg(e, ip="127.0.0.1"): + exc_type, exc_value, exc_traceback = sys.exc_info() + msg = "[%s] %s %s %s" % (ip, sys._getframe(1).f_code.co_name, exc_type, exc_value) + logger.error( msg ) + logger.exception(e) + + +def rand(lower_limit=None, upper_limit=None): + return random.randint(lower_limit, upper_limit) + + +class MqttConfigure: + def __init__(self): + self.broker_ip = '127.0.0.1' + self.port = 8764 + self.username = 'username' + self.passwd = 'passwd' + self.publish_topic = 'public' + self.publish_interval = 5000 + self.publish_qos = 0 + self.subscribe_qos = 0 + self.subscriber_topic = '' + self.client_id = f'python-mqtt-{random.randint(0, 1000)}' + self.max_publish_count = 0 + + +class PublishData: + def __init__(self): + self.publish_data = {} + + +def read_configure(mqtt_conf=None, publish_data=None): + abspath = os.path.abspath(__file__) + json_path = os.path.dirname(abspath) + "/" + CFG_FILE_PATH # '/config.json' + try: + with io.open(json_path, 'r') as f: + json_data = json.load(f) + mqtt_conf.broker_ip = json_data['broker_ip'] + mqtt_conf.port = json_data['port'] + mqtt_conf.username = json_data['username'] + mqtt_conf.passwd = json_data['passwd'] + mqtt_conf.publish_topic = json_data['publish_topic'] + mqtt_conf.publish_interval = json_data['publish_interval'] + mqtt_conf.publish_qos = json_data['publish_qos'] + mqtt_conf.subscribe_qos = json_data['subscribe_qos'] + mqtt_conf.subscriber_topic = json_data['subcriber_topic'] + mqtt_conf.client_id = json_data['client_id'] + publish_data.publish_data = json_data['publish_data'] + except Exception as e: + dump_exception_msg(e) + + +def pub_connect_mqtt(): + def on_connect(client, userdata, flags, rc): + if rc == 0: + logger.info("Connected to MQTT Broker!") + else: + logger.error("Failed to connect, return code %d", rc) + + client = mqtt_client.Client(protocol=3, client_id=mqtt_conf.client_id) + client.on_connect = on_connect + client.username_pw_set(username=mqtt_conf.username, password=mqtt_conf.passwd) + client.connect(host=mqtt_conf.broker_ip, port=mqtt_conf.port, keepalive=60) + time.sleep(1) + return client + + +def sub_connect_mqtt(): + def on_connect(client, userdata, flags, rc): + if rc == 0: + logger.info("Connected to MQTT Broker!") + else: + logger.error("Failed to connect, return code %d", rc) + + def on_message(client, userdata, msg): + logger.info(msg.topic + " " + str(msg.payload)) + + client = mqtt_client.Client(protocol=3,client_id=mqtt_conf.client_id) + client.on_connect = on_connect + client.on_message = on_message + client.username_pw_set(username=mqtt_conf.username, password=mqtt_conf.passwd) + client.connect(host=mqtt_conf.broker_ip, port=mqtt_conf.port, keepalive=60) + time.sleep(1) + return client + + +# 根据配置信息构建发布的报文消息 +def build_publish_msg(): + pd = publish_data.publish_data + msg = {"FsuCode": pd["FsuCode"], "type": pd["type"], "IdCodeContent": [], + "TimeStamp": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())} + for one in pd["IdCodeContent"]: + idcode = one["OID"] + factor = one["Factor"] + t = one["type"] + value = one["Value"] + if t == "random_number": + lo = one["RND"][0] + up = one["RND"][1] + value = rand(lo, up) + + msg['IdCodeContent'].append({ + "OID": idcode, + "Factor": factor, + "Value": value + }) + json_data = json.dumps(msg, ensure_ascii=False) + return json_data + + +def publish(client): + msg_count = 0 + while True: + if mqtt_conf.max_publish_count != 0 and msg_count > mqtt_conf.max_publish_count: + break + msg = build_publish_msg() + result = client.publish(topic=mqtt_conf.publish_topic, payload=msg, qos=mqtt_conf.publish_qos) + # result: [0, 1] + status = result[0] + msg_count += 1 + if status == 0: + logger.info(f"Send `#{msg_count}` message to topic `{mqtt_conf.publish_topic}`: `{msg}` ") + else: + logger.error(f"Failed to send `#{msg_count}` message to topic {mqtt_conf.publish_topic}") + time.sleep(mqtt_conf.publish_interval/1000) + + +if __name__ == '__main__': + log_setup() + try: + running_mode = "pub" + + argv = sys.argv + if len(argv) == 1: + print('Usage: gwsimulator.py -m -c ') + print(' or: gwsimulator.py --mode=pub|sub -c ') + sys.exit(2) + + opts, args = getopt.getopt(argv[1:], "m:c:", ["mode=", "config="]) + for opt, arg in opts: + if opt in ("-m", "--mode"): + running_mode = arg + elif opt in("-c", "--config"): + CFG_FILE_PATH = arg + else: + print('Usage: gwsimulator.py -m -c ') + print(' or: gwsimulator.py --mode=pub|sub -c ') + sys.exit(2) + + logger.info(f"Running in {running_mode} mode") + + mqtt_conf = MqttConfigure() + publish_data = PublishData() + + if sys.version_info >= (3, 0): + read_configure(mqtt_conf, publish_data) + else: + logger.error("I need python version > 3.0.0") + sys.exit(2) + + if running_mode.lower() == "pub": + client = pub_connect_mqtt() + client.loop_start() + publish(client) + else: + client = sub_connect_mqtt() + client.subscribe(topic=mqtt_conf.subscriber_topic, qos=mqtt_conf.subscribe_qos) + client.loop_forever() + + except KeyboardInterrupt: + logger.error("Disconnected from broker and EXIT") + + client.disconnect() + logger.info("===== log end, I will be back ====")