-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingServiceClient.py
executable file
·123 lines (92 loc) · 3.83 KB
/
pingServiceClient.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
import paho.mqtt.client as mqtt
import logging
import time
import json
import datetime
AllowedActions = ['both', 'publish', 'subscribe']
def replyToPing(sequence):
pingData = {}
pingData['sequence'] = sequence
pingData['message'] = "Ping response."
message = {}
message['device_mac'] = "D4:25:8B:D9:E7:2F"
message['timestamp'] = str(datetime.datetime.now())
message['event_id'] = 1
message['event'] = pingData
messageJson = json.dumps(message)
myAWSIoTMQTTClient.publishAsync("pl19/event", messageJson, 1)
print(' * Ping answered!')
def sendLog(data, message, event_id, id):
pingData = {}
pingData['id'] = id
pingData['value'] = data
pingData['message'] = message
message = {}
message['device_mac'] = "D4:25:8B:D9:E7:2F"
message['timestamp'] = str(datetime.datetime.now())
message['event_id'] = event_id
message['event'] = pingData
messageJson = json.dumps(message)
print("sending log: ")
print(messageJson)
myAWSIoTMQTTClient.publishAsync("pl19/event", messageJson, 1)
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
print("Client subscribed!")
client.subscribe("+/event/+")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print("message received!")
if(str(msg.topic[-6:]) == "status"):
sendLog((msg.payload).decode('utf-8'), "Status changed", 2, str(msg.topic[:15]))
elif(str(msg.topic[-7:]) == "status"):
sendLog((msg.payload).decode('utf-8'), "Desired temp changed", 3, str(msg.topic[:15]))
elif(str(msg.topic[-7:]) == "setname"):
sendLog((msg.payload).decode('utf-8'), "Room name changed", 4, str(msg.topic[:15]))
elif(str(msg.topic[-5:]) == "state"):
sendLog((msg.payload).decode('utf-8'), "ESP state", 5, str(msg.topic[:15]))
elif(str(msg.topic[-5:]) == "onoff"):
sendLog((msg.payload).decode('utf-8'), "Room on/off", 6, str(msg.topic[:15]))
# Custom MQTT message callback
def customCallback(client, userdata, message):
messageContent = json.loads(message.payload.decode('utf-8'))
messageData = messageContent['event']
if messageContent['event_id'] == 0:
print("* Received ping!")
replyToPing(messageData['sequence'])
else:
print(json.dumps(messageData))
host = "a3cezb6rg1vyed-ats.iot.us-west-2.amazonaws.com"
rootCAPath = "root-CA.crt"
certificatePath = "PL-student.cert.pem"
privateKeyPath = "PL-student.private.key"
port = 8883
clientId = "pl19-18"
topic = "pl19/event"
# Configure logging
streamHandler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
# Init AWSIoTMQTTClient
myAWSIoTMQTTClient = None
myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId)
myAWSIoTMQTTClient.configureEndpoint(host, port)
myAWSIoTMQTTClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath)
# AWSIoTMQTTClient connection configuration
myAWSIoTMQTTClient.configureAutoReconnectBackoffTime(1, 32, 20)
myAWSIoTMQTTClient.configureOfflinePublishQueueing(-1) # Infinite offline Publish queueing
myAWSIoTMQTTClient.configureDrainingFrequency(2) # Draining: 2 Hz
myAWSIoTMQTTClient.configureConnectDisconnectTimeout(10) # 10 sec
myAWSIoTMQTTClient.configureMQTTOperationTimeout(5) # 5 sec
myAWSIoTMQTTClient.connect()
myAWSIoTMQTTClient.subscribe("pl19/notification", 1, customCallback)
print(" * Ping subscribed!")
myAWSIoTMQTTClient2 = None
myAWSIoTMQTTClient2 = AWSIoTMQTTClient("localAWSLog")
myAWSIoTMQTTClient2.configureEndpoint("localhost", 1883)
myAWSIoTMQTTClient2.connect()
myAWSIoTMQTTClient2.subscribe("+/event/+", 1, on_message)
time.sleep(2)
while True:
time.sleep(5)