-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
66 lines (49 loc) · 1.59 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import urllib3
import requests
import threading
from google.cloud import pubsub_v1
# Disable unverified SSL certificate warning
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# TODO use this: https://cloud.google.com/community/tutorials/delivering-cloud-monitoring-notifications-to-third-party-services
# TODO read from config
project_id = "craftnote-live"
subscription_name = "projects/craftnote-live/subscriptions/cloud-function-errors"
bridgeIp = "192.168.1.113"
username = "lx-2rUum1alLQS9ncdvxbAoROxKdgv7bsTRsKSl5"
lampIds = ["18","19"] # 5: living room, 6: kitchen, 18 studio links
count = 0
def set_lights(value):
min_value = 0
max_value = 5
scaling_factor = 4000
hue = max(min_value, max_value - value) * scaling_factor
for lampId in lampIds:
set_lamp_color(lampId, hue)
def set_lamp_color(lampId, hue):
url = f"https://{bridgeIp}/api/{username}/lights/{lampId}/state"
body = {"on": True, "sat": 254, "bri": 254, "hue": hue}
r = requests.put(url, json=body, verify=False)
print(r.text)
def subscribe():
subscriber = pubsub_v1.SubscriberClient()
def callback(message):
print(message.data)
global count
count = count + 1
message.ack()
future = subscriber.subscribe(subscription_name, callback)
try:
future.result()
except KeyboardInterrupt:
future.cancel()
def run():
global count
print(f"The count was {count}")
set_lights(count)
count = 0
threading.Timer(10.0, run).start()
def main():
run()
subscribe()
if __name__ == "__main__":
main()