61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
import sys
|
|
import socket
|
|
import math
|
|
from flask import Flask, render_template, request, Response
|
|
|
|
sys.path.append('/var/services/homes/Francesco/.local/lib/python3.8/site-packages')
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Configurazione CUPS-PDF
|
|
CUPS_IP = "192.168.1.14"
|
|
CUPS_PORT = 6310
|
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
|
def index():
|
|
res = None
|
|
if request.method == 'POST':
|
|
try:
|
|
d = request.form
|
|
def to_m(v, u):
|
|
val = float(v.replace(',', '.')) if v else 0
|
|
return val / 1000 if u == "mm" else (val / 100 if u == "cm" else val)
|
|
|
|
vol = to_m(d.get('l','0'), d.get('ul','m')) * to_m(d.get('p','0'), d.get('up','m')) * to_m(d.get('a','0'), d.get('ua','m'))
|
|
peso_tot = vol * 2400
|
|
mats = ['cem', 'sab', 'ghi']
|
|
p = {m: float(d.get(f'p_{m}', '0').replace(',','.')) for m in mats}
|
|
w = {m: float(d.get(f'w_{m}', '25').replace(',','.')) for m in mats}
|
|
|
|
somma_p = sum(p.values())
|
|
res = {"vol": f"{vol:.3f}", "peso": f"{peso_tot:.0f}"}
|
|
tot_s = 0
|
|
for m in mats:
|
|
if p[m] <= 0: # Correzione bug ghiaia a 0
|
|
res[f's_{m}'] = 0
|
|
continue
|
|
s_calc = math.ceil(((p[m]/somma_p)*peso_tot)/w[m]) if somma_p > 0 else 0
|
|
res[f's_{m}'] = s_calc
|
|
tot_s += s_calc
|
|
res["tot_sacc"] = tot_s
|
|
except: res = None
|
|
return render_template('index.html', res=res, form=request.form)
|
|
|
|
@app.route('/stampa_pdf', methods=['POST'])
|
|
def stampa_pdf():
|
|
f = request.form
|
|
# Intestazione formattata per CUPS-PDF
|
|
report = "--- CALCOLO CEMENTO V1.1 ---\n"
|
|
report += f"Volume: {f.get('v')} m3 | Peso: {f.get('p')} kg\n"
|
|
report += f"Totale Sacchetti: {f.get('ts')}\n"
|
|
|
|
try:
|
|
with socket.create_connection((CUPS_IP, CUPS_PORT), timeout=5) as s:
|
|
s.sendall(report.encode('utf-8'))
|
|
return "OK - Report inviato a Virtual_PDF"
|
|
except Exception as e:
|
|
return f"Errore: {str(e)}"
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5000) |