122 lines
4.1 KiB
C#
122 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Integry_Smart_Gateway.Core.ExternalCom.Contracts;
|
|
using Integry_Smart_Gateway.Core.ExternalCom.Protocols.Socket;
|
|
using Integry_Smart_Gateway.Core.Logger;
|
|
using Integry_Smart_Gateway.Core.Machine.Contracts;
|
|
using Integry_Smart_Gateway.Core.MachineConnections.Contracts;
|
|
using Integry_Smart_Gateway.Core.Orders;
|
|
using ISG___Gramm__SC__Base.Core.Machine.VideoJetCommandsModel;
|
|
using ISG___Gramm__SC__Base.Core.Model;
|
|
|
|
namespace ISG___Gramm__SC__Base.Core.Machine.Implementation;
|
|
|
|
public class VideoJetMachineCollectorService(
|
|
IOrdersHandlerService ordersHandlerService,
|
|
ILoggerService loggerService,
|
|
IMachineLoggerService machineLoggerService,
|
|
IMachineConnectionsService machineConnectionsService,
|
|
string ip,
|
|
Type modelType,
|
|
string codCmac)
|
|
: SocketConnector(codCmac, ip, 3004, loggerService, machineLoggerService), IVideoJetFronteCollectorService, IVideoJetRetroCollectorService, IVideoJetCartoniCollectorService
|
|
{
|
|
|
|
private class Commands
|
|
{
|
|
public const string GET_PRINTER_STATE = "GST";
|
|
}
|
|
|
|
private string _ip;
|
|
|
|
private bool _connected;
|
|
|
|
|
|
public async Task<bool> Connect(bool autoReconnect = true)
|
|
{
|
|
return await base.Connect();
|
|
}
|
|
|
|
|
|
protected override Task PostConnect()
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
protected override async Task OnConnected()
|
|
{
|
|
await machineConnectionsService.RegisterMachineConnection(codCmac, true);
|
|
}
|
|
|
|
protected override async Task OnDisconnected()
|
|
{
|
|
await machineConnectionsService.RegisterMachineConnection(codCmac, false);
|
|
}
|
|
|
|
public async Task<IList<IMachineComModel>> CollectData()
|
|
{
|
|
var externalMachineComModels = new List<IMachineComModel>();
|
|
|
|
try
|
|
{
|
|
var etichettatriceModel = (IEtichettatriceModel) Activator.CreateInstance(modelType);
|
|
await getPrinterState(etichettatriceModel);
|
|
|
|
var ordineInCorso = ordersHandlerService.GetOpenedOrdineLavorazione();
|
|
if (ordineInCorso != null)
|
|
{
|
|
etichettatriceModel.DescrizioneOrdineInCorso = ordineInCorso.DescrizioneOrdine;
|
|
etichettatriceModel.LottoOrdineInCorso = ordineInCorso.PartitaMag;
|
|
etichettatriceModel.ScadenzaOrdineInCorso = ordineInCorso.DataScad;
|
|
}
|
|
|
|
if(etichettatriceModel != null) externalMachineComModels.Add(etichettatriceModel);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
loggerService.LogVerboseError(ex, nameof(VideoJetMachineCollectorService));
|
|
}
|
|
|
|
return externalMachineComModels;
|
|
}
|
|
|
|
public async void WriteData(IVideoJetCommandModel videoJetCommandModel)
|
|
{
|
|
string command = videoJetCommandModel.Command;
|
|
string parameters = string.Join("|", videoJetCommandModel.Parameters);
|
|
|
|
string finalCommand = $"{command}|{parameters}|";
|
|
|
|
await base.Send(finalCommand + '\u000d');
|
|
IList<string> statusResponse = await base.Receive('|');
|
|
}
|
|
|
|
private async Task<IEtichettatriceModel> getPrinterState(IEtichettatriceModel etichettatriceModel)
|
|
{
|
|
await base.Send(Commands.GET_PRINTER_STATE + '\u000d');
|
|
|
|
IList<string> statusDataList = await base.Receive('|');
|
|
|
|
if (statusDataList == null || statusDataList.Count <= 5)
|
|
{
|
|
etichettatriceModel.ConnectionStatus = false;
|
|
//etichettatriceModel.ContatoreParziale = 0;
|
|
//etichettatriceModel.ContatoreTotale = 0;
|
|
//etichettatriceModel.JobAttuale = null;
|
|
//etichettatriceModel.VelocitaEffettiva = 0;
|
|
|
|
//etichettatriceModel = null;
|
|
}
|
|
else
|
|
{
|
|
etichettatriceModel.ConnectionStatus = true;
|
|
etichettatriceModel.ContatoreParziale = Convert.ToInt32(statusDataList[4]);
|
|
etichettatriceModel.ContatoreTotale = Convert.ToInt64(statusDataList[5]);
|
|
etichettatriceModel.JobAttuale = statusDataList[3];
|
|
etichettatriceModel.VelocitaEffettiva = Convert.ToInt32(statusDataList[2]);
|
|
}
|
|
|
|
return etichettatriceModel;
|
|
}
|
|
} |