using EAS.Services; using SOH.BLL; using SOH.JianYan.YiQi.Base; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace SOH.JianYan.DEC_HT800 { public partial class HT800 : SOH.JianYan.YiQi.Base.baseMain { const string enq = "\u0005"; const string eot = "\x0004"; const string stx = "\x0002"; const string etx = "\x0003"; const string cr = "\x000d"; const string lf = "\x000a"; public HT800() { InitializeComponent(); } string _yqbm = ""; private void HT800_Load(object sender, EventArgs e) { System.Configuration.Configuration conf = System.Configuration.ConfigurationManager.OpenExeConfiguration(Application.StartupPath + "\\local.ext"); if (conf.AppSettings.Settings.AllKeys.Contains("yqbm")) { _yqbm = conf.AppSettings.Settings["yqbm"].Value; } conf.Save(); if (System.IO.File.Exists(Application.StartupPath + "\\SOH.OnLine.exe")) { System.Diagnostics.Process p = System.Diagnostics.Process.Start(Application.StartupPath + "\\SOH.OnLine.exe"); Program.ps = p; } } protected override void decoder() { base.decoder(); string decodefile = Application.StartupPath + "\\raw\\data.txt"; if (System.IO.File.Exists(decodefile)) { int a = 0; string data = ""; aa: try { FileStream fs = new FileStream(decodefile, FileMode.Open, FileAccess.Read, FileShare.None); StreamReader sr = new StreamReader(fs); data = sr.ReadToEnd(); sr.Close(); sr.Dispose(); } catch { a++; if (a > 11) goto aa; } if (data.StartsWith(enq) && data.EndsWith(eot)) { File.Delete(decodefile); } else { return; } while (data.Length > 0) { List ys = new List(); int post = 0; while (true) { post = data.IndexOf(stx, post); if (post < 0) break; string decdata = data.Substring(post + 1, data.IndexOf(etx, post) - post); ys.Add(decdata); post = data.IndexOf(etx, post); } DateTime sampledate = DateTime.Now; string sampleno = ""; //ys.Select() string msgtype = ""; string querytime = ""; string queryno = ""; string bno = ""; string eno = ""; string bt = ""; string et = ""; foreach (string y in ys) { string[] zs = y.Split('|'); if (zs[0] == "1H") { Lis_RiZhi.resultlog(zs[0]); if (zs[11] == "SRLT") { //msgid = zs[9]; msgtype = "sendmsg"; continue; } //QRY^Q02 else if (zs[11] == "QREQ") { msgtype = "query"; continue; } } if (msgtype == "sendmsg") { if (zs[0].EndsWith("O")) { sampleno = zs[2]; string[] ss = sampleno.Split('^'); if(ss[0].Length>10) { sampleno = ss[0]; } else { sampleno = ss[1]; } //string sj = zs[7]; //sampledate = DateTime.Parse(sj.Substring(0, 4) + "-" + sj.Substring(4, 2) + "-" + sj.Substring(6)); continue; } else if (zs[0].EndsWith("R")) { string tdh, jg; tdh = zs[2]; jg = zs[3]; sampledate = DateTime.Parse(zs[12].Substring(0,10)); if (YiQi.Base.Lis_result.canmodifybyjytm(_yqbm, sampledate, sampleno)) { YiQi.Base.Lis_result.addresult(sampledate, _yqbm, sampleno, tdh, jg); } } } else if (msgtype == "query") { if (zs[0] == "2Q") { sampleno = zs[4]; bno = zs[2]; eno = zs[3]; bt = zs[6]; et = zs[7]; querytime = zs[6]; Lis_RiZhi.resultlog(sampleno); Lis_RiZhi.resultlog(bno); Lis_RiZhi.resultlog(eno); Lis_RiZhi.resultlog(bt); Lis_RiZhi.resultlog(et); //string sj = zs[7]; //sampledate = DateTime.Parse(sj.Substring(0, 4) + "-" + sj.Substring(4, 2) + "-" + sj.Substring(6)); continue; } } continue; } //接受消息 if (msgtype == "sendmsg") YiQi.Base.Lis_result.calcsample(_yqbm, sampledate.Date, sampleno); else if (msgtype == "query") { if (sampleno.Length == 12) { Lis_RiZhi.resultlog("有条码,通过条码获取!"); var vserver = ServiceContainer.GetService(); var yhxx = vserver.getjyyhxxbytm(sampleno); if (yhxx == null) { Lis_RiZhi.resultlog("没有申请单"); break; } else { string lisfile = Application.StartupPath + "\\result"; if (!Directory.Exists(lisfile)) { Directory.CreateDirectory(lisfile); } lisfile += "\\lis.txt"; StreamWriter sw = new StreamWriter(lisfile, false, Encoding.Default); sw.Write(enq + "$#$" + stx); string h = "1H|\\^&|||sancailis|||||LIS||QRES|E1394-97|" + DateTime.Now.ToString("yyyyMMddHHmmss") + "" + cr + etx; sw.Write(h); sw.Write(getcs(h)); sw.Write(cr + lf); sw.Write("$#$"); sw.Write(stx); h = "2P|1|" + yhxx.tm + "|||" + yhxx.xm + "|||" + yhxx.xb + "||||||" + yhxx.nl + "^岁" + cr + etx; sw.Write(h); sw.Write(getcs(h)); sw.Write(cr + lf); sw.Write("$#$"); sw.Write(stx); h = "3O|1|" + yhxx.jytm + "^" + yhxx.bbh + "^0^0^0||"; var lisxm = vserver.getjyxmbytm(sampleno, _yqbm); Lis_RiZhi.resultlog(lisfile); lisxm.ForEach(t => { h += t.tdh + "\\"; }); h= h.TrimEnd('\\'); h += "|R|" + yhxx.jyrq.ToString("yyyy-MM-dd") + "|||||||||0||||||||||F" + cr + etx; sw.Write(h); sw.Write(getcs(h)); sw.Write(cr + lf); sw.Write("$#$"); sw.Write(stx + "4L|1|N" + cr + etx + "07" + cr + lf); sw.Write("$#$" + eot); sw.Close(); Lis_RiZhi.resultlog("输出成功!"); } } else { if (bno != "0" && eno != "0" && (int.Parse(eno) >= int.Parse(bno))) { for (int i = int.Parse(bno); i <= int.Parse(eno); i++) { var vserver = ServiceContainer.GetService(); var yhxx = vserver.getjyyhxxbybbh(i,querytime,_yqbm); if (yhxx == null) { Lis_RiZhi.resultlog("没有申请单"); break; } else { string lisfile = Application.StartupPath + "\\result"; if (!Directory.Exists(lisfile)) { Directory.CreateDirectory(lisfile); } lisfile += "\\lis.txt"; StreamWriter sw = new StreamWriter(lisfile, false, Encoding.Default); sw.Write(enq + "$#$" + stx); string h = "1H|\\^&|||sancailis|||||LIS||QRES|E1394-97|" + DateTime.Now.ToString("yyyyMMddHHmmss") + "" + cr + etx; sw.Write(h); sw.Write(getcs(h)); sw.Write(cr + lf); sw.Write("$#$"); sw.Write(stx); h = "2P|1|" + yhxx.tm + "|||" + yhxx.xm + "|||" + yhxx.xb + "||||||" + yhxx.nl + "^岁" + cr + etx; sw.Write(h); sw.Write(getcs(h)); sw.Write(cr + lf); sw.Write("$#$"); sw.Write(stx); h = "3O|1|" + yhxx.jytm + "^" + yhxx.bbh + "^0^0^0||"; var lisxm = vserver.getjyxmbytm(yhxx.jytm, _yqbm); Lis_RiZhi.resultlog(lisfile); lisxm.ForEach(t => { h += t.tdh + "\\"; }); h= h.TrimEnd('\\'); h += "|R|" + yhxx.jyrq.ToString("yyyy-MM-dd") + "|||||||||0||||||||||F" + cr + etx; sw.Write(h); sw.Write(getcs(h)); sw.Write(cr + lf); sw.Write("$#$"); sw.Write(stx + "4L|1|N" + cr + etx + "07" + cr + lf); sw.Write("$#$" + eot); sw.Close(); Lis_RiZhi.resultlog("输出成功!"); Thread.Sleep(2000); } } } } } data = ""; } } } private string getcs(string h) { byte[] bs = System.Text.Encoding.Default.GetBytes(h); byte hh = bs[0]; for (int i = 1; i < bs.Length; i++) { hh += bs[i]; } string s = hh.ToString("X2"); return s.ToLower(); } } }