using System; using GPSBusiness.Enum; using GPSBusiness.Helper; using GPSBusiness.GPSStandard; using Newtonsoft.Json; namespace GPSBusiness.BBGPSStandard { public static class BbGpsStandardService { /// /// 获取返回给设备的数据 /// /// /// /// /// /// public static GPSResponseData GetGpsResponseDataForEqu(byte[] bytes, IStandardToCarGPSHandler standardToCarGpsHandler, IntPtr conid, string mdtText) { byte[] byteDataId = new byte[2]; //反转义 byte[] buffer = ByteConvert.Fzy(bytes); Array.Copy(buffer, 1, byteDataId, 0, 2); Array.Reverse(byteDataId); UInt16 uint16DataId = BitConverter.ToUInt16(byteDataId, 0); string dataId = uint16DataId.ToString("X2").PadLeft(4, '0'); string terminalNo = ByteConvert.ByteArrayToHexStr(buffer).Substring(11, 11);//设备号 "NO".Log().Debug(ByteConvert.ByteArrayToHexStr(buffer).ToString()); DataHead dataHead = new DataHead(); dataHead.Frombytes(dataHead, buffer); "GetGpsResponseDataForEqu".Log().DebugFormat(" >terminalNo={0},uint16DataId=0x{1}", terminalNo, dataId); //"GetGpsResponseDataForEqu".Log().DebugFormat(" >dataHead={0}", JsonConvert.SerializeObject(dataHead)); //接收信息(分类) #region 0x0002,终端心跳 if (uint16DataId == 0x0002) { //平台通用应答 var responds = CommonResponse(dataHead); //确定该心跳是否为沃瑞特的gps,是则后续进行:gps在线状态更新。MyGpsServer.UpdateGpsRealTimeGpsStateWrt(gpsModel); //var vehicle = standardToCarGpsHandler.GetGps(bytes, terminalNo, conid, true); //if (vehicle != null && vehicle.TerminalType == TerminalTypeEnum.C12.ToEnumInt()) //{ // responds.GpsInfo = vehicle; //} return responds; } #endregion #region 0x0100,终端注册 0x0100 if (uint16DataId == 0x0100) { //注册消息 var model = new Data_0100(); model.FromBytes(model, dataHead.DataContent); //应答消息 var model2 = new Data_8100 { Mdt_SerialNo = dataHead.DataSerialNo, Result = 0x00, AuthorizationCode = "regist123" }; //0:成功;1:车辆已被注册;2:数据库中无该车辆;3:终端已被注册;4:数据库中无该终端 var dataHeadModel = new DataHead { DataID = 0x8100, CenterID = dataHead.CenterID, MdtID = dataHead.MdtID }; return new GPSResponseData() { ReturnData = model2.GetBytes(dataHeadModel), }; } #endregion #region 0x0102,终端鉴权,鉴权7-8次不成功,终端为再次注册 if (uint16DataId == 0x0102) { return CommonResponse(dataHead); } #endregion #region 0x0200,位置数据0x0200 if (uint16DataId == 0x0200) { var responds = CommonResponse(dataHead); responds.GpsInfo = standardToCarGpsHandler.GetGps(bytes, terminalNo, conid, false); "GetGpsResponseDataForEqu".Log().DebugFormat(" >GpsInfo={0}", JsonConvert.SerializeObject(responds.GpsInfo)); return responds; } #endregion #region 0x0704,定位数据批量上传--0x0704,(沃瑞特:盲区补传) if (uint16DataId == 0x0704) { //处理gps批量数据。然后通用回复。 return CommonResponse(dataHead) ; } #endregion #region 0xA003,请求服务器时间0xA003,回应:0xB003。//沃瑞特gps if (uint16DataId == 0xA003) { var now = DateTime.Now; int year = Convert.ToInt32(now.Year.ToString().Substring(2, 2)); byte rYear = ByteConvert.IntToBcd(year); byte rMonth = ByteConvert.IntToBcd(now.Month); byte rDay = ByteConvert.IntToBcd(now.Day); byte rHour = ByteConvert.IntToBcd(now.Hour); byte rMinute = ByteConvert.IntToBcd(now.Minute); byte rSecond = ByteConvert.IntToBcd(now.Second); byte[] times = { rYear, rMonth, rDay, rHour, rMinute, rSecond }; var model = new Data_B003 { Mdt_SerialNo = dataHead.DataSerialNo, SysTime = times }; var dataHeadModel = new DataHead { DataID = 0xB003, CenterID = dataHead.CenterID, MdtID = dataHead.MdtID, }; return new GPSResponseData() { ReturnData = model.GetBytes(dataHeadModel), }; } #endregion //0x8001,平台通用应答 return CommonResponse(dataHead); } /// /// 通用回复 /// /// /// private static GPSResponseData CommonResponse(DataHead dataHead) { var model2 = new Data_8001 { Mdt_SerialNo = dataHead.DataSerialNo, Mdt_DataID = dataHead.DataID, Result = 0x00 }; var dataHeadModel = new DataHead { DataID = 0x8001, CenterID = dataHead.CenterID, MdtID = dataHead.MdtID }; var responds = new GPSResponseData() { ReturnData = model2.GetBytes(dataHeadModel) }; return responds; } } }