1
0
mirror of https://github.com/daniviga/bite.git synced 2024-11-30 08:36:14 +01:00
bite/docker/simulators/simulator_http.py

68 lines
1.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import os
import json
import string
import random
import datetime
import urllib3
from time import sleep
2020-06-04 18:06:37 +02:00
DEBUG = bool(os.environ.get('IOT_DEBUG', False))
http = urllib3.PoolManager()
def post_json(host, url, data):
2020-06-04 18:06:37 +02:00
json_data = json.dumps(data)
if DEBUG:
print(json_data)
encoded_data = json_data.encode('utf8')
while True:
try:
r = http.request(
'POST',
host + url,
body=encoded_data,
headers={'content-type': 'application/json'})
return r
except urllib3.exceptions.MaxRetryError:
pass
def main():
host = os.environ.get('IOT_HOST', 'http://127.0.0.1:8000')
subscribe = '/api/subscribe/'
telemetry = '/telemetry/'
delay = int(os.environ.get('IOT_DELAY', 10))
serial = os.environ.get('IOT_SERIAL')
if serial is None:
serial = ''.join(
random.choices(string.ascii_lowercase + string.digits, k=8))
data = {'serial': serial}
post_json(host, subscribe, data)
data = {
'device': serial,
'clock': int(datetime.datetime.now().timestamp()),
'payload': {
2020-06-04 18:06:37 +02:00
'id': 'device_http_simulator',
'light': random.randint(300, 500),
"temperature": {
"celsius": random.uniform(20, 28)
}
}
}
while True:
post_json(host, telemetry, data)
sleep(delay)
if __name__ == "__main__":
main()