-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpub.py
56 lines (47 loc) · 1.37 KB
/
pub.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import threading
from config import config
import paho.mqtt.client #import client library
import time
from helper import *
from queue import Queue
# Global vars
q = Queue(maxsize=config.queue_size_limit)
img_proc = img_pre_proc(encoding='b64', compression='',device_id=config.mqtt_client_name)
# MQTT setup
client = paho.mqtt.client.Client(config.mqtt_client_name)
client.on_connect = on_connect
client.on_publish = on_publish
client.on_disconnect = on_disconnect
print('attempting to connect with id: ', config.mqtt_client_name)
client.connect(config.mqtt_borker_ip, port=1883)
time.sleep(0.5)
client.loop_start()
lock = threading.Lock()
# Start data loading
load_data_thr = threading.Thread(target=load_data, args=(lock,q,img_proc))
load_data_thr.start()
while True:
# check network and broker connection
weak, quality = is_network_weak()
timeout_cnt = 0
while (weak):
time.sleep(1)
timeout_cnt += 1
print("wating for better signal, current strength = ",quality,"%")
if timeout_cnt == config.max_timeouts:
break
weak, quality = is_network_weak()
if timeout_cnt == config.max_timeouts:
break
lock.acquire()
if not q.empty():
payload = q.get()
else: # nothing to publish
lock.release()
continue
lock.release()
print("attempting publish")
client.publish(topic="img",payload=payload,qos=2)
time.sleep(config.img_xmit_time )
load_data_thr.join()
client.loop_stop()