Update cemento.py
This commit is contained in:
60
cemento.py
60
cemento.py
@@ -1,83 +1,61 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
import os
|
||||
import math
|
||||
import socket
|
||||
import time
|
||||
import math
|
||||
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')
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# CONFIGURAZIONE STAMPA CUPS
|
||||
# Configurazione CUPS-PDF
|
||||
CUPS_IP = "192.168.1.14"
|
||||
CUPS_PORT = 6310
|
||||
OUTPUT_DIR = "/volume1/docker/cups-pdf/output"
|
||||
|
||||
def calcola_dati(d):
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
def index():
|
||||
res = None
|
||||
if request.method == 'POST':
|
||||
try:
|
||||
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
|
||||
return val / 1000 if u == "mm" else (val / 100 if u == "cm" else 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:
|
||||
if p[m] <= 0: # Correzione bug ghiaia a 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}']
|
||||
|
||||
s_calc = math.ceil(((p[m]/somma_p)*peso_tot)/w[m]) if somma_p > 0 else 0
|
||||
res[f's_{m}'] = s_calc
|
||||
tot_s += s_calc
|
||||
res["tot_sacc"] = tot_s
|
||||
return res
|
||||
except:
|
||||
return None
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
def index():
|
||||
res = None
|
||||
if request.method == 'POST':
|
||||
res = calcola_dati(request.form)
|
||||
except: res = None
|
||||
return render_template('index.html', res=res, form=request.form)
|
||||
|
||||
@app.route('/stampa_pdf', methods=['POST'])
|
||||
def stampa_pdf():
|
||||
f = request.form
|
||||
# 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"
|
||||
# Intestazione formattata per CUPS-PDF
|
||||
report = "--- CALCOLO CEMENTO V1.1 ---\n"
|
||||
report += f"Volume: {f.get('v')} m3 | Peso: {f.get('p')} kg\n"
|
||||
report += f"Totale Sacchetti: {f.get('ts')}\n"
|
||||
|
||||
try:
|
||||
# Invio diretto alla porta 6310
|
||||
with socket.create_connection((CUPS_IP, CUPS_PORT), timeout=5) as s:
|
||||
s.sendall(testo.encode('utf-8'))
|
||||
s.sendall(report.encode('utf-8'))
|
||||
return "OK - Report inviato a Virtual_PDF"
|
||||
except Exception as e:
|
||||
return f"Errore connessione porta 6310: {str(e)}"
|
||||
|
||||
@app.route('/download', methods=['POST'])
|
||||
def download():
|
||||
f = request.form
|
||||
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 f"Errore: {str(e)}"
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5000)
|
||||
Reference in New Issue
Block a user