269 lines
No EOL
7.9 KiB
Python
269 lines
No EOL
7.9 KiB
Python
from microdot import Microdot, send_file
|
|
import machine, sys, neopixel, time
|
|
import asyncio
|
|
|
|
app = Microdot()
|
|
|
|
# Difine NeoPixel object
|
|
nbPixels = 12*2
|
|
pinPixelStrip = 16 # ESP32 D1 Mini
|
|
neoPixelStrip = neopixel.NeoPixel(machine.Pin(pinPixelStrip), nbPixels)
|
|
|
|
# Define BusyLight status
|
|
statusDef = {
|
|
'off': {'name': 'off', 'code': 0, 'color': (0, 0, 0)}, # OFF
|
|
'on': {'name': 'on', 'code': 1, 'color': (255, 255, 255)}, # White
|
|
'busy': {'name': 'busy', 'code': 2, 'color': (255, 0, 0)}, # Red
|
|
'available': {'name': 'available', 'code': 3, 'color': (0, 255, 0)}, # Green
|
|
'away': {'name': 'away', 'code': 4, 'color': (246, 190, 0)}, # Yellow
|
|
'blinking': {'name': 'blinking', 'code': 5}, # No defined color
|
|
'colored': {'name': 'colored', 'code': 6} # No defined color
|
|
}
|
|
|
|
# Store BusyLight default global status
|
|
blColor = statusDef.get('off').get('color')
|
|
blPreviousColor = statusDef.get('off').get('color')
|
|
blStatus = statusDef.get('off')
|
|
blPreviousStatus = statusDef.get('off')
|
|
blBrightness = 0.1 # Adjust the brightness (0.0 - 1.0)
|
|
blBlinkingTask = None
|
|
|
|
def __setDimmedColor(color):
|
|
r, g , b = color
|
|
r = int(r * blBrightness)
|
|
g = int(g * blBrightness)
|
|
b = int(b * blBrightness)
|
|
return (r, g, b)
|
|
|
|
def __setBusyLightColorAndBrigthness(color, brightness):
|
|
global blBrightness
|
|
blBrightness = brightness
|
|
|
|
global blColor
|
|
blColor = color
|
|
dimmedColor = __setDimmedColor(color)
|
|
|
|
neoPixelStrip.fill(dimmedColor)
|
|
neoPixelStrip.write()
|
|
|
|
def __setBusyLightColored(color, brightness):
|
|
__setBusyLightColorAndBrigthness(color, brightness)
|
|
global blStatus
|
|
blStatus = statusDef.get('colored')
|
|
|
|
def __setBusyLightStatus(status = statusDef.get('off')):
|
|
if status == statusDef.get('colored'):
|
|
lColor = blColor
|
|
else:
|
|
lColor = status.get('color')
|
|
__setBusyLightColorAndBrigthness(lColor, blBrightness)
|
|
|
|
global blStatus
|
|
blStatus = status
|
|
|
|
def __setBusyLightBlinking(time_ms=500):
|
|
return 0
|
|
|
|
def __setBusyLightStill():
|
|
return 0
|
|
|
|
|
|
|
|
# Microdot APP routes
|
|
|
|
@app.get('/static/<path:path>')
|
|
async def staticRoutes(request, path):
|
|
if '..' in path:
|
|
# directory traversal is not allowed
|
|
return {'error': '4040 Not found'}, 404
|
|
return send_file('static/' + path)
|
|
|
|
@app.get('/')
|
|
async def getIndex(request):
|
|
return send_file('static/index.html')
|
|
|
|
@app.get('/api/brightness')
|
|
async def getBrightness(request):
|
|
return {'brightness': blBrightness}
|
|
|
|
@app.post('/api/brightness')
|
|
async def setBrightness(request):
|
|
brightness = request.json.get("brightness")
|
|
|
|
if brightness is None:
|
|
return {'error': 'missing brightness parameter'}, 400
|
|
|
|
if type(brightness) is float \
|
|
or type(brightness) is int:
|
|
if brightness < 0 or brightness > 1:
|
|
return {'error': 'brigthness out of bound (0.0 - 1.0)'}, 400
|
|
else:
|
|
return {'error': 'wrong brigthness type (float)'}, 400
|
|
|
|
# Apply new brightness to current color
|
|
__setBusyLightColorAndBrigthness(blColor, brightness)
|
|
|
|
global blBrightness
|
|
blBrightness = brightness
|
|
|
|
return {'brightness': blBrightness}
|
|
|
|
|
|
@app.post('/api/color')
|
|
async def setColor(request):
|
|
|
|
r = request.json.get("r")
|
|
g = request.json.get("g")
|
|
b = request.json.get("b")
|
|
|
|
if bool(r is None or g is None or b is None):
|
|
return {'error': 'missing color'}, 400
|
|
else:
|
|
if type(r) is int \
|
|
and type(g) is int \
|
|
and type(b) is int:
|
|
color = (r, g, b)
|
|
else:
|
|
return {'error': 'wrong color type (int)'}, 400
|
|
|
|
if (r < 0 or r > 255) \
|
|
or (g < 0 or g > 255) \
|
|
or (b < 0 or b > 255):
|
|
return {'error': 'color out of bound (0 - 255)'}, 400
|
|
|
|
brightness = request.json.get("brightness")
|
|
|
|
if not bool(brightness is None):
|
|
if type(brightness) is float \
|
|
or type(brightness) is int:
|
|
if brightness < 0 or brightness > 1:
|
|
return {'error': 'brightness out of bound (0.0 - 1.0)'}, 400
|
|
else:
|
|
return {'error': 'wrong brightness type (float)'}, 400
|
|
__setBusyLightColored(color, brightness)
|
|
|
|
__setBusyLightColored(color, blBrightness)
|
|
|
|
return {'status': blStatus}
|
|
|
|
@app.post('/api/blink')
|
|
async def setBlink(request):
|
|
time_ms = request.json.get("time_ms")
|
|
blinking = request.json.get("blinking")
|
|
|
|
if not bool(time_ms is None):
|
|
if type(time_ms) is int:
|
|
if time_ms < 100 or time_ms > 5000:
|
|
return {'error': 'parameter "time_ms" out of bound (100 - 5000)'}, 400
|
|
else:
|
|
return {'error': 'wrong parameter "time_ms" type (int)'}, 400
|
|
|
|
if bool(blinking is None):
|
|
return {'error': 'missing blinking parameter'}, 400
|
|
else:
|
|
if type(blinking) is bool:
|
|
if blinking:
|
|
if time_ms is None:
|
|
__setBusyLightBlinking()
|
|
else:
|
|
__setBusyLightBlinking(time_ms)
|
|
else:
|
|
__setBusyLightStill()
|
|
else:
|
|
return {'error': 'wrong blinking type (bool)'}, 400
|
|
|
|
return {'status': 'to be implemented'}
|
|
|
|
|
|
@app.route('/api/status/<status>', methods=['GET', 'POST'])
|
|
async def setStatus(request, status):
|
|
lStatus = status.lower()
|
|
if lStatus == 'on':
|
|
status = statusDef.get('on')
|
|
__setBusyLightStatus(status)
|
|
|
|
elif lStatus == 'off':
|
|
status = statusDef.get('off')
|
|
__setBusyLightStatus(status)
|
|
|
|
elif lStatus == 'available':
|
|
status = statusDef.get('available')
|
|
__setBusyLightStatus(status)
|
|
|
|
elif lStatus == 'away':
|
|
status = statusDef.get('away')
|
|
__setBusyLightStatus(status)
|
|
|
|
elif lStatus == 'busy':
|
|
status = statusDef.get('busy')
|
|
__setBusyLightStatus(status)
|
|
|
|
else:
|
|
return {'error': 'unknown /api/status/' + lStatus + ' route'}, 404
|
|
|
|
return {'status': blStatus}
|
|
|
|
@app.get('/api/color')
|
|
async def getColor(request):
|
|
r, g, b = neoPixelStrip.__getitem__(0)
|
|
return {'color': {'r': r, 'g': g, 'b': b}}
|
|
|
|
@app.get('/api/status')
|
|
async def getStatus(request):
|
|
return {'status': blStatus}
|
|
|
|
@app.get('/api/debug')
|
|
async def getDebugInfo(request):
|
|
r, g, b = blColor
|
|
dr, dg, db = neoPixelStrip.__getitem__(0)
|
|
return {'status': blStatus, 'brightness': blBrightness, 'color': {'r': r, 'g': g, 'b': b}, 'dimColor': {'r': dr, 'g': dg, 'b': db}}
|
|
|
|
@app.post('/api/mutedeck-webhook')
|
|
async def mutedeckWebhook(request):
|
|
|
|
if request.json.get('control') != 'system':
|
|
if request.json.get('call') == 'active':
|
|
if request.json.get('mute') == 'active':
|
|
isMuted = True
|
|
else:
|
|
isMuted = False
|
|
|
|
if request.json.get('video') == 'active':
|
|
isVideoOn = True
|
|
else:
|
|
isVideoOn = False
|
|
|
|
if isMuted:
|
|
__setBusyLightStatus(statusDef.get('away'))
|
|
else:
|
|
__setBusyLightStatus(statusDef.get('busy'))
|
|
else:
|
|
__setBusyLightStatus(statusDef.get('available'))
|
|
|
|
return {'status': blStatus}
|
|
|
|
|
|
@app.post('/shutdown')
|
|
async def shutdown(request):
|
|
request.app.shutdown()
|
|
return 'The server is shutting down...'
|
|
|
|
# Startup effect
|
|
def startUpSeq():
|
|
print('Start seq is started')
|
|
__setBusyLightStatus(statusDef.get('off'))
|
|
time.sleep_ms(100)
|
|
__setBusyLightStatus(statusDef.get('busy'))
|
|
time.sleep_ms(200)
|
|
__setBusyLightStatus(statusDef.get('away'))
|
|
time.sleep_ms(300)
|
|
__setBusyLightStatus(statusDef.get('available'))
|
|
time.sleep_ms(500)
|
|
__setBusyLightStatus(statusDef.get('off'))
|
|
print('Start seq is ended')
|
|
|
|
startUpSeq()
|
|
|
|
# Start API webserver
|
|
if __name__ == '__main__':
|
|
app.run(port=80, debug=True) |