-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
59 lines (44 loc) · 1.78 KB
/
client.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
import time
import threading
import sys
import requests
import argparse
try:
from retic.typing import *
except ImportError:
# If you don't have Reticulated Python installed
from retic_dummies import *
@fields({'id':str, 'url':str})
class BrewPiClientDevice(threading.Thread):
def __init__(self, id:str, url:str):
super().__init__()
self.id = id
if url.endswith('/'):
raise Exception('URL should not include a terminating slash')
self.url = url
def run(self):
while True:
data = requests.post('{}/socketmessage.php'.format(self.url), data={'messageType': 'getTemperatures'})
data = data.json()
beertemp = data['BeerTemp']
if beertemp is not None:
beertemp = (beertemp - 32) * (5 / 9)
requests.post('{}/signal/temp'.format(iotrickster), data={'temp':beertemp, 'mac':'{}beer'.format(self.id)})
fridgetemp = data['FridgeTemp']
if fridgetemp is not None:
fridgetemp = (fridgetemp - 32) * (5 / 9)
requests.post('{}/signal/temp'.format(iotrickster), data={'temp':fridgetemp, 'mac':'{}fridge'.format(self.id)})
time.sleep(60)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Proxy between existing connected device and IoTrickster')
parser.add_argument('host', help='IoTrickster host and port')
parser.add_argument('--brewpi', help='BrewPi host and port', default=None)
args = parser.parse_args(sys.argv[1:])
iotrickster = args.host
clients = []
if args.brewpi:
client = BrewPiClientDevice('brewpi', args.brewpi)
client.start()
clients.append(client)
for client in clients:
client.join()