Toastie 4 years ago
parent
commit
592a0823c1
3 changed files with 120 additions and 0 deletions
  1. 16 0
      rfidac/data/_action_template
  2. 44 0
      rfidac/data/_mpd_helper
  3. 60 0
      rfidac/data/rfidac.py

+ 16 - 0
rfidac/data/_action_template

@@ -0,0 +1,16 @@
+#!/usr/bin/bash
+
+source /opt/_mpd_helper
+tag=`basename "$0"`
+play_folder $tag
+
+
+# source _mpd_helper if you want to use mpd actions such as:
+# - mpd_play "<PATH/FILE within mpd library>"
+# - mpd_play_random "<PATH/FILE within mpd library>"
+# - mpd_set_volume VALUE (0 - 100)
+# - mpd_decrease_volume [VALUE] (defaults to 10)
+# - mpd_increase_volume [VALUE] (defaults to 10)
+# - $mpc = mpd client with options
+# - $music = path to mpd's music library
+

+ 44 - 0
rfidac/data/_mpd_helper

@@ -0,0 +1,44 @@
+# variables
+mpc="/usr/bin/mpc -h $mpd_host"
+
+# functions
+function play_folder {
+  tag = $1
+  mkdir -p /music/$tag
+  rm /music/$tag/.lastplay
+  touch /music/$tag/.lastplay
+  $mpc stop
+  $mpc clear
+  $mpc add $mpd_folder$tag
+  _ensure_volume_10
+  $mpc play 
+}
+
+function mpd_play {
+  $mpc stop
+  $mpc clear
+  $mpc add "$mpd_folder$1"
+  _ensure_volume_10
+  $mpc play 1
+}
+
+function mpd_play_shuffle {
+  $mpc stop
+  $mpc clear
+  $mpc add "$1"
+  $mpc shuffle
+  _ensure_volume_10
+  $mpc play 1
+}
+
+function mpd_set_volume {
+  if [ -n "$1" ]; then # non-zero length
+    $mpc volume $value
+  fi
+}
+
+function _ensure_volume_10 {
+  if (( $($mpc volume | grep -o '...%' | tr -d '%') < 10 )); then
+    mpd_set_volume 10
+  fi
+}

+ 60 - 0
rfidac/data/rfidac.py

@@ -0,0 +1,60 @@
+#! /usr/bin/python3
+
+"""
+rfid action commander deamon
+"""
+
+import os
+import subprocess
+import logging
+from shutil import copyfile
+from datetime import datetime
+from evdev import InputDevice, ecodes
+
+logging.basicConfig(level=logging.DEBUG)
+
+
+def read_rfid():
+    combined_string = ""
+    for event in InputDevice("/dev/input/event0").read_loop():
+        if event.type == ecodes.EV_KEY and event.value == 0:  # value 0 = release key
+            if event.code == 28:  # code 28 = KEY_ENTER
+                return combined_string
+            # [4:5]? .. KEY[] starts with 'KEY_' and we expect one char
+            combined_string += ecodes.KEY[event.code][4:5] 
+
+
+def run_action(script: str):
+    logging.info(f"RFID action: run {script}")
+    try:
+        r = subprocess.call(["/bin/bash", script])
+    except Exception as err:
+        logging.info(err)
+        last_rfid = None
+
+
+last_rfid = None
+last_ts = datetime.now()
+while True:
+    rfid = read_rfid()
+    seconds_gone = (datetime.now() - last_ts).total_seconds()
+
+    script = f"/opt/{rfid}"
+
+    if os.path.islink(script):  # special RFID - alsways run!
+        logging.info(f"RFID {rfid} is special. {script} is a link")
+        run_action(script)
+        continue
+
+    if not os.path.exists(script):
+        copyfile("/opt/_action_template", script)
+        logging.info(f"RFID action: empty {script} created from template")
+        continue
+
+    if rfid == last_rfid and seconds_gone <= 5:
+        logging.info(f"Same RFID after {seconds_gone:.1f} seconds. Do nothing.")
+        continue
+
+    last_ts = datetime.now()
+    last_rfid = rfid
+    run_action(script)