-
Notifications
You must be signed in to change notification settings - Fork 2
/
eventRecorder.py
executable file
·78 lines (59 loc) · 1.66 KB
/
eventRecorder.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from encodings.utf_8 import decode
import time
import os
import paho.mqtt.client as mqttClient
import time
import re
import mysql.connector
mydb=mysql.connector.connect(
host="",
user="",
password="",
database=""
)
print(mydb)
cursor=mydb.cursor()
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to broker")
global Connected
Connected = True
else:
print("Connection failed")
def on_message(client, userdata, message):
var=message.payload
var=var.decode()
print (var)
list=var.split('>')
GUID=list[0]
serial=list[1]
message=list[2]
time=list[3]
qr=list[5]
status=list[4]
sql="INSERT INTO events (GUID, serial_number,message,scan_time,qr_code,status) values (%s, %s, %s,%s,%s,%s)"
val=(var)
cursor.execute(sql,(GUID,serial,message,time,qr,status,))
mydb.commit()
Connected = False
broker_address= "mysql39.mydevil.net"
port = 1883
user = "nikodem"
password = "nikodem"
client = mqttClient.Client("DB")
client.username_pw_set(user, password=password)
client.on_connect= on_connect
client.on_message= on_message
client.connect(broker_address, port=port)
client.loop_start()
while Connected != True:
time.sleep(0.1)
client.subscribe("/iotlocks/v1/+/event")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print ("exiting")
client.disconnect()
client.loop_stop()
os.system("python MQTTlogPUSH.py")