mirror of
https://github.com/daniviga/bite.git
synced 2024-11-26 23:06:14 +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:
parent
4cebe6a8f5
commit
b6653c510d
|
@ -44,3 +44,14 @@ services:
|
||||||
ports:
|
ports:
|
||||||
- "15672:15672"
|
- "15672:15672"
|
||||||
- "5672:5672"
|
- "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
6
docker/edge/README.md
Normal 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
|
||||||
|
```
|
19
docker/edge/docker-compose.yml
Normal file
19
docker/edge/docker-compose.yml
Normal 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
|
6
docker/simulators/Dockerfile.http
Normal file
6
docker/simulators/Dockerfile.http
Normal 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"]
|
58
docker/simulators/simulator_http.py
Normal file
58
docker/simulators/simulator_http.py
Normal 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()
|
Loading…
Reference in New Issue
Block a user