1
0
mirror of https://github.com/daniviga/bite.git synced 2025-04-18 22:00:11 +02:00

Add dockerized python simulators

This commit is contained in:
Daniele Viganò 2020-06-03 19:15:41 +02:00
parent 4cebe6a8f5
commit 05e7664da8
Signed by: dani
GPG Key ID: DB49AFC03C40EE02
3 changed files with 75 additions and 0 deletions

View File

@ -44,3 +44,14 @@ services:
ports:
- "15672:15672"
- "5672:5672"
edge:
<<: *service_default
image: docker:dind
privileged: true
environment:
DOCKER_TLS_CERTDIR:
networks:
- net
ports:
- "127.0.0.1:22375:2375"

View File

@ -0,0 +1,6 @@
FROM python:3.8-alpine
RUN pip3 install urllib3
COPY ./simulator_http.py /opt/freedcs/simulator_http.py
ENTRYPOINT ["python3", "/opt/freedcs/simulator_http.py"]

View File

@ -0,0 +1,58 @@
#!/usr/bin/env python3
import os
import json
import string
import random
import datetime
import urllib3
from time import sleep
http = urllib3.PoolManager()
def post_json(host, url, data):
encoded_data = json.dumps(data).encode('utf8')
while True:
try:
retry = False
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': {
'data': 'sample_data'
}
}
while True:
post_json(host, telemetry, data)
sleep(delay)
if __name__ == "__main__":
main()