ConvertData.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package com.wsm.admin.device.tcp;
  2. import com.alibaba.fastjson.JSON;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.TreeMap;
  7. /**
  8. * 井盖报文解析
  9. */
  10. public class ConvertData {
  11. private List<Rule> ruleList = new ArrayList<>();
  12. public ConvertData() {
  13. ruleList.add(new Rule("sn1", "string", 22, 26));
  14. ruleList.add(new Rule("sn2", null, 26, 46));
  15. ruleList.add(new Rule("yyyy", "int", 46, 48));
  16. ruleList.add(new Rule("MM", "int", 48, 50));
  17. ruleList.add(new Rule("dd", "int", 50, 52));
  18. ruleList.add(new Rule("HH", "int", 52, 54));
  19. ruleList.add(new Rule("mm", "int", 54, 56));
  20. ruleList.add(new Rule("ss", "int", 56, 58));
  21. ruleList.add(new Rule("x", "int", 58, 62));
  22. ruleList.add(new Rule("y", "int", 62, 66));
  23. ruleList.add(new Rule("z", "int", 66, 70));
  24. ruleList.add(new Rule("CSQ", "int", 74, 78));
  25. ruleList.add(new Rule("RSRQ", "int", 78, 82));
  26. ruleList.add(new Rule("SNR", "int", 82, 86));
  27. ruleList.add(new Rule("RSRP", "int", 86, 90));
  28. ruleList.add(new Rule("经度方向", "string", 90, 92));
  29. ruleList.add(new Rule("经度", "string", 92, 112));
  30. ruleList.add(new Rule("纬度方向", "string", 112, 114));
  31. ruleList.add(new Rule("纬度", "string", 114, 134));
  32. ruleList.add(new Rule("status", null, 134, 142));
  33. }
  34. public Map<String, Object> convert(String date) {
  35. //此例子没做对数据的校验
  36. Map<String, Object> reHm = new TreeMap<>();
  37. for (Rule rule : ruleList) {
  38. if (rule.getStart() < date.length() && rule.getEnd() < date.length()) {
  39. String inString = date.substring(rule.getStart(), rule.getEnd());
  40. if ("int".equals(rule.getType())) {
  41. reHm.put(rule.getName(), hexStr2Int(inString));
  42. } else if ("string".equals(rule.getType())) {
  43. reHm.put(rule.getName(), hexStr2String(inString));
  44. } else {
  45. reHm.put(rule.getName(), inString);
  46. }
  47. }
  48. }
  49. return reHm;
  50. }
  51. private int hexStr2Int(String hexString) {
  52. int ret = Integer.parseInt(hexString, 16);
  53. ret = ((ret & 0x8000) > 0) ? (ret - 0x10000) : (ret);
  54. return ret;
  55. }
  56. private String hexStr2String(String hexStr) {
  57. String str = "0123456789ABCDEF";
  58. char[] hexs = hexStr.toCharArray();
  59. byte[] bytes = new byte[hexStr.length() / 2];
  60. int n;
  61. for (int i = 0; i < bytes.length; i++) {
  62. n = str.indexOf(hexs[2 * i]) * 16;
  63. n += str.indexOf(hexs[2 * i + 1]);
  64. bytes[i] = (byte) (n & 0xff);
  65. }
  66. return new String(bytes);
  67. }
  68. public static void main(String[] args) {
  69. String d = "010001004A0000A3010044514300010003020100000001130C020B0B1EFFF3000000F100000000000000000000453131332E3536393639354E3032322E3337363735390000000000000000000000004140";
  70. System.out.println(d.length());
  71. ConvertData cd = new ConvertData();
  72. Map<String, Object> hm = cd.convert(d);
  73. System.out.println(JSON.toJSONString(hm));
  74. }
  75. }