parent
60e942431c
commit
99d1876dec
98
main.py
98
main.py
|
@ -1,16 +1,13 @@
|
||||||
from base64 import b16decode
|
from base64 import b16decode
|
||||||
import time
|
import time
|
||||||
import os
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from requests import request
|
from requests import request
|
||||||
try:
|
try:
|
||||||
from rpi_ws281x import *
|
from rpi_ws281x import *
|
||||||
import RPi.GPIO as GPIO
|
import RPi.GPIO as GPIO
|
||||||
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify
|
|
||||||
import json
|
import json
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
import threading
|
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
except ImportError:
|
except ImportError:
|
||||||
print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"')
|
print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"')
|
||||||
|
@ -66,99 +63,6 @@ def mqtt_on_connect(client, userdata, flags, rc):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app = Flask(__name__)
|
|
||||||
|
|
||||||
#api
|
|
||||||
@app.route('/api/')
|
|
||||||
def index():
|
|
||||||
return '''
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>Server Rack LED stripe</title>
|
|
||||||
</head>
|
|
||||||
<style>
|
|
||||||
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
|
|
||||||
body {
|
|
||||||
font-family: 'Roboto', sans-serif;
|
|
||||||
background-color: #1E1E1E;
|
|
||||||
color: #fff;
|
|
||||||
}
|
|
||||||
|
|
||||||
code {
|
|
||||||
background-color: #333333;
|
|
||||||
border: 5px solid #333333;
|
|
||||||
border-radius: 10px;
|
|
||||||
|
|
||||||
.head-text {
|
|
||||||
font-size: 25px;
|
|
||||||
#put text in middle of page
|
|
||||||
margin-left: auto;
|
|
||||||
margin-right: auto;
|
|
||||||
left: 50%;
|
|
||||||
text-align: center;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
<body>
|
|
||||||
<p class="head-text">My Server Rack LED stripe control API</p>
|
|
||||||
<br>
|
|
||||||
<p>How to use the API:</p>
|
|
||||||
<code>/power?power=True</code>
|
|
||||||
<br>
|
|
||||||
<code>/power?power=False</code>
|
|
||||||
<br>
|
|
||||||
<code>/mode?mode=0</code>
|
|
||||||
<br>
|
|
||||||
<code>/mode?mode=1&r=0&g=0&b=0</code>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
'''
|
|
||||||
|
|
||||||
@app.route('/api/mode', methods=['GET'])
|
|
||||||
def mode():
|
|
||||||
global mode
|
|
||||||
global r
|
|
||||||
global g
|
|
||||||
global b
|
|
||||||
args = request.args
|
|
||||||
mode = str(args.get('mode'))
|
|
||||||
r = str(args.get('r'))
|
|
||||||
g = str(args.get('g'))
|
|
||||||
b = str(args.get('b'))
|
|
||||||
if mode is None:
|
|
||||||
return 'No mode specified'
|
|
||||||
elif mode == "":
|
|
||||||
return 'No mode specified'
|
|
||||||
elif mode == "0":
|
|
||||||
mode = 0
|
|
||||||
return 'Rainbow Cycle'
|
|
||||||
elif mode == "1":
|
|
||||||
if r is None or g is None or b is None:
|
|
||||||
return 'No color specified'
|
|
||||||
elif r == "" or g == "" or b == "":
|
|
||||||
return 'No color specified'
|
|
||||||
else:
|
|
||||||
mode = 1
|
|
||||||
return 'Color: '+str(r)+','+str(g)+','+str(b)
|
|
||||||
|
|
||||||
@app.route('/api/power', methods=['GET'])
|
|
||||||
def power():
|
|
||||||
global power
|
|
||||||
power = request.args.get('power')
|
|
||||||
if power is None:
|
|
||||||
return 'No power argument specified'
|
|
||||||
elif power == "":
|
|
||||||
return 'No power argument specified'
|
|
||||||
elif power == "True":
|
|
||||||
power = "True"
|
|
||||||
return 'Power: ON'
|
|
||||||
elif power == "False":
|
|
||||||
power = "False"
|
|
||||||
return 'Power: OFF'
|
|
||||||
|
|
||||||
def mqtt_on_message(client, userdata, msg):
|
def mqtt_on_message(client, userdata, msg):
|
||||||
print("topic: " ,str(msg.topic))
|
print("topic: " ,str(msg.topic))
|
||||||
print("payload: " ,str(msg.payload.decode("utf-8")))
|
print("payload: " ,str(msg.payload.decode("utf-8")))
|
||||||
|
@ -191,8 +95,6 @@ if __name__ == '__main__':
|
||||||
mode = 0
|
mode = 0
|
||||||
power = "True"
|
power = "True"
|
||||||
|
|
||||||
threading.Thread(target=lambda: app.run(host='0.0.0.0', port=80)).start()
|
|
||||||
|
|
||||||
mqttBroker ="homeassistant.ping-mee.local"
|
mqttBroker ="homeassistant.ping-mee.local"
|
||||||
client = mqtt.Client("server_rack_led")
|
client = mqtt.Client("server_rack_led")
|
||||||
client.username_pw_set("mqtt", "pmMQTT_11!")
|
client.username_pw_set("mqtt", "pmMQTT_11!")
|
||||||
|
|
Loading…
Reference in New Issue