1649 lines
		
	
	
		
			50 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			1649 lines
		
	
	
		
			50 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System;
 | ||
| using System.Collections.Generic;
 | ||
| using System.ComponentModel;
 | ||
| using System.Data;
 | ||
| using System.Drawing;
 | ||
| using System.Linq;
 | ||
| using System.Text;
 | ||
| using System.Windows.Forms;
 | ||
| using elelab;
 | ||
| using DeiNiu.wms.win.utils;
 | ||
| using System.Configuration;
 | ||
| using System.Threading;
 | ||
| using System.ServiceModel;
 | ||
| using DeiNiu.wms.win;
 | ||
| using System.ServiceModel.Description;
 | ||
| using DeiNiu.Utils;
 | ||
| using Model;
 | ||
| using System.Net;
 | ||
| using System.IO;
 | ||
| using System.Runtime.Serialization.Json;
 | ||
| using System.Timers;
 | ||
| 
 | ||
| namespace epickHost
 | ||
| {
 | ||
|     public partial class FormHost : Form
 | ||
|     {
 | ||
|         static log4net.ILog log = log4net.LogManager.GetLogger("light");
 | ||
|         String oCom = "";
 | ||
|         Dictionary<int, dnwms> coms = new Dictionary<int, dnwms>();
 | ||
|         dnwms activePort;
 | ||
|         int currentPort = 0;
 | ||
| 
 | ||
|         private ServiceHost host = null;
 | ||
|         public static List<ELabel> labelPool = new List<ELabel>();
 | ||
|         string svrUrl = "http://127.0.0.1:9998/DNLight";
 | ||
|         private string postUrl="";
 | ||
|       
 | ||
|          
 | ||
|  
 | ||
|        
 | ||
|         void startHost2()
 | ||
|         {
 | ||
|           //  lbStatus.Text = "服务启动失败";
 | ||
|             string startupOk = "服务已启动";
 | ||
|             try
 | ||
|             {
 | ||
|                 svrUrl = System.Configuration.ConfigurationManager.AppSettings["BaseUri"];
 | ||
|                 Uri baseAddress = new Uri(svrUrl);
 | ||
|                 ServiceLight service = new ServiceLight(); 
 | ||
|                 service.turnOnLts += this.turnOnLights;
 | ||
|                 service.turnOffLts += this.turnOffLights;
 | ||
|                 service.lightUp += this.lightUp;
 | ||
|                 service.resetPort += this.resetPort;
 | ||
|                 service.getCarIpAddress += this.getCarIpAddress;
 | ||
|                 service.clearLablesByPortOrder += this.clearLablesByPortOrder;
 | ||
|                 service.outMsg += this.addLog;
 | ||
|                 service.turnOffLb += this.turnOffLable;
 | ||
|                 elelab.pick.lightOffEvent += testPicked;
 | ||
| 
 | ||
|                 host = new ServiceHost(service, baseAddress);
 | ||
|                
 | ||
|                 WebHttpBinding binding = new WebHttpBinding();
 | ||
|                 ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IServiceLight), binding, baseAddress);
 | ||
|                 WebHttpBehavior httpBehavior = new WebHttpBehavior();
 | ||
|                 endpoint.Behaviors.Add(httpBehavior);
 | ||
|                  List<string> lstIp =Util.GetLocalIpAddress("InterNetwork");
 | ||
|                  string showSvr = svrUrl;
 | ||
|                 if (lstIp.Count > 0)
 | ||
|                  {
 | ||
|                     showSvr = svrUrl.Replace("127.0.0.1", lstIp[0]);
 | ||
|                  }
 | ||
|             
 | ||
|                 host.Opened += delegate
 | ||
|                 {
 | ||
| 
 | ||
|                     if (InvokeRequired)
 | ||
|                     {
 | ||
|                         this.Invoke(new showStatus(delegate()
 | ||
|                         {
 | ||
|                             lbStatus.Text = startupOk;
 | ||
|                             txtboxUrl.Text = showSvr;
 | ||
|                             addLog("服务已启动");
 | ||
|                            // addLog("累计亮灯次数:" + getConfigValue("count"));  
 | ||
| 
 | ||
|                         }));
 | ||
| 
 | ||
|                     }
 | ||
|                     else
 | ||
|                     {
 | ||
|                         lbStatus.Text = startupOk;
 | ||
|                         txtboxUrl.Text = showSvr;
 | ||
|                         addLog("服务已启动");
 | ||
|                       //  addLog("累计亮灯次数:" + getConfigValue("count"));  
 | ||
|                        // lbStatus.Text = "服务启动失败";
 | ||
|                       //  lbUrl.Text ="";
 | ||
|                     }
 | ||
|                 };
 | ||
|                 host.Open();
 | ||
| 
 | ||
|             }
 | ||
|             catch (Exception e)
 | ||
| 
 | ||
|             {              
 | ||
|                 MessageBox.Show("启动服务失败:  "+e.Message);
 | ||
|                 addLog(e.StackTrace);
 | ||
|             }
 | ||
| 
 | ||
|              
 | ||
|         }
 | ||
| 
 | ||
|         private bool turnOffLable(List<ELabel> lables)
 | ||
|         {
 | ||
|              foreach(ELabel lable in lables)
 | ||
|             {
 | ||
|                 pick.turnOffLabel(currentPort, lable.labelId);
 | ||
|             }
 | ||
|             return true;
 | ||
|         }
 | ||
| 
 | ||
|         private Cart getCarIpAddress(string carNo)
 | ||
|         {
 | ||
|             string ip = getConfigValue(carNo);
 | ||
| 
 | ||
|             if (!vaildService())
 | ||
|             {
 | ||
|                 ip = "无法获取IP地址";
 | ||
|             }
 | ||
|            
 | ||
|             if (String.IsNullOrEmpty(ip))
 | ||
|             {
 | ||
|                 ip= "";
 | ||
|             }
 | ||
| 
 | ||
|             Cart cart = new Cart();
 | ||
|             cart.carNo = carNo;
 | ||
|             cart.ip = ip;
 | ||
|             ip = String.Format("\"carNo\":\"{0}\",\"ip\":\"{1}\"",carNo, ip);
 | ||
|             ip = "{" + ip + "}";
 | ||
|             addLog(ip);
 | ||
|             Thread threadPreProcess = new Thread(clearDis);
 | ||
|             threadPreProcess.IsBackground = true;
 | ||
|             threadPreProcess.Start();
 | ||
|            // clearDis();
 | ||
|            
 | ||
|             return cart;
 | ||
|         }
 | ||
| 
 | ||
|         
 | ||
| 
 | ||
|         public FormHost()
 | ||
|         {
 | ||
|             InitializeComponent();
 | ||
|             elelab.pick.init_port(enumLabelPickType.seed);
 | ||
|             Thread threadPreProcess = new Thread(startHost2);
 | ||
|             threadPreProcess.IsBackground = true;
 | ||
|             threadPreProcess.Start();
 | ||
|             if (elelab.pick.activeComports.Count == 0)
 | ||
|             {
 | ||
|                 lbLightInfo.Text = "拣选设备连接错误";
 | ||
|                 lbComStatus.Text = "拣选设备连接错误";
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 initcoms();
 | ||
|                 lbLightInfo.Text = "控制器就绪";
 | ||
|                 lbComStatus.Text = "控制器就绪";
 | ||
|             }
 | ||
|             comcolor.SelectedIndex = 0;
 | ||
|             txtUrl.Text =  System.Configuration.ConfigurationManager.AppSettings["BaseUri"];
 | ||
|             txtCallBack.Text = System.Configuration.ConfigurationManager.AppSettings["WMSuri"];
 | ||
|             readRowIds();
 | ||
|              string tmp = System.Configuration.ConfigurationManager.AppSettings["hostInterv"];
 | ||
|             if (!string.IsNullOrEmpty(tmp))
 | ||
|             {
 | ||
|                 try {  nud.Value = Convert.ToInt32(tmp); } catch { }
 | ||
|                
 | ||
|             }
 | ||
|            
 | ||
|             initialTimer();
 | ||
|             initialTimer2();
 | ||
|         }
 | ||
| 
 | ||
|         private void Form1_Load(object sender, EventArgs e)
 | ||
|         {
 | ||
|             comboxcolor.SelectedIndex = 0;
 | ||
|         }
 | ||
|         private void initcoms()
 | ||
|         {
 | ||
|             String[] Portname = System.IO.Ports.SerialPort.GetPortNames();
 | ||
| 
 | ||
|             
 | ||
|             comComs.Items.Clear();
 | ||
|             
 | ||
| 
 | ||
|             oCom = System.Configuration.ConfigurationManager.AppSettings["Com"];
 | ||
|             int oldIndex = 0;
 | ||
|             try
 | ||
|             {
 | ||
| 
 | ||
|                 for (int i = 0; i < Portname.Length; i++)
 | ||
|                 {
 | ||
|                     //col.Add(i);
 | ||
|                     comComs.Items.Add(Portname[i]);
 | ||
|                     if (!string.IsNullOrEmpty(oCom) && oCom == Portname[i])
 | ||
|                     {
 | ||
|                         oldIndex = i;
 | ||
|                     }
 | ||
|                 }
 | ||
| 
 | ||
|             }
 | ||
|             finally
 | ||
|             {
 | ||
|                 
 | ||
|             }
 | ||
| 
 | ||
|             comComs.SelectedIndex = oldIndex;
 | ||
|             btnCom.Enabled = comComs.Items.Count > 0;
 | ||
|             
 | ||
|             if (comComs .Items.Count > 0)
 | ||
|             {
 | ||
|                 for (int i = comComs.Items.Count-1; i >= 0; i--)
 | ||
|                 {
 | ||
|                     int port = Convert.ToInt16(comComs.Items[i].ToString().Substring(3));
 | ||
|                     initialCom(port);
 | ||
|                 }
 | ||
|               
 | ||
|             }
 | ||
|             
 | ||
|             btnReset.Enabled = currentPort > 0;
 | ||
|         }
 | ||
|        
 | ||
|         void initialCom(int port)
 | ||
|         {
 | ||
| 
 | ||
|             try
 | ||
|             {
 | ||
|                 //int port = Convert.ToInt16(comComs.Text.Substring(3));
 | ||
|                 resetLights();
 | ||
|                 if (coms.Keys.Contains(port))
 | ||
|                 {
 | ||
|                     if (String.IsNullOrEmpty(oCom) || !oCom.Equals(comComs.Text))
 | ||
|                     {
 | ||
|                         WriteConfig("Com", comComs.Text);
 | ||
|                         oCom = comComs.Text;
 | ||
|                     }
 | ||
| 
 | ||
|                     currentPort = port;
 | ||
|                     this.lbComStatus.Text = string.Format(comComs.Text + " 打开{0}", "成功");
 | ||
|                 }
 | ||
|                 else
 | ||
|                 {
 | ||
|                     this.lbComStatus.Text = string.Format(comComs.Text + " 打开{0}", "失败");
 | ||
|                 }
 | ||
| 
 | ||
|             }
 | ||
|             catch (Exception e)
 | ||
|             {
 | ||
|                 lbComStatus.Text = string.Format(e.Message);
 | ||
|             }
 | ||
|              
 | ||
|         }
 | ||
| 
 | ||
|         private void resetLights()
 | ||
|         {
 | ||
|             if (comComs.Items.Count == 0 || String.IsNullOrEmpty(comComs.Text))
 | ||
|             {
 | ||
|                 return;
 | ||
|             }
 | ||
|             int port = Convert.ToInt16(comComs.Text.Substring(3));
 | ||
|             if (unart_manage.com_manage[port] == null)
 | ||
|             {
 | ||
|                 return;
 | ||
|             }
 | ||
|             activePort = unart_manage.com_manage[port].init_port;
 | ||
|             
 | ||
|             if (pick.reset_device(port))
 | ||
|             {
 | ||
|                 coms[port] = activePort;
 | ||
| 
 | ||
|             }
 | ||
| 
 | ||
|            
 | ||
|         }
 | ||
|         protected void WriteConfig(string key, string value)
 | ||
|         {
 | ||
|             //ShowText("正在写入配置文件!请稍等....");
 | ||
| 
 | ||
|             string appConfigPath = Application.StartupPath + "\\" + Application.ProductName + ".exe"; 
 | ||
| 
 | ||
|             ConfigurationOperator co = new ConfigurationOperator(appConfigPath, ConfigType.ExeConfig);
 | ||
| 
 | ||
|             co.AddAppSetting(key, value);
 | ||
| 
 | ||
|             co.Save();
 | ||
|         }
 | ||
| 
 | ||
|         int baseLightId = 5000;
 | ||
| 
 | ||
|         bool turnOnLights(int portNo, List<ELight> lights)
 | ||
|         {
 | ||
|             if (portNo == 0)
 | ||
|             {
 | ||
|                 portNo = currentPort;
 | ||
|             }
 | ||
|            // bool rt=false;
 | ||
|             List<ELight> fail = new List<ELight>();
 | ||
|             log.Debug("start light on request ------>");
 | ||
|             foreach (ELight el in lights)
 | ||
|             {
 | ||
|                 channel_led id = new channel_led();
 | ||
|                 id.channel_id = baseLightId + el.lightId;
 | ||
|                 //id.config_word = led_rgb;
 | ||
|                 id.color = el.which;
 | ||
|                 id.state = 1;
 | ||
|                 if (el.port == 0)
 | ||
|                 {
 | ||
|                     el.port = portNo;
 | ||
|                 }
 | ||
|                 if (!pick.turnOnLight(el.port, id))
 | ||
|                 {
 | ||
|                     log.Debug(getJson(el) + " light on failed. redo...");
 | ||
| 
 | ||
|                     fail.Add(el);
 | ||
|                 }
 | ||
|                 else
 | ||
|                 {
 | ||
|                     log.Debug(getJson(el));
 | ||
|                 }
 | ||
| 
 | ||
|                  Thread.Sleep(50);
 | ||
|             }
 | ||
| 
 | ||
|             if (fail.Count > 0)
 | ||
|             {
 | ||
|                 turnOnLights(portNo, fail);
 | ||
|             }
 | ||
|             log.Debug("<----------- end light on request ");
 | ||
|             return fail.Count==0;
 | ||
|         }
 | ||
| 
 | ||
|         bool turnOffLights(int portNo, List<ELight> lights)
 | ||
|         {
 | ||
|             if (portNo == 0)
 | ||
|             {
 | ||
|                 portNo = currentPort;
 | ||
|             }
 | ||
|             bool rt = false;
 | ||
|             List<ELight> fail = new List<ELight>();
 | ||
|             log.Debug("start light off request ------>");
 | ||
|             foreach (ELight el in lights)
 | ||
|             {
 | ||
| 
 | ||
|                 channel_led id = new channel_led();
 | ||
|                 id.channel_id = baseLightId + el.lightId;  
 | ||
|                 //id.config_word = led_rgb;
 | ||
|                 id.color = el.which;
 | ||
|                 id.state = 1;
 | ||
|                 if (el.port == 0)
 | ||
|                 {
 | ||
|                     el.port = portNo;
 | ||
|                 }
 | ||
| 
 | ||
|                 if (!pick.turnOffLight(el.port, id))
 | ||
|                 {
 | ||
|                     log.Debug(getJson(el) + " light off failed. redo...");
 | ||
| 
 | ||
|                     fail.Add(el);
 | ||
|                 }
 | ||
|                 else {
 | ||
|                     log.Debug(getJson(el));
 | ||
|                 }
 | ||
|                 Thread.Sleep(50);
 | ||
|                 
 | ||
|             }
 | ||
|             if (fail.Count > 0)
 | ||
|                 {
 | ||
|                     turnOffLights(portNo, fail);
 | ||
|                    
 | ||
|                 }
 | ||
|             log.Debug("<----------- end light off  request ");
 | ||
|             return fail.Count == 0;
 | ||
|         }
 | ||
| 
 | ||
|         private byte getFloor()
 | ||
|         {
 | ||
|             byte floor =0;
 | ||
|             /*if (InvokeRequired)
 | ||
|             {
 | ||
|                 this.Invoke(new showStatus(delegate()
 | ||
|                 {
 | ||
|                     int color = Convert.ToByte(comboxcolor.SelectedIndex) + 1;
 | ||
|                     floor= Convert.ToByte(color);
 | ||
| 
 | ||
|                 }));
 | ||
| 
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 int color = Convert.ToByte(comboxcolor.SelectedIndex) + 1;
 | ||
|                 floor = Convert.ToByte(color);
 | ||
|             }
 | ||
|             */
 | ||
|             int color = comboxcolor.SelectedIndex;
 | ||
|             floor = Convert.ToByte(color);
 | ||
|             return floor;
 | ||
|         }
 | ||
| 
 | ||
|         private void btnReset_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
|         private void btnTest_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|            
 | ||
|         }
 | ||
| 
 | ||
|         private void btnReset_Click_1(object sender, EventArgs e)
 | ||
|         {
 | ||
| 
 | ||
|         }
 | ||
|         
 | ||
|         private void tabPage2_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
|         private void btnTestOn_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             Thread threadPreProcess = new Thread(new ParameterizedThreadStart(turnOnLight));
 | ||
|             threadPreProcess.IsBackground = true;
 | ||
|             threadPreProcess.Start(getFloor());
 | ||
| 
 | ||
|             
 | ||
|         }
 | ||
|         delegate void showStatus();
 | ||
|         void turnOnLight(object floor)
 | ||
|         {
 | ||
|             svrUrl = System.Configuration.ConfigurationManager.AppSettings["BaseUri"];
 | ||
| 
 | ||
|             // svrUrl = svrUrl + "turnOnLightById?id={0}";
 | ||
|             svrUrl = svrUrl + "turnOnLight?id={0}&which={1}&port={2}";
 | ||
|             string lightId = txtLightId.Text.ToString();
 | ||
|             int id = Convert.ToInt16(lightId);
 | ||
|             // int color = 0;
 | ||
|             string portNo = currentPort + "";
 | ||
|             svrUrl = string.Format(svrUrl, id, floor, portNo);
 | ||
|             //   this.lbOnUrl.Text = svrUrl;
 | ||
|             
 | ||
|             this.Invoke(new showStatus(delegate()
 | ||
|             {   txtOut.Text += "Turn On Light:-->  \r\n " + svrUrl + "\r\n";
 | ||
|                 lbOnStatus.Text = HttpGet(svrUrl, null);
 | ||
| 
 | ||
|             }));
 | ||
|            // lbOnStatus.Text = HttpGet(svrUrl, null);
 | ||
|         }
 | ||
| 
 | ||
|         void turnOffLight(object floor)
 | ||
|         {
 | ||
|             svrUrl = System.Configuration.ConfigurationManager.AppSettings["BaseUri"];
 | ||
| 
 | ||
|             // svrUrl = svrUrl + "turnOffLightById?id={0}";
 | ||
|             svrUrl = svrUrl + "turnOffLight?id={0}&which={1}&port={2}";
 | ||
|             string lightId = this.txtLightId.Text;
 | ||
|             int id = Convert.ToInt16(lightId);
 | ||
|             string portNo = currentPort + "";
 | ||
|             svrUrl = string.Format(svrUrl, id, floor, portNo);
 | ||
|             //this.lbOffLight.Text = svrUrl;
 | ||
|             
 | ||
|           //  lbOnStatus.Text = HttpGet(svrUrl, null);
 | ||
|             this.Invoke(new showStatus(delegate()
 | ||
|             {   txtOut.Text += "Turn Off Light:-->  \r\n " + svrUrl + "\r\n";
 | ||
|                 lbOnStatus.Text = HttpGet(svrUrl, null); 
 | ||
|             }));
 | ||
|         }
 | ||
|         private void btnTestOff_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             Thread threadPreProcess = new Thread(new ParameterizedThreadStart(turnOffLight));
 | ||
|             threadPreProcess.IsBackground = true;
 | ||
|             threadPreProcess.Start(getFloor());
 | ||
|           
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// GET请求与获取结果
 | ||
|         /// </summary>
 | ||
|         public static string HttpGet(string Url, string postDataStr)
 | ||
|         {
 | ||
|             try
 | ||
|             {
 | ||
|                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (string.IsNullOrEmpty(postDataStr) ? "" : "?") + postDataStr);
 | ||
|                 request.Method = "GET";
 | ||
|                 request.ContentType = "text/html;charset=UTF-8";
 | ||
|                 request.Timeout = 10000;
 | ||
|                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 | ||
|                 Stream myResponseStream = response.GetResponseStream();
 | ||
|                 StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
 | ||
|                 string retString = myStreamReader.ReadToEnd();
 | ||
|                 myStreamReader.Close();
 | ||
|                 myResponseStream.Close();
 | ||
|                 return retString;
 | ||
|             }
 | ||
|             catch (Exception ex)
 | ||
|             {
 | ||
|                 return ex.Message;
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|   
 | ||
| 
 | ||
|         private void btnReset_Click_2(object sender, EventArgs e)
 | ||
|         {
 | ||
|             resetLights();
 | ||
|         }
 | ||
| 
 | ||
|         private void btnCom_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             int port = Convert.ToInt16(comComs.Text.Substring(3));
 | ||
|             initialCom(port);
 | ||
|         }
 | ||
| 
 | ||
|         private void btnUrl_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             svrUrl = System.Configuration.ConfigurationManager.AppSettings["BaseUri"];
 | ||
|             if (svrUrl.Equals(txtUrl.Text))
 | ||
|             {
 | ||
|                 return;
 | ||
|             }
 | ||
| 
 | ||
|             WriteConfig("BaseUri", txtUrl.Text);
 | ||
| 
 | ||
|             MessageBox.Show("地址修改成功,重启程序后生效。", "通知", MessageBoxButtons.OK, MessageBoxIcon.Information);
 | ||
|             restart();
 | ||
|         }
 | ||
|         Dictionary<string,string> dicRowId = new Dictionary<string,string >(); // 
 | ||
|         bool lightUp(List<ELabel> labels)
 | ||
|         {
 | ||
|             //TODO: 把传来的实体按portNo分组,按portNo 分别点亮
 | ||
| 
 | ||
|             writeBadRowIds();
 | ||
|             Dictionary<int, List<ELabel>> lbsOrder = new Dictionary<int, List<ELabel>>(); //按orderNo 分组
 | ||
|             Dictionary<int, List<ELabel>> lbsPort;  //按port分组
 | ||
|             foreach (ELabel el in labels)
 | ||
|             {
 | ||
|                 if (el == null)
 | ||
|                 {
 | ||
|                     break;
 | ||
|                 }
 | ||
| 
 | ||
|                 if (el.orderNo > 200 || el.orderNo < 1)
 | ||
|                 {
 | ||
| 
 | ||
|                     // addLog( "orderNo 只能取1-200范围的值");
 | ||
|                     //  addLog("eorderNo 错误:" + getJson(el));
 | ||
| 
 | ||
|                 }
 | ||
| 
 | ||
|                 //  lbsPort.ContainsKey(el.port);
 | ||
|                 // if (!lbsPort.Keys.Contains(el.port))
 | ||
|                 if (!lbsOrder.ContainsKey(el.orderNo))
 | ||
|                 {
 | ||
|                     lbsOrder[el.orderNo] = new List<ELabel>();
 | ||
|                 }
 | ||
| 
 | ||
|                 lbsOrder[el.orderNo].Add(el);
 | ||
| 
 | ||
|             }
 | ||
| 
 | ||
|             foreach (int keyOrder in lbsOrder.Keys)
 | ||
|             {
 | ||
|                 List<ELabel> lbs = lbsOrder[keyOrder];
 | ||
|                 lbsPort = new Dictionary<int, List<ELabel>>();
 | ||
|                 foreach (ELabel el in lbs)
 | ||
|                 {
 | ||
|                     //  lbsPort.ContainsKey(el.port);
 | ||
|                     // if (!lbsPort.Keys.Contains(el.port))
 | ||
|                     if (!lbsPort.ContainsKey(el.port))
 | ||
|                     {
 | ||
|                         lbsPort[el.port] = new List<ELabel>();
 | ||
|                     }
 | ||
| 
 | ||
|                     lbsPort[el.port].Add(el);
 | ||
|                     string rowKey = getRowKey(el);
 | ||
|                     dicRowId[rowKey] = el.rowId+"";
 | ||
|                 }
 | ||
| 
 | ||
| 
 | ||
|                 foreach (int keyPort in lbsPort.Keys)
 | ||
|                 {
 | ||
|                   
 | ||
|                     if (!doLightUp(keyOrder, keyPort, lbsPort[keyPort]))
 | ||
|                     {
 | ||
|                         return false ;
 | ||
|                     }  Thread.Sleep(100);
 | ||
|                 }
 | ||
|             }
 | ||
|             return true ; 
 | ||
| 
 | ||
|         }
 | ||
|         string getRowKey(ELabel el)
 | ||
|         {
 | ||
|             return el.labelId + ":" + el.color;
 | ||
|         }
 | ||
|         bool doLightUp(int orderNo, int portNo, List<ELabel> lables)
 | ||
|         {
 | ||
|            
 | ||
|             int[] ids = new int[lables.Count];
 | ||
|             int[] address = new int[lables.Count]; ;//标签坐标(行、列)
 | ||
|             int[] counts = new int[lables.Count]; ;//显示数量
 | ||
|             int[] colors = new int[lables.Count]; ;
 | ||
| 
 | ||
|             addLog("receive light request --->");
 | ||
|             for (int i = 0; i < lables.Count; i++)
 | ||
|             {
 | ||
|                 ids[i] = lables[i].labelId;
 | ||
|                 address[i] = lables[i].num1;
 | ||
|                 counts[i] = lables[i].num3;
 | ||
|                 colors[i] = lables[i].color;
 | ||
|                 string json = getJson(lables[i]);
 | ||
|                 addLog(json);
 | ||
|             }
 | ||
|            
 | ||
|             addLog("<--- receive light request end");
 | ||
|             //elelab.DNLights.initPort(); 
 | ||
|             if (portNo == 0 && currentPort > 0)
 | ||
|             {
 | ||
|                 portNo = currentPort;
 | ||
|             }
 | ||
| 
 | ||
|             bool result = false;
 | ||
|            
 | ||
|                  //result = DNLights.lightLabels(portNo, colors, ids, address, counts);
 | ||
| 
 | ||
|             if (chkClear.Checked)
 | ||
|             {
 | ||
|                 if (orderNo <= 0)
 | ||
|                 {
 | ||
|                     orderNo = 1;
 | ||
|                 }
 | ||
|             }
 | ||
| 
 | ||
|             saveRowIds();
 | ||
| 
 | ||
|             result = elelab.pick.lightLabels(lables,chkClear.Checked, orderNo, portNo, enumLabelPickType.seed);
 | ||
| 
 | ||
| 
 | ||
|             if (!result)
 | ||
|             {
 | ||
|                 addLog(string.Format("亮灯失败: orderNo: {0},portNo: {1} ", orderNo, portNo));
 | ||
| 
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 
 | ||
| 
 | ||
|                 addLog(string.Format("亮灯成功: orderNo: {0},portNo: {1} ", orderNo, portNo));
 | ||
|                 int cnt = Convert.ToInt32( getConfigValue("count"))+1;
 | ||
|                 writeConfig("count", cnt + "");
 | ||
| 
 | ||
|             }
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|             return result;
 | ||
| 
 | ||
| 
 | ||
|         }
 | ||
|         char splitStr = '%';
 | ||
|        
 | ||
|         /// <summary>
 | ||
|         /// rowId 保存到配置文件
 | ||
|         /// 每次亮灯时调用
 | ||
|         /// </summary>
 | ||
|         private void saveRowIds()
 | ||
|         {
 | ||
|            
 | ||
|             string value = "";
 | ||
|             foreach(string rowKey in dicRowId.Keys)
 | ||
|             {
 | ||
|                 value+=rowKey+"#"+dicRowId[rowKey]+splitStr;
 | ||
|             }
 | ||
|             writeConfig("rowIds", value);
 | ||
|         }
 | ||
|         /// <summary>
 | ||
|         /// 从配置文件读取rowId 和亮灯信息
 | ||
|         /// 程序启动时调用
 | ||
|         /// </summary>
 | ||
|         private void readRowIds()
 | ||
|         {
 | ||
|             try { 
 | ||
|             string[] vs = getConfigValue("rowIds") .Split(splitStr);
 | ||
|             dicRowId.Clear();
 | ||
|             foreach(string value in vs)// 21:2#1234567
 | ||
|             {
 | ||
|                 string[] tmp = value.Split('#');
 | ||
|                 if (tmp.Length > 1)
 | ||
|                 {
 | ||
|                     dicRowId[tmp[0]] = tmp[1];
 | ||
|                 }
 | ||
|             }
 | ||
|             }
 | ||
|             catch(Exception ex)
 | ||
|             {
 | ||
|                 addLog(ex.Message);
 | ||
|             }
 | ||
|             readBadRowIds();
 | ||
|         }
 | ||
|         /// <summary>
 | ||
|         /// 
 | ||
|         /// 
 | ||
|         /// </summary>
 | ||
|           List<string> badRowIds = new List<string>();
 | ||
|         /// <summary>
 | ||
|         /// 按灭时加入新rowId
 | ||
|         /// 接口返回时去除rowId
 | ||
|         /// </summary>
 | ||
|        // List<string> finishedRowIds = new List<string>();
 | ||
|         /// <summary>
 | ||
|         /// 从配置文件读取返回接口失败的rowId
 | ||
|         /// 程序启动时调用
 | ||
|         /// 亮灯时调用
 | ||
|         /// </summary>
 | ||
|         private void readBadRowIds()
 | ||
|         {
 | ||
|             try
 | ||
|             {
 | ||
|                 badRowIds = getConfigValue("badRowIds").Split(splitStr).ToList();
 | ||
|                  
 | ||
|             }
 | ||
|             catch (Exception ex)
 | ||
|             {
 | ||
|                 addLog(ex.Message);
 | ||
|             }
 | ||
| 
 | ||
|         }
 | ||
|         /// <summary>
 | ||
|         /// 保存接口失败的rowId
 | ||
|         /// 调用失败时保存
 | ||
|         /// </summary>
 | ||
|         private void writeBadRowIds()
 | ||
|         {
 | ||
|             //if (badRowIds.Count == 0)
 | ||
|            //     return;
 | ||
|             string value = "";
 | ||
|             foreach (string rowId in badRowIds)
 | ||
|             {
 | ||
|                 value += rowId +    splitStr;
 | ||
|             }
 | ||
|             writeConfig("badRowIds", value);
 | ||
| 
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
| 
 | ||
|         void clearLablesByPortOrder(int port, int order)
 | ||
|         {
 | ||
|             if (port == -1)
 | ||
|             {
 | ||
|                 port = currentPort;
 | ||
|             }
 | ||
|             if (order == -1)
 | ||
|             {
 | ||
|                 order = 1;
 | ||
|             }
 | ||
|             elelab.pick.clearDis(port, order);
 | ||
|             addLog("port " + currentPort + " cleared");
 | ||
|         }
 | ||
|      
 | ||
|         private void clearDis()
 | ||
|         {
 | ||
|             clearLablesByPortOrder(currentPort, 1);
 | ||
|         }
 | ||
|         /*void lightUp(List<ELabel> lables)
 | ||
|         {
 | ||
|             int portNo = currentPort;
 | ||
| 
 | ||
|             if (portNo == 0)
 | ||
|             {
 | ||
|                 addLog("无活动端口,亮灯失败");
 | ||
|                 return;
 | ||
|             }
 | ||
|               
 | ||
|             elelab.pick.init_port(enumLabelPickType.seed);
 | ||
|             elelab.pick.lightLabels(lables, portNo,  enumLabelPickType.seed);
 | ||
|         }
 | ||
|         */
 | ||
| 
 | ||
| 
 | ||
|        // delegate void showStatus();
 | ||
|         void testPicked(int orderNo, int eleId, int color, int eleAddress, int count,int num2)
 | ||
|         {
 | ||
| 
 | ||
|             ELabel lb = new ELabel();
 | ||
|             lb.num3 = count;
 | ||
|             lb.color = color;
 | ||
|             lb.num1 = eleAddress;
 | ||
|             lb.orderNo = orderNo;
 | ||
|             lb.labelId = eleId;
 | ||
|             lb.num2 = num2;
 | ||
|             int rowId = dicRowId.ContainsKey(getRowKey(lb)) ? Convert.ToInt32(dicRowId[getRowKey(lb)]) : 0;
 | ||
|             lb.rowId = rowId;
 | ||
| 
 | ||
|             if (rowId>0)
 | ||
|             {
 | ||
|                 badRowIds.Add(rowId+"");
 | ||
|             }
 | ||
|            
 | ||
|             testPickedRowId(lb.rowId); //调用客户第三方的api,通知灯已经灭掉了
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|             /*
 | ||
|             if (InvokeRequired)
 | ||
|             {
 | ||
|                 this.Invoke(new showStatus(delegate()
 | ||
|                 {
 | ||
|                     returnResult(lb);
 | ||
|                 }));
 | ||
| 
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 returnResult(lb);
 | ||
|             }
 | ||
|             */
 | ||
| 
 | ||
|         }
 | ||
|         void testPickedRowId(int rowId)
 | ||
|         {
 | ||
|             ELabel lb = new ELabel();
 | ||
| 
 | ||
|             lb.rowId = rowId;
 | ||
|             if (InvokeRequired)
 | ||
|             {
 | ||
|                 this.Invoke(new showStatus(delegate ()
 | ||
|                 {
 | ||
|                     returnResult(lb);
 | ||
|                 }));
 | ||
| 
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 returnResult(lb);
 | ||
|             }
 | ||
| 
 | ||
|         }
 | ||
|         private void returnResult(ELabel lb)
 | ||
|         {
 | ||
|             //to show result
 | ||
| 
 | ||
|             //to call client and send data
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|             String url = svrUrl + "testPost";
 | ||
|             addLog("收到按键返回事件,开始回传..." + getJson(lb));
 | ||
|             //  addLog(getJson(lb));
 | ||
|             string rowId = lb.rowId + "";
 | ||
| 
 | ||
|             // if (dicRowId.ContainsKey(getRowKey(lb)))
 | ||
|             //      rowId = dicRowId[getRowKey(lb)];
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|             //  String s = getJson(lb);
 | ||
|             postUrl = System.Configuration.ConfigurationManager.AppSettings["WMSuri"];
 | ||
|             if (postUrl.StartsWith("http"))
 | ||
|             {
 | ||
|                 Thread threadPreProcess = new Thread(new ParameterizedThreadStart(HttpPost));
 | ||
|                 threadPreProcess.IsBackground = true;
 | ||
|                 threadPreProcess.Start(lb);
 | ||
|             }
 | ||
|             else if (postUrl.StartsWith("https"))
 | ||
|             {
 | ||
|                 Thread threadPreProcess = new Thread(new ParameterizedThreadStart(HttpsPost));
 | ||
|                 threadPreProcess.IsBackground = true;
 | ||
|                 threadPreProcess.Start(lb);
 | ||
|             }
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
|         private void returnResult(int orderNo, int eleId, int color, int eleAddress, int count, int num2)
 | ||
|         {
 | ||
|             //to show result
 | ||
| 
 | ||
|             //to call client and send data
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|             ELabel lb = new ELabel();
 | ||
|             lb.num3 = count;
 | ||
|             lb.color = color;
 | ||
|             lb.num1 = eleAddress;
 | ||
|             lb.orderNo = orderNo;
 | ||
|             lb.labelId = eleId;
 | ||
|             lb.num2 = num2;
 | ||
|            
 | ||
|             String url = svrUrl + "testPost";
 | ||
|             addLog("收到按键返回事件,开始回传...");
 | ||
|             addLog(getJson(lb));
 | ||
| 
 | ||
| 
 | ||
|             //  String s = getJson(lb);
 | ||
|             postUrl =   System.Configuration.ConfigurationManager.AppSettings["WMSuri"];
 | ||
|             Thread threadPreProcess = new Thread(new ParameterizedThreadStart(HttpPost));
 | ||
|             threadPreProcess.IsBackground = true;
 | ||
|             threadPreProcess.Start(lb);
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
| 
 | ||
|         private void HttpPost(Object ticket)
 | ||
|         {
 | ||
|             //string Url = svrUrl + "testPost";
 | ||
|             DataContractJsonSerializer serializer = new DataContractJsonSerializer(ticket.GetType());
 | ||
|             MemoryStream stream = new MemoryStream();
 | ||
|             serializer.WriteObject(stream, ticket);
 | ||
|             byte[] dataBytes = new byte[stream.Length];
 | ||
|             stream.Position = 0;
 | ||
|             stream.Read(dataBytes, 0, (int)stream.Length);
 | ||
|             string param = Encoding.UTF8.GetString(dataBytes);
 | ||
|             byte[] bs = Encoding.ASCII.GetBytes(param);
 | ||
|             HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(postUrl);
 | ||
|             req.Method = "POST";
 | ||
|             req.ContentType = "application/json";
 | ||
|             req.ContentLength = bs.Length;
 | ||
|             try
 | ||
|             {
 | ||
|                 using (Stream reqStream = req.GetRequestStream())
 | ||
|                 {             
 | ||
|                    reqStream.Write(bs, 0, bs.Length);
 | ||
|                  }
 | ||
|                           
 | ||
|                 req.BeginGetResponse(new AsyncCallback(Compleate), req);
 | ||
|             }
 | ||
|             catch(Exception e)
 | ||
|             {
 | ||
|                 addLog(e.Message);
 | ||
|             }
 | ||
|             /*
 | ||
|             HttpWebResponse hwr = req.GetResponse() as HttpWebResponse;
 | ||
|             System.IO.StreamReader myreader = new System.IO.StreamReader(hwr.GetResponseStream(), Encoding.UTF8);
 | ||
|             string responseText = myreader.ReadToEnd();
 | ||
|             Console.WriteLine( responseText);
 | ||
|              * */
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
|         private void HttpsPost(Object ticket)
 | ||
|         {
 | ||
|             //string Url = svrUrl + "testPost";
 | ||
|             DataContractJsonSerializer serializer = new DataContractJsonSerializer(ticket.GetType());
 | ||
|             MemoryStream stream = new MemoryStream();
 | ||
|             serializer.WriteObject(stream, ticket);
 | ||
|             byte[] dataBytes = new byte[stream.Length];
 | ||
|             stream.Position = 0;
 | ||
|             stream.Read(dataBytes, 0, (int)stream.Length);
 | ||
|             string param = Encoding.UTF8.GetString(dataBytes);
 | ||
|             byte[] bs = Encoding.ASCII.GetBytes(param);
 | ||
|             HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(postUrl);
 | ||
|             req.Method = "POST";
 | ||
|             req.ContentType = "application/json";
 | ||
|             req.ContentLength = bs.Length;
 | ||
|             try
 | ||
|             {
 | ||
|                 using (Stream reqStream = req.GetRequestStream())
 | ||
|                 {
 | ||
|                     reqStream.Write(bs, 0, bs.Length);
 | ||
|                 }
 | ||
| 
 | ||
|                 req.BeginGetResponse(new AsyncCallback(Compleate), req);
 | ||
|             }
 | ||
|             catch (Exception e)
 | ||
|             {
 | ||
|                 addLog(e.Message);
 | ||
|             }
 | ||
|             /*
 | ||
|             HttpWebResponse hwr = req.GetResponse() as HttpWebResponse;
 | ||
|             System.IO.StreamReader myreader = new System.IO.StreamReader(hwr.GetResponseStream(), Encoding.UTF8);
 | ||
|             string responseText = myreader.ReadToEnd();
 | ||
|             Console.WriteLine( responseText);
 | ||
|              * */
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
|         public void Compleate(IAsyncResult asyncResult)
 | ||
|         {
 | ||
|             try
 | ||
|             {
 | ||
|                 HttpWebRequest req = (asyncResult.AsyncState as HttpWebRequest);
 | ||
|                 HttpWebResponse res = req.EndGetResponse(asyncResult) as HttpWebResponse;
 | ||
|                 StreamReader reader = new StreamReader(res.GetResponseStream());
 | ||
|                 string responseText = reader.ReadToEnd();
 | ||
|                 Console.WriteLine(responseText);
 | ||
|                 this.Invoke(new showStatus(delegate()
 | ||
|                 {
 | ||
|                     addLog("收到服务器响应: " + responseText);
 | ||
|                     addLog("-------------------------------------------------------------");
 | ||
|                 }));
 | ||
| 
 | ||
|                 if (res.StatusCode == HttpStatusCode.OK)
 | ||
|                 {
 | ||
|                    // ELabel elb = new ELabel();
 | ||
|                  //   elb.rowId = 1;
 | ||
|                  //   elb.labelId = 2;
 | ||
| 
 | ||
|                   // responseText = getJson(elb);
 | ||
|                   //  addLog("---->");
 | ||
|                    // addLog(getJson(elb));
 | ||
|                  //   addLog("<----");
 | ||
|                     var mStream = new MemoryStream(Encoding.Default.GetBytes(responseText));
 | ||
|                     try {
 | ||
|                     ELabel lb = (ELabel)jsonSerializer.ReadObject(mStream);
 | ||
|                     string rowid = lb.rowId + "";
 | ||
|                     addLog("收到服务器响应 rowid: " + rowid );
 | ||
|                     if (badRowIds.Contains(rowid))
 | ||
|                     {
 | ||
|                         badRowIds.Remove(rowid);
 | ||
|                        addLog(" 已处理 rowid: " + rowid );
 | ||
|                             //   return;
 | ||
|                         }
 | ||
|                     }
 | ||
|                     catch(Exception er) {
 | ||
|                         addLog("收到服务器响应:\n" + responseText +", 不包含亮灯数据");
 | ||
|                     }
 | ||
|                 }   
 | ||
|                   else
 | ||
|                 {  
 | ||
|                     writeBadRowIds();
 | ||
|                 }
 | ||
|              
 | ||
|              //   Console.WriteLine(responseText);
 | ||
|                 // MessageBox.Show(reader.ReadToEnd());
 | ||
|             }
 | ||
|             catch (Exception e)
 | ||
|             {
 | ||
|                 addLog("错误消息:" + e.Message);
 | ||
|                 string errStack = "";
 | ||
|                 foreach (char s in e.StackTrace)
 | ||
|                 {
 | ||
|                     errStack += s;
 | ||
|                 }
 | ||
|                 addLog(errStack);
 | ||
| 
 | ||
|                 //MessageBox.Show("获取失败.");
 | ||
|             }
 | ||
|         }
 | ||
|  
 | ||
| 
 | ||
|        System.Runtime.Serialization.Json.DataContractJsonSerializer jsonSerializer
 | ||
|                          = new DataContractJsonSerializer(typeof(ELabel));
 | ||
|         public string getJson(ELabel label)
 | ||
|         {
 | ||
|              return Util.getLbJson(label);
 | ||
|         }
 | ||
| 
 | ||
|         public string getJson(ELight light)
 | ||
|         {
 | ||
|             string json = "";
 | ||
|             /*   if (label.port == 0) //按键返回信息
 | ||
|                {
 | ||
|                    json = String.Format("\"labelId\":{0},\"num1\":{1},\"num2\":{2},\"color\":{3},\"orderNo\":{4}"
 | ||
|                   , label.labelId, label.num1, label.num2 , label.color, label.orderNo);
 | ||
|         
 | ||
|                }else //亮灯信息
 | ||
|               */
 | ||
|             json = String.Format("\"lightId\":{0},\"which\":{1}, \"port\":{2}"
 | ||
|               , light.lightId,  light.which, light.port);
 | ||
|              
 | ||
| 
 | ||
|             return "{" + json + "}";
 | ||
|         }
 | ||
|         delegate void showLog();
 | ||
|         void addLog(string info)
 | ||
|         {
 | ||
|             if (InvokeRequired)
 | ||
|             {
 | ||
|                 this.Invoke(new showLog(delegate()
 | ||
|                 {
 | ||
|                     doLogTxt(info);
 | ||
|                 }));
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 doLogTxt(info);
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
| 
 | ||
|         void doLogTxt(string info)
 | ||
|         {
 | ||
|             if (this.textLog.Lines.Length > 100)
 | ||
|             {
 | ||
|                 this.textLog.Text = "";
 | ||
|             }
 | ||
| 
 | ||
|             while (info.Length < 100)
 | ||
|             {
 | ||
|                 info += " ";
 | ||
|             }            
 | ||
|             
 | ||
|             log.Debug(getMsg("ServiceForm", info));
 | ||
|             
 | ||
|             info = " " + System.DateTime.Now + "    " + info;
 | ||
|             this.textLog.Text = info + "\r\n" + this.textLog.Text;
 | ||
| 
 | ||
|            
 | ||
| 
 | ||
|             if (this.textBoxLog.Lines.Length > 100)
 | ||
|             {
 | ||
|                 this.textBoxLog.Text = "";
 | ||
|             }
 | ||
|             this.textBoxLog.Text = "\r\n" + info + "\r\n" + this.textBoxLog.Text;
 | ||
| 
 | ||
|             if (this.txtOut.Lines.Length > 100)
 | ||
|             {
 | ||
|                 this.txtOut.Text = "";
 | ||
|             }
 | ||
|             this.txtOut.Text = "\r\n" + info + "\r\n" + this.txtOut.Text;
 | ||
|             
 | ||
|             
 | ||
|         }
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|         static string getMsg(string className, string msg)
 | ||
|         {
 | ||
|             return string.Format("{0}  {1}", className, msg);
 | ||
|         }
 | ||
| 
 | ||
| 
 | ||
|         ELabel getTestLabel()
 | ||
|         {
 | ||
|             int color = comcolor.SelectedIndex + 1;
 | ||
|             int num1 = 0;
 | ||
|             int num2 = 0;
 | ||
|             int num3 = 0;
 | ||
|             int id = 1;
 | ||
| 
 | ||
|             try
 | ||
|             {
 | ||
|                 num1 = Convert.ToInt16(txtNumber1.Text);
 | ||
|                 num2 = Convert.ToInt32(txtNum2.Text);
 | ||
|                 num3 = Convert.ToInt32(txtNum3.Text);
 | ||
|                 id = Convert.ToInt32(txtLabelId.Text);
 | ||
|             }
 | ||
|             catch
 | ||
|             {
 | ||
|                 MessageBox.Show("请输入整数");
 | ||
|                 return new ELabel();
 | ||
|             }
 | ||
| 
 | ||
|             ELabel el = new ELabel();
 | ||
|             el.rowId = new Random().Next();
 | ||
|             el.labelId = id;
 | ||
|             el.num2 = num2;
 | ||
|             el.num3 = num3;
 | ||
|             el.num1 = num1;
 | ||
|             el.color = color;// new Random().Next(1, 6);
 | ||
|             el.port = currentPort;
 | ||
|             return el;
 | ||
|         }
 | ||
|         private void btnTestLabel_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             svrUrl = System.Configuration.ConfigurationManager.AppSettings["BaseUri"];
 | ||
|            
 | ||
|       
 | ||
|             List<ELabel> list = new List<ELabel>();
 | ||
|             list.Add(getTestLabel());
 | ||
|             lightUp(list);
 | ||
| 
 | ||
|           /*  postUrl = svrUrl + "lightLabels";
 | ||
|             addLog("开始测试亮灯");
 | ||
|           
 | ||
|             
 | ||
|             
 | ||
|             Thread threadPreProcess = new Thread(new ParameterizedThreadStart(HttpPost));
 | ||
|             threadPreProcess.IsBackground = true;
 | ||
|             threadPreProcess.Start(els);
 | ||
| 
 | ||
|             addLog(postUrl);
 | ||
| 
 | ||
|             String json = "亮灯数据:["+ getJson(el) +"]";
 | ||
| 
 | ||
|             addLog(json);
 | ||
|             */
 | ||
|             /*
 | ||
|             int color = 0;
 | ||
|             for (int i = 1; i < 6; i++)
 | ||
|             {
 | ||
|                 color++;
 | ||
|                 if (color > 7)
 | ||
|                 {
 | ||
|                     color = 0;
 | ||
|                 }
 | ||
|                 testLight(i + 1, color);
 | ||
|                 Thread.Sleep(100);
 | ||
|                 //break;
 | ||
|             }
 | ||
| 
 | ||
|             */
 | ||
|         }
 | ||
|         void testLight(int orderNo, int color)
 | ||
|         {
 | ||
|             svrUrl = System.Configuration.ConfigurationManager.AppSettings["BaseUri"];
 | ||
| 
 | ||
|             ELabel[] els = new ELabel[3];
 | ||
| 
 | ||
|             for (int i = 1; i < 4; i++)
 | ||
|             {
 | ||
|                 ELabel el = new ELabel();
 | ||
| 
 | ||
|                 el.labelId = i;
 | ||
|                 el.num3 = new Random().Next(1, 8888);
 | ||
|                 el.num1 = new Random().Next(1, 88);
 | ||
|                 el.color = color;// new Random().Next(1, 6);
 | ||
|                 el.port = currentPort;
 | ||
|                 el.orderNo = orderNo;
 | ||
| 
 | ||
|                 els[i - 1] = el;
 | ||
|                 Thread.Sleep(100);
 | ||
|             }
 | ||
| 
 | ||
|             /*for (int i = 5; i < 9; i++)
 | ||
|               {
 | ||
|                   ELabel el1 = new ELabel();
 | ||
|                   el1.labelId = i;// new Random().Next(5, 9);
 | ||
|                   el1.count = new Random().Next(1, 8666);
 | ||
|                   el1.address = new Random().Next(1, 86);
 | ||
|                   el1.color = color; //new Random().Next(1, 6);
 | ||
|                   el1.port = 9;
 | ||
|                   el1.orderNo = orderNo;
 | ||
| 
 | ||
|                   els[i-1] = el1;
 | ||
|               }
 | ||
|               */
 | ||
|             postUrl = svrUrl + "lightLabels";
 | ||
|             addLog("开始测试亮灯");
 | ||
|             Thread threadPreProcess = new Thread(new ParameterizedThreadStart(HttpPost));
 | ||
|             threadPreProcess.IsBackground = true;
 | ||
|             threadPreProcess.Start(els);
 | ||
|         }
 | ||
| 
 | ||
|         private void btnRestart_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             restart();
 | ||
|         }
 | ||
|         private void restart()
 | ||
|         {
 | ||
|             System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
 | ||
|             System.Environment.Exit(0);
 | ||
|         }
 | ||
|         private void btnCallback_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             postUrl = System.Configuration.ConfigurationManager.AppSettings["WMSuri"];
 | ||
|             if (postUrl.Equals(this.txtCallBack.Text))
 | ||
|             {
 | ||
|                 return;
 | ||
|             }
 | ||
| 
 | ||
|             WriteConfig("WMSuri", txtCallBack.Text);
 | ||
|             restart();
 | ||
|         }
 | ||
| 
 | ||
|         private void btnTestBack_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             postUrl = System.Configuration.ConfigurationManager.AppSettings["WMSuri"]; ;
 | ||
|             addLog("开始测试回传WMS");
 | ||
|             Thread threadPreProcess = new Thread(new ParameterizedThreadStart(HttpPost));
 | ||
|             threadPreProcess.IsBackground = true;
 | ||
|             threadPreProcess.Start(getTestLabel());
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
|         bool resetPort(int port)
 | ||
|         {
 | ||
|             if (port <= 0)
 | ||
|             {
 | ||
|                 port = currentPort;
 | ||
|             }
 | ||
| 
 | ||
|             if (pick.reset_device(port))
 | ||
|             {
 | ||
|                 addLog("Reset Port " + port + " succeed.");
 | ||
|                 return true;
 | ||
|             }
 | ||
|            
 | ||
|             addLog("Reset Port " + port + " failed.");
 | ||
|             return false;
 | ||
|         }
 | ||
| 
 | ||
|         private void FormHost_FormClosing(object sender, FormClosingEventArgs e)
 | ||
|         {
 | ||
|             writeBadRowIds();
 | ||
|             resetPort(0);
 | ||
|         }
 | ||
| 
 | ||
| 
 | ||
|         /// <summary>  
 | ||
|         /// 写入配置信息  
 | ||
|         /// </summary>  
 | ||
|         protected void writeConfig(string key, string value)
 | ||
|         {
 | ||
|             //ShowText("正在写入配置文件!请稍等....");
 | ||
| 
 | ||
|             string appConfigPath = Application.StartupPath + "\\" + Application.ProductName + ".exe";
 | ||
| 
 | ||
|             ConfigurationOperator co = new ConfigurationOperator(appConfigPath, ConfigType.ExeConfig);
 | ||
| 
 | ||
|             co.AddAppSetting(key, value);
 | ||
| 
 | ||
| 
 | ||
|             co.Save();
 | ||
|         }
 | ||
| 
 | ||
|         protected string getConfigValue(string key)
 | ||
|         {
 | ||
|             return System.Configuration.ConfigurationManager.AppSettings[key];
 | ||
|         }
 | ||
| 
 | ||
|         private void label13_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
|         private void btnSetCartIP_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             string carNo = "",ip="";
 | ||
| 
 | ||
|             string carIp = txtCartIp.Text;
 | ||
|             string[] ss = carIp.Split(':');
 | ||
|             if (ss.Length > 1)
 | ||
|             {
 | ||
|                 carNo = ss[0];
 | ||
|                 ip = ss[1];
 | ||
| 
 | ||
|                 writeConfig(carNo, ip);
 | ||
| 
 | ||
|                 MessageBox.Show("设置成功");
 | ||
|                 txtCartIp.Text = "";
 | ||
|                 restart();
 | ||
|             }
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
| 
 | ||
|         private bool vaildService()
 | ||
|         {
 | ||
|           string cnt =  getConfigValue("Service");
 | ||
|           int value = Convert.ToInt32(cnt);
 | ||
| 
 | ||
|           if (value > 0)
 | ||
|           {
 | ||
|               value++;
 | ||
|               writeConfig("Service",value+"");
 | ||
|               return value > 3000000;
 | ||
|           }
 | ||
|           else
 | ||
|           {
 | ||
|             return true;
 | ||
|           }
 | ||
|  
 | ||
|         }
 | ||
|         bool isShowID = true;
 | ||
|         private void btnShowId_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             if (isShowID)
 | ||
|             {
 | ||
| 
 | ||
|                 btnShowId.Text = "Clear ID";
 | ||
|                 pick.displayLabels(currentPort);
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 btnShowId.Text = "Show ID";
 | ||
|                 pick.clearLabels(currentPort);
 | ||
|             }
 | ||
| 
 | ||
| 
 | ||
|             isShowID = !isShowID;
 | ||
|         }
 | ||
| 
 | ||
|         private void txtBarcode_KeyDown(object sender, KeyEventArgs e)
 | ||
|         {
 | ||
|             if (e.KeyCode == Keys.Enter)
 | ||
|             {
 | ||
|             }
 | ||
|             
 | ||
|         }
 | ||
|         int index = -1;
 | ||
|         private void txtBarcode_TextChanged(object sender, EventArgs e)
 | ||
|         {
 | ||
|             string barcode = txtBarcode.Text;
 | ||
|             if (barcode.Length == 9)
 | ||
|             {
 | ||
|                 txtBarcode.SelectAll();
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 return;
 | ||
|             }
 | ||
| 
 | ||
|             if (index >= 0 && index<=5)
 | ||
|             {
 | ||
|                 turnOffLight(index); Thread.Sleep(100);
 | ||
|             }
 | ||
|                 switch (barcode)
 | ||
|                 {
 | ||
|                     case "H-1-3-209":
 | ||
|                        
 | ||
|                         index = 0;
 | ||
|                         break;
 | ||
|                     case "H-1-3-208":
 | ||
|                         index = 1;
 | ||
|                         break;
 | ||
| 
 | ||
|                     case "H-1-3-207":
 | ||
|                         index = 2;
 | ||
|                         break;
 | ||
|                     case "H-1-3-206":
 | ||
|                         index = 3;
 | ||
|                         break;
 | ||
|                     case "H-1-3-205":
 | ||
|                         index = 4;
 | ||
|                         break; 
 | ||
|                     default:
 | ||
|                         index = 6;
 | ||
|                         break;
 | ||
|                 }
 | ||
|                 if (index < 5)
 | ||
|                 {
 | ||
|                     lbLightColor.Text = "格子编号:010"+(index +1);
 | ||
|                     string log = barcode + " 识别成功,格子 " + (index+1) + " 开始亮灯...";
 | ||
|                     txtOut.Text  = log + "\r\n"; 
 | ||
|                     turnOnLight(index);
 | ||
|                 }
 | ||
|                 else
 | ||
|                 {
 | ||
|                     lbLightColor.Text = "无法识别";
 | ||
|                     string log = barcode + " 无法识别,亮灯失败!!!!!" ;
 | ||
|                     txtOut.Text = log + "\r\n"; 
 | ||
|                 }
 | ||
|              
 | ||
|         }
 | ||
|          
 | ||
|     
 | ||
|         void resetLights(int port)
 | ||
|         {
 | ||
|             postUrl = " http://192.168.0.102:9998/DNlight/resetPort";
 | ||
|             Thread threadPreProcess = new Thread(new ParameterizedThreadStart(HttpPost));
 | ||
|             threadPreProcess.IsBackground = true;
 | ||
|             threadPreProcess.Start(port);
 | ||
|         }
 | ||
| 
 | ||
|         private void btnOffLabel_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             svrUrl = System.Configuration.ConfigurationManager.AppSettings["BaseUri"];
 | ||
|             int color = comcolor.SelectedIndex + 1;
 | ||
|             int num1 = 0;
 | ||
|             int num2 = 0;
 | ||
|             int num3 = 0;
 | ||
|             int id = 1;
 | ||
| 
 | ||
|             try
 | ||
|             {
 | ||
|                 num1 = Convert.ToInt16(txtNumber1.Text);
 | ||
|                 num2 = Convert.ToInt32(txtNum2.Text);
 | ||
|                 num3 = Convert.ToInt32(txtNum3.Text);
 | ||
|                 id = Convert.ToInt32(txtLabelId.Text);
 | ||
|             }
 | ||
|             catch
 | ||
|             {
 | ||
|                 MessageBox.Show("请输入整数");
 | ||
|                 return;
 | ||
|             }
 | ||
| 
 | ||
|             ELabel el = new ELabel();
 | ||
| 
 | ||
|             el.labelId = id;
 | ||
|             el.num2 = num2;
 | ||
|             el.num3 = num3;
 | ||
|             el.num1 = num1;
 | ||
|             el.color = color;// new Random().Next(1, 6);
 | ||
|             el.port = currentPort;
 | ||
|             ELabel[] els = new ELabel[1];
 | ||
|             els[0] = el;
 | ||
|             postUrl = svrUrl + "labelOff";
 | ||
|             addLog("开始测试关闭标签");
 | ||
|             Thread threadPreProcess = new Thread(new ParameterizedThreadStart(HttpPost));
 | ||
|             threadPreProcess.IsBackground = true;
 | ||
|             threadPreProcess.Start(els);
 | ||
| 
 | ||
|             addLog(postUrl);
 | ||
| 
 | ||
|             String json = "关闭标签数据:[" + getJson(el) + "]";
 | ||
| 
 | ||
|             addLog(json);
 | ||
|         }
 | ||
| 
 | ||
|         private void label3_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
|         System.Timers.Timer timer = new System.Timers.Timer();//实例化Timer类
 | ||
| 
 | ||
|       
 | ||
|         private void initialTimer()
 | ||
|         {
 | ||
|             int intTime = Convert.ToInt32( nud.Value)*1000;
 | ||
|             intTime = intTime > 0 ? intTime : 10000;
 | ||
|             timer.Interval = intTime;//设置间隔时间,为毫秒;
 | ||
|             timer.Elapsed += new System.Timers.ElapsedEventHandler(checkHost);//到达时间的时候执行事件;
 | ||
|             timer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
 | ||
|             timer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
 | ||
|             timer.Start();
 | ||
|         }
 | ||
| 
 | ||
|         bool isChecking = false;
 | ||
|         private void checkHost(object sender, ElapsedEventArgs e)
 | ||
|         {
 | ||
|             if (isChecking)
 | ||
|             {
 | ||
|                 addLog(" check is working..... return");
 | ||
|                 return;
 | ||
|             }
 | ||
|             
 | ||
|             isChecking = true;
 | ||
|             addLog("开始检查及再次回调服务。。。。。--->" );
 | ||
| 
 | ||
|             if (badRowIds.Count == 0)
 | ||
|             {
 | ||
|                 isChecking = false;
 | ||
|                 addLog("待处理数量: " + badRowIds.Count);
 | ||
|                 addLog("<--- 结束再次回调服务 ");
 | ||
|                 return;
 | ||
|               
 | ||
|             }
 | ||
|                 List<string> rowIds = new List<string>();
 | ||
| 
 | ||
|             foreach (string row in badRowIds)
 | ||
|             {
 | ||
|                 if (string.IsNullOrEmpty(row))
 | ||
|                 {
 | ||
|                     continue;
 | ||
|                 }
 | ||
|                rowIds.Add(row);
 | ||
|             }
 | ||
|            
 | ||
|             badRowIds.Clear();
 | ||
|             foreach (string row in rowIds)
 | ||
|             {
 | ||
|                 if (string.IsNullOrEmpty(row))
 | ||
|                 {
 | ||
|                     continue;
 | ||
|                 }
 | ||
|                 badRowIds.Add(row);
 | ||
|                 addLog("待处理数据: " + rowIds +"  ");
 | ||
|             }
 | ||
|             addLog("待处理数量: " + rowIds.Count);
 | ||
|             foreach (string row in rowIds)
 | ||
|             {
 | ||
|                 if (string.IsNullOrEmpty(row))
 | ||
|                 {
 | ||
|                     continue;
 | ||
|                 }
 | ||
|                 addLog("开始回调 row ID " + row);
 | ||
|                 testPickedRowId(Convert.ToInt32(row));
 | ||
|                 Thread.Sleep(500);
 | ||
|             }
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|             rowIds.Clear();
 | ||
|             foreach (string row in badRowIds)
 | ||
|             {
 | ||
|                 if (string.IsNullOrEmpty(row))
 | ||
|                 {
 | ||
|                     continue;
 | ||
|                 }
 | ||
|                 rowIds.Add(row);
 | ||
|             }
 | ||
|             if (rowIds.Count > 0)
 | ||
|             {
 | ||
|                 addLog("待处理回调异常记录: ");
 | ||
|                 foreach (string row in rowIds)
 | ||
|                 {
 | ||
|                     if (string.IsNullOrEmpty(row))
 | ||
|                     {
 | ||
|                         continue;
 | ||
|                     }
 | ||
|                     addLog("rowId: " + row);
 | ||
| 
 | ||
|                 }
 | ||
|             }
 | ||
| 
 | ||
| 
 | ||
|             addLog("<--- 结束再次回调服务 ");
 | ||
|             writeBadRowIds();
 | ||
|             isChecking =false;
 | ||
|         }
 | ||
| 
 | ||
|         private void nud_ValueChanged(object sender, EventArgs e)
 | ||
|         {    int intTime = Convert.ToInt32(nud.Value) * 1000;
 | ||
|             WriteConfig("hostInterv", nud.Value + "");
 | ||
|             timer.Stop(); 
 | ||
|          
 | ||
|             timer.Interval = intTime;//设置间隔时间,为毫秒;
 | ||
|           
 | ||
|             timer.Start();
 | ||
|         }
 | ||
| 
 | ||
|         private void lbComStatus_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
| 
 | ||
|         }
 | ||
|         System.Timers.Timer timer2 = new System.Timers.Timer();//实例化Timer类
 | ||
| 
 | ||
|         //每2小时重启服务
 | ||
|         private void initialTimer2()
 | ||
|         {
 | ||
|            //int intTime = 1* 60 * 1000; //1分
 | ||
|            int intTime = 8 * 60 * 60 * 1000; //8小时
 | ||
| 
 | ||
|             timer2.Interval = intTime;//设置间隔时间,为毫秒;
 | ||
|             timer2.Elapsed += new System.Timers.ElapsedEventHandler(reset);//到达时间的时候执行事件;
 | ||
|             timer2.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
 | ||
|             timer2.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
 | ||
|             timer2.Start();
 | ||
|             
 | ||
|         }
 | ||
| 
 | ||
|         private void reset(object sender, ElapsedEventArgs e)
 | ||
|         {
 | ||
|             int hour = System.DateTime.Now.Hour;
 | ||
|             addLog("current hour is " + hour);
 | ||
|             if (hour >7  &&  hour <19)
 | ||
|             {
 | ||
|                 return;
 | ||
|             }
 | ||
|             addLog("Restarting....Service..... " );
 | ||
|             restart();
 | ||
|         }
 | ||
|     }
 | ||
| }
 |