Skip to content
Open
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
47 changes: 45 additions & 2 deletions MFRC522-trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
from os import path
import urllib.request
import urllib.parse
import subprocess
import time
import json
Expand All @@ -18,12 +19,25 @@
pathname = path.dirname(path.abspath(__file__))
logging.config.fileConfig(pathname + '/logging.ini')
config = json.load(open(pathname + '/config.json', encoding="utf-8"))
baseurl = "http://localhost:3000/api/v1/commands/"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This closely ties MFRC522-trigger to a particular application. Something I'd like to avoid.



"""
Mayba need to implement a web radio play command

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, this can also be achieved by putting the web radio into a playlist in Volumio.

curl -i --header "Content-Type: application/json" localhost:3000/api/v1/replaceAndPlay --data '{"service": "webradio", "type": "webradio", "title": "Bayern3", "uri": "http://opml.radiotime.com/Tune.ashx?id=s14991"}'

"""

def execute_curl(url):
logging.info("Gonna curl '" + url + "'")
try:
urllib.request.urlopen(url)
html = urllib.request.urlopen(url)
html_read = html.read()
j = json.loads(html_read.decode("utf-8"))
if j["Error"]:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Evaluating the http response is a good idea. But looking for certain properties in the JSON response is a matter of the API contract of the application we're trying to control here. Error and response might be properties that exist in the JSON response of Volumio, but they will certainly not work for other applications.

logging.error(f'{j["Error"]}')
elif j["response"]:
logging.info(f'{j["response"]}')
except Exception:
logging.error("Unable to open url " + url, sys.exc_info()[0])

Expand All @@ -33,9 +47,38 @@ def execute_command(command):
subprocess.call(command, shell=True, stdout=subprocess.DEVNULL)


def execute_playlist(playlistname):
playlistname = urllib.parse.quote(playlistname)
logging.info("Gonna execute '" + playlistname + "'")
execute_curl(f"{baseurl}?cmd=playplaylist&name={playlistname}")


def execute_play():
logging.info("Gonna execute play")
execute_curl(f"{baseurl}?cmd=play")


def execute_pause():
logging.info("Gonna execute pause")
execute_curl(f"{baseurl}?cmd=pause")


def execute_volume(volume):
try:
volume = int(volume)
except ValueError as e:
logging.error(f"given volume is not an int.")
logging.info(f"Set volume to {volume}")
execute_curl(f"{baseurl}?cmd=volume&volume={volume}")


ACTION_MAP = {
"curl": lambda action: execute_curl(action["url"]),
"command": lambda action: execute_command(action["command"])
"command": lambda action: execute_command(action["command"]),
"list": lambda action: execute_playlist(action["name"]),
"play": lambda action: execute_play(),
"pause": lambda action: execute_pause(),
"volume": lambda action: execute_volume(action["value"])
}


Expand Down
46 changes: 45 additions & 1 deletion config/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,51 @@
"title": "Command to execute when the tag is detected."
}
}
}
},
{
"type": "object",
"title": "Play Action",
"required": ["type"],
"additionalProperties": false,
"properties": {
"type": {
"type": "string",
"title": "Type of action. Must be 'play'.",
"patter": "^play$"
}
}
},
{
"type": "object",
"title": "Pause Action",
"required": ["type"],
"additionalProperties": false,
"properties": {
"type": {
"type": "string",
"title": "Type of action. Must be 'pause'.",
"patter": "^pause$"
}
}
},
{
"type": "object",
"title": "Set Volume",
"required": ["type", "value"],
"additionalProperties": false,
"properties": {
"type": {
"type": "string",
"title": "Type of action. Must be 'volume'.",
"pattern": "^volume$"
},
"value": {
"type": "string",
"title": "Must be a volume 'value' as int [0,100]."
}

}
}
]
}
}
Expand Down