Update cemento.py
This commit is contained in:
63
cemento.py
63
cemento.py
@@ -1,15 +1,17 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
import os
|
||||
import math
|
||||
import socket
|
||||
from flask import Flask, render_template, request
|
||||
|
||||
# Percorso librerie locali Synology
|
||||
# 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__)
|
||||
|
||||
# Configurazione stampante Docker
|
||||
# --- CONFIGURAZIONE STAMPANTE CUPS DOCKER ---
|
||||
# IP locale del NAS e porta mappata nel Container Manager
|
||||
CUPS_IP = "192.168.1.14"
|
||||
CUPS_PORT = 6310
|
||||
|
||||
@@ -19,29 +21,66 @@ def index():
|
||||
if request.method == 'POST':
|
||||
try:
|
||||
d = request.form
|
||||
|
||||
# Funzione per convertire le unità di misura in metri
|
||||
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
|
||||
|
||||
vol = to_m(d.get('l','1'), d.get('ul','m')) * to_m(d.get('p','1'), d.get('up','m')) * to_m(d.get('a','1'), d.get('ua','m'))
|
||||
# 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
|
||||
|
||||
# Logica calcolo materiali (V1.12)
|
||||
res = {"vol": f"{vol:.3f}", "peso": f"{peso_tot:.0f}", "tot_sacc": 3, "s_cem": 1, "s_sab": 1, "s_ghi": 1}
|
||||
except: res = None
|
||||
# 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)
|
||||
|
||||
@app.route('/stampa_pdf', methods=['POST'])
|
||||
def stampa_pdf():
|
||||
"""Invia i dati al container cups-pdf-server via socket"""
|
||||
f = request.form
|
||||
report = f"--- REPORT CEMENTO ---\nVol: {f.get('v')} m3\nTotale: {f.get('ts')} sacchetti"
|
||||
# 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:
|
||||
# Invio diretto via socket alla porta 6310
|
||||
# Apertura connessione verso il container Docker
|
||||
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"
|
||||
s.sendall(report_text.encode('utf-8'))
|
||||
return "OK - Report inviato alla stampante. Controlla la cartella /docker/cups-pdf/output"
|
||||
except Exception as e:
|
||||
return f"Errore di rete CUPS: {str(e)}"
|
||||
# Gestione errore di connessione (es. container spento)
|
||||
return f"Errore di connessione alla stampante (Porta {CUPS_PORT}): {str(e)}"
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Configurazione per l'ascolto su tutte le interfacce per Web Station
|
||||
app.run(host='0.0.0.0', port=5000)
|
||||
Reference in New Issue
Block a user