400 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C#
		
	
	
	
		
		
			
		
	
	
			400 lines
		
	
	
		
			12 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; | |||
|  | 
 | |||
|  | namespace DeiNiu.wms.win | |||
|  | { | |||
|  |     public partial class LightSeviceTestForm : BasicRibbonForm | |||
|  |     { | |||
|  |         private ServiceHost host = null; | |||
|  |         public static List<ELabel> labelPool = new List<ELabel>(); | |||
|  |          string svrUrl = "http://127.0.0.1:9998/DNLight"; | |||
|  |          public LightSeviceTestForm() | |||
|  |         { | |||
|  |             InitializeComponent(); | |||
|  |             | |||
|  |         } | |||
|  | 
 | |||
|  | 
 | |||
|  |          | |||
|  |         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 btnStartSeeds_Click(object sender, EventArgs e) | |||
|  |         { | |||
|  |               | |||
|  |         } | |||
|  |         #endregion | |||
|  |         private void btnLightSeeds_Click(object sender, EventArgs e) | |||
|  |         { | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  |         private void LightSeviceHostForm_FormClosing(object sender, FormClosingEventArgs e) | |||
|  |         { | |||
|  |            // turnOffLights(); | |||
|  |            // host.Close(); | |||
|  |         } | |||
|  | 
 | |||
|  |         void turnOff(int portNo) | |||
|  |         { | |||
|  |              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 btnStartSeeds_Click_1(object sender, EventArgs e) | |||
|  |         { | |||
|  |             string seedsNo = txtSeedsNo.Text; | |||
|  |             string deskId = txtDeskId.Text; | |||
|  |             svrUrl = System.Configuration.ConfigurationManager.AppSettings["BaseUri"]; | |||
|  | 
 | |||
|  |             svrUrl = svrUrl + "startSeeds?deskId={0}&seedsNo={1}"; | |||
|  | 
 | |||
|  |             svrUrl = string.Format(svrUrl, deskId, seedsNo); | |||
|  | 
 | |||
|  |             txtStartSeedsRequest.Text = svrUrl; | |||
|  | 
 | |||
|  |              lbStartSeedsStatus.Text=    HttpGet(svrUrl, null); | |||
|  |              | |||
|  |              lbSeedsNo.Text = seedsNo; | |||
|  |              lbDeskId.Text = deskId; | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  | 
 | |||
|  |         /// <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 simpleButton1_Click(object sender, EventArgs e) | |||
|  |         { | |||
|  |             string seedsNo = lbSeedsNo.Text; | |||
|  |             string deskId = lbDeskId.Text; | |||
|  |             string barcode = txtBarcode.Text; | |||
|  |             svrUrl = System.Configuration.ConfigurationManager.AppSettings["BaseUri"]; | |||
|  | 
 | |||
|  |             svrUrl = svrUrl + "lightSeed?deskId={0}&seedsNo={1}&barcode={2}"; | |||
|  | 
 | |||
|  |             svrUrl = string.Format(svrUrl, deskId, seedsNo, barcode); | |||
|  |             txtLightRequest.Text = svrUrl; | |||
|  |             lbLightStatus.Text = HttpGet(svrUrl, null); | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  |         private void btnOpen_Click(object sender, EventArgs e) | |||
|  |         { | |||
|  |             svrUrl = System.Configuration.ConfigurationManager.AppSettings["BaseUri"]; | |||
|  | 
 | |||
|  |             svrUrl = svrUrl + "turnOnLight?id={0}&color={1}&port={2}"; | |||
|  |             string lightId = this.txtLightId.Text; | |||
|  |             int id = Convert.ToInt16(lightId) + 5000; | |||
|  |             int color = comColor.SelectedIndex; | |||
|  |             string portNo = this.txtPort.Text; | |||
|  |             svrUrl = string.Format(svrUrl, id, color, portNo); | |||
|  |             lblturl.Text = svrUrl; | |||
|  |             lbLtStatus.Text = HttpGet(svrUrl, null); | |||
|  |         } | |||
|  | 
 | |||
|  |         private void btnclose_Click(object sender, EventArgs e) | |||
|  |         { | |||
|  |             svrUrl = System.Configuration.ConfigurationManager.AppSettings["BaseUri"]; | |||
|  | 
 | |||
|  |             svrUrl = svrUrl + "turnOffLight?id={0}&port={1}"; | |||
|  |             string lightId = this.txtLightId.Text; | |||
|  |             int id = Convert.ToInt16(lightId) + 5000; | |||
|  |             string portNo = this.txtPort.Text; | |||
|  |             svrUrl = string.Format(svrUrl, id, portNo); | |||
|  |             lblturl.Text = svrUrl; | |||
|  |             lbLtStatus.Text = HttpGet(svrUrl, null); | |||
|  |         } | |||
|  | 
 | |||
|  |         private void label5_Click(object sender, EventArgs e) | |||
|  |         { | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  |         private void btnTestPost_Click(object sender, EventArgs e) | |||
|  |         { | |||
|  | 
 | |||
|  |             string url = svrUrl + "/uploadSeeds"; | |||
|  | 
 | |||
|  |             try | |||
|  |             { | |||
|  | 
 | |||
|  |                 //    #region JosnPost | |||
|  |                 List<SeedsItem> seeds = new List<SeedsItem>(); | |||
|  | 
 | |||
|  |                 int maxLbId = 10; | |||
|  |                 int k = 0; | |||
|  |                 for (int i = 0; i < 100; 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(); | |||
|  |                         lbpoststate.Text = result; | |||
|  | 
 | |||
|  |                     } | |||
|  |                 } | |||
|  | 
 | |||
|  |                 /* | |||
|  |                 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); | |||
|  |             } | |||
|  |         } | |||
|  | 
 | |||
|  |         | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |     } | |||
|  | } |