Compare commits
2 Commits
b5c9a2761f
...
69da949aa8
Author | SHA1 | Date | |
---|---|---|---|
|
69da949aa8 | ||
|
fa67553299 |
1
.env.dev
1
.env.dev
@ -12,3 +12,4 @@ alarminator_token=""
|
|||||||
alarminator_zvies_use_PEALGRP="False"
|
alarminator_zvies_use_PEALGRP="False"
|
||||||
printer=DEFAULT
|
printer=DEFAULT
|
||||||
print_num=0
|
print_num=0
|
||||||
|
BASIC_AUTH_PASSWORD=""
|
@ -14,6 +14,7 @@ ENV print_num=0
|
|||||||
ENV printer="DEFAULT"
|
ENV printer="DEFAULT"
|
||||||
ENV REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
|
ENV REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
|
||||||
ENV MAPS_API_KEY=""
|
ENV MAPS_API_KEY=""
|
||||||
|
ENV BASIC_AUTH_PASSWORD=""
|
||||||
|
|
||||||
|
|
||||||
COPY *.deb /
|
COPY *.deb /
|
||||||
@ -62,4 +63,4 @@ COPY ./app .
|
|||||||
COPY *.crt /usr/local/share/ca-certificates/
|
COPY *.crt /usr/local/share/ca-certificates/
|
||||||
RUN update-ca-certificates
|
RUN update-ca-certificates
|
||||||
|
|
||||||
CMD [ "sh","-c","/etc/init.d/cups start && python3 /usr/src/app/http.py" ]
|
CMD [ "sh","-c","/etc/init.d/cups start && python3 /usr/src/app/app.py" ]
|
||||||
|
136
app/app.py
Normal file
136
app/app.py
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
from flask import Flask, flash, request, redirect, send_from_directory, get_flashed_messages
|
||||||
|
import os
|
||||||
|
import glob
|
||||||
|
import securecad_parser
|
||||||
|
import datetime
|
||||||
|
from hooks import webhook, alarminator_api, cups_print
|
||||||
|
from pathlib import Path
|
||||||
|
from flask_basicauth import BasicAuth
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
app.secret_key = 'super secret key'
|
||||||
|
app.config['SESSION_TYPE'] = 'filesystem'
|
||||||
|
app.config['BASIC_AUTH_USERNAME'] = 'admin'
|
||||||
|
app.config['BASIC_AUTH_PASSWORD'] = os.environ.get('BASIC_AUTH_PASSWORD','password')
|
||||||
|
basic_auth = BasicAuth(app)
|
||||||
|
|
||||||
|
def wrapHtml(innerHtml):
|
||||||
|
return '''<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>JFW Alarm</title>
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
|
||||||
|
</head>
|
||||||
|
<body><div class="container text-center">
|
||||||
|
''' + flashMessages() +'''
|
||||||
|
''' + innerHtml + '''
|
||||||
|
</div>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
|
||||||
|
</body>
|
||||||
|
</html>'''
|
||||||
|
|
||||||
|
def flashMessages():
|
||||||
|
messages = get_flashed_messages(with_categories=True)
|
||||||
|
html = ""
|
||||||
|
if messages:
|
||||||
|
for category, message in messages:
|
||||||
|
html = html + category + ": "+message+"<br>"
|
||||||
|
return html
|
||||||
|
|
||||||
|
def alarmsPath():
|
||||||
|
dir_path = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
return dir_path + "/alarms" + os.sep
|
||||||
|
|
||||||
|
def getAlarmFiles():
|
||||||
|
a_list = glob.glob(alarmsPath()+"*.html")
|
||||||
|
a_list.sort()
|
||||||
|
return [os.path.basename(f) for f in a_list]
|
||||||
|
|
||||||
|
def parseAlarm(f):
|
||||||
|
f = alarmsPath() + os.sep + f
|
||||||
|
with open(f, "r") as o:
|
||||||
|
_r: str = o.read()
|
||||||
|
now = datetime.datetime.now()
|
||||||
|
_r = _r.replace('%DATUM%',now.strftime("%d.%m.%Y"))
|
||||||
|
_r = _r.replace('%UHRZEIT%',now.strftime("%H:%M"))
|
||||||
|
if _r != '':
|
||||||
|
return [securecad_parser.parse_securecad_message(_r),_r]
|
||||||
|
|
||||||
|
def allowed_file(filename:str):
|
||||||
|
print(filename.rsplit('.', 1)[1].lower())
|
||||||
|
return '.' in filename and \
|
||||||
|
filename.rsplit('.', 1)[1].lower() in ["html"]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
|
@basic_auth.required
|
||||||
|
def root():
|
||||||
|
html = ""
|
||||||
|
file = request.args.get('sendalarm')
|
||||||
|
if file is not None:
|
||||||
|
parsed_body, raw = parseAlarm(file)
|
||||||
|
if parsed_body != None:
|
||||||
|
if 'ALARMDEPESCHE' in parsed_body:
|
||||||
|
webhook(parsed_body)
|
||||||
|
alarminator_api(parsed_body)
|
||||||
|
cups_print(parsed_body,raw)
|
||||||
|
flash("Alarm gesendet!")
|
||||||
|
return redirect('/')
|
||||||
|
|
||||||
|
html = html + '<div class="row align-items-start p-3 ">'
|
||||||
|
i: str
|
||||||
|
files = getAlarmFiles()
|
||||||
|
for f in files:
|
||||||
|
html = html + "<div class=\"col-6\" style='padding: 5px;'><a style='display:block; padding:20px' class=\"btn btn-block btn-danger\" href=\"/?sendalarm="+f+"\">" + f + "</a></div>"
|
||||||
|
html = html + "</div>"
|
||||||
|
return wrapHtml(html)
|
||||||
|
|
||||||
|
@app.route('/files', methods=['POST'])
|
||||||
|
@basic_auth.required
|
||||||
|
def upload_file():
|
||||||
|
# check if the post request has the file part
|
||||||
|
print(request.files)
|
||||||
|
if 'file' not in request.files:
|
||||||
|
flash('No file part')
|
||||||
|
return redirect(request.url)
|
||||||
|
file = request.files['file']
|
||||||
|
# If the user does not select a file, the browser submits an
|
||||||
|
# empty file without a filename.
|
||||||
|
print(file.filename)
|
||||||
|
if file.filename == '':
|
||||||
|
flash('No selected file')
|
||||||
|
return redirect(request.url)
|
||||||
|
if file and allowed_file(file.filename):
|
||||||
|
file.save(os.path.join(alarmsPath(), file.filename))
|
||||||
|
return redirect('/files')
|
||||||
|
|
||||||
|
@app.route('/files/<path:filename>', methods=['GET'])
|
||||||
|
@basic_auth.required
|
||||||
|
def download(filename):
|
||||||
|
uploads = alarmsPath()
|
||||||
|
return send_from_directory(uploads, filename, as_attachment=True)
|
||||||
|
|
||||||
|
@app.route("/files", methods=['GET'])
|
||||||
|
@basic_auth.required
|
||||||
|
def files():
|
||||||
|
Path(alarmsPath()).mkdir(parents=True, exist_ok=True)
|
||||||
|
file = request.args.get('del')
|
||||||
|
if file is not None:
|
||||||
|
try:
|
||||||
|
os.remove(alarmsPath()+os.sep+file)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
flash("Datei gelöscht!")
|
||||||
|
return redirect('/files')
|
||||||
|
|
||||||
|
files = getAlarmFiles()
|
||||||
|
html = '<ul class="list-group">'
|
||||||
|
for f in files:
|
||||||
|
html = html + '<li class="list-group-item"><a href="/files/'+f+'">'+f+'</a> <a href="/files?del='+f+'">löschen</a></li>'
|
||||||
|
html = html + '</ul><form method="post" enctype="multipart/form-data"><input type="file" name="file"><input type=submit value=Upload></form>'
|
||||||
|
return html
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run(port=5000)
|
65
app/http.py
65
app/http.py
@ -1,65 +0,0 @@
|
|||||||
from flask import Flask, request
|
|
||||||
import os
|
|
||||||
import glob
|
|
||||||
import securecad_parser
|
|
||||||
import datetime
|
|
||||||
from hooks import webhook, alarminator_api, cups_print
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
|
||||||
|
|
||||||
def wrapHtml(innerHtml):
|
|
||||||
return '''<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<title>Bootstrap demo</title>
|
|
||||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
|
|
||||||
</head>
|
|
||||||
<body><div class="container text-center">
|
|
||||||
''' + innerHtml + '''
|
|
||||||
</div>
|
|
||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
|
|
||||||
</body>
|
|
||||||
</html>'''
|
|
||||||
|
|
||||||
def getAlarmFiles():
|
|
||||||
dir_path = os.path.dirname(os.path.realpath(__file__))
|
|
||||||
dir_path = dir_path + "/alarms" + os.sep
|
|
||||||
a_list = glob.glob(dir_path+"*.html")
|
|
||||||
a_list.sort()
|
|
||||||
return a_list
|
|
||||||
|
|
||||||
def parseAlarm(f):
|
|
||||||
dir_path = os.path.dirname(os.path.realpath(__file__))
|
|
||||||
f = dir_path + "/alarms" + os.sep + f
|
|
||||||
with open(f, "r") as o:
|
|
||||||
_r: str = o.read()
|
|
||||||
now = datetime.datetime.now()
|
|
||||||
_r = _r.replace('%DATUM%',now.strftime("%d.%m.%Y"))
|
|
||||||
_r = _r.replace('%UHRZEIT%',now.strftime("%H:%M"))
|
|
||||||
if _r != '':
|
|
||||||
return [securecad_parser.parse_securecad_message(_r),_r]
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
def root():
|
|
||||||
|
|
||||||
file = request.args.get('sendalarm')
|
|
||||||
if file is not None:
|
|
||||||
parsed_body, raw = parseAlarm(file)
|
|
||||||
if parsed_body != None:
|
|
||||||
if 'ALARMDEPESCHE' in parsed_body:
|
|
||||||
webhook(parsed_body)
|
|
||||||
alarminator_api(parsed_body)
|
|
||||||
cups_print(parsed_body,raw)
|
|
||||||
|
|
||||||
html = '<div class="row align-items-start p-3 ">'
|
|
||||||
i: str
|
|
||||||
files = getAlarmFiles()
|
|
||||||
for f in files:
|
|
||||||
html = html + "<div class=\"col-6\" style='padding: 5px;'><a style='display:block; padding:20px' class=\"btn btn-block btn-danger\" href=\"/?sendalarm="+os.path.basename(f)+"\">" + os.path.basename(f) + "</a></div>"
|
|
||||||
html = html + "</div>"
|
|
||||||
return wrapHtml(html)
|
|
||||||
|
|
||||||
app.run(port=5000)
|
|
@ -2,9 +2,12 @@ version: "2"
|
|||||||
services:
|
services:
|
||||||
app:
|
app:
|
||||||
build: ./
|
build: ./
|
||||||
image: gitea.simonzeyer.de/simon/wnd_ils_alarmfax_parser:latest
|
#image: gitea.simonzeyer.de/simon/wnd_ils_alarmfax_parser:latest
|
||||||
restart: always
|
restart: always
|
||||||
privileged: true
|
privileged: true
|
||||||
|
# ports:
|
||||||
|
# - 631:631
|
||||||
|
# - 5000:5000
|
||||||
environment:
|
environment:
|
||||||
- username=${username}
|
- username=${username}
|
||||||
- password=${password}
|
- password=${password}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user