diff --git a/MQTT.py b/MQTT.py index f369b46..b49b19b 100755 --- a/MQTT.py +++ b/MQTT.py @@ -3,6 +3,7 @@ import os import time import paho.mqtt.client as mqtt +from device import device_name from tool import push_stream, close_stream, update, exec_sh, get_record, get_list_record, get_status @@ -19,7 +20,7 @@ def valid(msg, client): if 'device_name' not in origin_data: client.publish('error', payload='device_name must be supplied', qos=0) return False - if os.getenv('device_name').strip() != origin_data['device_name']: + if device_name != origin_data['device_name']: return False return True @@ -71,7 +72,7 @@ def exec_shutdown(): if __name__ == '__main__': while True: try: - client = mqtt.Client(client_id=os.getenv('device_name').strip()) + client = mqtt.Client(client_id=device_name) client.username_pw_set("demo", "123456") # Specify callback function client.on_connect = on_connect diff --git a/data_upload.py b/data_upload.py index 46b86c8..92bb152 100755 --- a/data_upload.py +++ b/data_upload.py @@ -3,6 +3,8 @@ import subprocess import time import paho.mqtt.client as mqtt +from device import device_name + def on_connect(client, userdata, flags, rc): global times @@ -21,7 +23,7 @@ times = 3 if __name__ == '__main__': while True: try: - client = mqtt.Client(client_id=os.getenv('device_name').strip()) + client = mqtt.Client(client_id=device_name) client.username_pw_set("demo", "123456") # Specify callback function client.on_connect = on_connect diff --git a/device.py b/device.py index fa05591..de7b687 100644 --- a/device.py +++ b/device.py @@ -1,5 +1,7 @@ +import os import subprocess -p = subprocess.Popen(['cat /home/pi/device_name'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) -out, err = p.communicate() -device_name = out.decode('utf-8').strip() +# p = subprocess.Popen(['cat /home/pi/device_name'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) +# out, err = p.communicate() +# device_name = out.decode('utf-8').strip() +device_name = os.getenv('device_name').strip() diff --git a/sensor_to_local.py b/sensor_to_local.py index 52ac798..7eff865 100755 --- a/sensor_to_local.py +++ b/sensor_to_local.py @@ -5,6 +5,7 @@ import serial from db.models.lot_data_model import LOT_DATA from api import add +from device import device_name def local(): @@ -210,7 +211,7 @@ def test(): 'wind_speed': 1, 'wind_direction': 1, 'create_time': int(time.time()), - 'device_name': os.getenv('device_name') + 'device_name': device_name } t2 = LOT_DATA(**data) add(t2) diff --git a/sensor_to_server.py b/sensor_to_server.py index 126624b..0b6a6c7 100755 --- a/sensor_to_server.py +++ b/sensor_to_server.py @@ -8,6 +8,7 @@ import paho.mqtt.client as mqtt from api import add from db.models.lot_data_model import LOT_DATA +from device import device_name def t(): @@ -226,7 +227,7 @@ def on_connect(client, userdata, flags, rc): if __name__ == '__main__': - client = mqtt.Client(client_id=os.getenv('device_name').strip()) + client = mqtt.Client(client_id=device_name) client.username_pw_set("demo", "123456") # Specify callback function client.on_connect = on_connect