-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
120 lines (91 loc) · 3 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from flask import *
import json
import eiscp
import sys
import sqlite3
import paho.mqtt.client as mqtt
import requests
app = Flask(__name__)
@app.route('/', methods=['GET'])
def home_splash():
# volume = int(raw_send_command('MVLQSTN')[3:], 16) # causes hang if AV isn't fully on :(
# print(volume)
return render_template('index.html')
@app.route('/tv-control', methods=['GET', 'POST'])
def tv_splash():
return render_template('tv-control.html')
# AUDIO
receiver_ip = '192.168.2.3'
@app.route('/sound-control', methods=['GET'])
def sound_splash():
return render_template('onkyo-audio.html')
@app.route("/onkyo/<cmd>", methods=['GET'])
def send_command(cmd):
if cmd == "PWR":
pwr = raw_send_command("PWRQSTN")
cmd += "0" + str(
abs(int(pwr[-1]) - 1) # 1 -> 0; 0 -> 1
)
response = raw_send_command(cmd)
if cmd.startswith("MVL"):
volume = int(response[3:], 16)
print(volume)
else:
print(response)
return("")
def code_send_command(command):
with eiscp.eISCP(receiver_ip) as r:
return r.command(command)
def raw_send_command(command):
with eiscp.eISCP(receiver_ip) as r:
try:
return r.raw(command)
except:
return str("failed")
# TELEVISION
# excellent Roku documentation can be found at: https://sdkdocs.roku.com/display/sdkdoc/External+Control+API
TV_ip = "192.168.2.5"
@app.route("/roku/<path:command>", methods=['GET'])
def send_roku(command):
adr = 'http://{}:8060/{}'.format(TV_ip, command) # TODO: change to f strings when raspbian updates python3
if command.startswith('keypress') | command.startswith('launch'):
requests.post(adr)
elif command.startswith('$~'):
[requests.post("http://{}:8060/keypress/Lit_{}".format(TV_ip, i)) for i in command[2:]]
else:
r = requests.get(adr)
print(r.text)
return("")
# VLC
computer_ip = "192.168.2.2"
@app.route("/vlc-control", methods=["GET"])
def vlc_control():
return render_template('vlc-control.html')
@app.route("/vlc/<cmd>", methods=['GET'])
def send_vlc(cmd):
r = requests.get('http://{}:8080/requests/status.xml?command={}'.format(computer_ip, cmd), auth=('', 'password'))
print(r.text)
return("")
# MQTT FOR MCS
@app.route("/mqtt/<path:msg>", methods=['GET'])
def send_mqtt(msg):
msg = json.loads(msg)
client.publish(msg['topic'], str(msg['cmd']))
return(str(msg))
def on_connect(client, userdata, flags, rc):
print("Connected with code: {}".format(str(rc)))
# sub_topics = ["lights/bookcase"]
# [client.subscribe(topic) for topic in sub_topics]
def on_message(client, userdata, msg):
print("message received")
print(str(msg.payload))
def mqtt_init():
client = mqtt.Client()
client.connect("localhost", 1883, 60)
client.on_connect = on_connect
client.on_message = on_message
client.loop_start()
return(client)
if __name__ == "__main__":
client = mqtt_init()
app.run(debug=True, host='0.0.0.0', threaded=True)