Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish "card swiped" event via MQTT #1496

Merged
merged 7 commits into from
Feb 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions components/smart-home-automation/MQTT-protocol/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,16 @@ Phoniebox' MQTT client will do the following things:
2. at shutdown send state info to
- `phoniebox/state` (offline)
3. periodically send *all* attributes to `phoniebox/attribute/$attributeName` (this interval can be defined through `refreshIntervalPlaying` and `refreshIntervalIdle` in the `SETTINGS` section)
4. listen for attribute requests on `phoniebox/get/$attribute`
5. listen for commands on `phoniebox/cmd/$command` (if a command needs a parameter it has to be provided via payload)
4. send specific events to `phoniebox/event/$eventName` right away
5. listen for attribute requests on `phoniebox/get/$attribute`
6. listen for commands on `phoniebox/cmd/$command` (if a command needs a parameter it has to be provided via payload)

## Topic: phoniebox/event/$event
This is a read-only topic. The following events trigger immediate messages through this topic:

- card_swiped

Use these topics to get notified of time-critical events right away rather than having to wait for the periodic send of all attributes or requesting an attribute through the get topic. Currently the only event triggering a message is the "card_swiped" event with the obvious use-case of letting your smart home react upon swiped cards. These cards needn't be configured in Phoniebox as the MQTT daemon will just relay any swiped card to the MQTT server. So it's possible to have a "dim lights" card that will not trigger any Phoniebox action but is picked up by your smart home to dim the lights.

## Topic: phoniebox/get/$attribute
MQTT clients can (additionally to the periodic updates) request an attribute of Phoniebox. Sending an empty payload to `phoniebox/get/volume` will trigger Phoniebox' MQTT client to fetch the current volume from MPD and send the result to `phoniebox/attribute/volume`.
Expand Down Expand Up @@ -114,7 +122,7 @@ Sending empty payload to `phoniebox/cmd/help` will be responded by a list of all
Install missing python packages for MQTT:

~~~
sudo pip3 install paho-mqtt
sudo python3 -m pip install --upgrade --force-reinstall -q -r requirements.txt
~~~

All relevant files can be found in the folder:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import paho.mqtt.client as mqtt
import os, subprocess, re, ssl, time, datetime

import inotify.adapters
from threading import *

# ----------------------------------------------------------
# Prerequisites
# ----------------------------------------------------------
# pip3 install paho-mqtt
# pip3 install paho-mqtt inotify


# ----------------------------------------------------------
Expand Down Expand Up @@ -43,6 +44,29 @@
arAvailableAttributes = ['volume', 'mute', 'repeat', 'random', 'state', 'file', 'artist', 'albumartist', 'title', 'album', 'track', 'elapsed', 'duration', 'trackdate', 'last_card', 'maxvolume', 'volstep', 'idletime', 'rfid', 'gpio', 'remaining_stopafter', 'remaining_shutdownafter', 'remaining_idle', 'throttling', 'temperature']


def watchForNewCard():
i = inotify.adapters.Inotify()
i.add_watch(path + "/../settings/Latest_RFID")

# wait for inotify events
for event in i.event_gen(yield_nones=False):
if event is not None:
# fetch event attributes
(e_header, e_type_names, e_path, e_filename) = event

# file was closed and written => a new card was swiped
if "IN_CLOSE_WRITE" in e_type_names:
# fetch card ID
cardid = readfile(path + "/../settings/Latest_RFID")

# publish event "card_swiped"
client.publish(mqttBaseTopic + "/event/card_swiped", payload=cardid)
print(" --> Publishing event card_swiped = " + cardid)

# process all attributes
processGet("all")


def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connection established.")
Expand Down Expand Up @@ -386,6 +410,11 @@ def fetchData():
print("Subscribing to " + mqttBaseTopic + "/get/#")
client.subscribe(mqttBaseTopic + "/get/#")

# register thread for watchForNewCard
tWatchForNewCard = Thread(target=watchForNewCard)
tWatchForNewCard.setDaemon(True)
tWatchForNewCard.start()

# start endless loop
client.loop_start()
while True:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# MQTT daemon related requirements
# you need to install these with `sudo python3 -m pip install --upgrade --force-reinstall -q -r requirements.txt`

paho-mqtt
inotify