|
楼主 |
发表于 2017-5-15 14:59:45
|
显示全部楼层
本帖最后由 kappa8086 于 2017-5-15 15:23 编辑
老办法,python + Flask 写一个简单的 webserver 并用 service 文件包装一下成为 systemd 服.务。- #!/bin/python
- from flask import Flask,request
- import subprocess
- app = Flask(__name__)
- ctrlscript = os.path.expanduser('~/bin/homectrl.sh')
- @app.route('/')
- def index():
- return ''
- @app.route('/entry', methods=['GET', 'POST'])
- def entry():
- subprocess.Popen([ctrlscript, request.args.get('action'),
- request.args.get('mac'), request.args.get('ip')])
- return 'ok'
- if __name__ == '__main__':
- app.run(host= '0.0.0.0', port=1024)
复制代码 执行脚本,在一个mpd的曲库列表里随机抽三首播放。这里的关键是执行条件,除了要判断手机的MAC地址,还需要有个时间限定,以及执行次数限定,这个场景一天只应该出现一次- #!/bin/bash
- action=$1
- mac=$2
- ipaddr=$3
- ts_file=/var/tmp/homectrl.ts
- tm_start=1800
- tm_end=2200
- music_list_file=~/welcome-music.list
- function mpcfadein {
- #mpd音量渐入过程,进门被突然出现的高音吓一跳绝对不是一种好的体验
- ((steps=($2-$1)/5))
- ((interval=$3/$steps))
- c=0
- while [ $c -lt $steps ]; do
- ((cvol=$1+5*$c))
- ((c=$c+1))
- mpc volume $cvol
- usleep $interval
- done
- mpc volume $2
- }
- if [ "$mac" == "01:23:45:67:89:ab" ] && [ "$action" == "old" ]; then #除第一次注册时是add,之后都是old...
- dt=`date +%Y%m%d`
- ts=`date +%H%M`
- saved_dt=""
- [ -f $ts_file ] && saved_dt=`cat $ts_file`
- if [ "$dt" != "$saved_dt" ] && [ $ts -gt $tm_start ] && [ $ts -lt $tm_end ]; then
- randomsongs=`sort -R $music_list_file | head -n 3`
- mpc clear
- IFS=
- \n'
- for song in $randomsongs; do
- mpc add "$song"
- mpc volume 10
- mpc play
- mpcfadein 10 60 2000000
- done
- echo $dt > $ts_file
- fi
- fi
复制代码
|
|