web server initial approach. Missing detection of old data
This commit is contained in:
parent
85a76cd047
commit
32dfb91a0a
|
@ -1,7 +1,7 @@
|
||||||
FROM python:3.10
|
FROM python:3.10
|
||||||
|
|
||||||
RUN pip install --no-cache-dir --upgrade pip && \
|
RUN pip install --no-cache-dir --upgrade pip && \
|
||||||
pip install --no-cache-dir selenium-wire
|
pip install --no-cache-dir selenium-wire flask
|
||||||
|
|
||||||
RUN apt-get update && apt-get -y install chromium
|
RUN apt-get update && apt-get -y install chromium
|
||||||
|
|
||||||
|
|
12
README.md
12
README.md
|
@ -5,3 +5,15 @@ Das Projekt wird gestartet mit `docker compose up` (`docker-compose up`)
|
||||||
Bisher werden nur daten des Typs "online-level" verarbeitet, aber der Rest sollte einigermassen trivial sein.
|
Bisher werden nur daten des Typs "online-level" verarbeitet, aber der Rest sollte einigermassen trivial sein.
|
||||||
|
|
||||||
Ich habe keine Ahnung, wie und in welchem Format ihr das an euren zentralen Thinger Server senden wollt, aber kÃnnt ihr vieleicht auch selber erledigen.
|
Ich habe keine Ahnung, wie und in welchem Format ihr das an euren zentralen Thinger Server senden wollt, aber kÃnnt ihr vieleicht auch selber erledigen.
|
||||||
|
|
||||||
|
### server-web.py
|
||||||
|
|
||||||
|
Hier wird der web server direkt im Programm gestartet. Der Server lauscht auf Port 80. Im docker container gestartet kann man den server auf port 8050 anfragen
|
||||||
|
|
||||||
|
```
|
||||||
|
# Liste mit ids
|
||||||
|
curl http://127.0.0.1:8050/ids
|
||||||
|
# Wert der Id
|
||||||
|
curl http://127.0.0.1:8050/nmt/6
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
@ -5,5 +5,7 @@ services:
|
||||||
build: .
|
build: .
|
||||||
restart: always
|
restart: always
|
||||||
mem_limit: 1024m
|
mem_limit: 1024m
|
||||||
|
ports:
|
||||||
|
- 127.0.0.1:8050:80
|
||||||
volumes:
|
volumes:
|
||||||
- ./server.py:/app/server.py
|
- ./server-web.py:/app/server.py
|
||||||
|
|
71
server-web.py
Normal file
71
server-web.py
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
from seleniumwire import webdriver # Import from seleniumwire
|
||||||
|
from seleniumwire.utils import decode
|
||||||
|
from time import sleep
|
||||||
|
from collections import deque
|
||||||
|
from flask import Flask as f
|
||||||
|
from flask import Response
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
|
||||||
|
options = webdriver.ChromeOptions()
|
||||||
|
options.add_argument('--headless')
|
||||||
|
options.add_argument("--incognito")
|
||||||
|
options.add_argument("--nogpu")
|
||||||
|
options.add_argument("--disable-gpu")
|
||||||
|
options.add_argument("--window-size=1280,1280")
|
||||||
|
options.add_argument("--no-sandbox")
|
||||||
|
options.add_argument("--enable-javascript")
|
||||||
|
options.add_argument('--disable-dev-shm-usage')
|
||||||
|
|
||||||
|
# Create a new instance of the Chrome driver
|
||||||
|
driver = webdriver.Chrome(options=options)
|
||||||
|
q = deque()
|
||||||
|
valuestore = {}
|
||||||
|
app = f("web")
|
||||||
|
|
||||||
|
def interceptor(request, response): # A response interceptor takes two args
|
||||||
|
if 'ws-travis.dus.com/socket.io/?EIO=3&transport=polling' in request.url :
|
||||||
|
body = decode(response.body, response.headers.get('Content-Encoding', 'identity'))
|
||||||
|
x = re.split("\d\d\d:\d\d\/dus,", body.decode("utf-8"))
|
||||||
|
for i in [i for i in x if i]:
|
||||||
|
try:
|
||||||
|
j = json.loads(i)
|
||||||
|
handleJson(j)
|
||||||
|
except ValueError:
|
||||||
|
if i.find("online-level") > 0:
|
||||||
|
print ("Fixed: ", i)
|
||||||
|
j = json.loads(i[:i.rfind(']') + 1])
|
||||||
|
handleJson(j)
|
||||||
|
else:
|
||||||
|
print ("Err: ", i)
|
||||||
|
|
||||||
|
|
||||||
|
def handleJson(jsonObj):
|
||||||
|
if isinstance(jsonObj, list):
|
||||||
|
if 'online-level' in jsonObj:
|
||||||
|
#typ = jsonObj[1]["LevelValues"][0]["Type"]
|
||||||
|
value = jsonObj[1]["LevelValues"][0]["Values"]
|
||||||
|
NmtId = jsonObj[1]["NmtId"]
|
||||||
|
#time = jsonObj[1]["Time"]
|
||||||
|
valuestore[NmtId] = value
|
||||||
|
|
||||||
|
# Gibt den letzten bekannten Wert fuer die NMTID zurueck
|
||||||
|
@app.route('/nmt/<int:id>')
|
||||||
|
def nmtid(id):
|
||||||
|
if id in valuestore:
|
||||||
|
return valuestore[id]
|
||||||
|
else:
|
||||||
|
return "Not Found"
|
||||||
|
|
||||||
|
# Gibt eine Liste mit verfuegbaren ids zurueck
|
||||||
|
@app.route('/ids')
|
||||||
|
def ids():
|
||||||
|
l = list(valuestore.keys())
|
||||||
|
return ','.join(map(str,l))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
driver.response_interceptor = interceptor
|
||||||
|
|
||||||
|
driver.get('https://dus-travis.dus.com/')
|
||||||
|
|
||||||
|
app.run(host='0.0.0.0', port=80, debug=False)
|
37
server.py
37
server.py
|
@ -2,6 +2,8 @@ from seleniumwire import webdriver # Import from seleniumwire
|
||||||
from seleniumwire.utils import decode
|
from seleniumwire.utils import decode
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from collections import deque
|
from collections import deque
|
||||||
|
from flask import Flask as f
|
||||||
|
from flask import Response
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
@ -18,6 +20,8 @@ options.add_argument('--disable-dev-shm-usage')
|
||||||
# Create a new instance of the Chrome driver
|
# Create a new instance of the Chrome driver
|
||||||
driver = webdriver.Chrome(options=options)
|
driver = webdriver.Chrome(options=options)
|
||||||
q = deque()
|
q = deque()
|
||||||
|
valuestore = {}
|
||||||
|
app = f("web")
|
||||||
|
|
||||||
def interceptor(request, response): # A response interceptor takes two args
|
def interceptor(request, response): # A response interceptor takes two args
|
||||||
if 'ws-travis.dus.com/socket.io/?EIO=3&transport=polling' in request.url :
|
if 'ws-travis.dus.com/socket.io/?EIO=3&transport=polling' in request.url :
|
||||||
|
@ -37,26 +41,31 @@ def interceptor(request, response): # A response interceptor takes two args
|
||||||
|
|
||||||
|
|
||||||
def handleJson(jsonObj):
|
def handleJson(jsonObj):
|
||||||
|
|
||||||
if isinstance(jsonObj, list):
|
if isinstance(jsonObj, list):
|
||||||
if 'online-level' in jsonObj:
|
if 'online-level' in jsonObj:
|
||||||
typ = jsonObj[1]["LevelValues"][0]["Type"]
|
#typ = jsonObj[1]["LevelValues"][0]["Type"]
|
||||||
value = jsonObj[1]["LevelValues"][0]["Values"]
|
value = jsonObj[1]["LevelValues"][0]["Values"]
|
||||||
NmtId = jsonObj[1]["NmtId"]
|
NmtId = jsonObj[1]["NmtId"]
|
||||||
time = jsonObj[1]["Time"]
|
#time = jsonObj[1]["Time"]
|
||||||
|
valuestore[NmtId] = value
|
||||||
|
|
||||||
q.append([typ, value, NmtId, time])
|
# Gibt den letzten bekannten Wert fuer die NMTID zurueck
|
||||||
|
@app.route('/nmt/<int:id>')
|
||||||
|
def nmtid(id):
|
||||||
|
if id in valuestore:
|
||||||
|
return valuestore[id]
|
||||||
|
else:
|
||||||
|
return "Not Found"
|
||||||
|
|
||||||
|
# Gibt eine Liste mit verfuegbaren ids zurueck
|
||||||
|
@app.route('/ids')
|
||||||
|
def ids():
|
||||||
|
l = list(valuestore.keys())
|
||||||
|
return ','.join(map(str,l))
|
||||||
|
|
||||||
driver.response_interceptor = interceptor
|
if __name__ == '__main__':
|
||||||
|
driver.response_interceptor = interceptor
|
||||||
|
|
||||||
driver.get('https://dus-travis.dus.com/')
|
driver.get('https://dus-travis.dus.com/')
|
||||||
|
|
||||||
while True:
|
|
||||||
sleep(1)
|
|
||||||
l = len(q)
|
|
||||||
for i in range(1, l):
|
|
||||||
a = q.pop()
|
|
||||||
#print ("[", a[3], "]: ", "NmtId: ", a[2] , " Typ: ", a[0], " Wert: ", a[1])
|
|
||||||
print("Popped ",l," Messages")
|
|
||||||
|
|
||||||
|
app.run(host='0.0.0.0', port=80, debug=False)
|
||||||
|
|
Loading…
Reference in a new issue