/// <summary> /// Thread which receives packets from AltiGen Server and /// calls the appropriate procedures - to process event or response. /// </summary> private void inThread() { AltiLinkPlus.ALPPacket inThreadPacket = new AltiLinkPlus.ALPPacket(); // While inThreadLivingStatus == true we should receive incoming packets. while (inThreadLivingStatus) { try { // Reding next packet. inThreadPacket.Read(br); // Loop through every block of data in the received packet. for (int j = 0; j < inThreadPacket.BlockCount; j++) { // Getting block. AltiLinkPlus.ALPDataBlock dataBlock = inThreadPacket.PacketData[j]; if (dataBlock is AltiLinkPlus.ALPEvent) { // Process received event. ProcessEvent((AltiLinkPlus.ALPEvent)dataBlock); } else if (dataBlock is AltiLinkPlus.ALPResponse) { // Process received response. ProcessResponse((AltiLinkPlus.ALPResponse)dataBlock); } } } catch (Exception x) { if (inThreadLivingStatus) { TraceOut.Put(x); RaiseEvent(SPStatus.ERROR_CONNECTION, x.Message); } } } }
/// <summary> /// Prints <see cref="AltiLinkPlus.ALPDataBlock"/> to standard error stream of application. /// </summary> /// <param name="time">Current time the data is putting into stream.</param> /// <param name="aData">Data block to print.</param> public static void Put(DateTime time, AltiLinkPlus.ALPDataBlock aData) { if (aData == null) { return; } string str = String.Empty; string HTMLStr = time.ToString("[HH:mm:ss:fff]") + " "; // The command. if (aData is AltiLinkPlus.ALPCommand) { AltiLinkPlus.ALPCommand cmd = ((AltiLinkPlus.ALPCommand)(aData)); str = "Command:" + Environment.NewLine; HTMLStr += HTML.GHF("Command:") + Environment.NewLine; str += String.Format("ID: {0} [{1}], location ID: {2}, sequence ID: {3}, number of parameters: {4}{5}", ((ALPCmdID)(cmd.CommandId)), cmd.CommandId, cmd.LocationId, cmd.SequenceId, cmd.Count, Environment.NewLine); HTMLStr += HTML.GPF("ID: ") + HTML.GCF(((ALPCmdID)(cmd.CommandId)).ToString()) + HTML.GPF(" [") + cmd.CommandId.ToString() + HTML.GPF("], location ID: ") + cmd.LocationId.ToString() + HTML.GPF(", sequence ID: ") + cmd.SequenceId.ToString() + HTML.GPF(", number of parameters: ") + cmd.Count.ToString() + Environment.NewLine; for (int i = 0; i < cmd.Count; i++) { str += String.Format("param#{0}: {1}{2}", i, BitConverter.ToString(cmd[i].GetBytes()), Environment.NewLine); } for (int i = 0; i < cmd.Count; i++) { HTMLStr += String.Format("{0}{1}{2} {3}{4}", HTML.GPF("param#"), i, HTML.GPF(":"), BitConverter.ToString(cmd[i].GetBytes()), Environment.NewLine); } } else if (aData is AltiLinkPlus.ALPEvent) { AltiLinkPlus.ALPEvent ev = ((AltiLinkPlus.ALPEvent)(aData)); str = "Event:" + Environment.NewLine; HTMLStr += HTML.GHF("Event:") + Environment.NewLine; str += String.Format("ID: {0} [{1}], location ID: {2}, sequence ID: {3}, number of parameters: {4}{5}", ((ALPEvID)(ev.CommandId)), ev.CommandId, ev.LocationId, ev.SequenceId, ev.Count, Environment.NewLine); HTMLStr += HTML.GPF("ID: ") + HTML.GCF(((ALPEvID)(ev.CommandId)).ToString()) + HTML.GPF(" [") + ev.CommandId.ToString() + HTML.GPF("], location ID: ") + ev.LocationId.ToString() + HTML.GPF(", sequence ID: ") + ev.SequenceId.ToString() + HTML.GPF(", number of parameters: ") + ev.Count.ToString() + Environment.NewLine; for (int i = 0; i < ev.Count; i++) { str += String.Format("param#{0}: {1}{2}", i, BitConverter.ToString(ev[i].GetBytes()), Environment.NewLine); } for (int i = 0; i < ev.Count; i++) { HTMLStr += String.Format("{0}{1}{2} {3}{4}", HTML.GPF("param#"), i, HTML.GPF(":"), BitConverter.ToString(ev[i].GetBytes()), Environment.NewLine); } } else if (aData is AltiLinkPlus.ALPResponse) { AltiLinkPlus.ALPResponse rsp = ((AltiLinkPlus.ALPResponse)(aData)); str = "Responce:" + Environment.NewLine; HTMLStr += HTML.GHF("Responce:") + Environment.NewLine; str += String.Format("ID: {0} [{1}], location ID: {2}, sequence ID: {3}, responce code: {4} [{5}], number of parameters: {6}{7}", ((ALPCmdID)(rsp.CommandId)), rsp.CommandId, rsp.LocationId, rsp.SequenceId, ((ALPRespID)(rsp.ResponseCode)), rsp.ResponseCode, rsp.Count, Environment.NewLine); HTMLStr += HTML.GPF("ID: ") + HTML.GCF(((ALPCmdID)(rsp.CommandId)).ToString()) + HTML.GPF(" [") + rsp.CommandId.ToString() + HTML.GPF("], location ID: ") + rsp.LocationId.ToString() + HTML.GPF(", sequence ID: ") + rsp.SequenceId.ToString() + HTML.GPF(", responce code: ") + HTML.GCF(((ALPRespID)(rsp.ResponseCode)).ToString()) + HTML.GPF(" [") + rsp.ResponseCode.ToString() + HTML.GPF("], number of parameters: ") + rsp.Count.ToString() + Environment.NewLine; for (int i = 0; i < rsp.Count; i++) { str += String.Format("param#{0}: {1}{2}", i, BitConverter.ToString(rsp[i].GetBytes()), Environment.NewLine); } for (int i = 0; i < rsp.Count; i++) { HTMLStr += String.Format("{0}{1}{2} {3}{4}", HTML.GPF("param#"), i, HTML.GPF(":"), BitConverter.ToString(rsp[i].GetBytes()), Environment.NewLine); } if (rsp.CommandId.Equals(1021)) { // GET_LINE_INFO responce. /* StreamWriter sw = new StreamWriter(DateTime.Now.ToFileTime().ToString()+".html", false, Encoding.Unicode); * sw.WriteLine(""); * sw.WriteLine("<html>{0}<head>{0}<meta http-equiv=\"Content-Type\" content=\"text/html; charset=unicode\">{0}</head>{0}<body>{0}<pre>{0}", Environment.NewLine); * sw.WriteLine(DateTime.Now.ToString("yyyy.MM.dd, HH:mm:ss:fff")); * for(int i = 0; i < 80; i++) sw.Write("*"); * sw.WriteLine(""); * for(int i = 0; i < rsp.Count; i++) * { * sw.WriteLine("param #{0}", i); * Diacom.AltiGen.AltiLinkPlus.ALPParameter par = ((Diacom.AltiGen.AltiLinkPlus.ALPParameter)(rsp[i])); * sw.WriteLine("ID: {0}, length: {1}, size: {2}, type: {3}", par.ParameterID, par.Length, par.ParameterSize, par.ParameterType); * int l = par.Length; * char [] c = new char[l]; * byte [] b = par.GetBytes(); * for(int j = 0; j < l; j++) * { * if(j%10 == 0) sw.Write("|"); * else sw.Write(" "); * c[j] = Convert.ToChar(b[j]); * } * sw.WriteLine(""); * sw.WriteLine(c); * sw.WriteLine(""); * } * sw.WriteLine("</pre>{0}</body>{0}</html>{0}", Environment.NewLine); * sw.Close(); */ } } else { str = "Unrecognized:" + Environment.NewLine; HTMLStr += HTML.GHF("Unrecognized:") + Environment.NewLine; str += String.Format("ID: ... [{0}], location ID: {1}, sequence ID: {2}, number of parameters: {3}{4}", aData.CommandId, aData.LocationId, aData.SequenceId, aData.Count, Environment.NewLine); HTMLStr += HTML.GPF("ID: ... [") + aData.CommandId.ToString() + HTML.GPF("], location ID: ") + aData.LocationId.ToString() + HTML.GPF(", sequence ID: ") + aData.SequenceId.ToString() + HTML.GPF(", number of parameters: ") + aData.Count.ToString() + Environment.NewLine; for (int i = 0; i < aData.Count; i++) { str += String.Format("param#{0}: {1}{2}", i, BitConverter.ToString(aData[i].GetBytes()), Environment.NewLine); } for (int i = 0; i < aData.Count; i++) { HTMLStr += String.Format("{0}{1}{2} {3}{4}", HTML.GPF("param#"), i, HTML.GPF(":"), BitConverter.ToString(aData[i].GetBytes()), Environment.NewLine); } } // Saving string to a storage and putting it to trace output. TraceOut.Put(str); Storage.Append(HTMLStr + Environment.NewLine + "<hr>" + Environment.NewLine); }