ECJetECL7012Driver

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

using System;

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>();
}

/// ******************************************************************

public Result<bool> ConfigureCoder(int systemID, string ipAddr, int


port, List<string> lines, string templateName, string poNum, int deviceID)
{
string ps = string.Empty;
List<string> commands = new List<string>(); // list of commands I
will send to the printer
string msg;
int index = 0;
string commandSetVariables = string.Empty;
//Setting the commands I will send to the printer

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");

//Pass the info to the method


Result<bool> sendResult = SendToECJetPrinter(ipAddr, port,
templateName, commands, poNum, ps);

var tag = _bdf.Tags.GetTagForSystemAndAbbreviation(systemID,


"RunDBCExecuted");

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);

// Get a client stream for reading and writing.


NetworkStream stream = client.GetStream();
stream.ReadTimeout = 2000;

//Add to the interactions list.


string msg = "Connected with printer IP:{0} Port:{1} 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));

//Check the Connection Acepted response

// Receive the TcpipAddr.response.


// Buffer to store the response bytes.
Byte[] data = new Byte[256];

// string to store the response UTF8 representation.


string responseData = string.Empty;

//Send commands
foreach (string command in commands)
{

// Translate the passed command into UTF8 and store


it as a Byte array.
data = System.Text.Encoding.UTF8.GetBytes(command);

// Send the message to the connected TcpipAddr.


stream.Write(data, 0, data.Length);

// Receive the TcpipAddr.response.


// Buffer to store the response bytes.
data = new Byte[256];

// string to store the response UTF8 representation.


responseData = string.Empty;

// Read the first batch of the TcpipAddr response


bytes.
int bytes = stream.Read(data, 0, data.Length);
responseData =
System.Text.Encoding.UTF8.GetString(data, 0, bytes);

//Add to the interactions list


_interactions.Add("SENT: {0} - RECEIVED:
{1}".FormatWith(command, responseData).Replace(Environment.NewLine, " "));

this.result.Messages.Add(this.GenerateResultMessage("SENT: {0} - RECEIVED:


{1}".FormatWith(command, responseData).Replace(Environment.NewLine, " ")));
//this.Log("SENT: {0} - RECEIVED:
{1}".FormatWith(command,responseData).Replace(Environment.NewLine," "),
ETS.Core.Enums.LogType.Information, ReceiveMessageType.None, MdcInterfaceType.DBC,
ps, poNum);
Log("SENT: {0} - RECEIVED: {1}".FormatWith(command,
responseData).Replace(Environment.NewLine, " "),
ETS.Core.Enums.LogType.Information, ReceiveMessageType.None, MdcInterfaceType.DBC,
ps, poNum);
//Check the response.

// Check if the response message is acknowledged by


the printer, if not log a error.
if (responseData != "OK")
{
//Not Ready, ink system
msg = "ERROR! Not Ready, Current printer
status: {0}".FormatWith(responseData);
_interactions.Add(msg);
this.result.Messages.Add(this.GenerateResultMessage(msg));
Log(msg, ETS.Core.Enums.LogType.Error,
ReceiveMessageType.None, MdcInterfaceType.DBC, ps, poNum);

responseOK = false;
}
//if status is not correct, break the loop
if (!statusOK) break;

// Close everything.
stream.Close();
client.Close();

msg = "Disconnected with printer IP:{0} Port{1} at


{2}".FormatWith(ipAddr, port, _bdf.Api.Site.GetCurrentDateTime());
_interactions.Add(msg);
this.Log(msg, ETS.Core.Enums.LogType.Information,
ReceiveMessageType.None, MdcInterfaceType.DBC, ps, poNum);

//if all the resposes OK and the status OK


if ((responseOK) && (statusOK))
{
this.result.Success = true;
return result;
}
else
{
this.result.Success = false;
return result;
}
}

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();

Log.Application = "DBC Print";


Log.Message = message;
Log.Type = type; //ETS.Core.Enums.LogType.Information;
Log.ContentType = contentType;
Log.InterfaceType = interfaceType;
Log.LogDateTime = _bdf.Api.Site.GetCurrentDateTime();
Log.Content = content;
Log.EntityIdentifier = entityIdentifier;
_bdf.Data.BdfMesMessageLog.InsertAsNew(Log).ThrowIfFailed();
}
/// ******************************************************************
}
}

You might also like