2020-07-07 17:08:07 +02:00

77 lines
3.0 KiB
Python

import datetime
import time
import sys
import echo_serial.serial_controller as EchoSerial
import echo_db.database as EchoDB
import _thread
class LEDStrip:
@staticmethod
def handle_action(device, action, data):
actionData = device['actions'][action]
collection = EchoDB.EchoDB.database['Devices']
# Check if we send action too fast after last issued action
actionNow = datetime.datetime.now()
actionLast = device['last_used']
lastIssued = (actionNow - actionLast).total_seconds()
# if lastIssued < 2.0:
# return {'status': 'failed', 'message': 'Issued action too fast after last issued action!'}, 200
# Check if a thread is using the blinds, if so return.
if device['status'] == "busy":
return {'status': 'failed', 'message': 'LED Strip is being controlled by Echo, Please wait till finished.'}, 200
# Stop blinds if we are moving
if action == "off":
EchoSerial.EchoSerial.send_commmand(device, 'clear_color')
# Update Database
collection.update_one({'uuid': device['uuid']}, {
"$set": { 'last_used': datetime.datetime.now(),
"status": action
}})
return {'status': 'success', 'message': actionData['message']}, 200
if action == "on":
EchoSerial.EchoSerial.send_commmand(device, 'set_color', device['current_color'])
# Update Database
collection.update_one({'uuid': device['uuid']}, {
"$set": { 'last_used': datetime.datetime.now(),
"status": action
}})
return {'status': 'success', 'message': actionData['message']}, 200
if action == "set_color":
EchoSerial.EchoSerial.send_commmand(device, 'set_color', data)
# Update Database
collection.update_one({'uuid': device['uuid']}, {
"$set": { 'last_used': datetime.datetime.now(),
"current_color": data,
"status": "on"
}})
return {'status': 'success', 'message': actionData['message']}, 200
if action == "automatic_movement":
if data == "on":
# Update Database
now_time = datetime.datetime.now()
auto_shutdown = now_time + datetime.timedelta(minutes = 2)
collection.update_one({'uuid': device['uuid']}, {"$set": { 'last_used': datetime.datetime.now(), 'automatic_movement': True, 'automatic_shutdown': auto_shutdown }})
return {'status': 'success', 'message': actionData['message']}, 200
if data == "off":
# Update Database
collection.update_one({'uuid': device['uuid']}, {"$set": { 'last_used': datetime.datetime.now(), 'automatic_movement': False }})
return {'status': 'success', 'message': actionData['message']}, 200
return {'status': 'failed', 'message': 'Setting not found!'}, 200