UserHandler.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. package com.js.kbt.socket;
  2. import java.math.BigDecimal;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.List;
  7. import javax.annotation.Resource;
  8. import org.apache.log4j.Logger;
  9. import org.springframework.stereotype.Service;
  10. import com.alibaba.fastjson.JSONObject;
  11. import com.js.kbt.mapper.MouldDownPacketMapper;
  12. import com.js.kbt.mapper.MouldHistoryMapper;
  13. import com.js.kbt.mapper.MouldHistoryTimeMapper;
  14. import com.js.kbt.mapper.MouldHodingMapper;
  15. import com.js.kbt.mapper.TbFactoryMapper;
  16. import com.js.kbt.mapper.TbMouldEquipmentMapper;
  17. import com.js.kbt.mapper.TbMouldMapper;
  18. import com.js.kbt.mapper.TimeCalibrationRecordMapper;
  19. import com.js.kbt.model.MouldDownPacket;
  20. import com.js.kbt.model.MouldDownPacketExample;
  21. import com.js.kbt.model.MouldHistory;
  22. import com.js.kbt.model.MouldHistoryExample;
  23. import com.js.kbt.model.MouldHistoryTime;
  24. import com.js.kbt.model.MouldHistoryTimeExample;
  25. import com.js.kbt.model.TbMould;
  26. import com.js.kbt.model.TbMouldEquipment;
  27. import com.js.kbt.model.TbMouldEquipmentExample;
  28. import com.js.kbt.model.TbMouldExample;
  29. import com.js.kbt.model.TimeCalibrationRecord;
  30. import io.netty.buffer.ByteBuf;
  31. import io.netty.buffer.Unpooled;
  32. import io.netty.channel.ChannelHandlerContext;
  33. import io.netty.channel.SimpleChannelInboundHandler;
  34. import io.netty.handler.timeout.IdleState;
  35. import io.netty.handler.timeout.IdleStateEvent;
  36. @Service("userHandler")
  37. public class UserHandler extends SimpleChannelInboundHandler<String> {
  38. private static final Logger logger = Logger.getLogger(UserHandler.class);
  39. private ChannelHandlerContext ctx;
  40. @Resource
  41. private MouldHistoryMapper mouldHistoryMapper;
  42. @Resource
  43. private MouldHistoryTimeMapper mouldHistoryTimeMapper;
  44. @Resource
  45. private TbMouldMapper tbMouldMapper;
  46. @Resource
  47. private TbMouldEquipmentMapper tbMouldEquipmentMapper;
  48. @Resource
  49. private TbFactoryMapper tbFactoryMapper;
  50. @Resource
  51. private MouldHodingMapper mouldHodingMapper;
  52. @Resource
  53. private MouldDownPacketMapper mouldDownPacketMapper;
  54. @Resource
  55. private TimeCalibrationRecordMapper timeCalibrationRecordMapper;
  56. @Override
  57. protected void channelRead0(ChannelHandlerContext arg0, String arg1) {
  58. String pack = "FAAF0007001e781e50003C37D5";
  59. // 校准时间
  60. timeCalibration();
  61. System.out.println("收到===" + arg1 + "\n");
  62. String equipmentNo = processMsg(arg1);
  63. // 以下是配置下行数据
  64. // sendPackage(equipmentNo);
  65. }
  66. private String processMsg(String input) {
  67. String ret = "FA AF 00 07 02 1e 78 1e 50 00 3C";
  68. ret.replaceAll(" ", "");
  69. logger.info("=====接收到======" + input);
  70. if (!input.startsWith("FAAF")) {
  71. logger.info("非云模盒消息,不处理");
  72. return ret;
  73. }
  74. MouldHistory item = new MouldHistory();
  75. // 抽取手机号码4-5(预留)丢包情况硬件会上报状态01,服务器判断02时候下发printall
  76. String mobilePart = input.substring(4 * 2, 5 * 2);
  77. System.out.println("原始mobile=" + mobilePart);
  78. String mobile = getStringFromHexStr(mobilePart);
  79. System.out.println("手机号码为" + mobile);
  80. item.setSim(mobile);
  81. // 获取设备编码15-24
  82. String deviceNumPart = input.substring(5 * 2, 25 * 2);
  83. String deviceNum = getStringFromHexStr(deviceNumPart);
  84. System.out.println("设备No=" + deviceNum);
  85. item.setEquipmentNo(deviceNum);
  86. // 发送数据包
  87. MouldDownPacketExample pExp = new MouldDownPacketExample();
  88. pExp.createCriteria().andEquipmentNoEqualTo(deviceNum);
  89. List<MouldDownPacket> packetList = mouldDownPacketMapper.selectByExample(pExp);
  90. if (packetList.size() > 0) {
  91. MouldDownPacket packet = packetList.get(0);
  92. logger.info("开始下发数据包==>" + packet.getPacketStr());
  93. sendMsg(packet.getPacketStr());
  94. // 下发完就将数据删除(防止后续数据过大造成性能的影响)
  95. mouldDownPacketMapper.deleteByPrimaryKey(packet.getId());
  96. logger.info("判断是否开启或关闭已结束==>");
  97. }
  98. // 软件版本号25
  99. String version = input.substring(25 * 2, 26 * 2);
  100. System.out.println("软件版本号=" + version);
  101. item.setVersion(version);
  102. String status = input.substring(26 * 2, 27 * 2);
  103. System.out.println("工作状态=" + status);
  104. item.setStatus(Integer.decode("0x" + status).toString());
  105. // 经度27-37
  106. String longitude = getStringFromHexStr(input.substring(27 * 2, 38 * 2));
  107. System.out.println("经度=" + longitude);
  108. item.setLng(longitude);
  109. // 38-47纬度以 N 开头
  110. String latitude = getStringFromHexStr(input.substring(38 * 2, 48 * 2));
  111. System.out.println("纬度=" + latitude);
  112. item.setLat(latitude);
  113. System.out.println("==lac" + reverseParseHex(input.substring(48 * 2, 50 * 2)));
  114. System.out.println("==ci" + reverseParseHex(input.substring(50 * 2, 52 * 2)));
  115. item.setGprsLac("" + reverseParseHex(input.substring(48 * 2, 50 * 2)));
  116. item.setGprsCi("" + reverseParseHex(input.substring(50 * 2, 52 * 2)));
  117. item.setWifiBbsid(input.substring(52 * 2, 58 * 2));
  118. String temp = "0x" + input.substring(58 * 2, 59 * 2);
  119. System.out.println(temp);
  120. int i = Integer.decode(temp)-40;
  121. System.out.println(i);
  122. logger.info("温度-----------》: " + i);
  123. item.setTemperature(i);
  124. item.setBattery(Integer.decode("0x" + input.substring(59 * 2, 60 * 2)));
  125. item.setSig2g(Integer.decode("0x" + input.substring(60 * 2, 61 * 2)));
  126. item.setSigNb(Integer.decode("0x" + input.substring(61 * 2, 62 * 2)));
  127. item.setSigWifi(Integer.decode("0x" + input.substring(62 * 2, 63 * 2)));
  128. item.setExt0("" + Integer.decode("0x" + input.substring(63 * 2, 64 * 2)));
  129. item.setAlarm(Integer.decode("0x" + input.substring(64 * 2, 65 * 2)));
  130. String str = input.substring(65 * 2, 69 * 2);
  131. item.setRunCnt(reverseParseHex(str));
  132. // 根据基站lac和ci获取经纬度
  133. String api = "http://api.cellocation.com:81/cell/?mcc=460&mnc=0&lac=" + item.getGprsLac() + "&ci="
  134. + item.getGprsCi() + "&output=json";
  135. String resp = com.js.kbt.util.HttpRequest.sendGet(api, null);
  136. JSONObject json = JSONObject.parseObject(resp);
  137. if (json.getInteger("errcode") == 0) {
  138. item.setLng(json.getDouble("lon") + "");
  139. item.setLat(json.getDouble("lat") + "");
  140. } else {
  141. logger.error("调用基站解析平台出错: " + resp);
  142. }
  143. String crcStr = input.substring(input.length() - 4);
  144. item.setCrcCode("" + reverseParseHex(crcStr));
  145. //找寻上一条最新记录
  146. MouldHistoryExample mExp = new MouldHistoryExample();
  147. mExp.createCriteria().andEquipmentNoEqualTo(value)
  148. // 存入数据库
  149. mouldHistoryMapper.selectByExample(example);
  150. mouldHistoryMapper.insertSelective(item);
  151. logger.info("添加数据完成了-----------》: " + item);
  152. // 模具开合记录
  153. int cnt = item.getRunCnt();
  154. int start = 69;
  155. logger.info("累计开合模次数=" + cnt);
  156. int end = input.length() - 4; // 最后2位 0xXX,0xXX ,是CRC校验位
  157. logger.info("时间信息==" + input.substring(start * 2, end));
  158. if (end - start * 2 >= 24) {// 时间最少6位,1位显示0x00,开合算两个时间,所以是6*2*2=24
  159. // 本次运行周期内的开合模次数
  160. int periodCnt = (end - start * 2) / 24;
  161. logger.info("本次开合模次数==" + periodCnt);
  162. for (int pos = 0; pos < periodCnt; pos++) {
  163. // 合模时间
  164. MouldHistoryTime time = new MouldHistoryTime();
  165. time.setHistoryId(item.getId());
  166. time.setEquipmentNo(item.getEquipmentNo());
  167. time.setSeq(pos + 1);
  168. Date closeTime = parseDate(input, start, pos, 0);
  169. time.setCloseTime(closeTime);
  170. // 开模时间
  171. Date openTime = parseDate(input, start, pos, 6);
  172. time.setOpenTime(openTime);
  173. int timeCost = (int) (openTime.getTime() - closeTime.getTime());
  174. time.setTimeCost(timeCost);
  175. mouldHistoryTimeMapper.insertSelective(time);
  176. logger.info("mouldHistoryTime数据添加完毕");
  177. }
  178. } else {
  179. logger.info("本次开合模次数==0");
  180. }
  181. // 统计该模盒的平均开合周期
  182. MouldHistoryExample exp = new MouldHistoryExample();
  183. exp.createCriteria().andEquipmentNoEqualTo(item.getEquipmentNo()).andRunCntEqualTo(item.getRunCnt());
  184. // List<MouldHistoryTime> list = mouldHistoryTimeMapper.selectByEquipmentNoAndThanZero(item.getEquipmentNo());
  185. List<MouldHistory> list = mouldHistoryMapper.selectByExample(exp);
  186. int avgTime = 0;
  187. int totalTime = 0;
  188. if(list.size()==1){
  189. //证明盒子动了,找寻上一条记录
  190. }
  191. for (MouldHistory t : list) {
  192. //删选掉负值的数据
  193. // logger.info("第i条数据开合模周期耗时-------------》》"+t.getTimeCost());
  194. totalTime += t.getTimeCost();
  195. }
  196. // logger.info("开合模周期总耗时次数==》"+totalTime);
  197. if (list.size() == 0) {
  198. avgTime = 0;
  199. } else {
  200. avgTime = totalTime / list.size();
  201. }
  202. // logger.info("开合模平均周期耗时次数==》"+avgTime);
  203. handleModLogic(item, avgTime);
  204. // 判断预留字节的
  205. if ("02".equals(mobilePart)) {
  206. // 丢包数据已占满缓存,调用读取命令
  207. String strHexStr16 = strHexStr16("#PRINTFALL;");
  208. logger.info("丢包数据已占满缓存下发调用读取命令#PRINTFALL;==>" + strHexStr16);
  209. sendMsg(strHexStr16);
  210. return deviceNum;
  211. }
  212. // 返回设备编号
  213. return deviceNum;
  214. }
  215. public void sendPackage(String equipmentNo) {
  216. if (equipmentNo.indexOf("FAAF") != -1) {
  217. logger.info("非法云模盒编号,不作下发处理");
  218. return;
  219. }
  220. MouldDownPacketExample exp = new MouldDownPacketExample();
  221. exp.createCriteria().andEquipmentNoEqualTo(equipmentNo);
  222. List<MouldDownPacket> list = mouldDownPacketMapper.selectByExample(exp);
  223. if (list.size() > 0) {
  224. MouldDownPacket packet = list.get(0);
  225. if (0 == packet.getIsUse()) {
  226. logger.info("开始下发数据包==>" + packet.getPacketStr());
  227. sendMsg(packet.getPacketStr());
  228. }
  229. }
  230. }
  231. public void timeCalibration() {
  232. List<TimeCalibrationRecord> count = timeCalibrationRecordMapper.selectCountByToday();
  233. if (count.size() == 0) {
  234. Date date = new Date();
  235. SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd,HH:mm:ss");
  236. String dateStr = sdf.format(date);
  237. dateStr = "#TIME=" + dateStr + ";";
  238. // #TIME=18-03-06,15:31:48;
  239. logger.info("校准系统模块时间==>" + dateStr);
  240. // 将字符串转成16进制字符串
  241. String strHexStr16 = strHexStr16(dateStr);
  242. logger.info("校准系统模块时间转成16进制的字符串==>" + strHexStr16);
  243. sendMsg(strHexStr16);
  244. TimeCalibrationRecord timeCalibrationRecord = new TimeCalibrationRecord();
  245. timeCalibrationRecord.setIndate(new Date());
  246. timeCalibrationRecordMapper.insertSelective(timeCalibrationRecord);
  247. }
  248. }
  249. private void handleModLogic(MouldHistory item, int avgTime) {
  250. TbMouldEquipmentExample meqExp = new TbMouldEquipmentExample();
  251. meqExp.createCriteria().andEquipmentNoEqualTo(item.getEquipmentNo());
  252. if (tbMouldEquipmentMapper.countByExample(meqExp) > 0) {
  253. TbMouldEquipment me = tbMouldEquipmentMapper.selectByExample(meqExp).get(0);
  254. me.setHillNumber(item.getBattery() + "");
  255. if (item.getLng() != null) {
  256. me.setLng(Double.parseDouble(item.getLng()));
  257. me.setLat(Double.parseDouble(item.getLat()));
  258. }
  259. me.setTemperature(item.getTemperature());
  260. if (item.getAlarm() == 8) {
  261. me.setStage(3);//安装被拆
  262. }else if(item.getAlarm() == 1){
  263. me.setStage(2);// 低电量
  264. }else if(item.getAlarm() == 2){
  265. me.setStage(1);//高温
  266. }else{
  267. me.setStage(item.getAlarm());
  268. }
  269. // 处理报警
  270. if (item.getAlarm() > 0) {
  271. /**
  272. * 无报警 0; 低电量报警 1; 温度过热 2; 安装被拆 8。
  273. */
  274. logger.info("设备报警啦:" + item.getAlarm());
  275. }
  276. tbMouldEquipmentMapper.updateByPrimaryKeySelective(me);
  277. TbMouldExample tExp = new TbMouldExample();
  278. tExp.setOrderByClause("id desc limit 1");
  279. tExp.createCriteria().andEquipmentIdEqualTo(me.getId());
  280. if (tbMouldMapper.countByExample(tExp) > 0) {
  281. TbMould tm = tbMouldMapper.selectByExample(tExp).get(0);
  282. tm.setState(item.getStatus());
  283. // 云模盒运行次数+模具的历史运行次数
  284. tm.setRunTimes(item.getRunCnt() + tm.getHistoryRunTimes());
  285. // 处理每模平均周期(单位秒)
  286. BigDecimal bd = new BigDecimal(avgTime * 1.00 / 1000);
  287. tm.setOcCycle(bd);
  288. tbMouldMapper.updateByPrimaryKeySelective(tm);
  289. }
  290. }
  291. }
  292. /**
  293. * 低位在前的16进制解析
  294. *
  295. * @param rHex
  296. * @return
  297. */
  298. public static int reverseParseHex(String rHex) {
  299. int size = rHex.length() / 2;
  300. StringBuilder sb = new StringBuilder();
  301. for (int i = 0; i < size; i++) {
  302. sb.append(rHex.substring((size - i - 1) * 2, (size - i) * 2));
  303. }
  304. String str = sb.toString();
  305. return Integer.parseInt(str, 16);
  306. }
  307. private static Date parseDate(String input, int start, int pos, int dateStartPos) {
  308. String year = input.substring((start + pos * 6 + dateStartPos) * 2, (start + pos * 6 + 1 + dateStartPos) * 2);
  309. String month = input.substring((start + pos * 6 + 1 + dateStartPos) * 2,
  310. (start + pos * 6 + 2 + dateStartPos) * 2);
  311. String day = input.substring((start + pos * 6 + 2 + dateStartPos) * 2,
  312. (start + pos * 6 + 3 + dateStartPos) * 2);
  313. String hh = input.substring((start + pos * 6 + 3 + dateStartPos) * 2, (start + pos * 6 + 4 + dateStartPos) * 2);
  314. String mm = input.substring((start + pos * 6 + 4 + dateStartPos) * 2, (start + pos * 6 + 5 + dateStartPos) * 2);
  315. String ss = input.substring((start + pos * 6 + 5 + dateStartPos) * 2, (start + pos * 6 + 6 + dateStartPos) * 2);
  316. StringBuilder sb = new StringBuilder();
  317. int yearInt = Integer.parseInt(year, 16);
  318. if (yearInt == 0) {
  319. sb.append("2019-01-01");
  320. } else {
  321. sb.append(yearInt < 100 ? "20" : "2");
  322. sb.append(yearInt).append("-");
  323. int monthInt = Integer.parseInt(month, 16);
  324. sb.append(monthInt < 10 ? "0" : "").append(monthInt).append("-");
  325. int dayInt = Integer.parseInt(day, 16);
  326. sb.append(dayInt < 10 ? "0" : "").append(dayInt);
  327. }
  328. Date parseDate = null;
  329. try {
  330. parseDate = new SimpleDateFormat("yyyy-MM-dd").parse(sb.toString());
  331. parseDate.setHours(Integer.parseInt(hh, 16));
  332. parseDate.setMinutes(Integer.parseInt(mm, 16));
  333. parseDate.setSeconds(Integer.parseInt(ss, 16));
  334. } catch (ParseException e) {
  335. e.printStackTrace();
  336. }
  337. return parseDate;
  338. }
  339. private static String getStringFromHexStr(String hexStr) {
  340. StringBuilder sb = new StringBuilder();
  341. for (int i = 0; i < hexStr.length() / 2; i++) {
  342. String str = "0x" + hexStr.substring(i * 2, i * 2 + 2);
  343. int intVal = Integer.decode(str).intValue();
  344. char c = (char) intVal;
  345. sb.append(c);
  346. }
  347. return sb.toString();
  348. }
  349. public void close() {
  350. ctx.close();
  351. }
  352. public void sendMsg(String hexString) {
  353. System.out.println("发送消息==" + hexString);
  354. byte[] buffer = hexStrToBinaryStr(hexString);
  355. ByteBuf bf = Unpooled.buffer(hexString.length() / 2);
  356. bf.writeBytes(buffer);
  357. ctx.writeAndFlush(bf);
  358. // ctx.close();
  359. }
  360. /**
  361. * channel被激活时调用
  362. */
  363. @Override
  364. public void channelActive(ChannelHandlerContext ctx) {
  365. // TODO Auto-generated method stub
  366. this.ctx = ctx;
  367. System.out.println("==========Active=========" + ctx.channel().localAddress().toString() + ", connection num="
  368. + HelloServer.deviceMap.size());
  369. }
  370. @Override
  371. public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  372. super.channelInactive(ctx);
  373. System.out.println("==========Inactive=========");
  374. }
  375. @Override
  376. public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
  377. // TODO Auto-generated method stub
  378. super.handlerRemoved(ctx);
  379. System.out.println("[YunSu]handlerRemoved=掉线了===");
  380. }
  381. @Override
  382. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  383. // TODO Auto-generated method stub
  384. super.exceptionCaught(ctx, cause);
  385. System.out.println("我捕捉到异常信息了");
  386. }
  387. @Override
  388. public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
  389. if (evt instanceof IdleStateEvent) {
  390. IdleStateEvent event = (IdleStateEvent) evt;
  391. if (event.state().equals(IdleState.READER_IDLE)) {
  392. } else if (event.state().equals(IdleState.WRITER_IDLE)) {
  393. // System.out.println("WRITER_IDLE=="+userInfo.getWxName());
  394. // logger.debug(ctx.channel().remoteAddress().toString()+
  395. // "WRITER_IDLE");
  396. // 超时关闭channel
  397. // ctx.close();
  398. } else if (event.state().equals(IdleState.ALL_IDLE)) {
  399. // 发送心跳
  400. ctx.channel().writeAndFlush("$&_".toString());
  401. }
  402. }
  403. // super.userEventTriggered(ctx, evt);
  404. }
  405. /**
  406. * 将十六进制的字符串转换成字节数组
  407. *
  408. * @param hexString
  409. * @return
  410. */
  411. public static byte[] hexStrToBinaryStr(String hexString) {
  412. if (hexString == null || "".contentEquals(hexString)) {
  413. return null;
  414. }
  415. hexString = hexString.replaceAll(" ", "");
  416. int len = hexString.length();
  417. int index = 0;
  418. byte[] bytes = new byte[len / 2];
  419. while (index < len) {
  420. String sub = hexString.substring(index, index + 2);
  421. bytes[index / 2] = (byte) Integer.parseInt(sub, 16);
  422. index += 2;
  423. }
  424. return bytes;
  425. }
  426. /**
  427. * 将字节数组转换成十六进制的字符串
  428. *
  429. * @return
  430. */
  431. public static String BinaryToHexString(byte[] bytes) {
  432. String hexStr = "0123456789ABCDEF";
  433. String result = "";
  434. String hex = "";
  435. for (byte b : bytes) {
  436. hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
  437. hex += String.valueOf(hexStr.charAt(b & 0x0F));
  438. result += hex + " ";
  439. }
  440. return result;
  441. }
  442. /**
  443. * 普通字符串转16进制字符串
  444. *
  445. * @param str
  446. * @return
  447. */
  448. public static String strHexStr16(String str) {
  449. char[] chars = "0123456789ABCDEF".toCharArray();
  450. StringBuilder sb = new StringBuilder("");
  451. byte[] bs = str.getBytes();
  452. int bit;
  453. for (int i = 0; i < bs.length; i++) {
  454. bit = (bs[i] & 0x0f0) >> 4;
  455. sb.append(chars[bit]);
  456. bit = bs[i] & 0x0f;
  457. sb.append(chars[bit]);
  458. // sb.append(' ');
  459. }
  460. return sb.toString().trim();
  461. }
  462. public static void main(String[] args) {
  463. UserHandler h = new UserHandler();
  464. h.timeCalibration();
  465. // Date date = new Date();
  466. // SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd,HH:mm:ss");
  467. // String dateStr = sdf.format(date);
  468. // dateStr = "#TIME="+dateStr+";";
  469. // System.out.println(dateStr);
  470. // //#TIME=18-03-06,15:31:48;
  471. // logger.info("校准系统模块时间==>"+dateStr);
  472. // String reverseParseHex = strHexStr16(dateStr);
  473. // System.out.println("reverseParseHex-----"+reverseParseHex);
  474. // String str = "1F51";
  475. String input = "FAAF530000000000000000000000003137333030303239343310010000000000000000000000000000000000000000001F51E85F00000000000045004A54000100110000001308160F171F1308160F2D188FCA";
  476. // String temp = "0x"+str;
  477. // int i = Integer.decode(temp);
  478. // System.out.println(i);
  479. // int p = reverseParseHex(str);
  480. // System.out.println(p);
  481. // int start = 69;
  482. // System.out.println(str.substring(69*2));
  483. // Date d = parseDate(str, start, 0, 0);
  484. // System.out.println(d.toGMTString());
  485. int start = 69;
  486. int end = input.length() - 4; // 最后2位 0xXX,0xXX ,是CRC校验位
  487. logger.info("时间信息==" + input.substring(start * 2, end));
  488. if (end - start * 2 >= 24) {// 时间最少6位,1位显示0x00,开合算两个时间,所以是6*2*2=24
  489. // 本次运行周期内的开合模次数
  490. int periodCnt = (end - start * 2) / 24;
  491. logger.info("本次开合模次数==" + periodCnt);
  492. for (int pos = 0; pos < periodCnt; pos++) {
  493. // 合模时间
  494. MouldHistoryTime time = new MouldHistoryTime();
  495. time.setSeq(pos + 1);
  496. Date closeTime = parseDate(input, start, pos, 0);
  497. System.out.println(closeTime);
  498. time.setCloseTime(closeTime);
  499. // 开模时间
  500. Date openTime = parseDate(input, start, pos, 6);
  501. time.setOpenTime(openTime);
  502. System.out.println(openTime);
  503. int timeCost = (int) (openTime.getTime() - closeTime.getTime());
  504. time.setTimeCost(timeCost);
  505. }
  506. } else {
  507. logger.info("本次开合模次数==0");
  508. }
  509. }
  510. }