namespace
adcLabelService
{
public partial class Service1 : ServiceBase
{
private bool End;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
this.End = false;
beginService();
}
protected override void OnStop()
{
this.End = true;
// TODO: Add code here to perform any tear-down necessary to stop your service.
}
#region
Try to establish a connection to the database
public void beginService()
{
// TODO: Add code here to start your service.
string Server = "ADCTEST-DT";
string SSPI = "SSPI";
string Database = "ADC";
string ConnectionString = "Data Source=" + Server + ";";
ConnectionString +=
"user id=AS400;";
ConnectionString +=
"password=tank;";
ConnectionString +=
"Initial Catalog=" + Database + ";";
//ConnectionString += "Integrated Security=" + SSPI + ";";
SqlConnection SQLConnection = new SqlConnection();
bool fail=false;
do
{
try
{
SQLConnection.ConnectionString = ConnectionString;
SQLConnection.Open();
}
catch (Exception Ex)
{
if (SQLConnection != null)
SQLConnection.Dispose();
string ErrorMessage = "A error occurred while trying to connect to the server.";
ErrorMessage +=
Environment.NewLine;
ErrorMessage +=
Environment.NewLine;
ErrorMessage += Ex.Message;
fail=
true;
// Show error message (this = the parent Form object)
//MessageBox.Show(this, ErrorMessage, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
while (fail==true);
// Stop here
// return;
// }
MainLine(SQLConnection,
this.End);
SQLConnection.Close();
SQLConnection.Dispose();
}
public static void MainLine(SqlConnection SQLConnection, bool End)
{
Socket m_socClient;
int port = 9100;
while (!End)
{
string prt_address = "";
string format = "";
string item = "";
string item10 = "";
string img = "";
string fam_desc = "";
string fam_descraw = "";
string prod_desc = "";
string pcs = "";
string length_ctn = "";
string finish = "";
string lsize = "";
string color = "";
string trademark = "";
string fam_size = "";
string metric_carton = "";
string shift = "";
string user = "";
string plant = "";
string batch = "";
string workCenter = "";
string monthDayYear = "";
string ID = "";
SqlDataReader prtreader = null;
SqlCommand prtcommand = new SqlCommand("select * from toPrint", SQLConnection);
prtreader = prtcommand.ExecuteReader();
prtreader.Read();
if (prtreader.HasRows == true)
{
ID = (prtreader[
"ID"].ToString());
prt_address = (prtreader[
"printer"].ToString());
item = (prtreader[
"item"].ToString());
item10 = item.Substring(0, 10);
img = (prtreader[
"image"].ToString());
fam_descraw = (prtreader[
"fam_desc"].ToString());
prod_desc = (prtreader[
"itm_desc"].ToString());
pcs = (prtreader[
"pcs"].ToString());
length_ctn = (prtreader[
"len_ctn"].ToString());
finish = (prtreader[
"finish"].ToString());
lsize = (prtreader[
"size"].ToString());
color = (prtreader[
"color"].ToString());
trademark = (prtreader[
"tmk"].ToString());
fam_size = (prtreader[
"fam_size"].ToString());
metric_carton = (prtreader[
"metric_ctn"].ToString());
shift = (prtreader[
"shift"].ToString());
user = (prtreader[
"user_num"].ToString());
monthDayYear = (prtreader[
"date"].ToString());
plant = (prtreader[
"plant"].ToString());
batch = (prtreader[
"batch"].ToString());
workCenter = (prtreader[
"line"].ToString());
char[] zeros ={ '0' };
pcs = pcs.TrimStart(zeros);
length_ctn = length_ctn.TrimStart(zeros);
metric_carton = metric_carton.TrimStart(zeros);
//string tmrk = "¬FS¬FT,¬GSR,40¬FDA¬FS¬FT,¬A0R,41,47¬CI0¬FD";
string tmrk = ">FS>FT,>GSR,40>FDA>FS>FT,>A0R,41,47>CI0>FD";
fam_desc = fam_descraw.Replace(
"@", tmrk);
//®
if (lsize == "A")
format =
"ZOANNNY";
else if (lsize == "B")
format =
"ZOBNNNY";
else if (lsize == "C")
format =
"ZOCNNNN";
else if (lsize == "D")
format =
"ZOZNNNN";
prtreader.Close();
prtreader.Dispose();
prtcommand.Dispose();
try
{
//create a new client socket ...
m_socClient =
new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
char blanks = (' ');
String szIPSelected = prt_address.TrimEnd(blanks);
int alPort = port;
System.Net.
IPAddress remoteIPAddress = System.Net.IPAddress.Parse(szIPSelected);
System.Net.
IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, alPort);
m_socClient.Connect(remoteEndPoint);
SqlDataReader fmtreader = null;
SqlCommand fmtcommand = new SqlCommand("select * from LABEL_FORMATS where format='" + format + "'", SQLConnection);
string a = "";
// string qty = "PQ0001";
// if (format == "ZOANNNY")
// qty = "PQ0002";
string objData = "";
fmtreader = fmtcommand.ExecuteReader();
while (fmtreader.Read())
{
a = TrimString(fmtreader[
"lbldata"].ToString());
a = a.Replace(
"+", shift);
a = a.Replace(
"!18", user);
a = a.Replace(
"!16", monthDayYear);
a = a.Replace(
"!15", item10);//product_bar);
a = a.Replace(
"!11", metric_carton);
a = a.Replace(
"!10", plant);
a = a.Replace(
"!9", batch);
a = a.Replace(
"!8", fam_size);
a = a.Replace(
"!7", finish);
a = a.Replace(
"!6", length_ctn);
a = a.Replace(
"!5", pcs);
a = a.Replace(
"!4", prod_desc);
a = a.Replace(
"!3", fam_desc);
a = a.Replace(
"!2", img);
a = a.Replace(
"!1", workCenter);
// a = a.Replace("PQ0001", qty);
//a = a.Replace("!5", graphic_1);
//a = a.Replace("!14", graphic_2);
//if ((!a.Contains("%m/%d/%Y")) && (!a.Contains("%H:%M:%S")))
objData += a;
}
fmtreader.Close();
fmtreader.Dispose();
fmtcommand.Dispose();
byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
m_socClient.Send(byData);
m_socClient.Close();
}
catch
{
}
SqlCommand dltcommand = new SqlCommand("delete from toPrint where ID=" + ID, SQLConnection);
int x = dltcommand.ExecuteNonQuery();
dltcommand.Dispose();
}
else
{
prtreader.Close();
prtreader.Dispose();
prtcommand.Dispose();
}
}
}
public static string TrimString(string str)
{
try
{
string pattern = @"^[ \t]+|[ \t]+$";
Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
str = reg.Replace(str,
"");
return str;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}