165 lines
5.6 KiB
Python
Executable File
165 lines
5.6 KiB
Python
Executable File
#!/usr/bin/python
|
|
from bottle import Bottle, run, template, static_file, error, redirect
|
|
import yaml
|
|
import glob
|
|
import re
|
|
import time
|
|
import random
|
|
import string
|
|
import os
|
|
import shutil
|
|
|
|
app = Bottle()
|
|
|
|
APP_ROOT = "/home/beieli"
|
|
|
|
# Read Configuration from YAML-File
|
|
with open("%s/bin/beielimon-config.yaml" % (APP_ROOT), 'r') as stream:
|
|
try:
|
|
config_data = yaml.load(stream)
|
|
#print(config_data)
|
|
except yaml.YAMLError as exc:
|
|
print(exc)
|
|
|
|
def GetLastValues(scale_uuid):
|
|
last_weight = ""
|
|
last_weight_timestamp = ""
|
|
last_swarmalarm = ""
|
|
files = glob.glob("%s/data/weight-%s-????????.log" % (APP_ROOT,scale_uuid))
|
|
if len(files) > 0:
|
|
with open(sorted(files)[-1], "r") as f:
|
|
for line in f: pass
|
|
result = line
|
|
|
|
# Beispiel: 2017-11-07 08:23,0
|
|
m = re.match("(\d{4})-(\d{2})-(\d{2}) (\d\d:\d\d),(\d+)", result)
|
|
if m:
|
|
last_weight = "%sg" % (m.group(5))
|
|
last_weight_timestamp = "%s.%s.%s %s" % (m.group(3),m.group(2),m.group(1),m.group(4))
|
|
else:
|
|
last_weight = "Fehler beim Parsen: %s" % (result)
|
|
last_weight_timestamp = ""
|
|
|
|
else:
|
|
last_weight = "Keine Messwerte"
|
|
last_weight_timestamp = "N/A"
|
|
|
|
files = glob.glob("%s/data/swarmalarm-%s-????????.log" % (APP_ROOT,scale_uuid))
|
|
if len(files) > 0:
|
|
with open(sorted(files)[-1], "r") as f:
|
|
for line in f: pass
|
|
result = line
|
|
|
|
# Beispiel: 2017-11-07 08:23
|
|
m = re.match("(\d{4})-(\d{2})-(\d{2}) (\d\d:\d\d)", result)
|
|
if m:
|
|
last_swarmalarm = "%s.%s.%s %s" % (m.group(3),m.group(2),m.group(1),m.group(4))
|
|
else:
|
|
last_swarmalarm = "Fehler beim Parsen: %s" % (result)
|
|
|
|
else:
|
|
last_swarmalarm = "keiner"
|
|
|
|
return [last_weight, last_weight_timestamp, last_swarmalarm ]
|
|
|
|
def RefreshData():
|
|
scales = config_data['scales']
|
|
hash_scales = {}
|
|
for s in config_data['scales']:
|
|
uuid = s['scale_uuid']
|
|
(last_weight, last_weight_timestamp, last_swarmalarm) = GetLastValues(uuid)
|
|
hash_scales[uuid] = {}
|
|
hash_scales[uuid]['alias'] = s['alias']
|
|
hash_scales[uuid]['last_weight'] = last_weight
|
|
hash_scales[uuid]['last_weight_timestamp'] = last_weight_timestamp
|
|
hash_scales[uuid]['last_swarmalarm'] = last_swarmalarm
|
|
return hash_scales
|
|
|
|
def CreateDatafile(scale_uuid,infotime):
|
|
mycsvdir = "%s/web-csv" % (APP_ROOT)
|
|
my_pattern = '%s%s' % (infotime,'?' * (8 - len(infotime)))
|
|
files = glob.glob("%s/data/weight-%s-%s.log" % (APP_ROOT,scale_uuid,my_pattern))
|
|
filename = "%s/%s-%s.csv" % (mycsvdir,scale_uuid,infotime)
|
|
with open(filename, 'w') as file:
|
|
for ifile in sorted(files):
|
|
if infotime in ifile:
|
|
with_data = True
|
|
with open(ifile, 'r') as ifile:
|
|
for line in ifile:
|
|
file.write(line)
|
|
|
|
|
|
@app.route('/')
|
|
def index():
|
|
hash_scales = RefreshData()
|
|
data = {
|
|
'scales': hash_scales,
|
|
'infotime': time.strftime("%Y", time.localtime()),
|
|
'beielipi_mobile_number': config_data['beielipi_mobile_number']
|
|
}
|
|
|
|
return template('%s/web-root/index.tpl' % (APP_ROOT), data)
|
|
|
|
@app.route('/static/<filepath:path>')
|
|
def server_static(filepath):
|
|
return static_file(filepath, root='%s/web-root/static' % (APP_ROOT))
|
|
|
|
@app.route('/data/<filepath:path>')
|
|
def server_static(filepath):
|
|
return static_file(filepath, root='%s/web-csv' % (APP_ROOT))
|
|
|
|
@app.route('/scale/<scale>/<infotime>')
|
|
def scale_data(scale,infotime):
|
|
scale_uuid = ''
|
|
scale_alias = ''
|
|
for s in config_data['scales']:
|
|
if s['alias'].replace(' ','_') == scale:
|
|
scale_uuid = s['scale_uuid']
|
|
scale_alias = s['alias']
|
|
|
|
CreateDatafile(scale_uuid, infotime)
|
|
data = {
|
|
'scale_uuid': scale_uuid,
|
|
'scale_alias': scale_alias,
|
|
'infotime': infotime
|
|
}
|
|
|
|
return template('%s/web-root/scale.tpl' % (APP_ROOT), data)
|
|
|
|
@app.route('/download/<infotime>')
|
|
def download(infotime):
|
|
mypid = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(7))
|
|
mycsvdir = "%s/tmp/%s/csv" % (APP_ROOT,mypid)
|
|
if not os.path.exists(mycsvdir):
|
|
os.makedirs(mycsvdir)
|
|
myzipdir = "%s/tmp/%s/zip" % (APP_ROOT,mypid)
|
|
if not os.path.exists(myzipdir):
|
|
os.makedirs(myzipdir)
|
|
my_pattern = '%s%s' % (infotime,'?' * (8 - len(infotime)))
|
|
files = glob.glob("%s/data/weight-*-%s.log" % (APP_ROOT,my_pattern))
|
|
for f in files:
|
|
print("%s" % (f))
|
|
for s in config_data['scales']:
|
|
filename = "%s/%s-%s.csv" % (mycsvdir,s['alias'].replace(' ','_'),infotime)
|
|
with open(filename, 'a') as file:
|
|
for ifile in sorted(files):
|
|
if (s['scale_uuid'] in ifile) and (infotime in ifile):
|
|
with open(ifile, 'r') as ifile:
|
|
for line in ifile:
|
|
m = re.match("(\d{4})-(\d{2})-(\d{2}) (\d\d:\d\d),(\d+)", line)
|
|
if m:
|
|
file.write("%s.%s.%s %s,%s\n" % (m.group(3),m.group(2),m.group(1),m.group(4),m.group(5)))
|
|
else:
|
|
file.write("Fehler beim Parsen: %s" % (line))
|
|
|
|
zipfile = "%s/%s" % (myzipdir,infotime)
|
|
shutil.make_archive(zipfile, 'zip', mycsvdir)
|
|
zipfilename = "%s.zip" % (infotime)
|
|
return static_file(zipfilename, root=myzipdir, mimetype='application/zip',download=zipfilename)
|
|
|
|
@app.error(404)
|
|
def error404(error):
|
|
return index()
|
|
|
|
run(app, host='0.0.0.0', port=80)
|