439 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			C#
		
	
	
	
		
		
			
		
	
	
			439 lines
		
	
	
		
			13 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 System.ServiceModel; | |||
|  | using DeiNiu.Utils; | |||
|  | using System.ServiceModel.Description; | |||
|  | using System.Net; | |||
|  | using System.Runtime.Serialization.Json; | |||
|  | using System.IO; | |||
|  | using System.Threading; | |||
|  | using elelab; | |||
|  | using dn_wms; | |||
|  | using Model; | |||
|  | 
 | |||
|  | namespace DeiNiu.wms.win | |||
|  | { | |||
|  |     public partial class LightSeviceHostForm : BasicRibbonForm | |||
|  |     { | |||
|  |         private ServiceHost host = null; | |||
|  |         public static List<ELabel> labelPool = new List<ELabel>(); | |||
|  |          string svrUrl = "http://127.0.0.1:9998/DNLight"; | |||
|  |         public LightSeviceHostForm() | |||
|  |         { | |||
|  |             InitializeComponent(); | |||
|  |            // startHost(); | |||
|  |             //startHost2(); | |||
|  |             elelab.pick.init_port(enumLabelPickType.seed); | |||
|  |             Thread threadPreProcess = new Thread(startHost2); | |||
|  |             threadPreProcess.IsBackground = true; | |||
|  |             threadPreProcess.Start(); | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  | 
 | |||
|  |         void startHost() | |||
|  |         { | |||
|  |             ServiceLight service = new ServiceLight(); | |||
|  |             service.newLabels += this.newLabels; | |||
|  |             service.lightUp += this.lightUp; | |||
|  |              | |||
|  |             host = new ServiceHost(service); | |||
|  |             host.Open();   | |||
|  |         } | |||
|  |         delegate void showStatus(); | |||
|  |         void startHost2() | |||
|  |         { | |||
|  |           //  lbStatus.Text = "服务启动失败"; | |||
|  |             try | |||
|  |             { | |||
|  |                 svrUrl = System.Configuration.ConfigurationManager.AppSettings["BaseUri"]; | |||
|  |                 Uri baseAddress = new Uri(svrUrl); | |||
|  |                 ServiceLight service = new ServiceLight(); | |||
|  |                 service.newLabels += this.newLabels; | |||
|  |                 service.lightUp += this.lightUp; | |||
|  |                 service.turnOff += this.turnOff; | |||
|  |                 service.turnOnLts += this.turnOnLights; | |||
|  |                 service.turnOffLts += this.turnOffLights; | |||
|  | 
 | |||
|  | 
 | |||
|  |                 host = new ServiceHost(service, baseAddress); | |||
|  |                 elelab.pick.lightOffEvent += testPicked;  | |||
|  |                 WebHttpBinding binding = new WebHttpBinding(); | |||
|  |                 ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IServiceLight), binding, baseAddress); | |||
|  |                 WebHttpBehavior httpBehavior = new WebHttpBehavior(); | |||
|  |                 endpoint.Behaviors.Add(httpBehavior); | |||
|  |                 host.Opened += delegate | |||
|  |                 { | |||
|  | 
 | |||
|  |                     if (InvokeRequired) | |||
|  |                     { | |||
|  |                         this.Invoke(new showStatus(delegate() | |||
|  |                         { | |||
|  |                             lbStatus.Text = "服务已启动"; | |||
|  |                             lbUrl.Text = svrUrl; | |||
|  |                         })); | |||
|  | 
 | |||
|  |                     } | |||
|  |                     else | |||
|  |                     { | |||
|  |                         lbStatus.Text = "服务已启动"; | |||
|  |                         lbUrl.Text = svrUrl; | |||
|  |                        // lbStatus.Text = "服务启动失败"; | |||
|  |                       //  lbUrl.Text =""; | |||
|  |                     } | |||
|  |                 }; | |||
|  |                 host.Open(); | |||
|  | 
 | |||
|  |             } | |||
|  |             catch (Exception e) | |||
|  |             { | |||
|  |                 MessageBox.Show("启动服务失败:  "+e.Message); | |||
|  |             } | |||
|  | 
 | |||
|  |               | |||
|  |         } | |||
|  | 
 | |||
|  | 
 | |||
|  |         void testPicked(int eleId, int port, int eleAddress, int count) | |||
|  |         { | |||
|  |             if (InvokeRequired) | |||
|  |             { | |||
|  |                 this.Invoke(new showStatus(delegate() | |||
|  |                 { | |||
|  |                     showResult(eleId, port, eleAddress, count); | |||
|  |                 })); | |||
|  | 
 | |||
|  |             } | |||
|  |             else | |||
|  |             { | |||
|  |                 showResult(eleId, port, eleAddress, count); | |||
|  |             } | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  |         void showResult(int eleId, int color, int eleAddress, int count) | |||
|  |         { | |||
|  | 
 | |||
|  |             lbreturn.Text = string.Format("标签ID:{0} \n\n 端口:{1} \n\n 地址:{2} \n\n 数量:{3}", eleId, color, eleAddress, count); | |||
|  |         } | |||
|  | 
 | |||
|  |         void newLabels(List<ELabel> lables) | |||
|  |         { | |||
|  |             labelPool = labelPool.Union(lables).ToList<ELabel>(); | |||
|  |             labelPool = labelPool.Concat(lables).ToList<ELabel>(); | |||
|  | 
 | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  |         System.Timers.Timer timer = new System.Timers.Timer();//实例化Timer类  | |||
|  |    | |||
|  |        | |||
|  |         private void initialTimer() | |||
|  |         { | |||
|  |             int intTime = 5000; | |||
|  |             timer.Interval = intTime;//设置间隔时间,为毫秒; | |||
|  |             timer.Elapsed += new System.Timers.ElapsedEventHandler(lightUp);//到达时间的时候执行事件; | |||
|  |             timer.AutoReset = true;//设置是执行一次(false)还是一直执行(true); | |||
|  |             timer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; | |||
|  |             timer.Start(); | |||
|  |         } | |||
|  | 
 | |||
|  |         void lightUp(object source, System.Timers.ElapsedEventArgs e) | |||
|  |         { | |||
|  | 
 | |||
|  |             if (labelPool.Count == 0) | |||
|  |             {  | |||
|  |                 return;  | |||
|  |             } | |||
|  | 
 | |||
|  |             ELabel lb = labelPool[0]; | |||
|  | 
 | |||
|  |             int[] ids = new int[] { lb.labelId }; | |||
|  |             string[] ele_address = new string[] { lb.address.ToString() }; | |||
|  | 
 | |||
|  |             elelab.pick.init_port(enumLabelPickType.seed); | |||
|  | 
 | |||
|  |             //  string result = elelab.pick.send_data_pick(null, enumLabelPickType.pick); | |||
|  | 
 | |||
|  |             bool result = elelab.pick.lightLabels2(labelPool,1,1, enumLabelPickType.seed); | |||
|  | 
 | |||
|  | 
 | |||
|  |             if (result) | |||
|  |             { | |||
|  |                 labelPool.Remove(lb); | |||
|  |             } | |||
|  |         } | |||
|  | 
 | |||
|  |       | |||
|  |          | |||
|  | 
 | |||
|  |         void lightUp(List<ELabel> lables,int portNo) | |||
|  |         { | |||
|  |             //to lightup | |||
|  | 
 | |||
|  | #if DEBUG | |||
|  |            // portNo = 13; | |||
|  | #endif | |||
|  | 
 | |||
|  |             int[] ids = new int[lables.Count] ; | |||
|  |             string[] ele_address = new string[lables.Count]; | |||
|  |             for(int i=0; i< lables.Count;i++  ) | |||
|  |             { | |||
|  |                 ids[i] = lables[i].labelId; | |||
|  | 
 | |||
|  |                 ele_address[i] = lables[i].address.ToString();        | |||
|  |              | |||
|  |             }  | |||
|  |             elelab.pick.init_port(enumLabelPickType.seed); | |||
|  |             elelab.pick.lightLabels2(lables, portNo,1,enumLabelPickType.seed); | |||
|  |         } | |||
|  | 
 | |||
|  |         #region a | |||
|  |         private void insertTestData()  | |||
|  |         { | |||
|  |             string url = svrUrl + "/uploadSeeds"; | |||
|  | 
 | |||
|  |             try | |||
|  |             { | |||
|  | 
 | |||
|  |            //    #region JosnPost | |||
|  |                 List<SeedsItem> seeds   = new    List<SeedsItem>(); | |||
|  | 
 | |||
|  |                 int maxLbId = 1; | |||
|  |                 int k = 0; | |||
|  |                 for (int i = 401; i < 800; i++) | |||
|  |                 { | |||
|  | 
 | |||
|  |                     SeedsItem si = new SeedsItem(); | |||
|  | 
 | |||
|  | 
 | |||
|  |                     si.count = i * 9 +1; | |||
|  |                     si.custOrder = "CO" + i * 100 / 3 + 1; | |||
|  |                     si.seedsNo = "seedsNo0" + (i / maxLbId + 1); | |||
|  |                     if (k >= maxLbId) | |||
|  |                     { | |||
|  |                         k = 0; | |||
|  |                     } | |||
|  | 
 | |||
|  |                     k++; | |||
|  |                     si.elabId = k; | |||
|  |                     si.barcode = k / 5 + 1+ ""; | |||
|  |                     si.deskId = 1; | |||
|  | 
 | |||
|  | 
 | |||
|  |                     si.itemName = "name" + si.barcode; | |||
|  |                     si.port = si.deskId; | |||
|  |                     si.unit = "each"; | |||
|  |                     seeds.Add(si); | |||
|  |                 } | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |             DataContractJsonSerializer dcSerializer = new DataContractJsonSerializer(typeof(List<SeedsItem> )); | |||
|  | 
 | |||
|  |             MemoryStream stream = new MemoryStream(); | |||
|  | 
 | |||
|  |             dcSerializer.WriteObject(stream, seeds); | |||
|  | 
 | |||
|  |             string data = Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Length); | |||
|  | 
 | |||
|  |             //HttpClient client = new HttpClient(); | |||
|  | 
 | |||
|  |             //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |||
|  | 
 | |||
|  |             string link = url; | |||
|  | 
 | |||
|  |             //HttpResponseMessage respondse = await client.PostAsync(link, new StringContent(data)); | |||
|  | 
 | |||
|  |             //string dataWithJason = await respondse.Content.ReadAsStringAsync(); | |||
|  |   | |||
|  |             var request = (HttpWebRequest)WebRequest.Create(new Uri(link)); | |||
|  |             request.ContentType = "application/json"; | |||
|  |             request.Method = "POST"; | |||
|  |             var requestStream = request.GetRequestStream(); | |||
|  |             using (var writer = new StreamWriter(requestStream)) | |||
|  |             {  | |||
|  |                 writer.Write(data); | |||
|  |                 writer.Flush(); | |||
|  |             } | |||
|  | 
 | |||
|  |             /*using (var resp = request.GetResponse()) | |||
|  |             { | |||
|  |                 using (var responseStream = resp.GetResponseStream()) | |||
|  |                 { | |||
|  |                     var reader = new StreamReader(responseStream); | |||
|  |                     var result = reader.ReadToEnd(); | |||
|  |                 } | |||
|  |             }*/ | |||
|  | 
 | |||
|  |             HttpWebResponse response = (HttpWebResponse)request.GetResponse(); | |||
|  |           | |||
|  |             string json = null; | |||
|  |             using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"))) | |||
|  |             { | |||
|  |                 json = reader.ReadToEnd(); | |||
|  |             } | |||
|  |            // WebResponse response = request.GetResponse(); | |||
|  |           // string json = null; | |||
|  |          //   using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"))) | |||
|  |             { | |||
|  |          //       json = reader.ReadToEnd(); | |||
|  |             } | |||
|  |             | |||
|  | 
 | |||
|  |             /*    | |||
|  |             using (var requestStream = await  request.GetRequestStreamAsync()) | |||
|  |             { | |||
|  |                 var writer = new StreamWriter(requestStream); | |||
|  |                 writer.Write(data); | |||
|  |                 writer.Flush(); | |||
|  |             } | |||
|  | 
 | |||
|  |             using (var resp = await request.GetResponseAsync()) | |||
|  |             { | |||
|  |                 using (var responseStream = resp.GetResponseStream()) | |||
|  |                 { | |||
|  |                     var reader = new StreamReader(responseStream); | |||
|  |                     var result = reader.ReadToEnd(); | |||
|  |                 } | |||
|  |             }*/ | |||
|  | 
 | |||
|  | 
 | |||
|  |                 /* | |||
|  |              HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@url); | |||
|  |             request.ContentType = "application/json"; | |||
|  |             request.Method = "POST"; | |||
|  |             string inputString = data;//"{\"Key\":\"ABCDEFG\"}"; | |||
|  |             Byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(inputString); | |||
|  |             request.ContentLength = byteArray.Length; | |||
|  |             Stream rstream = request.GetRequestStream(); | |||
|  |             rstream.Write(byteArray, 0, byteArray.Length); | |||
|  |             rstream.Close(); | |||
|  |   | |||
|  |   | |||
|  |             HttpWebResponse response = (HttpWebResponse)request.GetResponse(); | |||
|  |             Stream streams = response.GetResponseStream(); | |||
|  |             StreamReader SR = new StreamReader(streams); | |||
|  |             String info = SR.ReadToEnd(); | |||
|  |             SR.Dispose(); | |||
|  |            */ | |||
|  | 
 | |||
|  |             MessageBox.Show("导入成功"); | |||
|  | 
 | |||
|  |             } | |||
|  |             catch (Exception ex) | |||
|  |             { | |||
|  |                 MessageBox.Show(ex.Message); | |||
|  |             } | |||
|  |               | |||
|  |         } | |||
|  |         #endregion | |||
|  |         private void btnLightSeeds_Click(object sender, EventArgs e) | |||
|  |         { | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  |         private void LightSeviceHostForm_FormClosing(object sender, FormClosingEventArgs e) | |||
|  |         { | |||
|  |             turnOffLights(); | |||
|  |             if (host != null) | |||
|  |             { | |||
|  |                 host.Close(); | |||
|  |             } | |||
|  |         } | |||
|  | 
 | |||
|  |         void turnOff(int portNo) | |||
|  |         { | |||
|  |             if (unart_manage.com_manage[portNo] == null) | |||
|  |             { | |||
|  |                 return; | |||
|  |             } | |||
|  |              Model.dis_id id; | |||
|  |             //id.ele_id = Convert.ToInt16(new_id.Text); | |||
|  |             id.order = 1; | |||
|  |             id.ele_id = 65535;  | |||
|  |             id.state = 1; | |||
|  |          | |||
|  |             unart_manage.com_manage[portNo].init_port.write_clear_comment(id); | |||
|  |         } | |||
|  | 
 | |||
|  | 
 | |||
|  |         void turnOffLights() | |||
|  |         { | |||
|  |              Model.dis_id id; | |||
|  |             //id.ele_id = Convert.ToInt16(new_id.Text); | |||
|  |             id.order = 1; | |||
|  |             id.ele_id = 65535;  | |||
|  |             id.state = 1; | |||
|  |             //id. | |||
|  | 
 | |||
|  |             foreach (int port in pick.activeComports)//WmsConstants.WAVE_CURRENT_LIGHTS_PORT_STATUS.Keys) | |||
|  |             { | |||
|  |                 if (port == 0) | |||
|  |                 { | |||
|  |                     continue; | |||
|  |                 } | |||
|  |                 //  unart_manage.com_manage[port].write_reset_device(); 复位各个硬件,通道灯,标签。。。 | |||
|  | 
 | |||
|  |                 unart_manage.com_manage[port].init_port.write_clear_comment(id); | |||
|  |             } | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  |         private void simpleButton1_Click(object sender, EventArgs e) | |||
|  |         { | |||
|  |             insertTestData(); | |||
|  |         } | |||
|  | 
 | |||
|  |         private void simpleButton1_Click_1(object sender, EventArgs e) | |||
|  |         { | |||
|  |             insertTestData(); | |||
|  |         } | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |         void turnOnLights( int portNo,List<ELight> lights ) | |||
|  |         { | |||
|  |             foreach(ELight el in lights){ | |||
|  |                  | |||
|  |                      channel_led id =new channel_led(); | |||
|  |                     id.channel_id = el.lightId; | |||
|  |                     //id.config_word = led_rgb; | |||
|  |                     id.color = el.color; | |||
|  |                     id.state = 1; | |||
|  |                   bool rt=  pick.turnOnLight(portNo, id); | |||
|  |                 | |||
|  |             } | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  |         void turnOffLights(int portNo, List<ELight> lights) | |||
|  |         { | |||
|  |             foreach (ELight el in lights) | |||
|  |             { | |||
|  | 
 | |||
|  |                 channel_led id = new channel_led(); | |||
|  |                 id.channel_id = el.lightId; | |||
|  |                 //id.config_word = led_rgb; | |||
|  |                 id.state = 1; | |||
|  |                 pick.turnOffLight(portNo, id); | |||
|  |                   | |||
|  |             } | |||
|  |         } | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |     } | |||
|  | } |