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"])
示例#2
0
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)
示例#3
0
    payload = "hand_shake " + payload["app_id"] + " " + payload[
        "device_id"] + " " + payload["state"]
    server.publish(topic, payload, 2)


@server.add_action2
def add_app(msg):
    """
        从app发来的设备添加信息
    """
    topic, payload = get_add_device_app_return_msg(msg)
    payload = json.dumps(payload)
    server.publish(topic, payload, 2)


def add_rusult():
    pass


if __name__ == "__main__":
    host = "52.184.15.163"
    flask_host = "10.0.2.4"
    flask_port = 5000
    port = 1883
    server.run("345", host, port)
    server.add_subscribe(server.topic["2server"])
    app.run(flask_host, flask_port)
    loop_time = 1
    while True:
        print("loop time is ", loop_time)
        time.sleep(10)
示例#4
0
    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)
    #     time.sleep(1)
    time.sleep(1000)
示例#5
0
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:
        print("on running, time is " + str(time.localtime(time.time())))
        test_client.publish("test", "led_off", 2)
        time.sleep(10)