Update cemento.py
Implementata Stamapa tramite CUPS
This commit is contained in:
101
cemento.py
101
cemento.py
@@ -1,25 +1,31 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 1. PERCORSO LIBRERIE SPECIFICO PER SYNOLOGY (Identificato con il test)
|
||||
# Questo permette a Gitea di trovare Flask anche se l'utente è diverso
|
||||
sys.path.append('/var/services/homes/Francesco/.local/lib/python3.8/site-packages')
|
||||
|
||||
from flask import Flask, render_template, request, Response
|
||||
import math
|
||||
import subprocess
|
||||
import time
|
||||
from flask import Flask, render_template, request, Response
|
||||
|
||||
# 1. PERCORSO LIBRERIE SYNOLOGY (Identificato con il test)
|
||||
sys.path.append('/var/services/homes/Francesco/.local/lib/python3.8/site-packages')
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# 2. DEFINIZIONE VERSIONE (Apparirà in rosso sul sito)
|
||||
VERSIONE = "Rev.1"
|
||||
# 2. CONFIGURAZIONE VERSIONE E STAMPA
|
||||
VERSIONE = "V1.1 - Synology CUPS Edition"
|
||||
CUPS_SERVER = "192.168.1.14:6310" # Indirizzo container CUPS
|
||||
PRINTER_NAME = "Virtual_PDF" # Nome stampante configurata
|
||||
OUTPUT_DIR = "/volume1/docker/cups-pdf/output" # Cartella destinazione PDF
|
||||
|
||||
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
|
||||
if u == "mm": return val / 1000
|
||||
if u == "cm": return val / 100
|
||||
return val
|
||||
|
||||
# Calcolo Volume e Peso
|
||||
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
|
||||
|
||||
@@ -33,6 +39,12 @@ def calcola(d):
|
||||
|
||||
tot_s, costo_t = 0, 0
|
||||
for m in mats:
|
||||
# Correzione BUG: Se la parte è 0, forza a 0 sacchetti
|
||||
if p[m] <= 0:
|
||||
res[f's_{m}'] = 0
|
||||
res[f'cost_{m}'] = "0.00"
|
||||
continue
|
||||
|
||||
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)
|
||||
@@ -42,12 +54,16 @@ def calcola(d):
|
||||
|
||||
res.update({"tot_sacc": tot_s, "costo_tot": f"{costo_t:.2f}", "acqua": f"{vol * 150:.0f}"})
|
||||
|
||||
# Calcolo carichi betoniera
|
||||
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}"
|
||||
if p[m] <= 0:
|
||||
res[f'bc_{m}'] = "0.00"
|
||||
else:
|
||||
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
|
||||
@@ -56,46 +72,39 @@ def calcola(d):
|
||||
def index():
|
||||
res = None
|
||||
if request.method == 'POST': res = calcola(request.form)
|
||||
# Passiamo la revisione al template HTML
|
||||
return render_template('index.html', res=res, form=request.form, rev=VERSIONE)
|
||||
|
||||
@app.route('/stampa_pdf', methods=['POST'])
|
||||
def stampa_pdf():
|
||||
f = request.form
|
||||
# Creazione testo per il PDF
|
||||
report_text = f"--- REPORT CALCOLO CEMENTO {VERSIONE} ---\n\n"
|
||||
report_text += f"Volume: {f.get('res_vol')} m3 | Peso: {f.get('res_peso')} kg\n"
|
||||
report_text += f"Sacchetti totali: {f.get('res_tot_s')} | Costo: {f.get('res_costo_t')} EUR\n\n"
|
||||
report_text += f"DOSAGGIO PER {f.get('res_nc')} CARICHI:\n"
|
||||
report_text += f"- Cemento: {f.get('res_bc_cem')} sacch.\n- Sabbia: {f.get('res_bc_sab')} sacch.\n"
|
||||
report_text += f"- Ghiaia: {f.get('res_bc_ghi')} sacch.\n- Acqua: {f.get('res_bc_aq')} L"
|
||||
|
||||
temp_file = "/tmp/stampa_cemento.txt"
|
||||
try:
|
||||
with open(temp_file, "w", encoding="utf-8") as tmp:
|
||||
tmp.write(report_text)
|
||||
|
||||
# Comando di stampa lp (standard Linux su Synology)
|
||||
subprocess.run(["lp", "-h", CUPS_SERVER, "-d", PRINTER_NAME, temp_file], check=True)
|
||||
|
||||
time.sleep(2) # Attesa per scrittura file nel container
|
||||
return f"OK - Inviato a Virtual_PDF. Verifica in: {OUTPUT_DIR}"
|
||||
except Exception as e:
|
||||
return f"Errore: {str(e)}"
|
||||
finally:
|
||||
if os.path.exists(temp_file): os.remove(temp_file)
|
||||
|
||||
@app.route('/download', methods=['POST'])
|
||||
def download():
|
||||
f = request.form
|
||||
report = f"""-------------------------------------------
|
||||
REPORT TECNICO CALCOLO CEMENTO
|
||||
-------------------------------------------
|
||||
|
||||
PARAMETRI DI INPUT:
|
||||
- Dimensioni: {f.get('a')} {f.get('ua')} (Alt) x {f.get('l')} {f.get('ul')} (Lar) x {f.get('p')} {f.get('up')} (Pro)
|
||||
- Miscela (Parti): Cem {f.get('p_cem')} | Sab {f.get('p_sab')} | Ghi {f.get('p_ghi')}
|
||||
- Peso Sacchi: Cem {f.get('w_cem')}kg | Sab {f.get('w_sab')}kg | Ghi {f.get('w_ghi')}kg
|
||||
- Costo Sacchi: Cem {f.get('c_cem')}€ | Sab {f.get('c_sab')}€ | Ghi {f.get('c_ghi')}€
|
||||
- Attrezzatura: Betoniera da {f.get('v_bet')} L
|
||||
|
||||
RISULTATI GENERALI:
|
||||
- Volume Totale: {f.get('res_vol')} m3
|
||||
- Peso Totale: {f.get('res_peso')} Kg
|
||||
- Acqua Totale: {f.get('res_aq')} L
|
||||
- Totale Sacchetti da acquistare: {f.get('res_tot_s')}
|
||||
- COSTO COMPLESSIVO: {f.get('res_costo_t')} €
|
||||
|
||||
DETTAGLIO ACQUISTI:
|
||||
- Cemento: {f.get('res_s_cem')} sacchetti (€ {f.get('res_c_cem')})
|
||||
- Sabbia: {f.get('res_s_sab')} sacchetti (€ {f.get('res_c_sab')})
|
||||
- Ghiaia: {f.get('res_s_ghi')} sacchetti (€ {f.get('res_c_ghi')})
|
||||
|
||||
DOSAGGIO PER SINGOLO CARICO ({f.get('res_nc')} carichi):
|
||||
- Cemento: {f.get('res_bc_cem')} sacchetti
|
||||
- Sabbia: {f.get('res_bc_sab')} sacchetti
|
||||
- Ghiaia: {f.get('res_bc_ghi')} sacchetti
|
||||
- Acqua: {f.get('res_bc_aq')} L
|
||||
|
||||
-------------------------------------------
|
||||
Generato da Software Cemento {VERSIONE}
|
||||
-------------------------------------------"""
|
||||
return Response(report, mimetype="text/plain", headers={"Content-disposition":"attachment;filename=Report_Dettagliato.txt"})
|
||||
report = f"REPORT TECNICO - Volume: {f.get('res_vol')} m3 - Totale: {f.get('res_tot_s')} sacchetti"
|
||||
return Response(report, mimetype="text/plain", headers={"Content-disposition":"attachment;filename=Report.txt"})
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Avvio del server sulla porta 5000
|
||||
app.run(host='0.0.0.0', port=5000)
|
||||
Reference in New Issue
Block a user