|
|
@ -20,13 +20,19 @@
|
|
|
|
**/
|
|
|
|
**/
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
|
|
|
|
import time
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#import RPi.GPIO as GPIO
|
|
|
|
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
import iotUtils
|
|
|
|
import iotUtils
|
|
|
|
|
|
|
|
|
|
|
|
global mqttClient
|
|
|
|
global mqttClient
|
|
|
|
mqttClient = mqtt.Client()
|
|
|
|
mqttClient = mqtt.Client()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global TOPIC_TO_PUBLISH_STREAM1
|
|
|
|
|
|
|
|
global TOPIC_TO_PUBLISH_STREAM2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# The callback for when the client receives a CONNACK response from the server.
|
|
|
|
# The callback for when the client receives a CONNACK response from the server.
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
@ -34,52 +40,107 @@ def on_connect(mqttClient, userdata, flags, rc):
|
|
|
|
print("MQTT_LISTENER: Connected with result code " + str(rc))
|
|
|
|
print("MQTT_LISTENER: Connected with result code " + str(rc))
|
|
|
|
# Subscribing in on_connect() means that if we lose the connection and
|
|
|
|
# Subscribing in on_connect() means that if we lose the connection and
|
|
|
|
# reconnect then subscriptions will be renewed.
|
|
|
|
# reconnect then subscriptions will be renewed.
|
|
|
|
print ("MQTT_LISTENER: Subscribing with topic " + TOPIC_TO_SUBSCRIBE)
|
|
|
|
print ("MQTT_LISTENER: Subscribing with topic " + SUBSCRIBE_FOR_LOCKER_COMMAND)
|
|
|
|
mqttClient.subscribe(TOPIC_TO_SUBSCRIBE)
|
|
|
|
mqttClient.subscribe(SUBSCRIBE_FOR_LOCKER_COMMAND)
|
|
|
|
|
|
|
|
print ("MQTT_LISTENER: Subscribing with topic " + SUBSCRIBE_FOR_FAN_COMMAND)
|
|
|
|
|
|
|
|
mqttClient.subscribe(SUBSCRIBE_FOR_FAN_COMMAND)
|
|
|
|
|
|
|
|
print ("MQTT_LISTENER: Subscribing with topic " + SUBSCRIBE_FOR_BULB_COMMAND)
|
|
|
|
|
|
|
|
mqttClient.subscribe(SUBSCRIBE_FOR_BULB_COMMAND)
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# The callback for when a PUBLISH message is received from the server.
|
|
|
|
# The callback for when a PUBLISH message is received from the server.
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
def on_message(mqttClient, userdata, msg):
|
|
|
|
def on_message(mqttClient, userdata, msg):
|
|
|
|
print( "MQTT_LISTENER: " + msg.topic + " " + str(msg.payload))
|
|
|
|
print("MQTT_LISTENER: " + msg.topic + " " + str(msg.payload))
|
|
|
|
|
|
|
|
if msg.topic == SUBSCRIBE_FOR_LOCKER_COMMAND:
|
|
|
|
|
|
|
|
request = str(msg.payload)
|
|
|
|
|
|
|
|
resource = ""
|
|
|
|
|
|
|
|
state = request.upper()
|
|
|
|
|
|
|
|
if state == "LOCK":
|
|
|
|
|
|
|
|
# GPIO.output(iotUtils.DOOR_LOCKER_2_PORT, GPIO.HIGH)
|
|
|
|
|
|
|
|
# GPIO.output(iotUtils.DOOR_LOCKER_1_PORT, GPIO.HIGH)
|
|
|
|
|
|
|
|
# GPIO.output(iotUtils.LOCK_STATE_OFF_NOTIFY_PORT, GPIO.HIGH)
|
|
|
|
|
|
|
|
# GPIO.output(iotUtils.LOCK_STATE_ON_NOTIFY_PORT, GPIO.LOW)
|
|
|
|
|
|
|
|
#mqttClient.publish(TOPIC_TO_PUBLISH, "Locker:LOCKED")
|
|
|
|
|
|
|
|
print "Door is locked"
|
|
|
|
|
|
|
|
elif state == "UNLOCK":
|
|
|
|
|
|
|
|
# GPIO.output(iotUtils.DOOR_LOCKER_2_PORT, GPIO.LOW)
|
|
|
|
|
|
|
|
# GPIO.output(iotUtils.DOOR_LOCKER_1_PORT, GPIO.LOW)
|
|
|
|
|
|
|
|
# GPIO.output(iotUtils.LOCK_STATE_OFF_NOTIFY_PORT, GPIO.LOW)
|
|
|
|
|
|
|
|
# GPIO.output(iotUtils.LOCK_STATE_ON_NOTIFY_PORT, GPIO.HIGH)
|
|
|
|
|
|
|
|
#mqttClient.publish(TOPIC_TO_PUBLISH, "Locker:UNLOCKED")
|
|
|
|
|
|
|
|
print "Door is unlocked"
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
print "MQTT message in the wrong format"
|
|
|
|
|
|
|
|
print "MQTT_LISTENER: Resource- " + resource
|
|
|
|
|
|
|
|
elif msg.topic == SUBSCRIBE_FOR_FAN_COMMAND:
|
|
|
|
|
|
|
|
request = str(msg.payload)
|
|
|
|
|
|
|
|
resource = ""
|
|
|
|
|
|
|
|
state = request.upper()
|
|
|
|
|
|
|
|
if state == "ON":
|
|
|
|
|
|
|
|
# GPIO.output(iotUtils.DOOR_LOCKER_2_PORT, GPIO.HIGH)
|
|
|
|
|
|
|
|
# GPIO.output(iotUtils.DOOR_LOCKER_1_PORT, GPIO.HIGH)
|
|
|
|
|
|
|
|
# GPIO.output(iotUtils.LOCK_STATE_OFF_NOTIFY_PORT, GPIO.HIGH)
|
|
|
|
|
|
|
|
# GPIO.output(iotUtils.LOCK_STATE_ON_NOTIFY_PORT, GPIO.LOW)
|
|
|
|
|
|
|
|
#mqttClient.publish(TOPIC_TO_PUBLISH, "Locker:LOCKED")
|
|
|
|
|
|
|
|
print "Fan is locked"
|
|
|
|
|
|
|
|
elif state == "OFF":
|
|
|
|
|
|
|
|
# GPIO.output(iotUtils.DOOR_LOCKER_2_PORT, GPIO.LOW)
|
|
|
|
|
|
|
|
# GPIO.output(iotUtils.DOOR_LOCKER_1_PORT, GPIO.LOW)
|
|
|
|
|
|
|
|
# GPIO.output(iotUtils.LOCK_STATE_OFF_NOTIFY_PORT, GPIO.LOW)
|
|
|
|
|
|
|
|
# GPIO.output(iotUtils.LOCK_STATE_ON_NOTIFY_PORT, GPIO.HIGH)
|
|
|
|
|
|
|
|
#mqttClient.publish(TOPIC_TO_PUBLISH, "Locker:UNLOCKED")
|
|
|
|
|
|
|
|
print "Fan is off"
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
print "MQTT message in the wrong format"
|
|
|
|
|
|
|
|
print "MQTT_LISTENER: Resource- " + resource
|
|
|
|
|
|
|
|
elif msg.topic == SUBSCRIBE_FOR_BULB_COMMAND:
|
|
|
|
|
|
|
|
request = str(msg.payload)
|
|
|
|
|
|
|
|
print "bulb is to blow"
|
|
|
|
|
|
|
|
resource = ""
|
|
|
|
|
|
|
|
print request
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# The callback for when a PUBLISH message to the server when door is open or close
|
|
|
|
# The callback for when a PUBLISH message to the server when door is open or close
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
def on_publish(mqttClient, stream1PlayLoad, stream2PlayLoad):
|
|
|
|
def on_publish(mqttClient, topic, msg):
|
|
|
|
mqttClient.publish(TOPIC_TO_PUBLISH_STREAM1, stream1PlayLoad)
|
|
|
|
mqttClient.publish(topic, msg)
|
|
|
|
mqttClient.publish(TOPIC_TO_PUBLISH_STREAM2, stream2PlayLoad)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sendSensorValue(stream1PlayLoad, stream2PlayLoad):
|
|
|
|
|
|
|
|
|
|
|
|
def sendLockerStatus(msg):
|
|
|
|
global mqttClient
|
|
|
|
global mqttClient
|
|
|
|
on_publish(mqttClient,stream1PlayLoad, stream2PlayLoad)
|
|
|
|
on_publish(mqttClient, msg)
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# The Main method of the server script
|
|
|
|
# The Main method of the server script
|
|
|
|
# This method is invoked from Agent.py on a new thread
|
|
|
|
# This method is invoked from DoorLockerAgent.py on a new thread
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
def main():
|
|
|
|
def main():
|
|
|
|
|
|
|
|
|
|
|
|
MQTT_ENDPOINT = iotUtils.MQTT_EP.split(":")
|
|
|
|
MQTT_ENDPOINT = iotUtils.MQTT_EP.split(":")
|
|
|
|
MQTT_IP = MQTT_ENDPOINT[1].replace('//','')
|
|
|
|
MQTT_IP = MQTT_ENDPOINT[1].replace('//', '')
|
|
|
|
MQTT_PORT = int(MQTT_ENDPOINT[2])
|
|
|
|
MQTT_PORT = int(MQTT_ENDPOINT[2])
|
|
|
|
|
|
|
|
|
|
|
|
DEV_OWNER = iotUtils.DEVICE_OWNER
|
|
|
|
DEV_OWNER = iotUtils.DEVICE_OWNER
|
|
|
|
DEV_ID = iotUtils.DEVICE_ID
|
|
|
|
DEV_ID = iotUtils.DEVICE_ID
|
|
|
|
DEV_TYPE =iotUtils.DEVICE_TYPE
|
|
|
|
DEV_TYPE =iotUtils.DEVICE_TYPE
|
|
|
|
TANENT_DOMAIN = iotUtils.SERVER_NAME
|
|
|
|
TANENT_DOMAIN = iotUtils.SERVER_NAME
|
|
|
|
global TOPIC_TO_SUBSCRIBE
|
|
|
|
|
|
|
|
TOPIC_TO_SUBSCRIBE = TANENT_DOMAIN + "/" + DEV_TYPE + "/" + DEV_ID + "/command"
|
|
|
|
global SUBSCRIBE_FOR_LOCKER_COMMAND
|
|
|
|
|
|
|
|
SUBSCRIBE_FOR_LOCKER_COMMAND = TANENT_DOMAIN + "/" + DEV_TYPE + "/" + DEV_ID + "/command"
|
|
|
|
|
|
|
|
global SUBSCRIBE_FOR_FAN_COMMAND
|
|
|
|
|
|
|
|
SUBSCRIBE_FOR_FAN_COMMAND = TANENT_DOMAIN + "/" + DEV_TYPE + "/" + DEV_ID + "/fan/command"
|
|
|
|
|
|
|
|
global SUBSCRIBE_FOR_BULB_COMMAND
|
|
|
|
|
|
|
|
SUBSCRIBE_FOR_BULB_COMMAND = TANENT_DOMAIN + "/" + DEV_TYPE + "/" + DEV_ID + "/bulb/command"
|
|
|
|
global TOPIC_TO_PUBLISH_STREAM1
|
|
|
|
global TOPIC_TO_PUBLISH_STREAM1
|
|
|
|
TOPIC_TO_PUBLISH_STREAM1 = TANENT_DOMAIN + "/" + DEV_TYPE + "/" + DEV_ID + "/smartLock"
|
|
|
|
TOPIC_TO_PUBLISH_STREAM1 = TANENT_DOMAIN + "/" + DEV_TYPE + "/" + DEV_ID + "/smartLock"
|
|
|
|
global TOPIC_TO_PUBLISH_STREAM2
|
|
|
|
global TOPIC_TO_PUBLISH_STREAM2
|
|
|
|
TOPIC_TO_PUBLISH_STREAM2 = TANENT_DOMAIN + "/" + DEV_TYPE + "/" + DEV_ID + "/smartFan"
|
|
|
|
TOPIC_TO_PUBLISH_STREAM2 = TANENT_DOMAIN + "/" + DEV_TYPE + "/" + DEV_ID + "/smartFan"
|
|
|
|
|
|
|
|
|
|
|
|
print ("MQTT_LISTENER: MQTT_ENDPOINT is " + str(MQTT_ENDPOINT))
|
|
|
|
print ("MQTT_LISTENER: MQTT_ENDPOINT is " + str(MQTT_ENDPOINT))
|
|
|
|
print ("MQTT_LISTENER: MQTT_TOPIC is " + TOPIC_TO_SUBSCRIBE)
|
|
|
|
print ("MQTT_LISTENER: MQTT_TOPIC is " + SUBSCRIBE_FOR_LOCKER_COMMAND)
|
|
|
|
global mqttClient
|
|
|
|
global mqttClient
|
|
|
|
mqttClient.username_pw_set(iotUtils.AUTH_TOKEN, password = "")
|
|
|
|
mqttClient.username_pw_set(iotUtils.AUTH_TOKEN, password = "")
|
|
|
|
mqttClient.on_connect = on_connect
|
|
|
|
mqttClient.on_connect = on_connect
|
|
|
@ -89,6 +150,10 @@ def main():
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
mqttClient.connect(MQTT_IP, MQTT_PORT, 60)
|
|
|
|
mqttClient.connect(MQTT_IP, MQTT_PORT, 60)
|
|
|
|
print "MQTT_LISTENER: " + time.asctime(), "Connected to MQTT Broker - %s:%s" % (MQTT_IP, MQTT_PORT)
|
|
|
|
print "MQTT_LISTENER: " + time.asctime(), "Connected to MQTT Broker - %s:%s" % (MQTT_IP, MQTT_PORT)
|
|
|
|
|
|
|
|
# Blocking call that processes network traffic, dispatches callbacks and
|
|
|
|
|
|
|
|
# handles reconnecting.
|
|
|
|
|
|
|
|
# Other loop*() functions are available that give a threaded interface and a
|
|
|
|
|
|
|
|
# manual interface.
|
|
|
|
mqttClient.loop_forever()
|
|
|
|
mqttClient.loop_forever()
|
|
|
|
|
|
|
|
|
|
|
|
except (KeyboardInterrupt, Exception) as e:
|
|
|
|
except (KeyboardInterrupt, Exception) as e:
|
|
|
@ -102,4 +167,3 @@ def main():
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
main()
|
|
|
|
|
|
|
|
|
|
|
|