1
0
mirror of https://github.com/daniviga/bite.git synced 2024-11-26 14:56:13 +01:00

Add a virtual EDGE which spawns http simulators (#8)

* Add dockerized python simulators

* Create an EDGE gateway via dind

* Fix README.md title
This commit is contained in:
Daniele Viganò 2020-06-03 19:26:35 +02:00 committed by GitHub
parent 4cebe6a8f5
commit b6653c510d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 100 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"

6
docker/edge/README.md Normal file
View File

@ -0,0 +1,6 @@
# EDGE device simulator (via docker-in-docker)
```bash
export DOCKER_HOST='127.0.0.1:22375'
docker-compose -f docker-compose.edge.yml up -d --scale device-http=4
```

View File

@ -0,0 +1,19 @@
version: "3.7"
networks:
localnet:
x-op-service-default: &service_default
restart: unless-stopped
init: true
services:
device-http:
<<: *service_default
image: daniviga/freedcs-device-http
environment:
IOT_HOST: "http://192.168.10.123:8000"
# IOT_SERIAL: "abcd1234"
# IOT_DELAY: 10
networks:
- localnet

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()