Update cemento.py
This commit is contained in:
100
cemento.py
100
cemento.py
@@ -2,83 +2,81 @@
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import math
|
import math
|
||||||
import socket # Usiamo i socket per evitare l'errore "lp not found"
|
import socket
|
||||||
import time
|
import time
|
||||||
from flask import Flask, render_template, request, Response
|
from flask import Flask, render_template, request, Response
|
||||||
|
|
||||||
|
# Percorso per le librerie installate sul NAS Synology
|
||||||
sys.path.append('/var/services/homes/Francesco/.local/lib/python3.8/site-packages')
|
sys.path.append('/var/services/homes/Francesco/.local/lib/python3.8/site-packages')
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
# Configurazione CUPS
|
# CONFIGURAZIONE STAMPA CUPS
|
||||||
CUPS_IP = "192.168.1.14"
|
CUPS_IP = "192.168.1.14"
|
||||||
CUPS_PORT = 631 # Porta standard IPP/CUPS
|
CUPS_PORT = 6310
|
||||||
|
OUTPUT_DIR = "/volume1/docker/cups-pdf/output"
|
||||||
|
|
||||||
|
def calcola_dati(d):
|
||||||
|
try:
|
||||||
|
def to_m(v, u):
|
||||||
|
val = float(v.replace(',', '.')) if v else 0
|
||||||
|
if u == "mm": return val / 1000
|
||||||
|
if u == "cm": return val / 100
|
||||||
|
return val
|
||||||
|
|
||||||
|
# Calcolo Volume base
|
||||||
|
vol = to_m(d.get('l','0'), d.get('ul','m')) * to_m(d.get('p','0'), d.get('up','m')) * to_m(d.get('a','0'), d.get('ua','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}
|
||||||
|
|
||||||
|
somma_p = sum(p.values())
|
||||||
|
res = {"vol": f"{vol:.3f}", "peso": f"{peso_tot:.0f}"}
|
||||||
|
|
||||||
|
tot_s = 0
|
||||||
|
for m in mats:
|
||||||
|
# Correzione BUG Ghiaia a 0
|
||||||
|
if p[m] <= 0:
|
||||||
|
res[f's_{m}'] = 0
|
||||||
|
continue
|
||||||
|
kg = (p[m]/somma_p)*peso_tot if somma_p > 0 else 0
|
||||||
|
res[f's_{m}'] = math.ceil(kg/w[m])
|
||||||
|
tot_s += res[f's_{m}']
|
||||||
|
|
||||||
|
res["tot_sacc"] = tot_s
|
||||||
|
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':
|
||||||
try:
|
res = calcola_dati(request.form)
|
||||||
d = request.form
|
|
||||||
def to_m(v, u):
|
|
||||||
val = float(v.replace(',', '.')) if v else 0
|
|
||||||
if u == "mm": return val / 1000
|
|
||||||
if u == "cm": return val / 100
|
|
||||||
return 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:
|
|
||||||
# Correzione bug per valori a zero
|
|
||||||
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)
|
|
||||||
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:
|
|
||||||
if p[m] <= 0: res[f'bc_{m}'] = "0.00"
|
|
||||||
else: res[f'bc_{m}'] = f"{((p[m]/somma_p)*peso_tot/w[m])/n_c:.2f}"
|
|
||||||
res["bc_aq"] = f"{(vol*150)/n_c:.2f}"
|
|
||||||
except: res = None
|
|
||||||
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():
|
||||||
f = request.form
|
f = request.form
|
||||||
testo = f"REPORT CALCOLO\nVolume: {f.get('res_vol')} m3\nSacchetti: {f.get('res_tot_s')}\nCosto: {f.get('res_costo_t')} EUR"
|
# Creazione stringa semplice per il report
|
||||||
|
testo = f"REPORT CALCOLO CEMENTO\n"
|
||||||
|
testo += f"Volume: {f.get('res_vol')} m3\n"
|
||||||
|
testo += f"Totale Sacchetti: {f.get('res_tot_s')}\n"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Invio diretto al socket del container per evitare l'errore 'lp'
|
# Invio diretto alla porta 6310
|
||||||
with socket.create_connection((CUPS_IP, CUPS_PORT), timeout=5) as s:
|
with socket.create_connection((CUPS_IP, CUPS_PORT), timeout=5) as s:
|
||||||
s.sendall(testo.encode('utf-8'))
|
s.sendall(testo.encode('utf-8'))
|
||||||
return "OK - Inviato a Virtual_PDF"
|
return "OK - Report inviato a Virtual_PDF"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return f"Errore di rete CUPS: {str(e)}"
|
return f"Errore connessione porta 6310: {str(e)}"
|
||||||
|
|
||||||
@app.route('/download', methods=['POST'])
|
@app.route('/download', methods=['POST'])
|
||||||
def download():
|
def download():
|
||||||
f = request.form
|
f = request.form
|
||||||
report = f"REPORT TECNICO\nVolume: {f.get('res_vol')} m3\nTotale: {f.get('res_tot_s')} sacchetti"
|
report = f"REPORT TECNICO\nVolume: {f.get('res_vol')} m3\nSacchetti: {f.get('res_tot_s')}"
|
||||||
return Response(report, mimetype="text/plain", headers={"Content-disposition":"attachment;filename=Report.txt"})
|
return Response(report, mimetype="text/plain", headers={"Content-disposition":"attachment;filename=Report.txt"})
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user