80 lines
3.0 KiB
Python
80 lines
3.0 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
#
|
|
# signal-talker-server.py
|
|
#
|
|
# Copyright 2022 matt <matt@tux>
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, write to the Free Software
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
# MA 02110-1301, USA.
|
|
#
|
|
#
|
|
from pydbus import SessionBus
|
|
from gi.repository import GLib
|
|
from zeroconf import ServiceBrowser, ServiceListener, Zeroconf
|
|
import paho.mqtt.client as mqtt
|
|
import time
|
|
|
|
mqttBroker ="127.0.0.1"
|
|
client = mqtt.Client("homeServer")
|
|
|
|
def on_message(client, userdata, message):
|
|
print("received message: " ,str(message.payload.decode("utf-8")))
|
|
signal.sendMessage(str(message.payload.decode("utf-8")), [], ['+16027106778'])
|
|
doorState = message
|
|
|
|
def on_signal_message(timestamp, source, groupID, message, attachments):
|
|
print("received signal message: ", message)
|
|
client.publish("esp32/switch", message)
|
|
signal.sendMessage('Turned light {}'.format(message), [], ['+16027106778'])
|
|
|
|
class MyListener(ServiceListener):
|
|
|
|
def update_service(self, zc: Zeroconf, type_: str, name: str) -> None:
|
|
print(f"Service {name} updated")
|
|
|
|
def remove_service(self, zc: Zeroconf, type_: str, name: str) -> None:
|
|
print(f"Service {name} removed")
|
|
|
|
def add_service(self, zc: Zeroconf, type_: str, name: str) -> None:
|
|
info = zc.get_service_info(type_, name)
|
|
print(f"Service {name} added, service info: {info}")
|
|
|
|
bus = SessionBus()
|
|
#signal = bus.get('org.asamk.Signal')
|
|
#signal.onMessageReceived = on_signal_message
|
|
#signal.sendMessage('Server online', [], ['+16027106778'])
|
|
zeroconf = Zeroconf()
|
|
listener = MyListener()
|
|
browser = ServiceBrowser(zeroconf, "_http._tcp.local.", listener)
|
|
|
|
if __name__ == "__main__":
|
|
doorState = "The door is closed."
|
|
while True :
|
|
client.connect(mqttBroker, 1883, 60)
|
|
client.loop_start()
|
|
client.subscribe("esp32/door")
|
|
client.on_message=on_message
|
|
|
|
loop = GLib.MainLoop()
|
|
signal = bus.get('org.asamk.Signal', object_path='/org/asamk/Signal')
|
|
|
|
def on_signal_message(timestamp, source, groupID, message, attachments):
|
|
print("received signal message: ", message)
|
|
client.publish("esp32/switch", message.lower())
|
|
signal.sendMessage('Turned light {}'.format(message.lower()), [], ['+16027106778'])
|
|
|
|
signal.onMessageReceived = on_signal_message
|
|
loop.run() |