Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
smarthotels/web/peripherals.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
51 lines (43 sloc)
1.46 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from lights.smartbulb import SmartBulb | |
import postgresLibrary as pL | |
import requests | |
from flask.json import jsonify | |
from flask import request, Response | |
def handlePeripheral(peripheral, body, routes): | |
print(peripheral, body, routes) | |
if len(routes) == 0: | |
return "No Route to Host." | |
elif peripheral['type'] == 'thermostat': | |
dest = routes[0] | |
url = 'http://' + dest['target'] + '/api/set-temp' | |
print(url) | |
power = "false" | |
if body['power']: | |
power = "true" | |
pL.updatePeripheralStateAndPower(int(body['state']), body['power'], peripheral['uuid']) | |
resp = requests.request( | |
method='POST', | |
url=url, | |
headers= { | |
'Content-Type': 'application/json' | |
}, | |
data='{ "state": ' + str(int(body['state'])) + ', "power": ' + power + ' }' , | |
cookies={}, | |
timeout=0.5, | |
allow_redirects=False | |
) | |
response = Response(resp.content, resp.status_code, {}) | |
return response | |
elif peripheral['type'] == 'lightDimmable': | |
bulb = SmartBulb(routes[0]['target']) | |
power = body['power'] | |
brightness = int(body['state']) | |
if power: | |
bulb.state('ON') | |
bulb.brightness(brightness) | |
else: | |
bulb.state('OFF') | |
pL.updatePeripheralStateAndPower(brightness, power, peripheral['uuid']) | |
return "OK" | |
else: | |
return "Unknown Type." |