from mqtt_client import device_interface as Client import flask import json host = "52.184.15.163" port = 1883 app = flask.Flask(__name__) client = Client("server") client.run("out", host, port) client.subscribe("out", 2) msg_queen = {} def add_to_queen(msg, client): topic = str(msg.topic, encoding="utf-8") payload = str(msg.payload, encoding="utf-8") if topic in msg_queen.keys(): msg_queen[topic].append(payload) else: msg_queen[topic] = [payload] print("**************************") print("get topic :" + topic) print("get payload:" + payload) print("**************************") client.default_func = add_to_queen @app.route("/lock", methods=["GET", "POST"])
from example_for_load import led_off, led_on, config_file from mqtt_client import device_interface as Client if __name__ == "__main__": host = "52.184.15.163" port = 1883 client = Client("device/1111") client.qos = 2 client.add_action(led_on) client.add_action(led_off) client.run("123", host, port) client.add_subscribe("test") client.save_to_config(config_file)
from mqtt_client import device_interface as Client t = Client("ccc") t.load_from_config() t.save_to_config("./test_config.json") print(t.action)
import flask import json import time from mqtt_client import device_interface as Server from mqtt_client import get_topic_and_payload server_id = "server" server = Server(server_id) app = flask.Flask(__name__) url_send_back = {} def generate_url(pic_name, topic, time): url = "http://52.184.15.163:5000/get_pic" return flask.url_for(url, pic_name=pic_name, topic=topic, time=time) @app.route("/upload", methods=["GET", "POST"]) def upload(pic_name, topic, time): uploadfile = flask.request.files.get("pic_name") path = "".join(get_pic_path(topic, time)) if uploadfile: uploadfile.save(path) url = generate_url(pic_name, topic, time) localtime = time.strftime("%Y%m%d%H%M%S", time.localtime()) payload_to_send = "find_stranger " + localtime + " " + url for app_topic in url_send_back[topic]: server.publish(app_topic, payload_to_send, 2) url_send_back[topic] = None
def time_test(msg): print("time_test:success,call by mqtt client") payload = str(msg.payload, encoding="utf-8") time1 = float(payload.split()[1]) time2 = time.perf_counter() print("time use is :{}".format((time2 - time1) * 1000)) if __name__ == "__main__": topic = "test" clinet_id = "test1" host = "52.184.15.163" port = 1883 t = Client(clinet_id) t.add2device_topic(topic) t.add_action(print_msg) t.add_action(print_msg2) t.add_action(print_msg3) t.add_action(time_test) # print(t.action.keys()) # print(type(t.action.keys())) # print("print_msg" in t.action.keys()) t.run("123", host, port) t.subscribe("test", 2) print("set down") t2 = Client("test3") t2.run("234", host, port) # for i in range(100): # t2.publish("test","time_test "+str(time.perf_counter()),2)
from mqtt_client import device_interface as Client from mqtt_client import _load_python_file client = Client("12333") if __name__ == "__main__": paths = "C:\\Users\\sl1729\\Documents\\Xilinx_summer_school_project\\face_s_server_project\\test_add_action2.py" print(type(client)) client.__init__("12333") print(dir(client)) module = client.load_python_module(paths) client2 = getattr(module, "client") print(client2 == client) # for func in dir(module): # if func.startswith("_"): # continue # func = getattr(module, func) # if callable(func): # func() print(client.action)
def time_test(msg): print("time_test:success,call by mqtt client") payload = str(msg.payload, encoding="utf-8") time1 = float(payload.split()[1]) time2 = time.perf_counter() print("time use is :{}".format((time2 - time1) * 1000)) if __name__ == "__main__": topic = "test" clinet_id = "test1" host = "52.184.15.163" port = 1883 t = Client(clinet_id) t.add2device_topic(topic) t.add_action(print_msg) t.add_action(print_msg2) t.add_action(print_msg3) t.add_action(time_test) print(t.action_load) # print(t.action.keys()) # print(type(t.action.keys())) # print("print_msg" in t.action.keys()) # t.run("123",host,port) # t.subscribe("test",2) # print("set down") # t2 = Client("test3") # t2.run("234",host,port) # # for i in range(100):
def time_test(msg): print("time_test:success,call by mqtt client") payload = str(msg.payload, encoding="utf-8") time1 = float(payload.split()[1]) time2 = time.perf_counter() print("time use is :{}".format((time2 - time1) * 1000)) if __name__ == "__main__": topic = "test" clinet_id = "test1" host = "52.184.15.163" port = 1883 t = Client(clinet_id) t.add2device_topic(topic) t.add_action(print_msg) t.add_action(print_msg2) t.add_action(print_msg3) t.add_action(time_test) # print(t.action_load) t.save_to_config() # print(t.action.keys()) # print(type(t.action.keys())) # print("print_msg" in t.action.keys()) # t.run("123",host,port) # t.subscribe("test",2) # print("set down") # t2 = Client("test3") # t2.run("234",host,port)
"pic_name": "test.jpg", "topic": "device_id", "time": "202007291709" } files = {'file': (frame)} r = requests.post(url, params=params_data, files=files) print("get state code is :", r.status_code) device_id = "device" device_topic_sub = "todevice" app_topic = "toapp" app_id = "app" host = "52.184.15.163" port = 1883 client = Client(device_id) client.run("123", host, port) time.sleep(1) client.add_subscribe(device_topic_sub) client.add_action(_load_python_file) client.add_action(_save_input_py_file) client.add2device_topic("todevice") client.add2app_device_topic("toapp") @client.add_action2 def starnger_test(): print("this is test for find stranger") find_stranger("toapp") print("finish to send msg 1") upload_pic("111")
from mqtt_client import device_interface as Client import time LOAD_FROM_FILE = True config_file = "./example_for_load_config.json" client = Client("device/1111") def led_on(): print("1234led_on") def led_off(): print("1234len_off") if __name__ == "__main__": host = "52.184.15.163" port = 1883 test_client = Client("device/2222") test_client.run("234", host, port) if LOAD_FROM_FILE: client.load_from_config(config_file) else: client.add_action(led_on) client.add_action(led_off) client.run("123", host, port) client.add_subscribe("test") client.save_to_config() while True: