private void handleBB0F(string sender, byte terminalType) { // 170020140FBB10FFFF0890014950000101089001495000 string cmd = "170020" + Utility.GetHex(terminalType) + "0FBB10FFFF0" + sender + "00001010" + sender + "000"; var TerminalInstance = new TerminalBLL(); var terminal = TerminalInstance.Find(f => f.Sim.Equals(sender)); var CommandInstance = new CommandBLL(); var obj = CommandInstance.GetObject(); //var simno = (simno[0] == '8' && simno[1] == '9' && simno.Length < 11) ? (simno + "000") : simno; obj.DestinationNo = sender + "000"; obj.Status = (byte)CommandStatus.Waiting; obj.Content = cmd; //obj.ActualSendTime = DateTime.Now; obj.Terminal = null == terminal ? (int?)null : terminal.id; obj.SendUser = (int?)null; obj = CommandInstance.Add(obj); //CommandUtility.SendSMSCommand(obj); // 保存SMS发送流量 //saveTerminalFlow(null == terminal ? -1 : terminal.id, sender); }
/// <summary> /// 发送命令并返回新增命令的id,外部捕获这个id并轮询这条命令的发送状态 /// </summary> /// <param name="Sim">sim卡号码(11位数字,everdigm的sim卡号码后面带三个0,如89001435000)</param> /// <param name="Content">命令内容</param> /// <param name="sender">命令发送者的ID</param> /// <returns>命令记录的id,后续通过这个id查询命令状态</returns> private static int SendSMSCommand(TB_Terminal terminal, string Content, int sender) { string simno = terminal.Sim; // 判断Unitel的卡号,前面两位是89,且长度是8位数字 simno = simno[0] == '8' && simno[1] == '9' ? simno.Substring(0, 8) : simno; string ret = SMSUtility.SendSMS(simno, Content); using (var bll = new CommandBLL()) { // 查看发送成功与否的状态 CommandStatus cs = ret.Equals("SUCCESS") ? CommandStatus.SentBySMS : CommandStatus.SentFail; // 新建一个命令发送类实体 TB_Command ct = bll.GetObject(); simno = (simno[0] == '8' && simno[1] == '9' && simno.Length < 11) ? (simno + "000") : simno; ct.DestinationNo = simno; ct.Status = (byte)cs; ct.Content = Content; ct.ActualSendTime = DateTime.Now; ct.Terminal = terminal.id; ct.SendUser = (0 == sender ? (int?)null : sender); var id = bll.Add(ct).id; // 保存终端的命令发送条数 return(id); } }
/// <summary> /// 发送命令 /// </summary> /// <param name="Sim"></param> /// <param name="code"></param> /// <param name="sms"></param> /// <param name="sender">发送者的ID,一般为当前登陆者的ID</param> /// <returns></returns> public static int SendCommand(TB_Terminal terminal, string code, bool sms, int sender, string extra = "") { Command cmd = GetCommand(code); string content = cmd.Content; if (cmd.Code.Equals("6007")) { // 保安命令 // DX 的保安命令需要增加一个日期 content += DateTime.Now.ToString("yyMMddHHmm") + cmd.Param; // DX 的保安命令长度不一样 content = "1700" + content.Substring(4); } else if (cmd.Code.Equals("3000")) { // 装载机的锁车、解锁命令 content = content.Substring(0, content.Length - 4) + cmd.Param + "00"; } else if (cmd.Code.Equals("600B")) { uint time = uint.Parse(extra); byte[] tm = BitConverter.GetBytes(time); string t = tm[0].ToString("X2") + tm[1].ToString("X2") + tm[2].ToString("X2") + tm[3].ToString("X2"); content = content.Substring(0, content.Length - 8) + t; } else if (cmd.Code.Equals("DD02")) { content = content.Substring(0, content.Length - 8) + cmd.Param + "000000"; } else if (cmd.Code.Equals("4000")) { content = content.Substring(0, content.Length - 4) + cmd.Param + "00"; } string sim = terminal.Sim; sim = (sim[0] == '8' && sim[1] == '9' && sim.Length < 11) ? (sim + "000") : sim; content = content.Replace(SIMNO, sim); // 终端类型更改,电子式的挖掘机和装载机一个类型,普通DX挖掘机一个类型 byte ttype = terminal.Type.Value; ttype = (ttype == TerminalTypes.DXE || ttype == TerminalTypes.LD) ? TerminalTypes.LD : TerminalTypes.DX; var type = CustomConvert.IntToDigit(ttype, CustomConvert.HEX, 2); content = content.Substring(0, 6) + type + content.Substring(8); // 终端不是卫星方式连接且需要SMS方式发送时,发送SMS命令 //if (terminal.OnlineStyle != (byte)LinkType.SATELLITE && sms) //{ // return SendSMSCommand(terminal, content, sender); //} //else //{ using (var bll = new CommandBLL()) { var command = bll.GetObject(); command.DestinationNo = sim; // 终端是卫星方式连接则使用卫星方式发送命令 command.Status = (terminal.OnlineStyle == (byte)LinkType.SATELLITE ? (byte)CommandStatus.WaitingForSatellite : // SMS发送时值为 re-sending (byte)(sms ? CommandStatus.WaitingForSMS : CommandStatus.Waiting)); command.Content = content; command.SendUser = (0 == sender ? (int?)null : sender); command.Terminal = terminal.id; return(bll.Add(command).id); } //} }
/// <summary> /// 处理接收到的数据 /// </summary> /// <param name="data"></param> private void HandleReceivedData(AsyncUserDataBuffer data) { int index = 0, len = data.Buffer.Length; while (index < len) { byte[] temp = new byte[len - index]; Buffer.BlockCopy(data.Buffer, index, temp, 0, len - index); TX300 x300 = new TX300(); x300.Content = temp; temp = null; x300.package_to_msg(); // 保存历史记录 //SaveTX300History(x300, data.ReceiveTime); // 更新设备状态 HandleTX300Status(x300, data); // 处理命令回复 if (x300.CommandID != 0xBB0F) { // 不要处理0xBB0F的命令返回数据 HandleGsmCommandResponsed(x300); } // 是否需要返回包 bool isNeedResponse = false; // SMS消息不需要返回包 if (x300.ProtocolType > Protocol.ProtocolTypes.SMS) { if (x300.CommandID == 0xCC00 || x300.CommandID == 0xBB0F) { isNeedResponse = true; } } else { isNeedResponse = true; } if (null != _server && isNeedResponse) { if (x300.CommandID == 0xCC00) { byte[] cc00 = Get0xCC00(x300); var ret = 0; if (data.PackageType == AsyncDataPackageType.TCP) { ret = _server.Send(data.SocketHandle, cc00); } else { ret = _server.Send(Port, IP, cc00); } if (1 != ret) { ShowUnhandledMessage(format("{0}Cannot send data to ip:{1}({2}): {3} [{4}]", Now, IP, Port, CustomConvert.GetHex(cc00), data.PackageType)); } cc00 = null; } else if (x300.CommandID == 0xBB0F) { byte smsType = byte.Parse(ConfigurationManager.AppSettings["SMS_SUBMIT_TYPE"]); // 0xBB0F命令需要服务器发送时 // 2600200A0FBB1001000000000000000101383938363030363631353039313130303136343302 if (smsType == SMSUtility.SUBMIT_BY_DB) { string sender = x300.TerminalID; // 170020140FBB10FFFF0139535986930101089001495000 string cmd = "170020" + CustomConvert.GetHex(x300.TerminalType) + "0FBB10FFFF0" + sender + "01010" + sender; using (var bll = new TerminalBLL()) { var terminal = bll.Find(f => f.Sim.Equals(sender)); using (var cbll = new CommandBLL()) { var obj = cbll.GetObject(); obj.DestinationNo = sender; obj.Status = (byte)CommandStatus.Waiting; obj.Content = cmd; obj.Terminal = null == terminal ? (int?)null : terminal.id; obj.SendUser = null; obj = cbll.Add(obj); } } } } else { // 返回回执 TX300Resp resp = new TX300Resp(); resp.CommandID = x300.CommandID; resp.PackageID = x300.PackageID; resp.SequenceID = x300.SequenceID; resp.Status = 0; resp.Package(); var ret = 0; if (data.PackageType == AsyncDataPackageType.TCP) { ret = _server.Send(data.SocketHandle, resp.Content); } else { ret = _server.Send(Port, IP, resp.Content); } if (1 != ret) { ShowUnhandledMessage(format("{0}Cannot send data to {1}:{2}: {3} [{4}]", Now, IP, Port, CustomConvert.GetHex(resp.Content), data.PackageType)); } } } index += x300.TotalLength; x300 = null; } }