|
@@ -0,0 +1,313 @@
|
|
|
+package com.xyhy.chargpilemqttcluster.workThread.message;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.xyhy.chargpilemqttcluster.common.Common;
|
|
|
+
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+
|
|
|
+public class MessageCommon extends Common {
|
|
|
+
|
|
|
+
|
|
|
+ public String _topic;
|
|
|
+
|
|
|
+ public String getTopic() {
|
|
|
+ return _topic;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setTopic(String topic) {
|
|
|
+ this._topic = topic;
|
|
|
+ }
|
|
|
+
|
|
|
+ public MessageCommon()
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getToClientTopic(JSONObject obj,String type){
|
|
|
+ String topic ="toclient";
|
|
|
+ topic = topic +"/" + type+"/" + obj.getString("cp_id");
|
|
|
+ return topic;
|
|
|
+ }
|
|
|
+
|
|
|
+ //接受命令总线
|
|
|
+ public void getMessageAdupter(String topic,byte[] cmd)
|
|
|
+ {
|
|
|
+ int cmdindex = topic.split("/").length;
|
|
|
+
|
|
|
+ int cmd_type_index = Integer.parseInt(topic.split("/")[cmdindex-1]);
|
|
|
+
|
|
|
+ String functionName = "Message_"+cmd_type_index;
|
|
|
+ try {
|
|
|
+ this._topic = topic;
|
|
|
+ Method m = this.getClass().getDeclaredMethod(functionName,JSONObject.class);
|
|
|
+ JSONObject js_cmd = JSONObject.parseObject(new String(cmd));
|
|
|
+ log.info("收到设备消息:"+js_cmd.toJSONString());
|
|
|
+ m.invoke(this,js_cmd);
|
|
|
+ }catch (Exception ex)
|
|
|
+ {
|
|
|
+ log.error("没有找到响应的处理单元!",ex);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //cp56time2a(电力专用时间)转Date
|
|
|
+ public Date Convert_cp56time2a_to_date(byte[] cp56time2a)
|
|
|
+ {
|
|
|
+ byte seconds0 = cp56time2a[0];
|
|
|
+ byte seconds1 = cp56time2a[1];
|
|
|
+ byte min = cp56time2a[2];
|
|
|
+ byte hours = cp56time2a[3];
|
|
|
+ byte week = cp56time2a[4];
|
|
|
+ byte day = cp56time2a[4];
|
|
|
+ byte months = cp56time2a[5];
|
|
|
+ byte year = cp56time2a[6];
|
|
|
+
|
|
|
+ if (year==0xff || months==0xff || day==0xff || hours==0xff || min==0xff || seconds1==0xff)
|
|
|
+ {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(1970, 0, 01);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ String bit_seconds0 = ""+(byte)((seconds0 >> 7) & 0x1)+
|
|
|
+ (byte)((seconds0 >> 6) & 0x1)+
|
|
|
+ (byte)((seconds0 >> 5) & 0x1)+
|
|
|
+ (byte)((seconds0 >> 4) & 0x1)+
|
|
|
+ (byte)((seconds0 >> 3) & 0x1)+
|
|
|
+ (byte)((seconds0 >> 2) & 0x1)+
|
|
|
+ (byte)((seconds0 >> 1) & 0x1)+
|
|
|
+ (byte)((seconds0 >> 0) & 0x1);
|
|
|
+
|
|
|
+ String bit_seconds1 = ""+(byte)((seconds1 >> 7) & 0x1)+
|
|
|
+ (byte)((seconds1 >> 6) & 0x1)+
|
|
|
+ (byte)((seconds1 >> 5) & 0x1)+
|
|
|
+ (byte)((seconds1 >> 4) & 0x1)+
|
|
|
+ (byte)((seconds1 >> 3) & 0x1)+
|
|
|
+ (byte)((seconds1 >> 2) & 0x1)+
|
|
|
+ (byte)((seconds1 >> 1) & 0x1)+
|
|
|
+ (byte)((seconds1 >> 0) & 0x1);
|
|
|
+
|
|
|
+ String bit_min = ""+(byte)((min >> 5) & 0x1)+
|
|
|
+ (byte)((min >> 4) & 0x1)
|
|
|
+ +(byte)((min >> 3) & 0x1)+
|
|
|
+ (byte)((min >> 2) & 0x1) +
|
|
|
+ (byte)((min >> 1) & 0x1)+
|
|
|
+ (byte)((min >>0) & 0x1);
|
|
|
+
|
|
|
+ String bit_hours = ""+(byte)((hours >> 4) & 0x1)
|
|
|
+ +(byte)((hours >> 3) & 0x1)+
|
|
|
+ (byte)((hours >> 2) & 0x1) +
|
|
|
+ (byte)((hours >> 1) & 0x1)+
|
|
|
+ (byte)((hours >> 0) & 0x1);
|
|
|
+
|
|
|
+ String bit_week = ""+(byte)((week >> 7) & 0x1)+
|
|
|
+ (byte)((week >> 6) & 0x1)+
|
|
|
+ (byte)((week >> 5) & 0x1);
|
|
|
+
|
|
|
+ String bit_day = ""+(byte)((day >> 4) & 0x1)
|
|
|
+ +(byte)((day >> 3) & 0x1)+
|
|
|
+ (byte)((day >> 2) & 0x1) +
|
|
|
+ (byte)((day >> 1) & 0x1)+
|
|
|
+ (byte)((day >> 0) & 0x1);
|
|
|
+
|
|
|
+ String bit_months = ""+(byte)((months >> 3) & 0x1)+
|
|
|
+ (byte)((months >> 2) & 0x1) +
|
|
|
+ (byte)((months >> 1) & 0x1)+
|
|
|
+ (byte)((months >> 0) & 0x1);
|
|
|
+
|
|
|
+ String bit_year = ""+(byte)((year >> 6) & 0x1)+
|
|
|
+ (byte)((year >> 5) & 0x1)+
|
|
|
+ (byte)((year >> 4) & 0x1)+
|
|
|
+ (byte)((year >> 3) & 0x1)+
|
|
|
+ (byte)((year >> 2) & 0x1)+
|
|
|
+ (byte)((year >> 1) & 0x1)+
|
|
|
+ (byte)((year >> 0) & 0x1);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ int seccound = Integer.parseInt(bit_seconds1+""+bit_seconds0,2);
|
|
|
+ int seccound_main = seccound/1000;
|
|
|
+ int seccound_millis = seccound%1000;
|
|
|
+
|
|
|
+
|
|
|
+ Calendar c_new = Calendar.getInstance();
|
|
|
+
|
|
|
+ c_new.set(2000+(Integer.parseInt(bit_year,2)),
|
|
|
+ Integer.parseInt(bit_months,2)-1,
|
|
|
+ Integer.parseInt(bit_day,2),
|
|
|
+ Integer.parseInt(bit_hours,2),
|
|
|
+ Integer.parseInt(bit_min,2));
|
|
|
+
|
|
|
+ c_new.set(Calendar.SECOND,seccound_main);
|
|
|
+ c_new.set(Calendar.MILLISECOND,seccound_millis);
|
|
|
+
|
|
|
+ //System.out.println(c_new.get(Calendar.YEAR)+"年"+(c_new.get(Calendar.MONTH)+1)+"月"+c_new.get(Calendar.DAY_OF_MONTH)+"日"+
|
|
|
+ // c_new.get(Calendar.HOUR)+"时"+c_new.get(Calendar.MINUTE)+"分"+c_new.get(Calendar.SECOND)+"秒"+c_new.get(Calendar.MILLISECOND)+"毫秒");
|
|
|
+
|
|
|
+ return c_new.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //date转cp56time2a字节时间
|
|
|
+ public byte[] Convert_date_to_cp56time2a(Date date)
|
|
|
+ {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+
|
|
|
+ int int_year = c.get(Calendar.YEAR);
|
|
|
+ String s_year = get2JZString(int_year-2000,7);
|
|
|
+ byte b_year = bitStringToByte("0"+s_year);
|
|
|
+
|
|
|
+ int int_month = c.get(Calendar.MONTH)+1;
|
|
|
+ String s_month = get2JZString(int_month,5);
|
|
|
+ byte b_month = bitStringToByte("000"+s_month);
|
|
|
+
|
|
|
+ int int_week = c.get(Calendar.DAY_OF_WEEK);
|
|
|
+ String s_week = get2JZString(int_week,3);
|
|
|
+
|
|
|
+ int int_day = c.get(Calendar.DAY_OF_MONTH);
|
|
|
+ String s_day = get2JZString(int_day,5);
|
|
|
+ byte b_day = bitStringToByte(s_week+""+s_day);
|
|
|
+
|
|
|
+
|
|
|
+ int int_hour = c.get(Calendar.HOUR_OF_DAY);
|
|
|
+ String s_hour = get2JZString(int_hour,5);
|
|
|
+ byte b_hour = bitStringToByte("100"+s_hour);
|
|
|
+
|
|
|
+ int int_minute = c.get(Calendar.MINUTE);
|
|
|
+ String s_minute = get2JZString(int_minute,6);
|
|
|
+ byte b_minute = bitStringToByte("00"+s_minute);
|
|
|
+
|
|
|
+ int int_secound = c.get(Calendar.SECOND);
|
|
|
+ int int_millisecound = c.get(Calendar.MILLISECOND);
|
|
|
+
|
|
|
+ int allmillsecound = int_secound*1000+int_millisecound;
|
|
|
+ String s_allmillsecound = get2JZString(allmillsecound,16);
|
|
|
+
|
|
|
+ byte millsecound0 = bitStringToByte(s_allmillsecound.substring(0,8));
|
|
|
+ byte millsecound1 = bitStringToByte(s_allmillsecound.substring(8,16));
|
|
|
+
|
|
|
+
|
|
|
+ byte[] cp56Time2a = new byte[7];
|
|
|
+ cp56Time2a[0]=millsecound1;
|
|
|
+ cp56Time2a[1]=millsecound0;
|
|
|
+ cp56Time2a[2]=b_minute;
|
|
|
+ cp56Time2a[3]=b_hour;
|
|
|
+ cp56Time2a[4]=b_day;
|
|
|
+ cp56Time2a[5]=b_month;
|
|
|
+ cp56Time2a[6]=b_year;
|
|
|
+
|
|
|
+ return cp56Time2a;
|
|
|
+ }
|
|
|
+
|
|
|
+ //10进制转2进制,带补位
|
|
|
+ public String get2JZString(int count,int bw)
|
|
|
+ {
|
|
|
+ String s_count = Integer.toBinaryString(count);
|
|
|
+ if (bw > s_count.length()){
|
|
|
+ String s_bw="";
|
|
|
+ for(int i = 0 ; i < bw-s_count.length();i++){
|
|
|
+ s_bw=s_bw+"0";
|
|
|
+ }
|
|
|
+ s_count = s_bw+s_count;
|
|
|
+ }
|
|
|
+
|
|
|
+ return s_count;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //2进制bit转byte
|
|
|
+ public byte bitStringToByte(String str) {
|
|
|
+ if(null == str){
|
|
|
+ throw new RuntimeException("when bit string convert to byte, Object can not be null!");
|
|
|
+ }
|
|
|
+ if (8 != str.length()){
|
|
|
+ throw new RuntimeException("bit string'length must be 8");
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ //判断最高位,决定正负
|
|
|
+ if(str.charAt(0) == '0'){
|
|
|
+ return (byte) Integer.parseInt(str,2);
|
|
|
+ }else if(str.charAt(0) == '1'){
|
|
|
+ return (byte) (Integer.parseInt(str,2) - 256);
|
|
|
+ }
|
|
|
+ }catch (NumberFormatException e){
|
|
|
+ throw new RuntimeException("bit string convert to byte failed, byte String must only include 0 and 1!");
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public JSONObject getRTDevType3(JSONObject cmd) {
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+
|
|
|
+ obj.put("devId",cmd.getString("devId"));
|
|
|
+ obj.put("devType",cmd.getInteger("devType"));
|
|
|
+
|
|
|
+ JSONArray devices = cmd.getJSONArray("rt000");
|
|
|
+ if (devices!=null && devices.size()>0){
|
|
|
+ JSONObject deviceItem = devices.getJSONObject(0);
|
|
|
+ obj.put("rt004",deviceItem.getInteger("rt004"));
|
|
|
+ obj.put("rt025",deviceItem.getDoubleValue("rt025"));
|
|
|
+ obj.put("rt026",deviceItem.getDoubleValue("rt026"));
|
|
|
+ obj.put("rt027",deviceItem.getString("rt027"));
|
|
|
+ obj.put("rt028",deviceItem.getString("rt028"));
|
|
|
+ obj.put("rt029",deviceItem.getInteger("rt029"));
|
|
|
+ obj.put("rt030",deviceItem.getInteger("rt030"));
|
|
|
+ obj.put("rt031",deviceItem.getInteger("rt031"));
|
|
|
+ obj.put("rt032",deviceItem.getInteger("rt032"));
|
|
|
+ obj.put("rt033",deviceItem.getLongValue("rt033"));
|
|
|
+ obj.put("rt034",deviceItem.getLongValue("rt034"));
|
|
|
+ obj.put("rt035",deviceItem.getLongValue("rt035"));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONArray getRTBettey(JSONObject cmd) {
|
|
|
+
|
|
|
+ JSONArray arr_js_betty = (JSONArray)cmd.getJSONArray("rt036").clone();
|
|
|
+ arr_js_betty.stream().forEach(item -> {
|
|
|
+ JSONObject jsitem= (JSONObject)item;
|
|
|
+ jsitem.put("devId",cmd.getString("devId"));
|
|
|
+ jsitem.put("devType",cmd.getInteger("devType"));
|
|
|
+ });
|
|
|
+ return arr_js_betty;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public JSONObject getSend(int controlCode){
|
|
|
+ JSONObject returnObj = new JSONObject();
|
|
|
+ returnObj.put("msgType","JSON");
|
|
|
+ returnObj.put("controlCode",controlCode);
|
|
|
+ return returnObj;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public JSONObject getSUCCESSResult(int controlCode){
|
|
|
+ return getSUCCESSResult(controlCode,true,"Success");
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONObject getSUCCESSResult(int controlCode,boolean ResultCode,String resultMessage){
|
|
|
+ JSONObject returnObj = new JSONObject();
|
|
|
+ returnObj.put("msgType","JSON");
|
|
|
+ returnObj.put("resultCode",ResultCode);
|
|
|
+ returnObj.put("controlCode",controlCode);
|
|
|
+ returnObj.put("message",resultMessage);
|
|
|
+ return returnObj;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|