public static void ReChange(string Msg) { try { if (Msg.IndexOf("ButtonSubmit2") > -1) { HashKey hk = Tools.HaseGetKey(Html.GetHttpValue(Msg), Value.hs); if (hk != null) { for (int i = 0; i < hk.Str.Length; i++) { string[] array = hk.Str[i].Split(':'); if (array.Length > 1) { hk.Str[i] = array[0] + ":" + GetPostValue(Msg, "TextBox" + i + "="); } } hk.Update = 1; } } if (Msg.IndexOf("ButtonSubmit1") > -1) { HashKey hk = Tools.HaseGetKey(Html.GetHttpValue(Msg), Value.hs); if (hk != null) { hk.Update = 2; } } } catch (Exception E) { Value.WriteLog.WriteLine("ReChange" + E.Message, LogType.LT_Warning); } }
public void ClientService() { byte[] RxBuffer = new byte[1500]; byte[] TxBuffer = new byte[1500]; bool DevTrue = false; byte[] BinFile = new byte[0]; HashKey HK = new HashKey(); string[] TextFile = new string[0]; byte[] BinCFG = new byte[0]; int fileBlackSize = 0; service.ReceiveTimeout = 10000; while (TagetValue.Run) { try { int RxLength = service.Receive(RxBuffer); string Msg = System.Text.Encoding.GetEncoding("GB2312").GetString(RxBuffer); if (Msg.IndexOf("GET /") == 0) { Value.WriteLog.WriteLine("Msg:" + Msg, LogType.LT_Warning); sendButtonResponse(service, Html.GetHttpValue(Msg)); Value.WriteLog.WriteLine("key:" + Html.GetHttpValue(Msg), LogType.LT_Warning); DevTrue = false; service.Close(); return; } if (Msg.IndexOf("POST /") == 0) { Value.WriteLog.WriteLine("Msg:" + Msg, LogType.LT_Warning); if (Msg.IndexOf("ButtonSubmit") < 0) { int receivedBCount = service.Receive(RxBuffer); Msg += charEncoder.GetString(RxBuffer, 0, receivedBCount); Value.WriteLog.WriteLine("接收2:" + Msg, LogType.LT_Warning); Value.WriteLog.WriteLine("key:" + Html.GetHttpValue(Msg), LogType.LT_Warning); } DevTrue = false; Tools.ReChange(Msg); sendButtonResponse(service, Html.GetHttpValue(Msg)); service.Close(); return; } if (service.Poll(10, SelectMode.SelectRead)) { Value.WriteLog.WriteLine("客户断开", LogType.LT_Warning); Value.DevOnlineCount--; service.Close(); if (DevTrue) { Tools.RemoveHK(HK.OnlyTime); } return; } if (RxLength > 1) { if (ZBUS.ZBUS_CheckCrc(RxBuffer)) { DevTrue = true; string str = Tools.HexToString(RxBuffer, RxLength); int ID = ((int)RxBuffer[0]) * 0xff + (int)RxBuffer[1]; Value.WriteLog.WriteLine("Revice:" + str, LogType.LT_Warning); switch (RxBuffer[2]) { case (byte)UpGradeCmd.UGC_SetBinFile://设置焦点文件设置块大小 fileBlackSize = RxBuffer[5]; fileBlackSize <<= 8; fileBlackSize += RxBuffer[6]; Value.WriteLog.WriteLine("Revice:" + fileBlackSize, LogType.LT_Warning); RxLength = ZBUS.ZBUS_SendMsg(ref TxBuffer, ID, (byte)UpGradeCmd.UGC_SetBinFile | 0x80, null, 0); Value.WriteLog.WriteLine("Send:" + Tools.HexToString(TxBuffer, RxLength), LogType.LT_Warning); service.Send(TxBuffer.Skip(0).Take(RxLength).ToArray()); Value.WriteLog.WriteLine("LoadBinFile:" + ID, LogType.LT_Warning); BinFile = Value.fileIO.ReadBin(ID); break; case (byte)UpGradeCmd.UGC_GetBinFileInfor://获取文件信息 RxLength = ZBUS.ZBUS_SendMsg(ref TxBuffer, ID, (byte)UpGradeCmd.UGC_GetBinFileInfor | 0x80, Tools.ByteFromU32((UInt32)BinFile.Length, 0).Concat(Tools.GetCrc16byte(BinFile, 0, BinFile.Length)).ToArray(), 6); Value.WriteLog.WriteLine("Send:" + Tools.HexToString(TxBuffer, RxLength), LogType.LT_Warning); service.Send(TxBuffer.Skip(0).Take(RxLength).ToArray()); break; case (byte)UpGradeCmd.UGC_ReadBinFile: byte[] Data = new byte[0]; if ((fileBlackSize * ID + fileBlackSize) <= BinFile.Length)//完整帧 { Data = BinFile.Skip(fileBlackSize * ID).Take(fileBlackSize).ToArray(); } else if ((fileBlackSize * ID) < BinFile.Length) //不足一帧 { Data = BinFile.Skip(fileBlackSize * ID).Take(BinFile.Length - fileBlackSize * ID).ToArray(); } RxLength = ZBUS.ZBUS_SendMsg(ref TxBuffer, ID, (byte)UpGradeCmd.UGC_ReadBinFile | 0x80, Data, Data.Length); Value.WriteLog.WriteLine("Send:" + Tools.HexToString(TxBuffer, RxLength), LogType.LT_Warning); service.Send(TxBuffer.Skip(0).Take(RxLength).ToArray()); break; case (byte)UpGradeCmd.UGC_SetTextFile: RxLength = ZBUS.ZBUS_SendMsg(ref TxBuffer, ID, (byte)UpGradeCmd.UGC_SetTextFile | 0x80, null, 0); Value.WriteLog.WriteLine("Send:" + Tools.HexToString(TxBuffer, RxLength), LogType.LT_Warning); service.Send(TxBuffer.Skip(0).Take(RxLength).ToArray()); Value.WriteLog.WriteLine("LoadTextFile:" + ID, LogType.LT_Warning); TextFile = Value.fileIO.ReadText(ID); break; case (byte)UpGradeCmd.UGC_ReadTextFile: byte[] Text = new byte[0]; if (ID < TextFile.Length) { Text = System.Text.Encoding.Default.GetBytes(TextFile[ID]).Concat(new byte[] { 0x00 }).ToArray(); Value.WriteLog.WriteLine("SendLine[" + ID + "]:" + TextFile[ID], LogType.LT_Warning); } RxLength = ZBUS.ZBUS_SendMsg(ref TxBuffer, ID, (byte)UpGradeCmd.UGC_ReadTextFile | 0x80, Text, Text.Length); service.Send(TxBuffer.Skip(0).Take(RxLength).ToArray()); break; case (byte)UpGradeCmd.UGC_GetTextFileInfor: RxLength = ZBUS.ZBUS_SendMsg(ref TxBuffer, ID, (byte)UpGradeCmd.UGC_GetTextFileInfor | 0x80, Tools.ByteFromU32((UInt32)TextFile.Length, 0), 4); Value.WriteLog.WriteLine("SendTextInfor:" + Tools.HexToString(TxBuffer, RxLength), LogType.LT_Warning); service.Send(TxBuffer.Skip(0).Take(RxLength).ToArray()); break; case (byte)UpGradeCmd.UCG_GetDateTime: byte[] TData = Tools.DateTimeToBytes(DateTime.Now); RxLength = ZBUS.ZBUS_SendMsg(ref TxBuffer, ID, (byte)UpGradeCmd.UCG_GetDateTime | 0x80, TData, TData.Length); Value.WriteLog.WriteLine("SendDate:" + Tools.HexToString(TxBuffer, RxLength), LogType.LT_Warning); service.Send(TxBuffer.Skip(0).Take(RxLength).ToArray()); break; case (byte)UpGradeCmd.UCG_SendBinCFG: fileBlackSize = RxBuffer[3]; fileBlackSize <<= 8; fileBlackSize += RxBuffer[4]; BinCFG = BinCFG.Concat(RxBuffer.Skip(5).Take(fileBlackSize)).ToArray(); byte[] Bin = new byte[0]; RxLength = ZBUS.ZBUS_SendMsg(ref TxBuffer, ID, (byte)UpGradeCmd.UCG_SendBinCFG | 0x80, Bin, Bin.Length); Value.WriteLog.WriteLine("SendDate:" + Tools.HexToString(TxBuffer, RxLength), LogType.LT_Warning); Value.WriteLog.WriteLine("BinCFG[" + BinCFG.Length + "]:" + Tools.HexToString(BinCFG, BinCFG.Length), LogType.LT_Warning); service.Send(TxBuffer.Skip(0).Take(RxLength).ToArray());; if (fileBlackSize == 0) { Value.fileIO.WriteBinCFG(ID, BinCFG); } break; case (byte)UpGradeCmd.UCG_SetBinCFGFile://设置焦点文件设置块大小 fileBlackSize = RxBuffer[5]; fileBlackSize <<= 8; fileBlackSize += RxBuffer[6]; Value.WriteLog.WriteLine("Revice:" + fileBlackSize, LogType.LT_Warning); RxLength = ZBUS.ZBUS_SendMsg(ref TxBuffer, ID, (byte)UpGradeCmd.UCG_SetBinCFGFile | 0x80, null, 0); Value.WriteLog.WriteLine("Send:" + Tools.HexToString(TxBuffer, RxLength), LogType.LT_Warning); service.Send(TxBuffer.Skip(0).Take(RxLength).ToArray()); Value.WriteLog.WriteLine("LoadBinFile:" + ID, LogType.LT_Warning); BinFile = Value.fileIO.ReadBinCFG(ID); break; case (byte)UpGradeCmd.UCG_SetMenuList: fileBlackSize = RxBuffer[3]; fileBlackSize <<= 8; fileBlackSize += RxBuffer[4]; if (RxBuffer[0] == 0xff) { HK.Key = "aabbccdd";// Tools.ConvertTo(RxBuffer.Skip(5).Take(12).ToArray()); if (Tools.HaseGetKey(HK.Key, Value.hs) == null) { HK.Update = 0; HK.OnlyTime = DateTime.Now.ToString("yyyyMMddHHmmssfff"); HK.Str = new string[RxBuffer[1]]; Value.hs.Add(HK); } } else { Tools.HaseSetJoin(HK.Key, Value.hs, RxBuffer[1], Encoding.Default.GetString(RxBuffer, 5, fileBlackSize)); HashKey hk1 = Tools.HaseGetKey(HK.Key, Value.hs); if (hk1 != null) { hk1.Update = 0; } } Value.WriteLog.WriteLine("Revice:" + Encoding.Default.GetString(RxBuffer, 5, fileBlackSize), LogType.LT_Warning); RxLength = ZBUS.ZBUS_SendMsg(ref TxBuffer, ID, (byte)UpGradeCmd.UCG_SetMenuList | 0x80, null, 0); Value.WriteLog.WriteLine("Send:" + Tools.HexToString(TxBuffer, RxLength), LogType.LT_Warning); service.Send(TxBuffer.Skip(0).Take(RxLength).ToArray()); break; case (byte)UpGradeCmd.UCG_GetMenuList: string temp = Tools.GetHaseKey(HK.Key, Value.hs)[RxBuffer[1]]; if (temp.IndexOf(':') > 0) { temp = temp.Substring(temp.IndexOf(':') + 1); BinCFG = Encoding.Default.GetBytes(temp); BinCFG = BinCFG.Concat(new byte[] { 0x00 }).ToArray(); } else { BinCFG = new byte[0]; } RxLength = ZBUS.ZBUS_SendMsg(ref TxBuffer, ID, (byte)UpGradeCmd.UCG_GetMenuList | 0x80, BinCFG, BinCFG.Length); Value.WriteLog.WriteLine("Send:" + Tools.HexToString(TxBuffer, RxLength), LogType.LT_Warning); service.Send(TxBuffer.Skip(0).Take(RxLength).ToArray()); break; case (byte)UpGradeCmd.UCG_CheckNewMenu: HashKey hk = Tools.HaseGetKey(HK.Key, Value.hs); BinCFG = new byte[0]; if (hk != null) { BinCFG = new byte[1] { hk.Update }; hk.Update = 0; } RxLength = ZBUS.ZBUS_SendMsg(ref TxBuffer, ID, (byte)UpGradeCmd.UCG_GetMenuList | 0x80, BinCFG, BinCFG.Length); Value.WriteLog.WriteLine("Send:" + Tools.HexToString(TxBuffer, RxLength), LogType.LT_Warning); service.Send(TxBuffer.Skip(0).Take(RxLength).ToArray()); break; default: break; } } else { Value.WriteLog.WriteLine("校验失败:" + Tools.HexToString(RxBuffer, RxLength), LogType.LT_Warning); } } } catch (Exception E) { Value.WriteLog.WriteLine("异常退出线程:" + E.Message, LogType.LT_Warning); try { if (DevTrue) { Tools.RemoveHK(HK.OnlyTime); } service.Close(); } catch { Value.WriteLog.WriteLine("已断开", LogType.LT_Warning); } return; } } }