Blame view

zteits-nbiot-fh/src/main/java/com/fh/party/ReportProcess.java 3.1 KB
2ceb8757   zhaowg   烽火-电信IOT插件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  package com.fh.party;
  
  import com.fasterxml.jackson.databind.ObjectMapper;
  import com.fasterxml.jackson.databind.node.ObjectNode;
  
  import java.text.DateFormat;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  
  public class ReportProcess {
      private byte cmdid = 0x01;
      private int pid;
      private int tid;
      private int eventcnt;
      private int verno;
      private byte eventstate;
  
      //
      private String factoryCode = "fh";
      private String model = "fh";
      private int reportType;
      private int reportTime;
      private int state;
      private int packCount;
      private int voltage;
      private int magId;
      private int rssi;
      private int version;
  
      public ReportProcess(byte[] binaryData) throws ParseException {
          pid = binaryData[1];
          tid = binaryData[2];
          eventcnt = binaryData[3];
          verno = binaryData[4];
          eventstate = binaryData[5];
          if((eventstate & 1) == 1){
              reportType = 33;
          }
          if((eventstate & 2) == 2){
              reportType = 32;
          }
          if((eventstate & 3) == 3){
              reportType = 32;
          }
          magId = tid;
          reportTime = calLastedTime();
          if((eventstate & 32) == 32){
              state = 1;
          }else{
              state = 0;
          }
      }
  
      public ObjectNode toJsonNode() {
          try {
              //组装body体
              ObjectMapper mapper = new ObjectMapper();
              ObjectNode root = mapper.createObjectNode();
              // root.put("identifier", this.identifier);
              root.put("factoryCode", this.factoryCode);
              root.put("model", this.model);
              root.put("reportType", this.reportType);
              root.put("reportTime", this.reportTime);
              root.put("state", this.state);
              root.put("magId", this.magId);
              return root;
          } catch (Exception e) {
              e.printStackTrace();
              return null;
          }
      }
  
      /**
       * 1970-1-1 经过的秒总数
       * @return
       */
      public static int calLastedTime() throws ParseException {
          long a = new Date().getTime();
          DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
          Date startDate = dateFormat.parse("1970-01-01");
          long b = startDate.getTime();
          return (int)((a - b) / 1000);
      }
  
      /**
       * byte转换为一个长度为8byte数组,数组每个值代表bit
       */
      public static byte[] byteToArray(byte b) {
          byte[] array = new byte[8];
          for (int i = 7; i >= 0; i--) {
              array[i] = (byte)(b & 1);
              b = (byte) (b >> 1);
          }
          return array;
      }
  
      /**
       * byte转为字符串的bit
       */
      public static String byteToBit(byte b) {
          return ""
                  + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1)
                  + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1)
                  + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1)
                  + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1);
      }
  
      public static void main(String[] args) throws ParseException {
      }
  }