[update] add parameter file & fix some mqtt bug
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
DC Voltage DC_BIAS
|
||||
AC Amplitude AC_AMP
|
||||
Frequency Range Max FREQ_START
|
||||
Frequency Range Min FREQ_STOP
|
||||
Points Per Decades PPD
|
||||
Point Spacing SCALE
|
||||
Delay DELAY
|
||||
Average AVERAGE_NUM
|
||||
Current Range RTIA
|
||||
+1
-1
@@ -8,7 +8,7 @@ class DeviceManager():
|
||||
|
||||
def set_device(self, device):
|
||||
self._device = device
|
||||
self._mac_address = device['data'][0]['device_address']
|
||||
self._mac_address = self._device['data'][0]['device_address']
|
||||
|
||||
def get_device(self):
|
||||
return self._device
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
import time
|
||||
import sys
|
||||
import json
|
||||
|
||||
from deviceManager import DeviceManager
|
||||
from requests import Requests
|
||||
@@ -12,32 +13,34 @@ class Main():
|
||||
def __init__(self, controller_id = 'b8:27:eb:0c:d3:11', mqtt_ip = '192.168.5.53') -> None:
|
||||
self._deviceManager = DeviceManager()
|
||||
self._mqttThread = MqttThread(self, controller_id, mqtt_ip, 1883, 'test')
|
||||
self._mqttThread.start()
|
||||
|
||||
# wait for mqtt run start
|
||||
time.sleep(1)
|
||||
|
||||
self._mqttThread.run()
|
||||
|
||||
self._mqttRequest = Requests(self._mqttThread)
|
||||
self._mqttRequest.get_device()
|
||||
# wait for getting device in controller
|
||||
time.sleep(1)
|
||||
|
||||
# instruction example 1
|
||||
# self._mqttRequest.device_parameter_set(self._deviceManager.get_mac_address(), 'BLE_WRITE', '040005')
|
||||
# self._mqttRequest.device_instruction_send(self._deviceManager.get_mac_address(), 'ble_instru_send')
|
||||
|
||||
# instruction example 2
|
||||
self._mqttRequest.device_parameter_set(self._deviceManager.get_mac_address(), 'BLE_WRITE', '01')
|
||||
self._mqttRequest.device_instruction_send(self._deviceManager.get_mac_address(), 'ble_instru_send')
|
||||
self._mqttRequest.device_parameter_get(self._deviceManager.get_mac_address(), 'ADC_VALUE_I')
|
||||
|
||||
def get_device_info_all(self, device) -> str:
|
||||
self._deviceManager.set_device(device)
|
||||
def get_device_info_all(self, device: str) -> str:
|
||||
deviceDict = json.loads(device)
|
||||
# until get device info
|
||||
self._deviceManager.set_device(deviceDict)
|
||||
|
||||
# instruction example 1
|
||||
# self._mqttRequest.device_parameter_array_set(self._deviceManager.get_mac_address(), 'BLE_WRITE', '040005')
|
||||
# self._mqttRequest.device_instruction_send(self._deviceManager.get_mac_address(), 'ble_instru_send')
|
||||
|
||||
# instruction example 2
|
||||
# self._mqttRequest.device_parameter_array_set(self._deviceManager.get_mac_address(), 'BLE_WRITE', '01')
|
||||
# self._mqttRequest.device_instruction_send(self._deviceManager.get_mac_address(), 'ble_instru_send')
|
||||
# self._mqttRequest.device_parameter_get(self._deviceManager.get_mac_address(), 'ADC_VALUE_I')
|
||||
|
||||
#instruction example 3
|
||||
self._mqttRequest.device_parameter_set(self._deviceManager.get_mac_address(), 'MODE', 4)
|
||||
self._mqttRequest.device_instruction_send(self._deviceManager.get_mac_address(), 'start')
|
||||
|
||||
def device_parameter_value(self, data) -> None:
|
||||
print('device_parameter_value', data)
|
||||
if data['data'].get('ADC_VALUE_I') is not None:
|
||||
value = format(data['data']['ADC_VALUE_I'], '040x')
|
||||
def device_parameter_value(self, data: str) -> None:
|
||||
dataDict = json.loads(data)
|
||||
print(dataDict)
|
||||
if dataDict['data'].get('ADC_VALUE_I') is not None:
|
||||
value = format(dataDict['data']['ADC_VALUE_I'], '040x')
|
||||
print(value)
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -2,14 +2,11 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
import time, random, threading
|
||||
import multiprocessing as mp
|
||||
import time, random
|
||||
import json
|
||||
|
||||
class MqttThread(threading.Thread):
|
||||
class MqttThread():
|
||||
def __init__(self, callback_API, controller_id: str, url: str, port: int, client_id: str, name='thread-1', alive=60):
|
||||
super(MqttThread, self).__init__(name = name)
|
||||
|
||||
self.__controller_ID = controller_id
|
||||
|
||||
self._mqtt_url = url
|
||||
@@ -54,9 +51,8 @@ class MqttThread(threading.Thread):
|
||||
|
||||
def on_connect(self, client: mqtt.Client, user_data, flags, rc: int):
|
||||
topic = self.__controller_ID + '_user'
|
||||
# print('cloud-server subscribe', topic)
|
||||
# self._mqtt_client.subscribe(topic)
|
||||
self._mqtt_client.subscribe(self.__controller_ID + '/+/+')
|
||||
self._mqtt_client.subscribe(self.__controller_ID + '/+/+')
|
||||
self._mqtt_client.subscribe(self.__controller_ID + '/data_server/device_data_stream/+/+')
|
||||
self._connected = True
|
||||
|
||||
def on_disconnect(self, client: mqtt.Client, user_data, rc):
|
||||
@@ -76,7 +72,11 @@ class MqttThread(threading.Thread):
|
||||
|
||||
def on_message(self, client: mqtt.Client, user_data, message):
|
||||
topic = message.topic.split('/')[1]
|
||||
payload = json.loads(message.payload.decode('utf-8'))
|
||||
try:
|
||||
payload = message.payload.decode('utf-8')
|
||||
print(message.topic, payload)
|
||||
except:
|
||||
print('error')
|
||||
|
||||
func = getattr(self._handler, topic, None)
|
||||
if func != None:
|
||||
|
||||
+10
-1
@@ -11,7 +11,7 @@ class Requests():
|
||||
}, True)
|
||||
return
|
||||
|
||||
def device_parameter_set(self, device: str, name: str, data: str) -> None:
|
||||
def device_parameter_array_set(self, device: str, name: str, data: str) -> None:
|
||||
dataArray = [0 for i in range(16)]
|
||||
for index in range(0, len(data), 2):
|
||||
dataArray[int(index / 2)] = int(data[index:index+2],16)
|
||||
@@ -24,6 +24,15 @@ class Requests():
|
||||
})
|
||||
return
|
||||
|
||||
def device_parameter_set(self, device: str, name: str, data: str) -> None:
|
||||
self._mqtt_client.publish({
|
||||
'header': 'device_parameter/0',
|
||||
'device': device,
|
||||
'parameter': name,
|
||||
'content': data
|
||||
})
|
||||
return
|
||||
|
||||
def device_parameter_get(self, device: str, name: str) -> None:
|
||||
self._mqtt_client.publish({
|
||||
'header': 'device_parameter_value/0',
|
||||
|
||||
Reference in New Issue
Block a user