-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added script to add MyUplink schedule names to MQTT
- Loading branch information
1 parent
476daff
commit 0d9c3c6
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#! /bin/bash | ||
# | ||
MyUplink_HOME="/data/MyUplink" | ||
# | ||
. /home/pi/.bashrc | ||
cd $MyUplink_HOME | ||
# | ||
python schedule2mqtt.py >> $MyUplink_HOME/schedule2mqtt.errlog 2>&1 | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import os | ||
import random | ||
from datetime import datetime | ||
import json | ||
import paho.mqtt.client as mqtt | ||
|
||
from MyUplinkUtil import MyUptimeConfig, myLogger | ||
from MyUplinkConst import FILE_SCHEDULE, FILE_SCHEDULE_MODE | ||
|
||
def readSchedule(sFile): | ||
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] | ||
with open(sFile, 'r') as f: | ||
data = json.load(f) | ||
f.close() | ||
nEvents = len(data[0]['events']) | ||
tNow = datetime.now() | ||
toDay = weekdays[tNow.weekday()] | ||
hNow = tNow.hour | ||
for i in range(nEvents-1, 0,- 1): | ||
if data[0]['events'][i]['startDay'] == toDay: | ||
dHour = int(data[0]['events'][i]['startTime'].split(':')[0]) | ||
if dHour <= hNow: | ||
fMode = data[0]['events'][i]['modeId'] | ||
return fMode | ||
|
||
def readScheduleMode(smFile, fMode): | ||
with open(smFile, 'r') as f: | ||
data = json.load(f) | ||
f.close() | ||
for i in range(len(data)): | ||
if data[i]['modeId'] == fMode: | ||
fName = data[i]['name'] | ||
return fName | ||
|
||
def main(): | ||
VERSION = "0.1.00" | ||
script_name = os.path.basename(__file__).split('.')[0] | ||
lo = myLogger(script_name, VERSION) | ||
logger = lo.getLogger() | ||
|
||
scheduleFile = FILE_SCHEDULE | ||
scheduleFileMode = FILE_SCHEDULE_MODE | ||
|
||
iniConf = MyUptimeConfig(".MyUplink.ini") | ||
|
||
if iniConf.getKey('SCHEDULE', 'SCHEDULEFILE'): | ||
scheduleFile = iniConf.getKey('SCHEDULE', 'SCHEDULEFILE') | ||
if iniConf.getKey('SCHEDULE', 'SCHEDULEFILEMODE'): | ||
scheduleFileMode = iniConf.getKey('SCHEDULE', 'SCHEDULEFILEMODE') | ||
|
||
mqttUSER = iniConf.getKey('MQTT', 'USERNAME') | ||
mqttPASS = iniConf.getKey('MQTT', 'PASSWORD') | ||
mqttHOST = iniConf.getKey('MQTT', 'SERVER') | ||
mqttPORT = iniConf.getKey('MQTT', 'PORT') | ||
TOPIC = iniConf.getKey('MQTT', 'TOPICBASE') | ||
|
||
fMode = readSchedule(scheduleFile) | ||
fName = readScheduleMode(scheduleFileMode, fMode) | ||
|
||
client = mqtt.Client(f'python-mqtt-{random.randint(0, 1000)}') | ||
|
||
client.username_pw_set(mqttUSER, mqttPASS) | ||
client.connect(mqttHOST, int(mqttPORT), 600) | ||
|
||
client.loop_start() | ||
mTopic = TOPIC + "/VVB/scheduleMode" | ||
result = client.publish(mTopic, fName, 0, True) | ||
if result[0] == 0: | ||
logger.debug("Sendt %s to topic %s", fName, mTopic) | ||
else: | ||
logger.error("Failed to send message to topic %s Error code %i", mTopic, result[0]) | ||
client.loop_stop() | ||
|
||
logger.info("Finishing ...") | ||
|
||
if __name__ == "__main__": | ||
main() |