525 lines
16 KiB
C#
525 lines
16 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 elelab;
|
|||
|
using System.ServiceModel.Description;
|
|||
|
using System.Threading;
|
|||
|
using System.Net;
|
|||
|
using System.IO;
|
|||
|
using System.Runtime.Serialization.Json;
|
|||
|
namespace DNLightSvr
|
|||
|
{
|
|||
|
public partial class form1 : Form
|
|||
|
{
|
|||
|
static log4net.ILog log = log4net.LogManager.GetLogger("light");
|
|||
|
private ServiceHost host = null;
|
|||
|
public static List<ELabel> labelPool = new List<ELabel>();
|
|||
|
string svrUrl = "http://127.0.0.1:9998/DNlightSvc";
|
|||
|
string wmsUrl = "";
|
|||
|
string postUrl = "";
|
|||
|
|
|||
|
public form1()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
}
|
|||
|
|
|||
|
private void Form1_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
//StartPosition = FormStartPosition.CenterParent;
|
|||
|
|
|||
|
startHost();
|
|||
|
//btnTest.Visible = false;
|
|||
|
}
|
|||
|
void startHost()
|
|||
|
{
|
|||
|
// lbStatus.Text = "服务启动失败";
|
|||
|
try
|
|||
|
{
|
|||
|
initPorts();
|
|||
|
svrUrl = System.Configuration.ConfigurationManager.AppSettings["BaseUri"];
|
|||
|
wmsUrl = System.Configuration.ConfigurationManager.AppSettings["WMSuri"];
|
|||
|
Uri baseAddress = new Uri(svrUrl);
|
|||
|
LightService service = new LightService();
|
|||
|
service.clearO += this.clearOrder;
|
|||
|
service.lightUp += this.lightUp;
|
|||
|
|
|||
|
host = new ServiceHost(service, baseAddress);
|
|||
|
elelab.DNLights.lightOffEvent += testPicked;
|
|||
|
|
|||
|
WebHttpBinding binding = new WebHttpBinding();
|
|||
|
ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ILightService), binding, baseAddress);
|
|||
|
WebHttpBehavior httpBehavior = new WebHttpBehavior();
|
|||
|
endpoint.Behaviors.Add(httpBehavior);
|
|||
|
host.Opened += delegate
|
|||
|
{
|
|||
|
addLog("服务已启动");
|
|||
|
if (InvokeRequired)
|
|||
|
{
|
|||
|
this.Invoke(new showStatus(delegate()
|
|||
|
{
|
|||
|
lbStatus.Text = "服务已启动";
|
|||
|
lbUrl.Text ="亮灯服务地址:" + svrUrl + "\r\n\nWMS回传地址: " + wmsUrl;
|
|||
|
|
|||
|
}));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
lbStatus.Text = "服务已启动";
|
|||
|
lbUrl.Text = "亮灯服务地址:" + svrUrl + "\r\n\nWMS回传地址: " + wmsUrl;
|
|||
|
}
|
|||
|
};
|
|||
|
host.Open();
|
|||
|
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
addLog("启动服务失败: " + e.Message);
|
|||
|
MessageBox.Show("启动服务失败: " + e.Message);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void initPorts()
|
|||
|
{
|
|||
|
elelab.DNLights.initPort();
|
|||
|
|
|||
|
List<int> ports = elelab.DNLights.getActiveComports();
|
|||
|
if (ports.Count > 0)
|
|||
|
{
|
|||
|
lbPorts.Text = "活动端口:";
|
|||
|
foreach (int i in ports)
|
|||
|
{
|
|||
|
lbPorts.Text += " com" +i + ",";
|
|||
|
}
|
|||
|
lbPorts.Text = lbPorts.Text.Substring(0, lbPorts.Text.Length - 1);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
lbPorts.Text = "请检查控制器连接,并重启服务";
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
bool lightUp(ELabel[] labels)
|
|||
|
{
|
|||
|
//TODO: 把传来的实体按portNo分组,按portNo 分别点亮
|
|||
|
|
|||
|
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);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
foreach (int keyPort in lbsPort.Keys)
|
|||
|
{
|
|||
|
Thread.Sleep(100);
|
|||
|
if (!doLightUp(keyOrder,keyPort, lbsPort[keyPort]))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return true;
|
|||
|
// int port = 5;
|
|||
|
// doLightUp(port, lables);
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
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]; ;
|
|||
|
|
|||
|
|
|||
|
for (int i = 0; i < lables.Count; i++)
|
|||
|
{
|
|||
|
ids[i] = lables[i].labelId;
|
|||
|
address[i] = lables[i].address;
|
|||
|
counts[i] = lables[i].count;
|
|||
|
colors[i] = lables[i].color;
|
|||
|
|
|||
|
}
|
|||
|
//elelab.DNLights.initPort();
|
|||
|
|
|||
|
bool result = false;
|
|||
|
if (orderNo == 0)
|
|||
|
{
|
|||
|
result = DNLights.lightLabels(portNo, colors, ids, address, counts);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
result = DNLights.lightLabels(orderNo, portNo, colors, ids, address, counts);
|
|||
|
|
|||
|
}
|
|||
|
if (!result)
|
|||
|
{
|
|||
|
addLog(string.Format("亮灯失败: orderNo {0},portNo{1} ",orderNo,portNo));
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
return result;
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
delegate void showStatus();
|
|||
|
void testPicked(int orderNo, int eleId, int color, int eleAddress, int count)
|
|||
|
{
|
|||
|
if (InvokeRequired)
|
|||
|
{
|
|||
|
this.Invoke(new showStatus(delegate()
|
|||
|
{
|
|||
|
returnResult(orderNo, eleId, color, eleAddress, count);
|
|||
|
}));
|
|||
|
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
returnResult(orderNo, eleId, color, eleAddress, count);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void returnResult(int orderNo, int eleId, int color, int eleAddress, int count)
|
|||
|
{
|
|||
|
//to show result
|
|||
|
|
|||
|
//to call client and send data
|
|||
|
|
|||
|
|
|||
|
|
|||
|
ELabel lb = new ELabel();
|
|||
|
lb.count = count;
|
|||
|
lb.color = color;
|
|||
|
lb.address = eleAddress;
|
|||
|
lb.orderNo = orderNo;
|
|||
|
lb.labelId = eleId;
|
|||
|
String url = svrUrl + "testPost";
|
|||
|
addLog("收到按键返回事件,开始回传...");
|
|||
|
addLog(getJson(lb));
|
|||
|
|
|||
|
|
|||
|
// String s = getJson(lb);
|
|||
|
postUrl = wmsUrl;
|
|||
|
Thread threadPreProcess = new Thread(new ParameterizedThreadStart(HttpPost));
|
|||
|
threadPreProcess.IsBackground = true;
|
|||
|
threadPreProcess.Start(lb);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void btnStart_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
Thread threadPreProcess = new Thread(startHost);
|
|||
|
threadPreProcess.IsBackground = true;
|
|||
|
threadPreProcess.Start();
|
|||
|
//startHost();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// GET请求与获取结果
|
|||
|
/// </summary>
|
|||
|
public void 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;
|
|||
|
request.BeginGetResponse(new AsyncCallback(Compleate), request);
|
|||
|
|
|||
|
|
|||
|
|
|||
|
/*
|
|||
|
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)
|
|||
|
{
|
|||
|
addLog("http get error: "+ ex.Message);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
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;
|
|||
|
using (Stream reqStream = req.GetRequestStream())
|
|||
|
{
|
|||
|
reqStream.Write(bs, 0, bs.Length);
|
|||
|
}
|
|||
|
|
|||
|
req.BeginGetResponse(new AsyncCallback(Compleate), req);
|
|||
|
/*
|
|||
|
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( "收到服务器响应:\n" + responseText);
|
|||
|
}));
|
|||
|
|
|||
|
|
|||
|
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("获取失败.");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
private void btnBack_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
postUrl = wmsUrl;
|
|||
|
addLog("开始测试回传WMS");
|
|||
|
Thread threadPreProcess = new Thread(new ParameterizedThreadStart(HttpPost));
|
|||
|
threadPreProcess.IsBackground = true;
|
|||
|
threadPreProcess.Start(new ELabel());
|
|||
|
|
|||
|
//HttpPost(url,lb);
|
|||
|
}
|
|||
|
|
|||
|
private void btnTestrequest_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
|
|||
|
String url ="http://127.0.0.1:9998/DNlightSvc/test";
|
|||
|
HttpGet(url, null);
|
|||
|
}
|
|||
|
public string getJson(ELabel label)
|
|||
|
{
|
|||
|
string json = String.Format("\"labelId\":{0},\"address\":{1},\"count\":{2},\"color\":{3},\"port\":{4},\"orderNo\":{5}"
|
|||
|
, label.labelId, label.address, label.count, label.color, label.port,label.orderNo);
|
|||
|
// Console.Write("get the lable: " + json);
|
|||
|
|
|||
|
return "{"+ json +"}";
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
void addLog(string info)
|
|||
|
{
|
|||
|
if (InvokeRequired)
|
|||
|
{
|
|||
|
this.Invoke(new showStatus(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;
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
static string getMsg(string className, string msg)
|
|||
|
{
|
|||
|
return string.Format("{0} {1}",className,msg);
|
|||
|
}
|
|||
|
|
|||
|
private void btnTest_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
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)
|
|||
|
{
|
|||
|
|
|||
|
ELabel[] els = new ELabel[9];
|
|||
|
|
|||
|
for (int i = 1; i < 10; i++)
|
|||
|
{
|
|||
|
ELabel el = new ELabel();
|
|||
|
|
|||
|
el.labelId = i;
|
|||
|
el.count = new Random().Next(1, 8888);
|
|||
|
el.address = new Random().Next(1, 88);
|
|||
|
el.color = color;// new Random().Next(1, 6);
|
|||
|
el.port = 6;
|
|||
|
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);
|
|||
|
}
|
|||
|
|
|||
|
bool clearOrder(ClearOrder order)
|
|||
|
{
|
|||
|
foreach (int port in order.ports)
|
|||
|
{
|
|||
|
if (!elelab.DNLights.clearOrder(order.orderNo, port))
|
|||
|
{
|
|||
|
addLog("清除订单失败");
|
|||
|
addLog(string.Format("端口: {0},订单:{1}",port,order.orderNo));
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
private void btnRefresh_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
|
|||
|
System.Environment.Exit(0);
|
|||
|
}
|
|||
|
|
|||
|
private void btnClear_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
elelab.DNLights.clearOrder(1, 6);
|
|||
|
}
|
|||
|
|
|||
|
private void btnReset_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
elelab.DNLights.resetDevice();
|
|||
|
}
|
|||
|
|
|||
|
private void form1_FormClosing(object sender, FormClosingEventArgs e)
|
|||
|
{
|
|||
|
elelab.DNLights.resetDevice();
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|