Skip to content
Permalink
8dcd9c7af4
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
51 lines (43 sloc) 1.46 KB
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."