|
|
@@ -5,56 +5,30 @@ import struct
|
|
|
import os
|
|
|
import time
|
|
|
import re
|
|
|
-from esp_mqtt import MQTTClient
|
|
|
+
|
|
|
|
|
|
class Client():
|
|
|
def __init__(self):
|
|
|
- self.data = os.urandom(1)
|
|
|
+ self.count = 0
|
|
|
+ self.timer = Timer(0)
|
|
|
+ self.timer.init(period=2000, mode=Timer.PERIODIC, callback=self.push)
|
|
|
|
|
|
- def push(self):
|
|
|
- self.data=os.urandom(1)
|
|
|
- print("upload:", self.data)
|
|
|
+ def push(self, timer):
|
|
|
+ self.count += 1
|
|
|
+ print("Client:", self.count)
|
|
|
|
|
|
|
|
|
class Test():
|
|
|
def __init__(self) -> None:
|
|
|
- temp = struct.unpack('!BBBB', os.urandom(4))
|
|
|
- print(temp)
|
|
|
- self.timer = Timer(-1)
|
|
|
- print("self", self)
|
|
|
-
|
|
|
- # self.client = Client()
|
|
|
- # # # 每 2000ms 定时回调一次 self.upload_data()
|
|
|
- # self.timer.init(period=2000, mode=Timer.PERIODIC, callback=self.publish_timer_callback)
|
|
|
- # self.count = 0
|
|
|
+ self.count = 0
|
|
|
+ self.timer = Timer(0)
|
|
|
+ self.timer.init(period=2000, mode=Timer.PERIODIC, callback=self.publish_timer_callback)
|
|
|
|
|
|
def publish_timer_callback(self, timer):
|
|
|
self.count += 1
|
|
|
- print("upload:", self.count)
|
|
|
- print("self", self.client)
|
|
|
- self.client.push()
|
|
|
- print("self.client")
|
|
|
- print("self.timer:", self.timer)
|
|
|
+ print("Test:", self.count)
|
|
|
|
|
|
- def receive_cmd_callback(self, topic_list, msg):
|
|
|
- if topic_list[4] == 'request':
|
|
|
- cmdid = topic_list[5]
|
|
|
- response_topic = f"$sys/{pid}/{device-name}/cmd/response/{cmdid}"
|
|
|
- print(response_topic)
|
|
|
-
|
|
|
-
|
|
|
- def client_msg_callback(self, topic, msg):
|
|
|
- print("\n------------- receive ------------- \n")
|
|
|
- print("topic:%s\n" % topic)
|
|
|
- print("payload:%s\n" % msg)
|
|
|
- re.compile("$sys/(\d+)/(w+)/(w+)/(w+)")
|
|
|
- topic_list = topic.split('/')
|
|
|
- # 收到服务器命令:$sys/{pid}/{device-name}/cmd/request/{cmdid}
|
|
|
- if topic_list[3] == 'cmd':
|
|
|
- self.receive_cmd_callback(topic_list, msg)
|
|
|
|
|
|
-t = Test()
|
|
|
-topick = "$sys/522417/emq_online/cmd/request/c9f24219-1ec2-4e21-9b37-4673c5ca3db7"
|
|
|
-payload = "open"
|
|
|
-t.client_msg_callback(topick, payload)
|
|
|
|
|
|
+t = Test()
|
|
|
+c= Client()
|