Integrate pyezviz MQTT with doorbell
This commit is contained in:
parent
c8bc300077
commit
b20e2845b5
47
main.py
47
main.py
|
@ -1,16 +1,27 @@
|
|||
import os
|
||||
import logging
|
||||
logging.basicConfig(
|
||||
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
|
||||
level=logging.DEBUG if os.environ.get('DEBUG') else logging.INFO)
|
||||
|
||||
import time
|
||||
import secrets
|
||||
import json
|
||||
|
||||
import pygame
|
||||
from pyezviz import EzvizClient, MQTTClient
|
||||
|
||||
import secrets
|
||||
|
||||
CHIME = 'chime.ogg'
|
||||
FRONTDOOR = 'frontdoor.ogg'
|
||||
BACKDOOR = 'backdoor.ogg'
|
||||
|
||||
|
||||
def play_sound(filename):
|
||||
pygame.mixer.music.load(filename)
|
||||
pygame.mixer.music.play()
|
||||
|
||||
logging.info('Playing sound %s', filename)
|
||||
|
||||
while pygame.mixer.music.get_busy():
|
||||
pygame.time.Clock().tick(10)
|
||||
|
||||
|
@ -18,12 +29,36 @@ def backdoor():
|
|||
play_sound(CHIME)
|
||||
play_sound(BACKDOOR)
|
||||
|
||||
time.sleep(1)
|
||||
time.sleep(0.75)
|
||||
|
||||
play_sound(CHIME)
|
||||
play_sound(BACKDOOR)
|
||||
|
||||
pygame.init()
|
||||
pygame.mixer.init()
|
||||
def on_message(client, userdata, mqtt_message):
|
||||
message = json.loads(mqtt_message.payload)
|
||||
#print(json.dumps(mqtt_message, indent=4))
|
||||
|
||||
backdoor()
|
||||
if message['alert'] == 'somebody there ring the door': # lmao
|
||||
logging.info('Received door bell press alert')
|
||||
if 'E80451501' in message['ext']:
|
||||
logging.info('Backdoor pressed')
|
||||
backdoor()
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.info('')
|
||||
logging.info('==========================')
|
||||
logging.info('Booting up...')
|
||||
pygame.init()
|
||||
pygame.mixer.init()
|
||||
|
||||
client = EzvizClient(secrets.EZVIZ_EMAIL, secrets.EZVIZ_PASS, 'apiius.ezvizlife.com')
|
||||
try:
|
||||
logging.info('Logging into EZVIZ client...')
|
||||
token = client.login()
|
||||
mqtt = MQTTClient(token, on_message)
|
||||
logging.info('Starting MQTT...')
|
||||
mqtt.start()
|
||||
except Exception as exp:
|
||||
logging.exception(str(exp))
|
||||
finally:
|
||||
client.close_session()
|
||||
|
|
|
@ -1,4 +1,9 @@
|
|||
"""Ezviz cloud MQTT client for push messages."""
|
||||
import os
|
||||
import logging
|
||||
logging.basicConfig(
|
||||
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
|
||||
level=logging.DEBUG if os.environ.get('DEBUG') else logging.INFO)
|
||||
|
||||
import base64
|
||||
import json
|
||||
|
@ -23,28 +28,28 @@ APP_SECRET = "17454517-cc1c-42b3-a845-99b4a15dd3e6"
|
|||
def on_subscribe(client, userdata, mid, granted_qos):
|
||||
"""On MQTT message subscribe."""
|
||||
# pylint: disable=unused-argument
|
||||
print("Subscribed: " + str(mid) + " " + str(granted_qos))
|
||||
logging.info("Subscribed: " + str(mid) + " " + str(granted_qos))
|
||||
|
||||
|
||||
def on_connect(client, userdata, flags, return_code):
|
||||
"""On MQTT connect."""
|
||||
# pylint: disable=unused-argument
|
||||
if return_code == 0:
|
||||
print("connected OK Returned code=", return_code)
|
||||
logging.info("connected OK Returned code=%s", return_code)
|
||||
else:
|
||||
print("Bad connection Returned code=", return_code)
|
||||
logging.info("Bad connection Returned code=%s", return_code)
|
||||
client.reconnect()
|
||||
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
"""On MQTT message receive."""
|
||||
# pylint: disable=unused-argument
|
||||
mqtt_message = json.loads(msg.payload)
|
||||
mqtt_message["ext"] = mqtt_message["ext"].split(",")
|
||||
|
||||
# Print payload message
|
||||
decoded_message = {mqtt_message['ext'][2]:{'id':mqtt_message['id'], 'alert':mqtt_message['alert'], 'time':mqtt_message['ext'][1], 'alert type':mqtt_message['ext'][4], 'image':mqtt_message['ext'][16]}}
|
||||
print(decoded_message)
|
||||
#def on_message(client, userdata, msg):
|
||||
# """On MQTT message receive."""
|
||||
# # pylint: disable=unused-argument
|
||||
# mqtt_message = json.loads(msg.payload)
|
||||
# mqtt_message["ext"] = mqtt_message["ext"].split(",")
|
||||
#
|
||||
# # Print payload message
|
||||
# decoded_message = {mqtt_message['ext'][2]:{'id':mqtt_message['id'], 'alert':mqtt_message['alert'], 'time':mqtt_message['ext'][1], 'alert type':mqtt_message['ext'][4], 'image':mqtt_message['ext'][16]}}
|
||||
# print(decoded_message)
|
||||
|
||||
|
||||
class MQTTClient(threading.Thread):
|
||||
|
@ -53,6 +58,7 @@ class MQTTClient(threading.Thread):
|
|||
def __init__(
|
||||
self,
|
||||
token,
|
||||
callback,
|
||||
timeout=DEFAULT_TIMEOUT,
|
||||
):
|
||||
"""Initialize the client object."""
|
||||
|
@ -64,6 +70,7 @@ class MQTTClient(threading.Thread):
|
|||
"username": None,
|
||||
"api_url": "apiieu.ezvizlife.com",
|
||||
}
|
||||
self._callback = callback
|
||||
self._timeout = timeout
|
||||
self._stop_event = threading.Event()
|
||||
self._mqtt_data = {
|
||||
|
@ -80,7 +87,7 @@ class MQTTClient(threading.Thread):
|
|||
)
|
||||
ezviz_mqtt_client.on_connect = on_connect
|
||||
ezviz_mqtt_client.on_subscribe = on_subscribe
|
||||
ezviz_mqtt_client.on_message = on_message
|
||||
ezviz_mqtt_client.on_message = self._callback
|
||||
ezviz_mqtt_client.username_pw_set(MQTT_APP_KEY, APP_SECRET)
|
||||
|
||||
ezviz_mqtt_client.connect(self._mqtt_data["push_url"], 1882, 60)
|
||||
|
|
|
@ -5,7 +5,6 @@ numpy==1.21.2
|
|||
paho-mqtt==1.5.1
|
||||
pandas==1.3.3
|
||||
pycryptodome==3.10.4
|
||||
pyezviz==0.1.9.4
|
||||
pygame==2.0.1
|
||||
python-dateutil==2.8.2
|
||||
pytz==2021.3
|
||||
|
|
Loading…
Reference in New Issue
Block a user