Update cemento.py
This commit is contained in:
99
cemento.py
99
cemento.py
@@ -1,58 +1,91 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import subprocess
|
from flask import Flask, render_template, request, Response
|
||||||
from flask import Flask, render_template, request
|
import math
|
||||||
|
import subprocess # Necessario per la stampa
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
def calcola(d):
|
||||||
|
try:
|
||||||
|
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('a','0'), d.get('ua','m')) * to_m(d.get('l','0'), d.get('ul','m')) * to_m(d.get('p','0'), d.get('up','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}
|
||||||
|
c = {m: float(d.get(f'c_{m}', '0').replace(',','.')) for m in mats}
|
||||||
|
|
||||||
|
somma_p = sum(p.values())
|
||||||
|
res = {"vol": f"{vol:.3f}", "peso": f"{peso_tot:.0f}"}
|
||||||
|
|
||||||
|
tot_s, costo_t = 0, 0
|
||||||
|
for m in mats:
|
||||||
|
kg = (p[m]/somma_p)*peso_tot if somma_p > 0 else 0
|
||||||
|
s_esatti = kg/w[m] if w[m] > 0 else 0
|
||||||
|
res[f's_{m}'] = math.ceil(s_esatti)
|
||||||
|
res[f'cost_{m}'] = f"{math.ceil(s_esatti)*c[m]:.2f}"
|
||||||
|
tot_s += math.ceil(s_esatti)
|
||||||
|
costo_t += math.ceil(s_esatti)*c[m]
|
||||||
|
|
||||||
|
res.update({"tot_sacc": tot_s, "costo_tot": f"{costo_t:.2f}", "acqua": f"{vol * 150:.0f}"})
|
||||||
|
|
||||||
|
v_b = float(d.get('v_bet', '160').replace(',','.'))
|
||||||
|
n_c = max(1, math.ceil((vol*1000)/v_b)) if vol > 0 else 1
|
||||||
|
res["n_c"] = n_c
|
||||||
|
for m in mats:
|
||||||
|
kg_tot = (p[m]/somma_p)*peso_tot if somma_p > 0 else 0
|
||||||
|
res[f'bc_{m}'] = f"{(kg_tot/w[m])/n_c:.2f}"
|
||||||
|
res["bc_aq"] = f"{(vol*150)/n_c:.2f}"
|
||||||
|
return res
|
||||||
|
except: return None
|
||||||
|
|
||||||
@app.route('/', methods=['GET', 'POST'])
|
@app.route('/', methods=['GET', 'POST'])
|
||||||
def index():
|
def index():
|
||||||
res = None
|
res = None
|
||||||
if request.method == 'POST':
|
if request.method == 'POST': res = calcola(request.form)
|
||||||
try:
|
|
||||||
# Recupero dati dal form del sito
|
|
||||||
v = request.form.get('v', '0')
|
|
||||||
ps = request.form.get('ps', '0')
|
|
||||||
ts = request.form.get('ts', '0')
|
|
||||||
res = {"vol": v, "peso": ps, "tot_sacc": ts}
|
|
||||||
except Exception as e:
|
|
||||||
res = {"errore": str(e)}
|
|
||||||
return render_template('index.html', res=res, form=request.form)
|
return render_template('index.html', res=res, form=request.form)
|
||||||
|
|
||||||
@app.route('/stampa_pdf', methods=['POST'])
|
@app.route('/stampa_pdf', methods=['POST'])
|
||||||
def stampa_pdf():
|
def stampa_pdf():
|
||||||
|
"""Nuova funzione integrata per la stampa su Docker"""
|
||||||
try:
|
try:
|
||||||
# Recupero i valori calcolati
|
f = request.form
|
||||||
v = request.form.get('v', '0')
|
# Prepariamo il contenuto PostScript per il PDF
|
||||||
ps = request.form.get('ps', '0')
|
|
||||||
ts = request.form.get('ts', '0')
|
|
||||||
|
|
||||||
# Formattazione PostScript: necessaria per evitare il file da 2.5KB vuoto
|
|
||||||
ps_content = f"""%!PS
|
ps_content = f"""%!PS
|
||||||
/Helvetica findfont 16 scalefont setfont
|
/Helvetica-Bold findfont 16 scalefont setfont
|
||||||
100 750 moveto (REPORT CALCOLO CEMENTO) show
|
100 750 moveto (REPORT TECNICO CALCOLO CEMENTO) show
|
||||||
/Helvetica findfont 12 scalefont setfont
|
/Helvetica findfont 12 scalefont setfont
|
||||||
100 720 moveto (Volume totale: {v} m3) show
|
100 720 moveto (Volume Totale: {f.get('res_vol')} m3) show
|
||||||
100 700 moveto (Peso stimato: {ps} kg) show
|
100 700 moveto (Peso Totale: {f.get('res_peso')} Kg) show
|
||||||
100 680 moveto (Totale sacchetti: {ts} pezzi) show
|
100 680 moveto (Totale Sacchetti: {f.get('res_tot_s')}) show
|
||||||
100 650 moveto (Data: 20/01/2026) show
|
100 660 moveto (Costo Complessivo: {f.get('res_costo_t')} Euro) show
|
||||||
|
100 630 moveto (Data: 20/01/2026) show
|
||||||
showpage"""
|
showpage"""
|
||||||
|
|
||||||
# Comando per iniettare i dati direttamente nel container via socket
|
# Iniezione diretta nel container tramite docker exec
|
||||||
# Questo comando richiede che /var/run/docker.sock abbia permessi 666
|
|
||||||
comando = ["docker", "exec", "-i", "cups-pdf-server", "lp", "-d", "Virtual_PDF"]
|
comando = ["docker", "exec", "-i", "cups-pdf-server", "lp", "-d", "Virtual_PDF"]
|
||||||
|
|
||||||
# Esecuzione del comando passandogli il testo PostScript codificato
|
|
||||||
processo = subprocess.run(comando, input=ps_content.encode('utf-8'), capture_output=True)
|
processo = subprocess.run(comando, input=ps_content.encode('utf-8'), capture_output=True)
|
||||||
|
|
||||||
if processo.returncode == 0:
|
if processo.returncode == 0:
|
||||||
return "OK - PDF generato correttamente in /docker/cups-pdf/output"
|
return "OK - PDF generato correttamente!"
|
||||||
else:
|
else:
|
||||||
# Restituisce l'errore del demone Docker (es. Permission Denied)
|
return f"Errore Docker: {processo.stderr.decode()}"
|
||||||
return f"Errore container: {processo.stderr.decode()}"
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return f"Errore script: {str(e)}"
|
return f"Errore: {str(e)}"
|
||||||
|
|
||||||
|
@app.route('/download', methods=['POST'])
|
||||||
|
def download():
|
||||||
|
# ... (resta invariato come nel tuo file originale) ...
|
||||||
|
f = request.form
|
||||||
|
report = f"""-------------------------------------------
|
||||||
|
REPORT TECNICO CALCOLO CEMENTO
|
||||||
|
-------------------------------------------
|
||||||
|
PARAMETRI DI INPUT..."""
|
||||||
|
return Response(report, mimetype="text/plain", headers={"Content-disposition":"attachment;filename=Report_Dettagliato.txt"})
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Configurato per rispondere sulla porta 5000 tramite Proxy Reverse
|
|
||||||
app.run(host='0.0.0.0', port=5000)
|
app.run(host='0.0.0.0', port=5000)
|
||||||
Reference in New Issue
Block a user