update client, build docker successfully
This commit is contained in:
		
							
								
								
									
										35
									
								
								client.py
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								client.py
									
									
									
									
									
								
							@@ -2,11 +2,15 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
import asyncio
 | 
					import asyncio
 | 
				
			||||||
 | 
					import numpy as np
 | 
				
			||||||
 | 
					import cv2 as cv
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from amqtt.client import MQTTClient
 | 
					from amqtt.client import MQTTClient
 | 
				
			||||||
from amqtt.mqtt.constants import QOS_0, QOS_1, QOS_2
 | 
					from amqtt.mqtt.constants import QOS_0, QOS_1, QOS_2
 | 
				
			||||||
import time
 | 
					import time
 | 
				
			||||||
import json
 | 
					import json
 | 
				
			||||||
 | 
					import yaml
 | 
				
			||||||
 | 
					import base64
 | 
				
			||||||
 | 
					
 | 
				
			||||||
logger = logging.getLogger(__name__)
 | 
					logger = logging.getLogger(__name__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -19,15 +23,27 @@ def get_uptime():
 | 
				
			|||||||
    print(uptime_seconds)
 | 
					    print(uptime_seconds)
 | 
				
			||||||
    return uptime_seconds
 | 
					    return uptime_seconds
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def get_frame():
 | 
				
			||||||
 | 
					        cap = cv.VideoCapture(0)
 | 
				
			||||||
 | 
					        if not cap.isOpened():
 | 
				
			||||||
 | 
					                print("Cannot open camera")
 | 
				
			||||||
 | 
					                exit()
 | 
				
			||||||
 | 
					        ret, frame = cap.read()
 | 
				
			||||||
 | 
					        gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
 | 
				
			||||||
 | 
					        frame = gray
 | 
				
			||||||
 | 
					        if not ret:
 | 
				
			||||||
 | 
					                print("Can't receive frame (stream end?). Exiting ...")
 | 
				
			||||||
 | 
					        print(len(base64.b64encode(frame)))
 | 
				
			||||||
 | 
					        height, width = frame.shape[:2]
 | 
				
			||||||
 | 
					        print(height, width)
 | 
				
			||||||
 | 
					        return base64.b64encode(frame).decode('utf-8')
 | 
				
			||||||
async def test_coro():
 | 
					async def test_coro():
 | 
				
			||||||
    C = MQTTClient()
 | 
					    C = MQTTClient()
 | 
				
			||||||
    print("START" + str(get_uptime()))
 | 
					    print("START" + str(get_uptime()))
 | 
				
			||||||
    await C.connect('mqtt://127.0.0.1:1883/')
 | 
					    await C.connect('mqtt://100.79.112.65:1883')
 | 
				
			||||||
    tasks = [
 | 
					    tasks = [
 | 
				
			||||||
 | 
					        asyncio.ensure_future(C.publish('sensors/pi1/camera', encoder({"data": str(get_frame()), "sensorID": 0, "timestamp": time.time_ns()}), qos=QOS_2)),
 | 
				
			||||||
        #asyncio.ensure_future(C.publish('sensors/pico1/distance', encoder({"data": 23.0, "sensorID": 0, "timestamp": time.time_ns()}))),
 | 
					        asyncio.ensure_future(C.publish('sensors/pi1/uptime', encoder({"data": get_uptime(), "sensorID": 0, "timestamp": time.time_ns()}), qos=QOS_2)),
 | 
				
			||||||
        #asyncio.ensure_future(C.publish('a/b', b'TEST MESSAGE WITH QOS_1', qos=QOS_1)),
 | 
					 | 
				
			||||||
        asyncio.ensure_future(C.publish('sensors/pico1/uptime', encoder({"data": get_uptime(), "sensorID": 0, "timestamp": time.time_ns()}), qos=QOS_2)),
 | 
					 | 
				
			||||||
    ]
 | 
					    ]
 | 
				
			||||||
    await asyncio.wait(tasks)
 | 
					    await asyncio.wait(tasks)
 | 
				
			||||||
    logger.info("messages published")
 | 
					    logger.info("messages published")
 | 
				
			||||||
@@ -37,16 +53,13 @@ async def test_coro():
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
    formatter = "[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
 | 
					    formatter = "[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
 | 
				
			||||||
    logging.basicConfig(level=logging.INFO, format=formatter)
 | 
					    logging.basicConfig(level=logging.DEBUG, format=formatter)
 | 
				
			||||||
    with open('client.yaml', 'r') as fileread:
 | 
					    with open('client.yaml', 'r') as fileread:
 | 
				
			||||||
        clientconfig = yaml.safe_load(fileread)
 | 
					        clientconfig = yaml.safe_load(fileread)
 | 
				
			||||||
    subdict(clientconfig, None)
 | 
					    print(clientconfig)
 | 
				
			||||||
    print(sublist)
 | 
					 | 
				
			||||||
    x = 0
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    while x < 5:
 | 
					    while True:
 | 
				
			||||||
        asyncio.get_event_loop().run_until_complete(test_coro())
 | 
					        asyncio.get_event_loop().run_until_complete(test_coro())
 | 
				
			||||||
        x = x + 1
 | 
					 | 
				
			||||||
    #asyncio.get_event_loop().run_until_complete(test_coro2())
 | 
					    #asyncio.get_event_loop().run_until_complete(test_coro2())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user