initial check-in

This commit is contained in:
2023-08-28 17:29:39 +02:00
commit c8a835cdae
6 changed files with 190 additions and 0 deletions

13
Dockerfile Normal file
View File

@@ -0,0 +1,13 @@
#$ docker build -t bsh-gateway .
#$ docker run -it --rm --name my-bsh-gateway bsh-gateway
FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python","-u","./Mqtt2ThingBoardGateWay.py"]

79
Mqtt2ThingBoardGateWay.py Normal file
View File

@@ -0,0 +1,79 @@
#!/usr/bin/python
#Bosch Home Automation <> MQTT Gateway
import requests, json, time
import paho.mqtt.client as mqtt
class Mqtt2ThingBoard:
#Mosquitto Settings
client_name = "Mqtt2ThingBoardGateWay"
host_name = "192.168.178.36"
host_port = 1884
mqtt_client = mqtt.Client(client_name)
tb_gateway_user = "K9iW4T5E7tVeP4ZyapxB"
tb_port = 1883
tb_client = mqtt.Client(tb_gateway_user)
#Sent MQTT Debugging Message
def publishDebugMsg(self,msg):
self.mqtt_client.publish("Mqtt2ThingBoardGateWay/debug",msg,0,True)
print(msg)
#Sent message to MQTT telemetry
def publishTelemetryMsg(self,msg):
self.tb_client.publish("v1/gateway/telemetry",msg,0,True)
#react to MQTT scenario requests:
def onMqttMessage(self, client, userdata, message):
#currently only react on trigger="ON"
if (str(message.payload.decode("utf-8")) == "ON"):
self.mqtt_client.publish(message.topic,"OFF", 0,True)
for s in self.Scenarios:
topic = "bsh/scenarios/"+s["name"].replace(" ","_")+"/trigger"
if (topic == message.topic):
self.publishDebugMsg(" Scenario triggered: "+s["name"])
url = self.baseurl + "/smarthome/scenarios/"+s["id"]+"/triggers"
postRequest = requests.post(url, data="", headers = self.getURLHeaders(),
verify = False, cert = self.getCertificate())
data = json.loads(postRequest.text)[0]['result']
if "errorCode" in data:
self.publishDebugMsg("Error occured during Scenario Call:")
self.publishDebugMsg(data["errorCode"])
#subscribe to message to trigger scenarios
def subscribeToMqttInputs(self):
pass
#TODO
#set up the system: login to MQTT + get all needed data from the BSH
def __init__(self):
self.publishDebugMsg("Connecting to MQTT Server")
self.mqtt_client.connect(self.host_name,self.host_port)
self.publishDebugMsg("Connecting to ThingsBoard Mqtt Gateway")
self.tb_client.connect(self.host_name,self.tb_port)
self.publishDebugMsg("Setting up the Environment...")
self.publishDebugMsg("Subscribe to MQTT Messages")
self.subscribeToMqttInputs()
self.publishTelemetryMsg("{\"TempSensorBuero\":[{\"temperature\":11.8}]}")
def loop(self):
#Restart Long Polling every second
while True:
self.mqtt_client.loop_start()
time.sleep(1)
self.mqtt_client.loop_stop()
Mqtt2ThingBoardGateWay = Mqtt2ThingBoard()
Mqtt2ThingBoardGateWay.loop()

View File

@@ -0,0 +1,17 @@
[Unit]
Description=BoschHomeAutomation 2 MQTT Gateway
After=multi-user.target
[Service]
Type=idle
Restart=always
RestartSec=30
StartLimitInterval=200
StartLimitBurst=5
WorkingDirectory=/opt/Mqtt2ThingBoardGateWay
ExecStart=/opt/Mqtt2ThingBoardGateWay/Mqtt2ThingBoardGateWay.py
ExecStop=/bin/sleep 1
[Install]
WantedBy=multi-user.target

View File

@@ -0,0 +1,26 @@
#!/usr/bin/env bash
mac=`cat /sys/class/net/$(ip route get 8.8.8.8 | sed -n 's/.* dev \([^ ]*\).*/\1/p')/address`
arch=`uname -m`
### installing bsh gateway
echo -e "\033[36m Installing BSH Gateway.\033[0m"
if [ -d "/opt/Mqtt2ThingBoardGateWay" ]; then
systemctl stop Mqtt2ThingBoardGateWay.service
cp Mqtt2ThingBoardGateWay.py /opt/Mqtt2ThingBoardGateWay/
cp example-key.pem /opt/Mqtt2ThingBoardGateWay/
cp example-cert.pem /opt/Mqtt2ThingBoardGateWay/
else
mkdir /opt/Mqtt2ThingBoardGateWay
cp Mqtt2ThingBoardGateWay.py /opt/Mqtt2ThingBoardGateWay/
cp example-key.pem /opt/Mqtt2ThingBoardGateWay/
cp example-cert.pem /opt/Mqtt2ThingBoardGateWay/
fi
cp Mqtt2ThingBoardGateWay.service /lib/systemd/system/
chmod 644 /lib/systemd/system/Mqtt2ThingBoardGateWay.service
systemctl daemon-reload
systemctl enable Mqtt2ThingBoardGateWay.service
systemctl start Mqtt2ThingBoardGateWay.service
echo -e "\033[32m Installation completed. Mqtt2ThingBoardGateWay is now up and running.\033[0m"

View File

@@ -0,0 +1,18 @@
#!/bin/bash
cd /tmp
echo -e "\033[36m Mqtt2ThingBoardGateWay Service.\033[0m"
systemctl stop Mqtt2ThingBoardGateWay.service
### Uninstalling Mqtt2ThingBoardGateWay
echo -e "\033[36m Uninstalling Mqtt2ThingBoardGateWay.\033[0m"
rm -rf /opt/Mqtt2ThingBoardGateWay/
systemctl disable Mqtt2ThingBoardGateWay.service
rm /lib/systemd/system/Mqtt2ThingBoardGateWay.service
systemctl daemon-reload
echo -e "\033[32m Uninstall complete!\033[0m"

37
requirements.txt Normal file
View File

@@ -0,0 +1,37 @@
astroid==2.4.2
certifi==2020.6.20
chardet==3.0.4
click==7.1.2
colorama==0.4.3
future==0.18.2
idna==2.10
iso8601==0.1.12
isort==5.4.2
Jinja2==2.11.3
joblib==1.0.1
lazy-object-proxy==1.4.3
livereload==2.6.3
lunr==0.5.8
Markdown==3.3.4
MarkupSafe==1.1.1
mccabe==0.6.1
mkdocs==1.1.2
mkdocs-material==7.0.4
mkdocs-material-extensions==1.0.1
nltk==3.5
paho-mqtt==1.5.0
Pygments==2.8.1
pylint==2.6.0
pymdown-extensions==8.1.1
pyserial==3.4
PyYAML==5.3.1
regex==2020.11.13
requests==2.24.0
rope==0.17.0
serial==0.0.97
six==1.15.0
toml==0.10.1
tornado==6.1
tqdm==4.59.0
urllib3==1.25.10
wrapt==1.12.1