internal List <CurveInfo> GetCurveList() { List <CurveInfo> list = new List <CurveInfo>(); if (usingConfig) { #region UsingConfig List <System.Xml.XmlElement> elist = reader.GetUnDefinedElement("DefaultDeviceInfo"); elist.ForEach((element) => { CurveInfo ci = new CurveInfo(); ci.Id = new Guid(element.Attributes["id"].Value); ci.Tag = element.Attributes["name"].Value; ci.SamplingRate = Convert.ToInt32(element.Attributes["samplingrate"].Value); list.Add(ci); }); #endregion } else { #region extract for (int i = 4; i < arguments.Length; i++) { CurveInfo info = CurveInfo.CreateObjectFromString(arguments[i]); if (info != null) { list.Add(info); } } #endregion } return(list); }
public static CurveInfo CreateObjectFromString(string args) { CurveInfo info = new CurveInfo(); string[] s = args.Split('&'); if (s.Length != 3) { Console.WriteLine("bad conversion :" + args); return(null); } else { try { Guid id = new Guid(s[0]); int rate = int.Parse(s[2]); info.Id = id; info.SamplingRate = rate; info.Tag = s[1]; return(info); } catch { Console.WriteLine("bad conversion :" + args); return(null); } } }
static void SendResponse(TcpClient client, CurveInfo info, Responses responses, ResponsePackage pack) { pack.Response = responses; pack.Id = info.Id; pack.Tag = info.Tag; BinaryFormatter formatter = new BinaryFormatter(); NetworkStream ns = client.GetStream(); lock (ns) { formatter.Serialize(ns, pack); } }
static void SendResponse(TcpClient client, CurveInfo info, Responses responses) { ResponsePackage p = new ResponsePackage(); p.Response = responses; p.Id = info.Id; p.Tag = info.Tag; BinaryFormatter formatter = new BinaryFormatter(); NetworkStream ns = client.GetStream(); lock (ns) { formatter.Serialize(ns, p); } Console.WriteLine("send response for " + info.Tag + " , " + responses); }
static Thread CreateDataTransferThread(object info, TcpClient client, Commands command) { Thread thread = new Thread((curveInfo) => { try { CurveInfo threadCurveInfo = curveInfo as CurveInfo; int timeSleep = 1000 / threadCurveInfo.SamplingRate; while (true) { ResponsePackage p = new ResponsePackage(); p.Points = new Coordinate[DefaultPackSize]; for (int i = 0; i < DefaultPackSize; i++) { Coordinate co = CurveAnalogData.GetCoordinateByHeight(currentHeight); p.Points[i] = co; if (timeSleep != 0) { Thread.Sleep(timeSleep); } if (!GetState(threadCurveInfo.Id)) { return; } } Console.WriteLine("send response data for " + threadCurveInfo.Tag + " , count:" + p.Points.Length + ", current height : " + currentHeight.ToString("0.00")); if (!GetState(threadCurveInfo.Id)) { return; } SendResponse(client, threadCurveInfo, Responses.Data, p); } } catch (Exception ex) { Console.WriteLine("error occured : " + ex.Message); return; } }); return(thread); }
static Thread CreateCommandManagerThread() { Thread thread = new Thread((info) => { CurveInfo curveInfo = info as CurveInfo; if (info == null) { return; } while (true) { try { TcpClient client = new TcpClient(); while (true) { try { client.Connect(new System.Net.IPEndPoint(IPAddress.Parse(DefaultIpAddress), DefaultPort)); Console.WriteLine("connected success!"); break; } catch { Thread.Sleep(2000); Console.WriteLine("can't connected to server! try again"); } } Thread dataThread = null; Commands command = Commands.Surpress; SendResponse(client, curveInfo, Responses.Ready); while (true) { command = GetCommand(client); #region Surpress if (command == Commands.Surpress) { SetState(curveInfo.Id, false); Thread.Sleep(DefaultRequestInterval); SendResponse(client, curveInfo, Responses.Ready); } #endregion #region DataRequired if (command == Commands.DataRequired) { SetState(curveInfo.Id, true); if (dataThread == null || !dataThread.IsAlive) { dataThread = CreateDataTransferThread(info, client, command); dataThread.IsBackground = true; dataThread.Start(info); Console.WriteLine("thread started : dataThread"); } else { Console.WriteLine("continue send data"); } } #endregion } } catch { continue; } } }); return(thread); }