1129 lines
41 KiB
C#
1129 lines
41 KiB
C#
using Newtonsoft.Json;
|
|
using SteamWare;
|
|
using SteamWare.IO;
|
|
using SteamWare.Reports;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using static LPA.Enums;
|
|
|
|
namespace LPA
|
|
{
|
|
public partial class MainForm : Form
|
|
{
|
|
#region variabili di base
|
|
|
|
/// <summary>
|
|
/// Lista delle code di stampa da gestire
|
|
/// </summary>
|
|
protected List<string> pqList;
|
|
/// <summary>
|
|
/// Indirizzo base del server x test ping
|
|
/// </summary>
|
|
protected string serverIp;
|
|
/// <summary>
|
|
/// Indirizzo base del server cui connettersi
|
|
/// </summary>
|
|
protected string serverBaseAddr;
|
|
/// <summary>
|
|
/// Path salvataggio locale reports
|
|
/// </summary>
|
|
protected string localReportPath;
|
|
/// <summary>
|
|
/// Status runing della coda di stampa
|
|
/// </summary>
|
|
protected bool spoolerRun = false;
|
|
/// <summary>
|
|
/// Directory di abse applicazione
|
|
/// </summary>
|
|
protected string baseDir;
|
|
/// <summary>
|
|
/// File conf x setup code
|
|
/// </summary>
|
|
protected string qSetFilePath
|
|
{
|
|
get
|
|
{
|
|
string answ = demoMode ? "queueSetupDemo.json" : "queueSetup.json";
|
|
return answ;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// File conf server
|
|
/// </summary>
|
|
protected string srvSetFilePath = "serverSetup.json";
|
|
/// <summary>
|
|
/// Configurazione CORRENTE delle code di stampa (coda --> printer disponibile)
|
|
/// </summary>
|
|
protected Dictionary<string, printQueue> queueSetupConf;
|
|
/// <summary>
|
|
/// Binding source degli elementi di print queue gestiti..
|
|
/// </summary>
|
|
private BindingSource pqBindSource = new BindingSource();
|
|
/// <summary>
|
|
/// Uptime globale applicazione
|
|
/// </summary>
|
|
protected DateTime appStart = DateTime.Now;
|
|
/// <summary>
|
|
/// Code di stampa disponibili (da server)
|
|
/// </summary>
|
|
protected List<queueConf> listQueue = new List<queueConf>();
|
|
/// <summary>
|
|
/// Elenco code gestite come array
|
|
/// </summary>
|
|
protected string listManQueue = "";
|
|
/// <summary>
|
|
/// Variabile appoggio semaforo divieto print x evitare accodamenti... 15 sec da last check...
|
|
/// </summary>
|
|
protected static DateTime vetoPrint;
|
|
/// <summary>
|
|
/// Modalità demo/test printing locale
|
|
/// </summary>
|
|
protected bool demoMode = false;
|
|
/// <summary>
|
|
/// Variabile di stato fase startup x resettare valore code
|
|
/// </summary>
|
|
protected bool StartupMode = true;
|
|
/// <summary>
|
|
/// Modalità di avvio
|
|
/// </summary>
|
|
protected StartMode ModoAvvio = StartMode.STD;
|
|
/// <summary>
|
|
/// VAriabile ausiliaria x forzare realmente chiusura
|
|
/// </summary>
|
|
protected bool forceClose = false;
|
|
|
|
#endregion
|
|
|
|
#region init form + dispose
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Classe apertura
|
|
/// </summary>
|
|
public MainForm()
|
|
{
|
|
InitializeComponent();
|
|
myInit();
|
|
checkShowTray();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Medoti init specifici
|
|
/// </summary>
|
|
private void myInit()
|
|
{
|
|
utils.lgInfo("Starting app");
|
|
lblAppName.Text = memLayer.ML.CRS("appName");
|
|
lblVers.Text = $" v.{System.Reflection.Assembly.GetExecutingAssembly().GetName().Version}";
|
|
spoolerRun = true;
|
|
createTrayMenu();
|
|
setupConf();
|
|
setupTimers();
|
|
setupQueuePrinter();
|
|
setupButtons();
|
|
showPrinterList();
|
|
setupPrintersList();
|
|
refreshLocaltemplate();
|
|
refreshQueueAvail();
|
|
setupDGV();
|
|
}
|
|
/// <summary>
|
|
/// Verifica modalità SHOW dati
|
|
/// </summary>
|
|
private void checkShowTray()
|
|
{
|
|
if (memLayer.ML.CRB("startOnTray"))
|
|
{
|
|
|
|
WindowState = FormWindowState.Minimized;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Setup parametri base di configurazione
|
|
/// </summary>
|
|
private void setupConf()
|
|
{
|
|
baseDir = AppDomain.CurrentDomain.BaseDirectory;
|
|
// cerco se ho conf da json
|
|
if (!string.IsNullOrEmpty(srvSetupJson.serverIp))
|
|
{
|
|
serverBaseAddr = srvSetupJson.serverBaseAddr;
|
|
serverIp = srvSetupJson.serverIp;
|
|
}
|
|
// altrimenti da app.config
|
|
else
|
|
{
|
|
serverBaseAddr = memLayer.ML.CRS("serverBaseAddr");
|
|
serverIp = memLayer.ML.CRS("serverIp");
|
|
// salvo...
|
|
srvSetupJson = new serverSetup() { serverBaseAddr = serverBaseAddr, serverIp = serverIp };
|
|
}
|
|
localReportPath = $"{AppDomain.CurrentDomain.BaseDirectory}{memLayer.ML.CRS("localReportPath")}\\";
|
|
vetoPrint = DateTime.Now;
|
|
}
|
|
/// <summary>
|
|
/// Fix timers
|
|
/// </summary>
|
|
private void setupTimers()
|
|
{
|
|
uiTimer.Interval = 50;
|
|
mainTimer.Interval = 900;
|
|
longTimer.Interval = 6000;
|
|
uiTimer.Tick += UiTimer_Tick;
|
|
mainTimer.Tick += MainTimer_Tick;
|
|
longTimer.Tick += LongTimer_Tick;
|
|
uiTimer.Start();
|
|
mainTimer.Start();
|
|
longTimer.Start();
|
|
}
|
|
/// <summary>
|
|
/// Setup code di stampa disponibili da server
|
|
/// </summary>
|
|
private void setupQueuePrinter()
|
|
{
|
|
queueSetupConf = queueSetupJson;
|
|
// cambio data/ora stared di ogni elemento...
|
|
foreach (var item in queueSetupConf)
|
|
{
|
|
item.Value.startTime = DateTime.Now;
|
|
}
|
|
// salvo valore aggiornato...
|
|
queueSetupJson = queueSetupConf;
|
|
}
|
|
/// <summary>
|
|
/// Setup dei bottoni stato start/stop
|
|
/// </summary>
|
|
private void setupButtons()
|
|
{
|
|
btnStart.Enabled = !spoolerRun;
|
|
btnStop.Enabled = spoolerRun;
|
|
lblStatus.ForeColor = spoolerRun ? Color.Green : Color.OrangeRed;
|
|
lblStatus.Text = spoolerRun ? "Running" : "Stopped";
|
|
// se demo aggiungo...
|
|
if (demoMode)
|
|
{
|
|
lblStatus.Text += " TEST/DEMO MODE";
|
|
}
|
|
btnDemoPrint.Visible = demoMode;
|
|
chkTryMargin.Visible = demoMode;
|
|
}
|
|
/// <summary>
|
|
/// Setup oggetto DGV
|
|
/// </summary>
|
|
private void setupDGV()
|
|
{
|
|
// gestione eventi binding source
|
|
pqBindSource.AddingNew += qpBSList_AddingNew;
|
|
pqBindSource.ListChanged += qpBSList_ListChanged;
|
|
}
|
|
/// <summary>
|
|
/// Setup elenco stampanti da disponibili su PC
|
|
/// </summary>
|
|
private void setupPrintersList()
|
|
{
|
|
lbPrinters.Items.Clear();
|
|
foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
|
|
{
|
|
lbPrinters.Items.Add(printer);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Pulizia finale delle risorse in uso.
|
|
/// </summary>
|
|
/// <param name="disposing">ha valore true se le risorse gestite devono essere eliminate, false in caso contrario.</param>
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
if (components != null)
|
|
{
|
|
components.Dispose();
|
|
}
|
|
|
|
// Dispose stuff here
|
|
pqBindSource.Dispose();
|
|
}
|
|
|
|
base.Dispose(disposing);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region gestione tray
|
|
|
|
/// <summary>
|
|
/// crea menù tray x applicazione
|
|
/// </summary>
|
|
private void createTrayMenu()
|
|
{
|
|
|
|
// fix icon!
|
|
notifyIcon1.Text = string.Format("Steamware LPA | {0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
|
|
// Fix testi menù tray...
|
|
trayMenu.Items.Clear();
|
|
// SE permessa massimizzazione...
|
|
if (memLayer.ML.CRB("windowCanMax"))
|
|
{
|
|
trayMenu.Items.Add("Show LPA");
|
|
}
|
|
// se è permesso tray close...
|
|
if (memLayer.ML.CRB("trayClose"))
|
|
{
|
|
trayMenu.Items.Add("Close LPA");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// doppio click su tray icon
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
|
|
{
|
|
// SOLO SE PERMESSO mostrare full...
|
|
if (memLayer.ML.CRB("windowCanMax"))
|
|
{
|
|
Show();
|
|
WindowState = FormWindowState.Normal;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// evento resize
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void MainForm_Resize(object sender, EventArgs e)
|
|
{
|
|
checkFormVisibility();
|
|
}
|
|
/// <summary>
|
|
/// Verifica stato windows (minimized/normal) e visibilità con tray...
|
|
/// </summary>
|
|
private void checkFormVisibility()
|
|
{
|
|
// se non può massimizzare imposto COMUNQUE a minimized...
|
|
if (!memLayer.ML.CRB("windowCanMax"))
|
|
{
|
|
WindowState = FormWindowState.Minimized;
|
|
}
|
|
// SOLO SE se sono in modo STD
|
|
if (ModoAvvio == StartMode.STD)
|
|
{
|
|
// controllo cosa devo mostrare...
|
|
if (WindowState == FormWindowState.Minimized)
|
|
{
|
|
notifyIcon1.Visible = false;
|
|
sendToTray();
|
|
}
|
|
else
|
|
{
|
|
notifyIcon1.Visible = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
notifyIcon1.Visible = false;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Gestisce "andata nel tray" della form
|
|
/// </summary>
|
|
private void sendToTray()
|
|
{
|
|
if (!notifyIcon1.Visible)
|
|
{
|
|
notifyIcon1.BalloonTipTitle = memLayer.ML.CRS("appName");
|
|
notifyIcon1.BalloonTipText = $"{memLayer.ML.CRS("appName")} running on tray";
|
|
notifyIcon1.Visible = true;
|
|
notifyIcon1.ShowBalloonTip(100);
|
|
}
|
|
Hide();
|
|
}
|
|
/// <summary>
|
|
/// click su menù contestuale in tray
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void trayMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
|
|
{
|
|
if (e.ClickedItem.Text.StartsWith("Close"))
|
|
{
|
|
// salvo che voglio chiudere davvero
|
|
forceClose = true;
|
|
// chiudo tray
|
|
trayMenu.Close();
|
|
// chiudo programma!
|
|
Close();
|
|
}
|
|
else if (e.ClickedItem.Text.StartsWith("Show"))
|
|
{
|
|
if (memLayer.ML.CRB("windowCanMax"))
|
|
{
|
|
Show();
|
|
WindowState = FormWindowState.Normal;
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region metodi interni ed helpers
|
|
|
|
/// <summary>
|
|
/// Chiamata async URL
|
|
/// </summary>
|
|
/// <param name="url2call"></param>
|
|
/// <returns></returns>
|
|
protected string callUrl(string url2call)
|
|
{
|
|
string result = "";
|
|
// recupero dati
|
|
var taskRes = Task.Run(() => utils.getPageAsync(url2call));
|
|
taskRes.Wait();
|
|
result = taskRes.Result;
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// Chiamata PUT async URL
|
|
/// </summary>
|
|
/// <param name="url2call"></param>
|
|
/// <param name="payload"></param>
|
|
/// <returns></returns>
|
|
protected string postUrl(string url2call, string payload)
|
|
{
|
|
string result = "";
|
|
// fix contenuto
|
|
Uri callUri = new Uri(url2call);
|
|
HttpContent callCont = new StringContent(payload, Encoding.UTF8, "application/json");
|
|
// chiamo!
|
|
var taskRes = Task.Run(() => utils.postAsync(callUri, callCont));
|
|
taskRes.Wait();
|
|
result = taskRes.Result;
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// Configurazione coda stampa da json file
|
|
/// </summary>
|
|
protected Dictionary<string, printQueue> queueSetupJson
|
|
{
|
|
get
|
|
{
|
|
Dictionary<string, printQueue> answ = new Dictionary<string, printQueue>();
|
|
string fullPath = $"{baseDir}conf\\{qSetFilePath}";
|
|
// cerco file x leggere conf...
|
|
if (File.Exists(fullPath))
|
|
{
|
|
try
|
|
{
|
|
string rawData = File.ReadAllText(fullPath);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
answ = JsonConvert.DeserializeObject<Dictionary<string, printQueue>>(rawData);
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
utils.lgError("Error in deserialization of queueSetupJson file", exc);
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
set
|
|
{
|
|
string fullPath = $"{baseDir}conf\\{qSetFilePath}";
|
|
string rawData = JsonConvert.SerializeObject(value, Formatting.Indented);
|
|
File.WriteAllText(fullPath, rawData);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Configurazione server da json file
|
|
/// </summary>
|
|
protected serverSetup srvSetupJson
|
|
{
|
|
get
|
|
{
|
|
serverSetup answ = new serverSetup();
|
|
string fullPath = $"{baseDir}conf\\{srvSetFilePath}";
|
|
// cerco file x leggere conf...
|
|
if (File.Exists(fullPath))
|
|
{
|
|
try
|
|
{
|
|
string rawData = File.ReadAllText(fullPath);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
answ = JsonConvert.DeserializeObject<serverSetup>(rawData);
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
utils.lgError("Error in deserialization of srvSetupJson file", exc);
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
set
|
|
{
|
|
string fullPath = $"{baseDir}conf\\{srvSetFilePath}";
|
|
string rawData = JsonConvert.SerializeObject(value, Formatting.Indented);
|
|
File.WriteAllText(fullPath, rawData);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Evento cambio
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void qpBSList_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
|
|
{
|
|
updateDGV();
|
|
}
|
|
/// <summary>
|
|
/// Evento aggiunta
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void qpBSList_AddingNew(object sender, System.ComponentModel.AddingNewEventArgs e)
|
|
{
|
|
updateDGV();
|
|
}
|
|
/// <summary>
|
|
/// aggiorna elemento DGV
|
|
/// </summary>
|
|
private void updateDGV()
|
|
{
|
|
// verifica preliminare se in startup...
|
|
if (StartupMode)
|
|
{
|
|
// imposto data avvio delle code...
|
|
foreach (var item in queueSetupConf)
|
|
{
|
|
item.Value.isRunning = true;
|
|
}
|
|
StartupMode = false;
|
|
}
|
|
// gestione effettiva
|
|
List<printQueue> elencoCode = new List<printQueue>();
|
|
foreach (var item in queueSetupConf)
|
|
{
|
|
elencoCode.Add(item.Value);
|
|
}
|
|
dgvQueueStatus.DataSource = elencoCode;
|
|
dgvQueueStatus.Invalidate();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region gestione cicli di esecuzione
|
|
|
|
/// <summary>
|
|
/// Timer principale di refresh
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void MainTimer_Tick(object sender, EventArgs e)
|
|
{
|
|
// effettuo chiamata coda stampa...
|
|
refreshQueueStatus();
|
|
// aggiorno visualizzazione
|
|
updateDGV();
|
|
}
|
|
/// <summary>
|
|
/// Timer lungo periodo (salvataggi)
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void LongTimer_Tick(object sender, EventArgs e)
|
|
{
|
|
// salvo conf ...
|
|
queueSetupJson = queueSetupConf;
|
|
// salvo 1/mese backup
|
|
string shortMonth = DateTime.Now.ToString("yyyy-MM");
|
|
string bckDir = $"{baseDir}conf\\bck\\{shortMonth}";
|
|
string fullPath = $"{bckDir}\\{qSetFilePath}";
|
|
// cerco file x leggere conf...
|
|
if (!File.Exists(fullPath))
|
|
{
|
|
Directory.CreateDirectory(bckDir);
|
|
string rawData = JsonConvert.SerializeObject(queueSetupConf);
|
|
File.WriteAllText(fullPath, rawData);
|
|
}
|
|
// pulizia files da area temp...
|
|
reportPrinter rPrint = new reportPrinter();
|
|
rPrint.pulisciDir();
|
|
}
|
|
/// <summary>
|
|
/// Rrefresh interfaccia
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void UiTimer_Tick(object sender, EventArgs e)
|
|
{
|
|
progBar.Increment(1);
|
|
if (progBar.Value >= 100)
|
|
{
|
|
progBar.Value = 0;
|
|
}
|
|
// aggiorno uptime...
|
|
DateTime adesso = DateTime.Now;
|
|
TimeSpan uptime = adesso.Subtract(appStart);
|
|
lblUptime.Text = $"Uptime: {utils.FormatTimeSpan(uptime, true)}";
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region interazione (buttons & co)
|
|
|
|
/// <summary>
|
|
/// Avvio processo gestione code
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void btnStart_Click(object sender, EventArgs e)
|
|
{
|
|
spoolerRun = true;
|
|
vetoPrint = DateTime.Now;
|
|
setupButtons();
|
|
// resetto data avvio delle code...
|
|
foreach (var item in queueSetupConf)
|
|
{
|
|
item.Value.startTime = DateTime.Now;
|
|
item.Value.isRunning = true;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// stop processo gestione code
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void btnStop_Click(object sender, EventArgs e)
|
|
{
|
|
spoolerRun = false;
|
|
setupButtons();
|
|
// resetto data avvio delle code...
|
|
foreach (var item in queueSetupConf)
|
|
{
|
|
item.Value.isRunning = false;
|
|
}
|
|
}
|
|
|
|
private void btnReloadPrinters_Click(object sender, EventArgs e)
|
|
{
|
|
setupPrintersList();
|
|
}
|
|
|
|
private void lbtgetPrintQueue_Click(object sender, EventArgs e)
|
|
{
|
|
// svuoto elenco code
|
|
listManQueue = "";
|
|
// continuo setup
|
|
refreshQueueAvail();
|
|
}
|
|
|
|
|
|
private void lbPrintQueue_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
showPrinterList();
|
|
}
|
|
/// <summary>
|
|
/// Sistema visibilità elenco printers
|
|
/// </summary>
|
|
private void showPrinterList()
|
|
{
|
|
bool doShow = lbPrintQueue.SelectedIndex >= 0;
|
|
lbPrinters.Visible = doShow;
|
|
btnSetQueuePrinter.Visible = false;
|
|
if (doShow)
|
|
{
|
|
setupPrintersList();
|
|
// provo a selezionare...
|
|
string currQueue = lbPrintQueue.SelectedItem.ToString();
|
|
if (queueSetupConf.ContainsKey(currQueue))
|
|
{
|
|
try
|
|
{
|
|
lbPrinters.SelectedItem = queueSetupConf[currQueue].Printer;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
utils.lgError($"Impossible to associate to queue {currQueue} requested printer: {queueSetupConf[currQueue]}", exc);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnSetQueuePrinter_Click(object sender, EventArgs e)
|
|
{
|
|
// svuoto elenco code
|
|
listManQueue = "";
|
|
// continuo setup
|
|
string currQueue = lbPrintQueue.SelectedItem.ToString();
|
|
string currPrinter = lbPrinters.SelectedItem.ToString();
|
|
// salvo associazione coda/printer (SE non ci fosse)
|
|
if (queueSetupConf.ContainsKey(currQueue))
|
|
{
|
|
queueSetupConf[currQueue].Printer = currPrinter;
|
|
queueSetupConf[currQueue].startTime = DateTime.Now;
|
|
}
|
|
else
|
|
{
|
|
printQueue newQueue = new printQueue()
|
|
{
|
|
QueueName = currQueue,
|
|
Printer = currPrinter,
|
|
isRunning = spoolerRun,
|
|
startTime = DateTime.Now
|
|
};
|
|
queueSetupConf.Add(currQueue, newQueue);
|
|
}
|
|
// salvo!
|
|
queueSetupJson = queueSetupConf;
|
|
btnSetQueuePrinter.Visible = false;
|
|
utils.lgInfo("Salvata nuova conf stampanti");
|
|
updateDGV();
|
|
}
|
|
|
|
private void lbPrinters_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
bool printerSel = lbPrinters.SelectedIndex >= 0;
|
|
if (printerSel)
|
|
{
|
|
string currQueue = lbPrintQueue.SelectedItem.ToString();
|
|
bool doShow = true;
|
|
if (queueSetupConf.ContainsKey(currQueue))
|
|
{
|
|
string currPrinter = lbPrinters.SelectedItem.ToString();
|
|
try
|
|
{
|
|
doShow = (queueSetupConf[currQueue].Printer != currPrinter);
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
btnSetQueuePrinter.Visible = doShow;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Testing print demo da files locali
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void btnDemoPrint_Click(object sender, EventArgs e)
|
|
{
|
|
string rawData = "";
|
|
string filePath = "";
|
|
// SE SONO in DEMO mode, leggo da file locali e chiamo procedura di stampa!
|
|
if (demoMode)
|
|
{
|
|
// verifico code gestite
|
|
foreach (var coda in queueSetupConf)
|
|
{
|
|
filePath = $"{baseDir}demo\\{coda.Key}.json";
|
|
rawData = File.ReadAllText(filePath);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
try
|
|
{
|
|
// deserializzo
|
|
List<reportData> report2print = JsonConvert.DeserializeObject<List<reportData>>(rawData);
|
|
if (report2print != null)
|
|
{
|
|
// aggiorno contatore...
|
|
coda.Value.NumWaiting = report2print.Count;
|
|
// verifico sia RUNNING...
|
|
if (coda.Value.isRunning)
|
|
{
|
|
// verifico non ci sia veto print..
|
|
DateTime adesso = DateTime.Now;
|
|
if (vetoPrint < adesso)
|
|
{
|
|
vetoPrint = adesso.AddSeconds(300);
|
|
foreach (var item in report2print)
|
|
{
|
|
reportPrinter rPrint = new reportPrinter();
|
|
bool fatto = false;
|
|
bool fattoPdf = false;
|
|
// se ho almeno 1 stampa --> lancio!
|
|
queueConf confCoda = listQueue.Find(conf => conf.name == coda.Key);
|
|
if (confCoda != null)
|
|
{
|
|
// se ho selezionato test margini --> 3 stampe con valori 0/5/10
|
|
if (chkTryMargin.Checked)
|
|
{
|
|
int numStep = 3;
|
|
string currMargin = "";
|
|
for (int i = 0; i < numStep; i++)
|
|
{
|
|
// cambio i valori dei parametri deviceInfo
|
|
currMargin = $"{i * 5}mm";
|
|
confCoda.deviceInfoParam.MarginBottom = currMargin;
|
|
confCoda.deviceInfoParam.MarginTop = currMargin;
|
|
confCoda.deviceInfoParam.MarginRight = currMargin;
|
|
confCoda.deviceInfoParam.MarginLeft = currMargin;
|
|
confCoda.deviceInfoParam.OutputFormat = confCoda.deviceInfoParam.OutputFormat.Replace("10mm", currMargin);
|
|
// stampo!
|
|
fatto = rPrint.printReport(confCoda.template, localReportPath, coda.Value.Printer, item.rdsData, confCoda.deviceInfoParam, true);
|
|
if (fatto)
|
|
{
|
|
coda.Value.NumWaiting--;
|
|
coda.Value.NumDone++;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
fatto = rPrint.printReport(confCoda.template, localReportPath, coda.Value.Printer, item.rdsData, confCoda.deviceInfoParam, true);
|
|
if (fatto)
|
|
{
|
|
coda.Value.NumWaiting--;
|
|
coda.Value.NumDone++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
vetoPrint = adesso;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
utils.lgError("Error in deserialization during btnDemoPrint_Click", exc);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void enableTestDemoModeToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
string message = demoMode ? "Confermi di voler abbandonare la modalità test/Demo?" : "Attenzione! sei sicuro di voler attivare la modalità Test/Demo? il sistema funzionerà in modalità offline e configurate le code di stampa effettuerà test di stampa locale";
|
|
string title = "Test / Demo mode";
|
|
DialogResult result = MessageBox.Show(message, title, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
|
|
if (result == DialogResult.Yes)
|
|
{
|
|
demoMode = !demoMode;
|
|
}
|
|
// ricarico dati code e conf...
|
|
|
|
// update buttons...
|
|
menuEnableDemo.Text = demoMode ? "&Exit Test Mode" : "&Enter Test Mode";
|
|
setupButtons();
|
|
// aggiorno code...
|
|
setupQueuePrinter();
|
|
refreshQueueAvail();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region metodi di chiamata a server
|
|
|
|
/// <summary>
|
|
/// Effettua download dei template report in locale
|
|
/// </summary>
|
|
private void refreshLocaltemplate()
|
|
{
|
|
// scarico TUTTI i report definiti
|
|
filePack objFiles = new filePack();
|
|
string rawData = "";
|
|
string url2call = $"{serverBaseAddr}/api/Report";
|
|
// recupero dati
|
|
var taskRes = Task.Run(() => utils.getPageAsync(url2call));
|
|
taskRes.Wait();
|
|
|
|
rawData = taskRes.Result;
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
try
|
|
{
|
|
// deserializzo
|
|
objFiles = JsonConvert.DeserializeObject<filePack>(rawData);
|
|
if (objFiles != null)
|
|
{
|
|
// salvo!
|
|
foreach (var item in objFiles.fileList)
|
|
{
|
|
string fullPath = $"{localReportPath}{item.fileName}";
|
|
File.WriteAllText(fullPath, item.content);
|
|
}
|
|
utils.lgInfo("Download template files aggiornati");
|
|
}
|
|
else
|
|
{
|
|
utils.lgError($"Error: invalid file data received, deserialized to null ({rawData})");
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
utils.lgError("Error in deserialization during refreshLocaltemplate", exc);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
utils.lgError("Error: no file data downloaded");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Aggiornamento code stampa disponibili
|
|
/// </summary>
|
|
private void refreshQueueAvail()
|
|
{
|
|
// svuoto
|
|
lbPrintQueue.Items.Clear();
|
|
string rawData = "";
|
|
// se sono in demo leggo da locale
|
|
if (demoMode)
|
|
{
|
|
// leggo da locale...
|
|
string filePath = $"{baseDir}demo\\PrintQueueConf.json";
|
|
rawData = File.ReadAllText(filePath);
|
|
}
|
|
// altrimenti da metodi REST su server...
|
|
else
|
|
{
|
|
// scarico TUTTI i report definiti
|
|
string url2call = $"{serverBaseAddr}/api/PrintQueueConf";
|
|
rawData = callUrl(url2call);
|
|
}
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
try
|
|
{
|
|
// deserializzo
|
|
listQueue = JsonConvert.DeserializeObject<List<queueConf>>(rawData);
|
|
if (listQueue != null)
|
|
{
|
|
// metto in elenco
|
|
foreach (var item in listQueue)
|
|
{
|
|
lbPrintQueue.Items.Add(item.name);
|
|
}
|
|
utils.lgInfo("Effettuato update elenco printQueue");
|
|
}
|
|
else
|
|
{
|
|
utils.lgError($"Error: invalid file data received, deserialized to null ({rawData})");
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
utils.lgError("Error in deserialization during refreshQueueAvail", exc);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
utils.lgError("Error: no file data downloaded");
|
|
}
|
|
showPrinterList();
|
|
}
|
|
/// <summary>
|
|
/// Aggiorna stato code
|
|
/// </summary>
|
|
private void refreshQueueStatus()
|
|
{
|
|
// verifico ed aggiorno quantità coda
|
|
int numTot = 0;
|
|
string urlApiQueue = $"{serverBaseAddr}/api/PrintQueue";
|
|
string urlApiQueueLen = $"{serverBaseAddr}/api/PrintQueueLen";
|
|
// verifico il modo di controllo code: SE è single --> chiamo metodo con le SOLE code gestite...
|
|
if (!string.IsNullOrEmpty(memLayer.ML.CRS("queueCheckMode")))
|
|
{
|
|
// se ho code configurate
|
|
if (queueSetupConf != null && queueSetupConf.Count > 0)
|
|
{
|
|
// se vuoto calcolo
|
|
if (string.IsNullOrEmpty(listManQueue))
|
|
{
|
|
foreach (var item in queueSetupConf)
|
|
{
|
|
listManQueue += $"|{item.Key}";
|
|
}
|
|
// aggiungo "|" finale;
|
|
listManQueue += "|";
|
|
}
|
|
urlApiQueueLen = $"{serverBaseAddr}/api/PrintQueueLen?queueList={listManQueue}";
|
|
}
|
|
}
|
|
// verifico non ci sia veto print..
|
|
DateTime adesso = DateTime.Now;
|
|
if (vetoPrint < adesso)
|
|
{
|
|
vetoPrint = adesso.AddSeconds(15);
|
|
// verifico ping!
|
|
if (!utils.pingAddress(serverIp))
|
|
{
|
|
// registro errore ping... attendo 3 sec
|
|
utils.lgError("Error: ping not responding");
|
|
vetoPrint = adesso.AddSeconds(3);
|
|
}
|
|
else
|
|
{
|
|
// verifico LEN coda
|
|
string rawData = callUrl(urlApiQueueLen);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
try
|
|
{
|
|
int.TryParse(rawData, out numTot);
|
|
lblQueueCount.Text = numTot.ToString("000");
|
|
if (numTot > 0)
|
|
{
|
|
// disabilitato perché da errore di concorrenza...
|
|
bool useTask = false;
|
|
if (useTask)
|
|
{
|
|
List<Task> tasks = new List<Task>();
|
|
// se ho stampe in coda --> verifico code gestite
|
|
foreach (var coda in queueSetupConf)
|
|
{
|
|
Task t = Task.Run(() => processQueue(urlApiQueue, coda));
|
|
tasks.Add(t);
|
|
}
|
|
Task.WaitAll(tasks.ToArray());
|
|
}
|
|
else
|
|
{
|
|
// se ho stampe in coda --> verifico code gestite
|
|
foreach (var coda in queueSetupConf)
|
|
{
|
|
processQueue(urlApiQueue, coda);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
utils.lgError("Error in deserialization during refreshQueueStatus", exc);
|
|
}
|
|
}
|
|
vetoPrint = adesso;
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Processa singola coda
|
|
/// </summary>
|
|
/// <param name="urlApiQueue"></param>
|
|
/// <param name="coda"></param>
|
|
private void processQueue(string urlApiQueue, KeyValuePair<string, printQueue> coda)
|
|
{
|
|
string rawData;
|
|
string urlSingleQueue = $"{urlApiQueue}/{coda.Key}";
|
|
rawData = callUrl(urlSingleQueue);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
try
|
|
{
|
|
// deserializzo
|
|
List<reportData> report2print = JsonConvert.DeserializeObject<List<reportData>>(rawData);
|
|
if (report2print != null)
|
|
{
|
|
// aggiorno contatore...
|
|
coda.Value.NumWaiting = report2print.Count;
|
|
// verifico sia RUNNING...
|
|
if (coda.Value.isRunning)
|
|
{
|
|
printSingleQueue(urlApiQueue, coda, report2print);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
utils.lgError($"Error in deserialization during processQueue | urlSingleQueue {urlSingleQueue} | rawData {rawData}", exc);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua la stampa di una singola coda...
|
|
/// </summary>
|
|
/// <param name="urlApiQueue"></param>
|
|
/// <param name="coda"></param>
|
|
/// <param name="report2print"></param>
|
|
private void printSingleQueue(string urlApiQueue, KeyValuePair<string, printQueue> coda, List<reportData> report2print)
|
|
{
|
|
#if false
|
|
//bool doParallel = false;
|
|
//if (doParallel)
|
|
//{
|
|
// Parallel.ForEach(report2print, item =>
|
|
// {
|
|
// bool fatto = false;
|
|
// // se ho almeno 1 stampa --> lancio!
|
|
// queueConf confCoda = listQueue.Find(conf => conf.name == coda.Key);
|
|
// if (confCoda != null)
|
|
// {
|
|
// fatto = rPrint.printReport(confCoda.template, localReportPath, coda.Value.Printer, item.rdsData, confCoda.deviceInfoParam, true);
|
|
// }
|
|
// // fatta stampa aggiorno server con indicazione che ho stampato
|
|
// if (fatto)
|
|
// {
|
|
// coda.Value.NumWaiting--;
|
|
// coda.Value.NumDone++;
|
|
// // compongo payload...
|
|
// printTask printResult = new printTask()
|
|
// {
|
|
// ticketNum = item.ticketNum,
|
|
// newStatus = 1,
|
|
// message = ""
|
|
// };
|
|
// string payload = JsonConvert.SerializeObject(printResult);
|
|
// // chiamo in put print result!
|
|
// postUrl(urlApiQueue, payload);
|
|
// }
|
|
// });
|
|
//}
|
|
#endif
|
|
|
|
// oggetto x stampa report...
|
|
reportPrinter rPrint = new reportPrinter();
|
|
foreach (var item in report2print)
|
|
{
|
|
bool fatto = false;
|
|
// se ho almeno 1 stampa --> lancio!
|
|
queueConf confCoda = listQueue.Find(conf => conf.name == coda.Key);
|
|
if (confCoda != null)
|
|
{
|
|
fatto = rPrint.printReport(confCoda.template, localReportPath, coda.Value.Printer, item.rdsData, confCoda.deviceInfoParam, true);
|
|
}
|
|
// fatta stampa aggiorno server con indicazione che ho stampato
|
|
if (fatto)
|
|
{
|
|
coda.Value.NumWaiting--;
|
|
coda.Value.NumDone++;
|
|
// compongo payload...
|
|
printTask printResult = new printTask()
|
|
{
|
|
ticketNum = item.ticketNum,
|
|
newStatus = 1,
|
|
message = ""
|
|
};
|
|
string payload = JsonConvert.SerializeObject(printResult);
|
|
// chiamo in put print result!
|
|
postUrl(urlApiQueue, payload);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
if (!forceClose)
|
|
{
|
|
e.Cancel = true;
|
|
}
|
|
sendToTray();
|
|
}
|
|
}
|
|
}
|