/// <summary> /// 接收数据 /// </summary> /// <param name="socket"></param> /// <param name="timeout"></param> /// <param name="content"></param> /// <param name="conLen"></param> /// <param name="type"></param> /// <param name="tel"></param> /// <returns></returns> public static bool rdata(ref string protocol, Socket socket, int timeout, ref byte[] content, ref int conLen, ref byte type, ref string tel) { if (protocol == DTU._ZG_DTU) { return(ZG_DTU.rdata(socket, timeout, ref content, ref conLen, ref type, ref tel)); //1秒超时 等待注册数据 } else if (protocol == DTU._HD_DTU) { return(HD_DTU.rdata(socket, timeout, ref content, ref conLen, ref type, ref tel)); //1秒超时 等待注册数据 } else if (protocol == DTU._JBT_DTU) { return(JBT_DTU.rdata(socket, timeout, ref content, ref conLen, ref type, ref tel)); //1秒超时 等待注册数据 } else { //在DTU未注册之前, socket接收都是调用以下的代码 , try { byte[] buffer = new byte[GlobalPara.SEND_DATA_LENGTH]; //接收数据的数组 socket.ReceiveTimeout = timeout; int len = socket.Receive(buffer, buffer.Length, SocketFlags.None); protocol = DTU.CheckProtocolByRegisterData(buffer, len); if (protocol == DTU._ZG_DTU) { return(ZG_DTU.UnPack(buffer, len, ref content, ref conLen, ref type, ref tel)); } else if (protocol == DTU._HD_DTU) { return(HD_DTU.UnPack(buffer, len, ref content, ref conLen, ref type, ref tel)); } else if (protocol == DTU._JBT_DTU) { return(JBT_DTU.UnPack(buffer, len, ref content, ref conLen, ref type, ref tel)); } else { return(false); } } catch (SocketException) { return(false); } catch (Exception ex) { LogMg.AddError(ex); return(false); } } }
/// <summary> /// 解析注册的数据是DTU协议还是DDP协议 /// </summary> /// <param name="buffer"></param> /// <returns></returns> public static string CheckProtocolByRegisterData(byte[] buffer, int len) { if (ZG_DTU.CheckProtocolByRegisterData(buffer, len) == true) { return(_ZG_DTU); } if (HD_DTU.CheckProtocolByRegisterData(buffer, len) == true) { return(_HD_DTU); } if (JBT_DTU.CheckProtocolByRegisterData(buffer, len) == true) { return(_JBT_DTU); } //如果不符合上面的协议标准 则返回空字符串 LogMg.AddDebug("检验注册数据格式不正确 ,即不符合佐格DTU协议,也不符合宏电DTU协议,也不符号金博通DTU协议"); return(""); }
/// <summary> /// 响应注册数据或心跳数据 /// </summary> /// <param name="protocol"></param> /// <param name="socket"></param> /// <param name="tel"></param> /// <returns></returns> public static bool ResponseRegisterOrHeart(string protocol, Socket socket, string tel) { if (protocol == DTU._ZG_DTU) { return(ZG_DTU.HeartbeatDataHandler(socket)); } else if (protocol == DTU._HD_DTU) { return(HD_DTU.ResponseRegister(socket, tel)); } else if (protocol == DTU._JBT_DTU) { return(JBT_DTU.Response_Reigster(socket, tel)); } else { return(false); } }
/// <summary> /// 发送数据 /// </summary> /// <param name="socket"></param> /// <param name="content"></param> /// <param name="conLen"></param> /// <param name="type"></param> public static void SendUserData(string protocol, Socket socket, byte[] content, int conLen, string tel, byte channel) { if (protocol == DTU._ZG_DTU) { ZG_DTU.Send(socket, content, conLen, ZG_DTU.USER_DATA); } else if (protocol == DTU._HD_DTU) { HD_DTU.Send(socket, content, conLen, HD_DTU.DSC_User_Data, tel); } else if (protocol == DTU._JBT_DTU) { JBT_DTU.SendUserData_Master(socket, content, conLen, tel); } else if (protocol == DTU._GPRS_CONTROL) { GPRS_Protocol.Send(socket, channel, content, conLen); } }
/// <summary> /// 处理数据 /// </summary> /// <param name="protocol"></param> /// <param name="clientSocket"></param> /// <param name="content"></param> /// <param name="conLen"></param> /// <param name="type"></param> /// <param name="tel"></param> public static bool HandlerData(string protocol, Socket clientSocket, byte[] content, int conLen, byte type, string tel) { if (protocol == DTU._ZG_DTU) { ZG_DTU.HandlerData(clientSocket, content, conLen, type, tel); return(true); } else if (protocol == DTU._HD_DTU) { HD_DTU.HandlerData(clientSocket, content, conLen, type, tel); return(true); } else if (protocol == DTU._JBT_DTU) { JBT_DTU.HandlerData(clientSocket, content, conLen, type, tel); return(true); } else { return(false); } }
/// <summary> /// 拍照 /// </summary> /// <param name="client"></param> /// <param name="timeout"></param> public static bool TakePhoto(DTUClientInfo client, int timeout, ref byte[] imageBytes) { //这个方法主要包含三个步骤, //第一步:发送拍照指令 //第二步:发送“获取照片包数量”的指令 //第三步:循环去读取每一个照片包内容 byte[] rubbish = new byte[JBT_DTU.ReceiveLength]; byte packcount = 0x00; int n = 1; //当发送的命令没接收到,这个变量就会+1,直到他等于5 int commandLen1 = 0; byte[] command1 = GetTakePhotoCommand(ref commandLen1); try { client.socket.Receive(rubbish, JBT_DTU.ReceiveLength, SocketFlags.None); } catch (Exception) { } //第一步:发送拍照指令 JBT_DTU.SendUserData_Assistant(client.socket, command1, commandLen1, client.TelOrGprsId); while (n <= 5) { try { byte[] content = new byte[JBT_DTU.ReceiveLength]; int conLen = 0; byte type = new byte(); string tel = ""; Thread.Sleep(3000); bool flag = JBT_DTU.rdata(client.socket, timeout, ref content, ref conLen, ref type, ref tel); if (flag == false) { n++; continue; } if (type != JBT_DTU.DTU_to_Server_SendData_Assistant) { n++; continue; } if (conLen != 7) { n++; continue; } break; } catch (SocketException ex) { n++; LogMg.AddError(ex); } catch (Exception ex) { n++; LogMg.AddError(ex); } } DTU_ClientManager.UpdateLastVisitTime(client, DateTime.Now); if (n > 5) { return(false); //如果读取5次都读不到数据,就表示这次拍照失败 } LogMg.AddError("拍照命令 拍照次数=" + n); n = 1; int commandLen2 = 0; //第二步:发送“获取照片包数量”的指令 byte[] command2 = GetPicPackCountCommand(ref commandLen2); JBT_DTU.SendUserData_Assistant(client.socket, command2, commandLen2, client.TelOrGprsId); while (n <= 5) { try { byte[] content = new byte[4096]; int conLen = 0; byte type = new byte(); string tel = ""; Thread.Sleep(1000); bool flag = JBT_DTU.rdata(client.socket, timeout, ref content, ref conLen, ref type, ref tel); if (flag == false) { n++; continue; } if (type != JBT_DTU.DTU_to_Server_SendData_Assistant) { n++; continue; } if (conLen != 8) { n++; continue; } packcount = content[4]; break; } catch (SocketException ex) { n++; LogMg.AddError(ex); } catch (Exception ex) { n++; LogMg.AddError(ex); } } if (n > 5) { return(false); } LogMg.AddError("获取照片包数量 n=" + n); imageBytes = new byte[packcount * 1000]; int picIndex = 0; //第三步:循环去读取每一个照片包内容 for (byte i = 0x00; i < packcount; i++) { n = 1; int commandLen3 = 0; byte[] command3 = GetReadPictureCommand(i, ref commandLen3); JBT_DTU.SendUserData_Assistant(client.socket, command3, commandLen3, client.TelOrGprsId); while (n <= 5) { try { byte[] content = new byte[4096]; int conLen = 0; byte type = new byte(); string tel = ""; Thread.Sleep(1000); bool flag = JBT_DTU.rdata(client.socket, timeout, ref content, ref conLen, ref type, ref tel); if (flag == false) { n++; continue; } if (type != JBT_DTU.DTU_to_Server_SendData_Assistant) { n++; continue; } if (content[0] != HEAD || content[1007] != HEAD) // { n++; continue; } if (content[04] != i) //如果包号不对 { n++; continue; } Buffer.BlockCopy(content, 5, imageBytes, picIndex, 1000); picIndex += 1000; break; } catch (SocketException ex) { n++; LogMg.AddError(ex); LogMg.AddError("循环读取包 n=" + n); } catch (Exception ex) { n++; LogMg.AddError(ex); } } if (n > 5) { return(false); } } //ImageUtil.SaveFormBytes(imageBytes, GetImageLen(imageBytes)); //len = GetImageLen(imageBytes); return(true); }