ECJetECL7012Driver
ECJetECL7012Driver
ECJetECL7012Driver
using System.Collections.Generic;
using ETS.Core.Api;
using ETS.Core.Api.Models;
using ETS.Core.Extensions;
using ETS.Core.Enums;
using System.Net.Sockets;
using System.IO;
using System.Linq;
using System.Text;
using BDF.Mes.Global.Dbc;
using BDF.Mes.Global.Enums;
using BDF.Mes.Global.Models;
using BDF.Mes.Global;
namespace BDF.Mes.Global.Dbc
{
/// ******************************************************************
public class ECJetECL7012Driver : IDirectBatchCoding
{
/// ******************************************************************
/// <summary>
/// Method Description
/// </summary>
/// ******************************************************************
private readonly BdfService _bdf;
private const bool _DEBUG = true;
private List<string> _interactions = new List<string>();
private List<string> _templates = new List<string>();
private const string LOG_CATEGORY = "ECJetECL7012Driver";
private Result<bool> result = new Result<bool>();
/// ******************************************************************
/// <summary>
/// Method Description
/// </summary>
/// ******************************************************************
public ECJetECL7012Driver(ETS.Core.Api.ApiService api)
{
_bdf = api.Create.InstanceOf<BdfService>();
}
/// ******************************************************************
if (lines.Count > 0)
{
commandSetVariables +=
"loadJobFile#\"{0}\"".FormatWith(templateName);
commands.Add(commandSetVariables);
string commandVariableSettings =
""; //"setVarText#";
foreach (string line in lines)
{
index++;
if(index != 1)
{
//commandVariableSettings += "&";
}
//commandVariableSettings +=
"row{0}={1}".FormatWith(index,line);
commandSetVariables =
"setText#\"row{0}={1}\"".FormatWith(index, line);
commands.Add(commandSetVariables);
}
//commandSetVariables = commandVariableSettings;
commands.Add(commandSetVariables);
}
ps = commandSetVariables;
this.Log(commandSetVariables, ETS.Core.Enums.LogType.Information,
ReceiveMessageType.None, MdcInterfaceType.DBC, commandSetVariables, "ECJET");
if (sendResult.Success)
{
msg = "Printer IP:{0} Port{1} ready to print at
{2}".FormatWith(ipAddr, port, _bdf.Api.Site.GetCurrentDateTime());
_interactions.Add(msg);
Log(msg, ETS.Core.Enums.LogType.Information,
ReceiveMessageType.None, MdcInterfaceType.DBC, ps, poNum);
this.result.Messages.Add(this.GenerateResultMessage(msg));
this.result.Success = true;
//this._api.Tags.UpdateVirtualTagByName(tag.Name,
DbcExecuted.Executed);
string updateSql = @"UPDATE dcp
SET dcp.Value = '{0}'
FROM tDeviceCustomProperty dcp
INNER JOIN tCustomProperty cp ON (dcp.CustomPropertyID = cp.ID)
WHERE dcp.DeviceID = {1}
AND cp.[Key] = '{2}'".FormatWith((int)DbcExecuted.Executed, deviceID, "DBCSTATUS");
var updateResult = _bdf.Api.Util.Db.ExecuteSql(updateSql);
if (!result.Success)
{ _bdf.Api.Util.Log.WriteErrorsFromResultObject(updateResult, "DBC PRINT
UPDATE"); }
return result;
}
else
{
msg = "Printer IP:{0} Port{1} NOT ready to print at
{2}".FormatWith(ipAddr, port, _bdf.Api.Site.GetCurrentDateTime());
_interactions.Add(msg);
this.Log(msg, ETS.Core.Enums.LogType.Error,
ReceiveMessageType.None, MdcInterfaceType.DBC, ps, poNum);
this.result.Messages.Add(this.GenerateResultMessage(msg));
this.result.Success = false;
//this._api.Tags.UpdateVirtualTagByName(tag.Name,
DbcExecuted.Error);
string updateSql = @"UPDATE dcp
SET dcp.Value = '{0}'
FROM tDeviceCustomProperty dcp
INNER JOIN tCustomProperty cp ON (dcp.CustomPropertyID = cp.ID)
WHERE dcp.DeviceID = {1}
AND cp.[Key] = '{2}'".FormatWith((int)DbcExecuted.Error, deviceID, "DBCSTATUS");
var updateResult = _bdf.Api.Util.Db.ExecuteSql(updateSql);
if (!result.Success)
{ _bdf.Api.Util.Log.WriteErrorsFromResultObject(updateResult, "DBC PRINT
UPDATE"); }
return result;
}
/// ******************************************************************
public Result<bool> SendToECJetPrinter(string ipAddr, int port, string
templateName, List<string> commands, string poNum, string ps)
{
//Set up variables
bool responseOK = true; //false if the response is not the
expected.
bool statusOK = true; // false if the status is not correct.
//_interactions.Clear();
try
{
// Create a TcpClient.
TcpClient client = new TcpClient(ipAddr, port);
//Send commands
foreach (string command in commands)
{
responseOK = false;
}
//if status is not correct, break the loop
if (!statusOK) break;
// Close everything.
stream.Close();
client.Close();
catch (ArgumentNullException e)
{
_interactions.Add("ERROR: ArgumentNullException:
{0}".FormatWith(e.Message));
this.result.Messages.Add(this.GenerateResultMessage("ERROR:
ArgumentNullException: {0}".FormatWith(e.Message)));
this.result.Success = false;
this.Log("ERROR: ArgumentNullException:
{0}".FormatWith(e.Message), ETS.Core.Enums.LogType.Information,
ReceiveMessageType.None, MdcInterfaceType.DBC, ps, poNum);
return result;
//return false;
}
catch (IOException e)
{
_interactions.Add("ERROR: IOException:
{0}".FormatWith(e.Message));
_bdf.Log.LogDbc("ERROR: IOException:
{0}".FormatWith(e.Message), ETS.Core.Enums.LogType.Error, ReceiveMessageType.None,
MdcInterfaceType.DBC, ps, poNum);
this.result.Messages.Add(this.GenerateResultMessage("ERROR:
IOException: {0}".FormatWith(e.Message)));
this.result.Success = false;
return result;
}
catch (SocketException e)
{
_interactions.Add("ERROR: SocketException:
{0}".FormatWith(e.Message));
this.result.Messages.Add(this.GenerateResultMessage("ERROR:
SocketException: {0}".FormatWith(e.Message)));
this.result.Success = false;
this.Log("ERROR: SocketException:
{0}".FormatWith(e.Message), ETS.Core.Enums.LogType.Information,
ReceiveMessageType.None, MdcInterfaceType.DBC, ps, poNum);
return result;
//return false;
}
}
/// ******************************************************************
private ResultMessage GenerateResultMessage(string message)
{
ResultMessage resultMessage = new ResultMessage();
resultMessage.Message = message;
return resultMessage;
}
/// ******************************************************************
private void Log(string message, ETS.Core.Enums.LogType type,
ReceiveMessageType contentType, MdcInterfaceType interfaceType, string content,
string entityIdentifier)
{
var Log = new BdfMesMessageLog();