UserHandler.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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.setOrderByClause("order by id desc limit 1");
  148. mExp.createCriteria().andEquipmentNoEqualTo(item.getEquipmentNo());
  149. // 存入数据库
  150. List<MouldHistory> newList = mouldHistoryMapper.selectByExample(mExp);
  151. mouldHistoryMapper.insertSelective(item);
  152. logger.info("添加数据完成了-----------》: " + item);
  153. // 模具开合记录
  154. int cnt = item.getRunCnt();
  155. int start = 69;
  156. logger.info("累计开合模次数=" + cnt);
  157. int end = input.length() - 4; // 最后2位 0xXX,0xXX ,是CRC校验位
  158. logger.info("时间信息==" + input.substring(start * 2, end));
  159. if (end - start * 2 >= 24) {// 时间最少6位,1位显示0x00,开合算两个时间,所以是6*2*2=24
  160. // 本次运行周期内的开合模次数
  161. int periodCnt = (end - start * 2) / 24;
  162. logger.info("本次开合模次数==" + periodCnt);
  163. for (int pos = 0; pos < periodCnt; pos++) {
  164. // 合模时间
  165. MouldHistoryTime time = new MouldHistoryTime();
  166. time.setHistoryId(item.getId());
  167. time.setEquipmentNo(item.getEquipmentNo());
  168. time.setSeq(pos + 1);
  169. Date closeTime = parseDate(input, start, pos, 0);
  170. time.setCloseTime(closeTime);
  171. // 开模时间
  172. Date openTime = parseDate(input, start, pos, 6);
  173. time.setOpenTime(openTime);
  174. int timeCost = (int) (openTime.getTime() - closeTime.getTime());
  175. time.setTimeCost(timeCost);
  176. mouldHistoryTimeMapper.insertSelective(time);
  177. logger.info("mouldHistoryTime数据添加完毕");
  178. }
  179. } else {
  180. logger.info("本次开合模次数==0");
  181. }
  182. // 统计该模盒的平均开合周期
  183. MouldHistoryExample exp = new MouldHistoryExample();
  184. exp.setOrderByClause("id");
  185. exp.createCriteria().andEquipmentNoEqualTo(item.getEquipmentNo()).andRunCntEqualTo(item.getRunCnt());
  186. // List<MouldHistoryTime> list = mouldHistoryTimeMapper.selectByEquipmentNoAndThanZero(item.getEquipmentNo());
  187. List<MouldHistory> list = mouldHistoryMapper.selectByExample(exp);
  188. int avgTime = 0;
  189. int totalTime = 0;
  190. if(list.size()==1){
  191. //证明盒子动了,找寻上一条记录
  192. if(newList.size()>0){
  193. newList.get(0).getIndate().getTime();
  194. }
  195. }
  196. for (MouldHistory t : list) {
  197. //删选掉负值的数据
  198. // logger.info("第i条数据开合模周期耗时-------------》》"+t.getTimeCost());
  199. totalTime += t.getTimeCost();
  200. }
  201. // logger.info("开合模周期总耗时次数==》"+totalTime);
  202. if (list.size() == 0) {
  203. avgTime = 0;
  204. } else {
  205. avgTime = totalTime / list.size();
  206. }
  207. // logger.info("开合模平均周期耗时次数==》"+avgTime);
  208. handleModLogic(item, avgTime);
  209. // 判断预留字节的
  210. if ("02".equals(mobilePart)) {
  211. // 丢包数据已占满缓存,调用读取命令
  212. String strHexStr16 = strHexStr16("#PRINTFALL;");
  213. logger.info("丢包数据已占满缓存下发调用读取命令#PRINTFALL;==>" + strHexStr16);
  214. sendMsg(strHexStr16);
  215. return deviceNum;
  216. }
  217. // 返回设备编号
  218. return deviceNum;
  219. }
  220. public void sendPackage(String equipmentNo) {
  221. if (equipmentNo.indexOf("FAAF") != -1) {
  222. logger.info("非法云模盒编号,不作下发处理");
  223. return;
  224. }
  225. MouldDownPacketExample exp = new MouldDownPacketExample();
  226. exp.createCriteria().andEquipmentNoEqualTo(equipmentNo);
  227. List<MouldDownPacket> list = mouldDownPacketMapper.selectByExample(exp);
  228. if (list.size() > 0) {
  229. MouldDownPacket packet = list.get(0);
  230. if (0 == packet.getIsUse()) {
  231. logger.info("开始下发数据包==>" + packet.getPacketStr());
  232. sendMsg(packet.getPacketStr());
  233. }
  234. }
  235. }
  236. public void timeCalibration() {
  237. List<TimeCalibrationRecord> count = timeCalibrationRecordMapper.selectCountByToday();
  238. if (count.size() == 0) {
  239. Date date = new Date();
  240. SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd,HH:mm:ss");
  241. String dateStr = sdf.format(date);
  242. dateStr = "#TIME=" + dateStr + ";";
  243. // #TIME=18-03-06,15:31:48;
  244. logger.info("校准系统模块时间==>" + dateStr);
  245. // 将字符串转成16进制字符串
  246. String strHexStr16 = strHexStr16(dateStr);
  247. logger.info("校准系统模块时间转成16进制的字符串==>" + strHexStr16);
  248. sendMsg(strHexStr16);
  249. TimeCalibrationRecord timeCalibrationRecord = new TimeCalibrationRecord();
  250. timeCalibrationRecord.setIndate(new Date());
  251. timeCalibrationRecordMapper.insertSelective(timeCalibrationRecord);
  252. }
  253. }
  254. private void handleModLogic(MouldHistory item, int avgTime) {
  255. TbMouldEquipmentExample meqExp = new TbMouldEquipmentExample();
  256. meqExp.createCriteria().andEquipmentNoEqualTo(item.getEquipmentNo());
  257. if (tbMouldEquipmentMapper.countByExample(meqExp) > 0) {
  258. TbMouldEquipment me = tbMouldEquipmentMapper.selectByExample(meqExp).get(0);
  259. me.setHillNumber(item.getBattery() + "");
  260. if (item.getLng() != null) {
  261. me.setLng(Double.parseDouble(item.getLng()));
  262. me.setLat(Double.parseDouble(item.getLat()));
  263. }
  264. me.setTemperature(item.getTemperature());
  265. if (item.getAlarm() == 8) {
  266. me.setStage(3);//安装被拆
  267. }else if(item.getAlarm() == 1){
  268. me.setStage(2);// 低电量
  269. }else if(item.getAlarm() == 2){
  270. me.setStage(1);//高温
  271. }else{
  272. me.setStage(item.getAlarm());
  273. }
  274. // 处理报警
  275. if (item.getAlarm() > 0) {
  276. /**
  277. * 无报警 0; 低电量报警 1; 温度过热 2; 安装被拆 8。
  278. */
  279. logger.info("设备报警啦:" + item.getAlarm());
  280. }
  281. tbMouldEquipmentMapper.updateByPrimaryKeySelective(me);
  282. TbMouldExample tExp = new TbMouldExample();
  283. tExp.setOrderByClause("id desc limit 1");
  284. tExp.createCriteria().andEquipmentIdEqualTo(me.getId());
  285. if (tbMouldMapper.countByExample(tExp) > 0) {
  286. TbMould tm = tbMouldMapper.selectByExample(tExp).get(0);
  287. tm.setState(item.getStatus());
  288. // 云模盒运行次数+模具的历史运行次数
  289. tm.setRunTimes(item.getRunCnt() + tm.getHistoryRunTimes());
  290. // 处理每模平均周期(单位秒)
  291. BigDecimal bd = new BigDecimal(avgTime * 1.00 / 1000);
  292. tm.setOcCycle(bd);
  293. tbMouldMapper.updateByPrimaryKeySelective(tm);
  294. }
  295. }
  296. }
  297. /**
  298. * 低位在前的16进制解析
  299. *
  300. * @param rHex
  301. * @return
  302. */
  303. public static int reverseParseHex(String rHex) {
  304. int size = rHex.length() / 2;
  305. StringBuilder sb = new StringBuilder();
  306. for (int i = 0; i < size; i++) {
  307. sb.append(rHex.substring((size - i - 1) * 2, (size - i) * 2));
  308. }
  309. String str = sb.toString();
  310. return Integer.parseInt(str, 16);
  311. }
  312. private static Date parseDate(String input, int start, int pos, int dateStartPos) {
  313. String year = input.substring((start + pos * 6 + dateStartPos) * 2, (start + pos * 6 + 1 + dateStartPos) * 2);
  314. String month = input.substring((start + pos * 6 + 1 + dateStartPos) * 2,
  315. (start + pos * 6 + 2 + dateStartPos) * 2);
  316. String day = input.substring((start + pos * 6 + 2 + dateStartPos) * 2,
  317. (start + pos * 6 + 3 + dateStartPos) * 2);
  318. String hh = input.substring((start + pos * 6 + 3 + dateStartPos) * 2, (start + pos * 6 + 4 + dateStartPos) * 2);
  319. String mm = input.substring((start + pos * 6 + 4 + dateStartPos) * 2, (start + pos * 6 + 5 + dateStartPos) * 2);
  320. String ss = input.substring((start + pos * 6 + 5 + dateStartPos) * 2, (start + pos * 6 + 6 + dateStartPos) * 2);
  321. StringBuilder sb = new StringBuilder();
  322. int yearInt = Integer.parseInt(year, 16);
  323. if (yearInt == 0) {
  324. sb.append("2019-01-01");
  325. } else {
  326. sb.append(yearInt < 100 ? "20" : "2");
  327. sb.append(yearInt).append("-");
  328. int monthInt = Integer.parseInt(month, 16);
  329. sb.append(monthInt < 10 ? "0" : "").append(monthInt).append("-");
  330. int dayInt = Integer.parseInt(day, 16);
  331. sb.append(dayInt < 10 ? "0" : "").append(dayInt);
  332. }
  333. Date parseDate = null;
  334. try {
  335. parseDate = new SimpleDateFormat("yyyy-MM-dd").parse(sb.toString());
  336. parseDate.setHours(Integer.parseInt(hh, 16));
  337. parseDate.setMinutes(Integer.parseInt(mm, 16));
  338. parseDate.setSeconds(Integer.parseInt(ss, 16));
  339. } catch (ParseException e) {
  340. e.printStackTrace();
  341. }
  342. return parseDate;
  343. }
  344. private static String getStringFromHexStr(String hexStr) {
  345. StringBuilder sb = new StringBuilder();
  346. for (int i = 0; i < hexStr.length() / 2; i++) {
  347. String str = "0x" + hexStr.substring(i * 2, i * 2 + 2);
  348. int intVal = Integer.decode(str).intValue();
  349. char c = (char) intVal;
  350. sb.append(c);
  351. }
  352. return sb.toString();
  353. }
  354. public void close() {
  355. ctx.close();
  356. }
  357. public void sendMsg(String hexString) {
  358. System.out.println("发送消息==" + hexString);
  359. byte[] buffer = hexStrToBinaryStr(hexString);
  360. ByteBuf bf = Unpooled.buffer(hexString.length() / 2);
  361. bf.writeBytes(buffer);
  362. ctx.writeAndFlush(bf);
  363. // ctx.close();
  364. }
  365. /**
  366. * channel被激活时调用
  367. */
  368. @Override
  369. public void channelActive(ChannelHandlerContext ctx) {
  370. // TODO Auto-generated method stub
  371. this.ctx = ctx;
  372. System.out.println("==========Active=========" + ctx.channel().localAddress().toString() + ", connection num="
  373. + HelloServer.deviceMap.size());
  374. }
  375. @Override
  376. public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  377. super.channelInactive(ctx);
  378. System.out.println("==========Inactive=========");
  379. }
  380. @Override
  381. public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
  382. // TODO Auto-generated method stub
  383. super.handlerRemoved(ctx);
  384. System.out.println("[YunSu]handlerRemoved=掉线了===");
  385. }
  386. @Override
  387. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  388. // TODO Auto-generated method stub
  389. super.exceptionCaught(ctx, cause);
  390. System.out.println("我捕捉到异常信息了");
  391. }
  392. @Override
  393. public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
  394. if (evt instanceof IdleStateEvent) {
  395. IdleStateEvent event = (IdleStateEvent) evt;
  396. if (event.state().equals(IdleState.READER_IDLE)) {
  397. } else if (event.state().equals(IdleState.WRITER_IDLE)) {
  398. // System.out.println("WRITER_IDLE=="+userInfo.getWxName());
  399. // logger.debug(ctx.channel().remoteAddress().toString()+
  400. // "WRITER_IDLE");
  401. // 超时关闭channel
  402. // ctx.close();
  403. } else if (event.state().equals(IdleState.ALL_IDLE)) {
  404. // 发送心跳
  405. ctx.channel().writeAndFlush("$&_".toString());
  406. }
  407. }
  408. // super.userEventTriggered(ctx, evt);
  409. }
  410. /**
  411. * 将十六进制的字符串转换成字节数组
  412. *
  413. * @param hexString
  414. * @return
  415. */
  416. public static byte[] hexStrToBinaryStr(String hexString) {
  417. if (hexString == null || "".contentEquals(hexString)) {
  418. return null;
  419. }
  420. hexString = hexString.replaceAll(" ", "");
  421. int len = hexString.length();
  422. int index = 0;
  423. byte[] bytes = new byte[len / 2];
  424. while (index < len) {
  425. String sub = hexString.substring(index, index + 2);
  426. bytes[index / 2] = (byte) Integer.parseInt(sub, 16);
  427. index += 2;
  428. }
  429. return bytes;
  430. }
  431. /**
  432. * 将字节数组转换成十六进制的字符串
  433. *
  434. * @return
  435. */
  436. public static String BinaryToHexString(byte[] bytes) {
  437. String hexStr = "0123456789ABCDEF";
  438. String result = "";
  439. String hex = "";
  440. for (byte b : bytes) {
  441. hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
  442. hex += String.valueOf(hexStr.charAt(b & 0x0F));
  443. result += hex + " ";
  444. }
  445. return result;
  446. }
  447. /**
  448. * 普通字符串转16进制字符串
  449. *
  450. * @param str
  451. * @return
  452. */
  453. public static String strHexStr16(String str) {
  454. char[] chars = "0123456789ABCDEF".toCharArray();
  455. StringBuilder sb = new StringBuilder("");
  456. byte[] bs = str.getBytes();
  457. int bit;
  458. for (int i = 0; i < bs.length; i++) {
  459. bit = (bs[i] & 0x0f0) >> 4;
  460. sb.append(chars[bit]);
  461. bit = bs[i] & 0x0f;
  462. sb.append(chars[bit]);
  463. // sb.append(' ');
  464. }
  465. return sb.toString().trim();
  466. }
  467. public static void main(String[] args) {
  468. UserHandler h = new UserHandler();
  469. h.timeCalibration();
  470. // Date date = new Date();
  471. // SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd,HH:mm:ss");
  472. // String dateStr = sdf.format(date);
  473. // dateStr = "#TIME="+dateStr+";";
  474. // System.out.println(dateStr);
  475. // //#TIME=18-03-06,15:31:48;
  476. // logger.info("校准系统模块时间==>"+dateStr);
  477. // String reverseParseHex = strHexStr16(dateStr);
  478. // System.out.println("reverseParseHex-----"+reverseParseHex);
  479. // String str = "1F51";
  480. String input = "FAAF530000000000000000000000003137333030303239343310010000000000000000000000000000000000000000001F51E85F00000000000045004A54000100110000001308160F171F1308160F2D188FCA";
  481. // String temp = "0x"+str;
  482. // int i = Integer.decode(temp);
  483. // System.out.println(i);
  484. // int p = reverseParseHex(str);
  485. // System.out.println(p);
  486. // int start = 69;
  487. // System.out.println(str.substring(69*2));
  488. // Date d = parseDate(str, start, 0, 0);
  489. // System.out.println(d.toGMTString());
  490. int start = 69;
  491. int end = input.length() - 4; // 最后2位 0xXX,0xXX ,是CRC校验位
  492. logger.info("时间信息==" + input.substring(start * 2, end));
  493. if (end - start * 2 >= 24) {// 时间最少6位,1位显示0x00,开合算两个时间,所以是6*2*2=24
  494. // 本次运行周期内的开合模次数
  495. int periodCnt = (end - start * 2) / 24;
  496. logger.info("本次开合模次数==" + periodCnt);
  497. for (int pos = 0; pos < periodCnt; pos++) {
  498. // 合模时间
  499. MouldHistoryTime time = new MouldHistoryTime();
  500. time.setSeq(pos + 1);
  501. Date closeTime = parseDate(input, start, pos, 0);
  502. System.out.println(closeTime);
  503. time.setCloseTime(closeTime);
  504. // 开模时间
  505. Date openTime = parseDate(input, start, pos, 6);
  506. time.setOpenTime(openTime);
  507. System.out.println(openTime);
  508. int timeCost = (int) (openTime.getTime() - closeTime.getTime());
  509. time.setTimeCost(timeCost);
  510. }
  511. } else {
  512. logger.info("本次开合模次数==0");
  513. }
  514. }
  515. }