From c99a069d06a4f55bcb6cf5219af1d9a20ffa7caa Mon Sep 17 00:00:00 2001 From: Francesco Date: Wed, 4 Feb 2026 19:20:04 +0100 Subject: [PATCH] Versione 1.0 - Reset Totale Progetto --- cemento.py | 205 +++++++++++++++++++++++++++++++++++++++++++ log.txt | 4 + output/report.pdf | 4 + output/report.txt | 4 + templates/index.html | 107 ++++++++++++++++++++++ 5 files changed, 324 insertions(+) create mode 100755 cemento.py create mode 100644 log.txt create mode 100755 output/report.pdf create mode 100755 output/report.txt create mode 100755 templates/index.html diff --git a/cemento.py b/cemento.py new file mode 100755 index 0000000..f11d6f5 --- /dev/null +++ b/cemento.py @@ -0,0 +1,205 @@ +# -*- coding: utf-8 -*- +from flask import Flask, render_template, request, Response +import math +import subprocess + +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 + + # Salvo le dimensioni originali per il report + dim_orig = {"a": d.get('a','0'), "ua": d.get('ua','m'), "l": d.get('l','0'), "ul": d.get('ul','m'), "p": d.get('p','0'), "up": d.get('up','m')} + + vol = to_m(dim_orig['a'], dim_orig['ua']) * to_m(dim_orig['l'], dim_orig['ul']) * to_m(dim_orig['p'], dim_orig['up']) + 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()) + perc = {m: (p[m]/somma_p)*100 if somma_p > 0 else 0 for m in mats} + + res = {"vol": f"{vol:.3f}", "peso": f"{peso_tot:.0f}", "perc": perc, "dim": dim_orig, "parti": p} + + 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) + 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 Exception as e: + print(f"Errore nel calcolo: {e}") + return None + +@app.route('/', methods=['GET', 'POST']) +def index(): + res = None + msg_stampa = None + if request.method == 'POST': + res = calcola(request.form) + if 'btn_stampa_cups' in request.form: + # Passa sia il form che i risultati del calcolo (se disponibili) + msg_stampa = esegui_stampa_cups(request.form, res) + return render_template('index.html', res=res, form=request.form, msg_stampa=msg_stampa) + +def esegui_stampa_cups(f, res_calcolo=None): + try: + # Ottieni i dati: prima dal form, poi da res_calcolo, poi dai campi originali + def get_val(key, default='0'): + # 1. Cerca nel form con il prefisso 'd_' (campi nascosti) + if key in ['a', 'ua', 'l', 'ul', 'p', 'up']: + val = f.get(f'd_{key}', None) + if val: + return val + + # 2. Cerca nel form direttamente + val = f.get(key, None) + if val: + return val + + # 3. Cerca in res_calcolo se disponibile + if res_calcolo: + if key in ['a', 'ua', 'l', 'ul', 'p', 'up']: + return res_calcolo['dim'].get(key, default) + elif key == 'vol': + return res_calcolo.get('vol', default) + elif key == 'peso': + return res_calcolo.get('peso', default) + + return default + + # Estrai tutti i valori necessari + d_a = get_val('a', '0') + d_ua = get_val('ua', 'm') + d_l = get_val('l', '0') + d_ul = get_val('ul', 'm') + d_p = get_val('p', '0') + d_up = get_val('up', 'm') + + # Ottieni i risultati con valori di default + res_vol = f.get('res_vol') or (res_calcolo['vol'] if res_calcolo and 'vol' in res_calcolo else '0') + res_peso = f.get('res_peso') or (res_calcolo['peso'] if res_calcolo and 'peso' in res_calcolo else '0') + res_s_cem = f.get('res_s_cem') or (res_calcolo['s_cem'] if res_calcolo and 's_cem' in res_calcolo else '0') + res_s_sab = f.get('res_s_sab') or (res_calcolo['s_sab'] if res_calcolo and 's_sab' in res_calcolo else '0') + res_s_ghi = f.get('res_s_ghi') or (res_calcolo['s_ghi'] if res_calcolo and 's_ghi' in res_calcolo else '0') + res_aq = f.get('res_aq') or (res_calcolo['acqua'] if res_calcolo and 'acqua' in res_calcolo else '0') + res_tot_s = f.get('res_tot_s') or (res_calcolo['tot_sacc'] if res_calcolo and 'tot_sacc' in res_calcolo else '0') + res_costo_t = f.get('res_costo_t') or (res_calcolo['costo_tot'] if res_calcolo and 'costo_tot' in res_calcolo else '0') + res_nc = f.get('res_nc') or (res_calcolo['n_c'] if res_calcolo and 'n_c' in res_calcolo else '1') + res_bc_cem = f.get('res_bc_cem') or (res_calcolo['bc_cem'] if res_calcolo and 'bc_cem' in res_calcolo else '0') + res_bc_sab = f.get('res_bc_sab') or (res_calcolo['bc_sab'] if res_calcolo and 'bc_sab' in res_calcolo else '0') + res_bc_ghi = f.get('res_bc_ghi') or (res_calcolo['bc_ghi'] if res_calcolo and 'bc_ghi' in res_calcolo else '0') + res_bc_aq = f.get('res_bc_aq') or (res_calcolo['bc_aq'] if res_calcolo and 'bc_aq' in res_calcolo else '0') + + p_cem = f.get('p_cem', '1') + p_sab = f.get('p_sab', '3') + p_ghi = f.get('p_ghi', '5') + pc_cem = f.get('pc_cem', '0') + pc_sab = f.get('pc_sab', '0') + pc_ghi = f.get('pc_ghi', '0') + + ps_content = f"""%!PS +/Helvetica-Bold findfont 11 scalefont setfont +70 760 moveto (DIMENSIONI GETTO) show +/Helvetica findfont 10 scalefont setfont +70 745 moveto (Altezza: {d_a} {d_ua} | Larghezza: {d_l} {d_ul} | Profondita: {d_p} {d_up}) show +/Helvetica-Bold findfont 11 scalefont setfont +70 720 moveto (VOLUME: {res_vol} m3 | Peso: {res_peso} Kg) show +70 700 moveto (ACQUISTI) show +/Helvetica findfont 10 scalefont setfont +70 685 moveto (o Cemento: {res_s_cem} sacchetti) show +70 670 moveto (o Sabbia: {res_s_sab} sacchetti) show +70 655 moveto (o Ghiaia: {res_s_ghi} sacchetti) show +70 640 moveto (o Acqua: {res_aq} L | Totale: {res_tot_s} sacch.) show +/Helvetica-Bold findfont 11 scalefont setfont +70 620 moveto (COSTO TOTALE: {res_costo_t} Euro) show +70 600 moveto (PROPORZIONI) show +/Helvetica findfont 10 scalefont setfont +70 585 moveto (Cem: {p_cem} ({pc_cem}%) | Sab: {p_sab} ({pc_sab}%) | Ghi: {p_ghi} ({pc_ghi}%)) show +70 560 moveto (PER OGNI CARICO ({res_nc} CARICHI):) show +70 545 moveto (o Cemento: {res_bc_cem} sacchetti) show +70 530 moveto (o Sabbia: {res_bc_sab} sacchetti) show +70 515 moveto (o Ghiaia: {res_bc_ghi} sacchetti) show +70 500 moveto (o Acqua: {res_bc_aq} L) show +showpage""" + + # Salva il contenuto PS per debug + try: + with open('/tmp/stampa_cups.ps', 'w', encoding='utf-8') as f_debug: + f_debug.write(ps_content) + except: + pass # Ignora errori di scrittura del debug + + # Converti in bytes + ps_bytes = ps_content.encode('utf-8') + + # Esegui il comando CUPS - VERSIONE SEMPLIFICATA + result = subprocess.run( + ["docker", "exec", "-i", "cups-pdf-server", "lp", "-d", "Virtual_PDF"], + input=ps_bytes, + capture_output=True, + timeout=30 + ) + + if result.returncode == 0: + return "OK - PDF generato in /output del container cups-pdf-server!" + else: + error_msg = result.stderr.decode('utf-8', errors='ignore') if result.stderr else "Errore sconosciuto" + return f"Errore CUPS: {error_msg}" + + except subprocess.TimeoutExpired: + return "Errore: Timeout durante la stampa CUPS" + except FileNotFoundError: + return "Errore: Docker o container cups-pdf-server non trovato" + except Exception as e: + # Gestisci l'errore in modo sicuro + error_msg = str(e) + if "'bytes' object has no attribute 'encode'" in error_msg: + return "Errore interno: problema nella conversione dei dati per la stampa" + else: + return f"Errore di stampa: {error_msg}" + +@app.route('/download', methods=['POST']) +def download(): + f = request.form + report = ( + f"DIMENSIONI GETTO\n" + f"Altezza: {f.get('d_a', '0')} {f.get('d_ua', 'm')} Larghezza: {f.get('d_l', '0')} {f.get('d_ul', 'm')} Profondità: {f.get('d_p', '0')} {f.get('d_up', 'm')}\n\n" + f"VOLUME: {f.get('res_vol', '0')} m3 | Peso: {f.get('res_peso', '0')} Kg\n\n" + f"ACQUISTI\n" + f"• Cemento: \t{f.get('res_s_cem', '0')} sacchetti\n" + f"• Sabbia: \t{f.get('res_s_sab', '0')} sacchetti\n" + f"• Ghiaia: \t{f.get('res_s_ghi', '0')} sacchetti\n" + f"• Acqua: \t{f.get('res_aq', '0')} L | Totale: {f.get('res_tot_s', '0')} sacch.\n\n" + f"COSTO TOTALE: \t{f.get('res_costo_t', '0')} €\n\n" + f"PROPORZIONI\n" + f"Cemento: {f.get('p_cem', '1')} \tSabbia: {f.get('p_sab', '3')} \tGhiaia: {f.get('p_ghi', '5')}\n" + f"Cemento: {f.get('pc_cem', '0')}% \tSabbia: {f.get('pc_sab', '0')}% \tGhiaia: {f.get('pc_ghi', '0')}%\n\n" + f"PER OGNI CARICO ({f.get('res_nc', '1')} CARICHI):\n\n" + f"• Cemento: \t{f.get('res_bc_cem', '0')} sacchetti\n" + f"• Sabbia: \t{f.get('res_bc_sab', '0')} sacchetti\n" + f"• Ghiaia: \t{f.get('res_bc_ghi', '0')} sacchetti\n" + f"• Acqua: \t{f.get('res_bc_aq', '0')} L" + ) + return Response(report, mimetype="text/plain", headers={"Content-disposition":"attachment;filename=Report_V1_2.txt"}) + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000, debug=True) \ No newline at end of file diff --git a/log.txt b/log.txt new file mode 100644 index 0000000..cce6e15 --- /dev/null +++ b/log.txt @@ -0,0 +1,4 @@ + * Serving Flask app 'cemento' + * Debug mode: on +Address already in use +Port 5000 is in use by another program. Either identify and stop that program, or start the server with a different port. diff --git a/output/report.pdf b/output/report.pdf new file mode 100755 index 0000000..b935a48 --- /dev/null +++ b/output/report.pdf @@ -0,0 +1,4 @@ +RISULTATI +Volume: 0.000 m3 +Betoniera: 160.0 L +Carichi: 1 diff --git a/output/report.txt b/output/report.txt new file mode 100755 index 0000000..b935a48 --- /dev/null +++ b/output/report.txt @@ -0,0 +1,4 @@ +RISULTATI +Volume: 0.000 m3 +Betoniera: 160.0 L +Carichi: 1 diff --git a/templates/index.html b/templates/index.html new file mode 100755 index 0000000..fbc45e2 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,107 @@ + + + + + Software Cemento V 1.2 + + + +
+
+
+ + + + +
Dimensioni
Altezza
Larghezza
Profondità
+ + +
Miscela (Parti)
Cem:
Sab:
Ghi:
+ + +
Peso sacchette (Kg)
Cem:Sab:Ghi:
+ + +
Costo sacchette (€)
Cem:Sab:Ghi:
+
Attrezzatura
Volume Betoniera: L
+ +
+ +
+ +
+

Risultati

+ {% if res %} +
+ DIMENSIONI GETTO
+ Altezza: {{res.dim.a}} {{res.dim.ua}}   Larghezza: {{res.dim.l}} {{res.dim.ul}}   Profondità: {{res.dim.p}} {{res.dim.up}}

+ VOLUME: {{ res.vol }} m³ | Peso: {{ res.peso }} Kg +
+ ACQUISTI +
    +
  • Cemento: {{ res.s_cem }} sacchetti
  • +
  • Sabbia: {{ res.s_sab }} sacchetti
  • +
  • Ghiaia: {{ res.s_ghi }} sacchetti
  • +
  • Acqua: {{ res.acqua }} L | Totale: {{ res.tot_sacc }} sacch.
  • +
+
+ COSTO TOTALE: {{ res.costo_tot }} € +
+ PROPORZIONI
+ Cemento: {{res.parti.cem}}   Sabbia: {{res.parti.sab}}   Ghiaia: {{res.parti.ghi}}
+ Cemento: {{ "%.1f"|format(res.perc.cem) }}%   Sabbia: {{ "%.1f"|format(res.perc.sab) }}%   Ghiaia: {{ "%.1f"|format(res.perc.ghi) }}% +
+ PER OGNI CARICO ({{ res.n_c }} CARICHI): +
    +
  • • Cemento: {{ res.bc_cem }} sacchetti
  • +
  • • Sabbia: {{ res.bc_sab }} sacchetti
  • +
  • • Ghiaia: {{ res.bc_ghi }} sacchetti
  • +
  • • Acqua: {{ res.bc_aq }} L
  • +
+
+
+ {% for k,v in form.items() %}{% endfor %} + + + + + + + + + + + + + + +
+ {% if msg_stampa %}

{{ msg_stampa }}

{% endif %} + {% endif %} +
+
+ + \ No newline at end of file