Update cemento.py
This commit is contained in:
101
cemento.py
101
cemento.py
@@ -1,86 +1,53 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import sys
|
import subprocess
|
||||||
import os
|
|
||||||
import math
|
|
||||||
import socket
|
|
||||||
from flask import Flask, render_template, request
|
from flask import Flask, render_template, request
|
||||||
|
|
||||||
# Aggiunta del percorso per le librerie installate localmente dall'utente Francesco
|
|
||||||
sys.path.append('/var/services/homes/Francesco/.local/lib/python3.8/site-packages')
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
# --- CONFIGURAZIONE STAMPANTE CUPS DOCKER ---
|
|
||||||
# IP locale del NAS e porta mappata nel Container Manager
|
|
||||||
CUPS_IP = "192.168.1.14"
|
|
||||||
CUPS_PORT = 6310
|
|
||||||
|
|
||||||
@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':
|
||||||
try:
|
try:
|
||||||
d = request.form
|
# Recupero dati dal form e calcolo
|
||||||
|
v = request.form.get('v', '0')
|
||||||
# Funzione per convertire le unità di misura in metri
|
ps = request.form.get('ps', '0')
|
||||||
def to_m(v, u):
|
ts = request.form.get('ts', '0')
|
||||||
val = float(v.replace(',', '.')) if v else 0
|
res = {"vol": v, "peso": ps, "tot_sacc": ts}
|
||||||
if u == "mm": return val / 1000
|
except:
|
||||||
if u == "cm": return val / 100
|
res = None
|
||||||
return val
|
|
||||||
|
|
||||||
# Calcolo del Volume e del Peso (densità media 2400kg/m3)
|
|
||||||
l = to_m(d.get('l', '0'), d.get('ul', 'm'))
|
|
||||||
p = to_m(d.get('p', '0'), d.get('up', 'm'))
|
|
||||||
a = to_m(d.get('a', '0'), d.get('ua', 'm'))
|
|
||||||
vol = l * p * a
|
|
||||||
peso_tot = vol * 2400
|
|
||||||
|
|
||||||
# Proporzioni materiali
|
|
||||||
mats = ['cem', 'sab', 'ghi']
|
|
||||||
p_mats = {m: float(d.get(f'p_{m}', '0').replace(',', '.')) for m in mats}
|
|
||||||
w_mats = {m: float(d.get(f'w_{m}', '25').replace(',', '.')) for m in mats}
|
|
||||||
|
|
||||||
somma_p = sum(p_mats.values())
|
|
||||||
res = {"vol": f"{vol:.3f}", "peso": f"{peso_tot:.0f}"}
|
|
||||||
|
|
||||||
tot_s = 0
|
|
||||||
if somma_p > 0:
|
|
||||||
for m in mats:
|
|
||||||
s_calc = math.ceil(((p_mats[m] / somma_p) * peso_tot) / w_mats[m])
|
|
||||||
res[f's_{m}'] = s_calc
|
|
||||||
tot_s += s_calc
|
|
||||||
res["tot_sacc"] = tot_s
|
|
||||||
|
|
||||||
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():
|
||||||
"""Invia i dati al container cups-pdf-server via socket"""
|
|
||||||
f = request.form
|
|
||||||
# Costruzione del testo del report
|
|
||||||
report_text = (
|
|
||||||
"--- REPORT CALCOLO CALCESTRUZZO ---\n"
|
|
||||||
f"Volume Totale: {f.get('v')} m3\n"
|
|
||||||
f"Peso Stimato: {f.get('ps')} kg\n"
|
|
||||||
"-----------------------------------\n"
|
|
||||||
f"Sacchetti Totali: {f.get('ts')}\n"
|
|
||||||
f"Note: Generato da {request.host}\n"
|
|
||||||
"\x0c" # Carattere 'Form Feed' per forzare la chiusura del PDF in CUPS
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Apertura connessione verso il container Docker
|
v = request.form.get('v', '0')
|
||||||
with socket.create_connection((CUPS_IP, CUPS_PORT), timeout=5) as s:
|
ps = request.form.get('ps', '0')
|
||||||
s.sendall(report_text.encode('utf-8'))
|
ts = request.form.get('ts', '0')
|
||||||
return "OK - Report inviato alla stampante. Controlla la cartella /docker/cups-pdf/output"
|
|
||||||
|
# Formattazione PostScript per garantire che il PDF non sia vuoto
|
||||||
|
ps_content = f"""%!PS
|
||||||
|
/Helvetica findfont 16 scalefont setfont
|
||||||
|
100 750 moveto (REPORT CALCOLO CEMENTO) show
|
||||||
|
/Helvetica findfont 12 scalefont setfont
|
||||||
|
100 720 moveto (Volume totale: {v} m3) show
|
||||||
|
100 700 moveto (Peso stimato: {ps} kg) show
|
||||||
|
100 680 moveto (Totale sacchetti: {ts}) show
|
||||||
|
100 650 moveto (Data: 20/01/2026) show
|
||||||
|
showpage"""
|
||||||
|
|
||||||
|
# Esecuzione tramite docker exec (metodo testato con successo via root)
|
||||||
|
comando = ["docker", "exec", "-i", "cups-pdf-server", "lp", "-d", "Virtual_PDF"]
|
||||||
|
processo = subprocess.run(comando, input=ps_content.encode('utf-8'), capture_output=True)
|
||||||
|
|
||||||
|
if processo.returncode == 0:
|
||||||
|
return "OK - PDF creato con successo!"
|
||||||
|
else:
|
||||||
|
return f"Errore container: {processo.stderr.decode()}"
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Gestione errore di connessione (es. container spento)
|
return f"Errore script: {str(e)}"
|
||||||
return f"Errore di connessione alla stampante (Porta {CUPS_PORT}): {str(e)}"
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Configurazione per l'ascolto su tutte le interfacce per Web Station
|
# Porta 5000 per il tuo 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