66 lines
1.9 KiB
Python
Executable File
66 lines
1.9 KiB
Python
Executable File
#!/usr/bin/python3.8
|
|
|
|
import paho.mqtt.client as mqtt #import the client1
|
|
from mqtt_print import get_message as M
|
|
from datetime import datetime
|
|
import time
|
|
import signal
|
|
import json
|
|
import requests
|
|
import logging
|
|
from daemonize import Daemonize
|
|
|
|
|
|
broker_address="172.16.1.254"
|
|
topic="heme/bell/ring"
|
|
scroller_topic="home/scroller"
|
|
user="bot"
|
|
password="push1234"
|
|
url="https://nuc.bh.ttx.sk/ocs/v2.php/apps/spreed/api/v1/chat/dahgo5em"
|
|
|
|
pid = "/tmp/bell.pid"
|
|
logger = logging.getLogger(__name__)
|
|
logger.setLevel(logging.DEBUG)
|
|
logger.propagate = False
|
|
fh = logging.FileHandler("/tmp/bell.log", "w")
|
|
fh.setLevel(logging.DEBUG)
|
|
logger.addHandler(fh)
|
|
keep_fds = [fh.stream.fileno()]
|
|
|
|
def signal_handler(signal_number, frame):
|
|
logger.debug("Proceed ...")
|
|
|
|
def on_message(client, userdata, message):
|
|
logger.debug("message received %s" ,str(message.payload.decode("utf-8")))
|
|
logger.debug("message topic=%s",message.topic)
|
|
logger.debug("message qos=%s",message.qos)
|
|
logger.debug("message retain flag=%s",message.retain)
|
|
|
|
d = {}
|
|
strdate = datetime.now().ctime()
|
|
d["message"] = "Bell ringing in time: " + strdate
|
|
headers = {'OCS-APIRequest': 'true'}
|
|
|
|
r = requests.post(url, headers=headers,json=d,auth=(user,password))
|
|
client.publish(scroller_topic,M("ZVONI",color=0));
|
|
|
|
def main_program():
|
|
logger.debug("Start")
|
|
logger.debug("creating new instance")
|
|
client = mqtt.Client("NUC-client-talk") #create new instance
|
|
logger.debug("connecting to broker")
|
|
client.connect(broker_address) #connect to broker
|
|
logger.debug("Subscribing to topic",topic)
|
|
client.subscribe(topic)
|
|
client.on_message=on_message
|
|
|
|
client.loop_start()
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
signal.pause()
|
|
client.loop_stop()
|
|
|
|
daemon = Daemonize(app="bell_talk_app", pid=pid, action=main_program, keep_fds=keep_fds)
|
|
daemon.start()
|
|
#broker_address="iot.eclipse.org"
|
|
|