From 2583078fdf32b87da154b1594f3e616a77824175 Mon Sep 17 00:00:00 2001 From: whycq <10027870+whycq@user.noreply.gitee.com> Date: 星期一, 12 八月 2024 17:10:31 +0800 Subject: [PATCH] # --- app/src/main/java/com/example/agvcontroller/protocol/AGV_02_DOWN.java | 12 app/src/main/java/com/example/agvcontroller/protocol/ICodedStatus.java | 9 app/src/main/java/com/example/agvcontroller/protocol/ICommandBody.java | 12 app/src/main/java/com/example/agvcontroller/protocol/AgvPackage.java | 51 - app/src/main/java/com/example/agvcontroller/protocol/Utils.java | 239 ++++++++ app/src/main/java/com/example/agvcontroller/protocol/PacHeader.java | 29 app/src/main/java/com/example/agvcontroller/protocol/PacBody.java | 38 + app/src/main/java/com/example/agvcontroller/protocol/AGV_80_DOWN.java | 24 app/src/main/java/com/example/agvcontroller/protocol/ProtocolUtils.java | 29 app/src/main/java/com/example/agvcontroller/protocol/IMessageBody.java | 2 app/src/main/java/com/example/agvcontroller/protocol/AGV_A1_DOWN.java | 28 + app/src/main/java/com/example/agvcontroller/protocol/EncryType.java | 66 ++ app/src/main/java/com/example/agvcontroller/protocol/AGV_01_DOWN.java | 50 + app/src/main/java/com/example/agvcontroller/protocol/ProtocolType.java | 82 +++ app/src/main/java/com/example/agvcontroller/protocol/AgvProtocol.java | 37 + app/src/main/java/com/example/agvcontroller/protocol/PacErrorType.java | 48 + app/src/main/java/com/example/agvcontroller/protocol/AGV_F0_DOWN.java | 32 + app/src/main/java/com/example/agvcontroller/protocol/ValidUtil.java | 55 ++ app/src/main/java/com/example/agvcontroller/protocol/AGV_03_DOWN.java | 23 app/src/main/java/com/example/agvcontroller/protocol/ProtocolPojoType.java | 54 ++ app/src/main/java/com/example/agvcontroller/protocol/DirectionType.java | 9 app/src/main/java/com/example/agvcontroller/MainActivity.java | 72 +- app/src/main/java/com/example/agvcontroller/protocol/Cools.java | 367 +++++++++++++ app/src/main/java/com/example/agvcontroller/StartActivity.java | 2 app/src/main/java/com/example/agvcontroller/protocol/PackagePart.java | 87 +++ app/src/main/java/com/example/agvcontroller/protocol/SystemProperties.java | 94 +++ app/src/main/java/com/example/agvcontroller/protocol/ProtocolEncoder.java | 42 app/src/main/java/com/example/agvcontroller/protocol/CRCUtils.java | 2 28 files changed, 1,466 insertions(+), 129 deletions(-) diff --git a/app/src/main/java/com/example/agvcontroller/MainActivity.java b/app/src/main/java/com/example/agvcontroller/MainActivity.java index 4b3d37a..2df3a7f 100644 --- a/app/src/main/java/com/example/agvcontroller/MainActivity.java +++ b/app/src/main/java/com/example/agvcontroller/MainActivity.java @@ -8,6 +8,8 @@ import android.os.Bundle; import android.os.Vibrator; import android.util.Log; +import android.view.KeyEvent; +import android.view.MotionEvent; import android.view.View; import android.widget.Button; @@ -32,54 +34,58 @@ private Button vibrateButton; private Socket socket; SocketManager socketManager; + + String clientId; NettyServerHandler nettyServerHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - // 浣滀负瀹㈡埛绔繛鎺� - //SocketManager.getInstance().setHostAndPort("192.168.4.188", 802); - //SocketManager.getInstance().connect(); - - //SocketManager.getInstance().setPort(8080); - //SocketManager.getInstance().startServer(); - - // 璁剧疆绔彛鍙峰苟鍚姩鏈嶅姟绔� - //SocketManager.getInstance().setPort(8080); - //socketManager = new SocketManager(); - //socketManager.startServer(8080); - - // 鍚姩鏈嶅姟绔� - //new Thread(() -> { - // SocketManager.getInstance().startServer(); - //}).start(); - vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); vibrateButton = findViewById(R.id.btn_stop); Intent intent = getIntent(); - String clientId = intent.getStringExtra("message"); + //String clientId = intent.getStringExtra("message"); + clientId = intent.getStringExtra("message"); Log.i("message1",clientId); + // 鍒濆鍖栧崟杞翠娇鑳� + byte[] message2 = new byte[]{0x01, 0x02, 0x03, 0x06}; // 绀轰緥娑堟伅 + nettyServerHandler.sendMessageToClient(clientId, message2); // 鍙戦�佹秷鎭埌瀹㈡埛绔� - vibrateButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (vibrator != null && vibrator.hasVibrator()) { - //vibrator.vibrate(500); - } - - //String clientId = "/192.168.4.188:63160"; - byte[] message1 = new byte[]{0x01, 0x02, 0x03, 0x04}; // 绀轰緥娑堟伅 - Log.i("message2",clientId); - Log.i("message3", Arrays.toString(message1)); - - nettyServerHandler.sendMessageToClient(clientId, message1); // 鍙戦�佹秷鎭埌瀹㈡埛绔� - } - }); + //vibrateButton.setOnClickListener(new View.OnClickListener() { + // @Override + // public void onClick(View v) { + // if (vibrator != null && vibrator.hasVibrator()) { + // //vibrator.vibrate(500); + // } + // + // //String clientId = "/192.168.4.188:63160"; + // byte[] message1 = new byte[]{0x01, 0x02, 0x03, 0x04}; // 绀轰緥娑堟伅 + // Log.i("message2",clientId); + // Log.i("message3", Arrays.toString(message1)); + // + // nettyServerHandler.sendMessageToClient(clientId, message1); // 鍙戦�佹秷鎭埌瀹㈡埛绔� + // } + //}); + vibrateButton.setOnTouchListener(new CarTouchButton()); } + private class CarTouchButton implements View.OnTouchListener { + + @Override + public boolean onTouch(View view, MotionEvent motionEvent) { + if (motionEvent.getAction() == KeyEvent.ACTION_DOWN) { + byte[] message2 = new byte[]{0x01, 0x02, 0x03, 0x06}; // 绀轰緥娑堟伅 + nettyServerHandler.sendMessageToClient(clientId, message2); // 鍙戦�佹秷鎭埌瀹㈡埛绔� + } else if (motionEvent.getAction() == KeyEvent.ACTION_UP) { + byte[] message2 = new byte[]{0x01, 0x02, 0x03, 0x07}; // 绀轰緥娑堟伅 + nettyServerHandler.sendMessageToClient(clientId, message2); // 鍙戦�佹秷鎭埌瀹㈡埛绔� + } + return false; + } + } @Override protected void onDestroy() { super.onDestroy(); diff --git a/app/src/main/java/com/example/agvcontroller/StartActivity.java b/app/src/main/java/com/example/agvcontroller/StartActivity.java index adb74d2..ebc0cdf 100644 --- a/app/src/main/java/com/example/agvcontroller/StartActivity.java +++ b/app/src/main/java/com/example/agvcontroller/StartActivity.java @@ -52,7 +52,7 @@ } }); socketManager = new SocketManager(); - socketManager.startServer(8022); + socketManager.startServer(8024); } @Subscribe(threadMode = ThreadMode.MAIN) diff --git a/app/src/main/java/com/example/agvcontroller/protocol/AGV_01_DOWN.java b/app/src/main/java/com/example/agvcontroller/protocol/AGV_01_DOWN.java new file mode 100644 index 0000000..058cd45 --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/AGV_01_DOWN.java @@ -0,0 +1,50 @@ +package com.example.agvcontroller.protocol; + + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +/** + * 璺緞鏁版嵁鍖� + * Created by vincent on 2023/3/16 + */ +public class AGV_01_DOWN implements IMessageBody, Serializable { + + private static final long serialVersionUID = -2786382674008041014L; + + @Override + public byte[] writeToBytes() { + String serialNo = Utils.zeroFill(this.serialNo, 16); + byte[] serialNoBytes = Utils.reverse(serialNo.getBytes()); + //byte pathLen = (byte) actionItems.size(); + + byte[] pathBytes = new byte[0]; + //for (AgvActionItem<? extends IActionBody> item : actionItems) { + // //byte[] bytes = RadixTools.intToBytes(Integer.parseInt(item.getQrCode())); + // //byte[] qrCodeBytes = Utils.reverse(bytes); + // //byte actionCodeByte = (byte) item.getActionCmdType().actionCode; + // //byte valByte = (byte) item.getVal(); + // //byte[] actionBodyBytes = item.getActionBody().writeToBytes(); + // //byte[] merge = Utils.merge(qrCodeBytes, actionCodeByte, valByte, actionBodyBytes); + // pathBytes = Utils.merge(pathBytes, merge); + //} + + return Utils.merge(serialNoBytes, pathLen, pathBytes); + } + + @Override + public void readFromBytes(byte[] messageBodyBytes) { + + } + + // 娴佹按鍙� - 16 + private String serialNo; + + // 璺緞闀垮害 + private int pathLen; + + // 璺緞鍒楄〃 + //private List<AgvActionItem<? extends IActionBody>> actionItems = new ArrayList<>(); + +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/AGV_02_DOWN.java b/app/src/main/java/com/example/agvcontroller/protocol/AGV_02_DOWN.java index 74bde9d..9578d44 100644 --- a/app/src/main/java/com/example/agvcontroller/protocol/AGV_02_DOWN.java +++ b/app/src/main/java/com/example/agvcontroller/protocol/AGV_02_DOWN.java @@ -1,8 +1,5 @@ -package com.zy.acs.common.domain.protocol; +package com.example.agvcontroller.protocol; -import com.zy.acs.common.utils.Utils; -import lombok.Data; -import lombok.extern.slf4j.Slf4j; import java.io.Serializable; @@ -10,7 +7,6 @@ * 鍔ㄤ綔鍛戒护鍖� * Created by vincent on 2023/3/21 */ -@Data public class AGV_02_DOWN implements IMessageBody, Serializable { private static final long serialVersionUID = 1664188062202647371L; @@ -19,8 +15,10 @@ public byte[] writeToBytes() { String serialNo = Utils.zeroFill(this.serialNo, 16); byte[] serialNoBytes = Utils.reverse(serialNo.getBytes()); - byte cmdCodeByte = (byte) this.getCmdCode(); - byte valByte = (byte) this.getVal(); + //byte cmdCodeByte = (byte) this.getCmdCode(); + byte cmdCodeByte = (byte) 1; + //byte valByte = (byte) this.getVal(); + byte valByte = (byte) 1; byte[] cmdBodyBytes = commandBody.writeToBytes(); diff --git a/app/src/main/java/com/example/agvcontroller/protocol/AGV_03_DOWN.java b/app/src/main/java/com/example/agvcontroller/protocol/AGV_03_DOWN.java new file mode 100644 index 0000000..882e607 --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/AGV_03_DOWN.java @@ -0,0 +1,23 @@ +package com.example.agvcontroller.protocol; + +import java.io.Serializable; + +/** + * 蹇冭烦 + * Created by vincent on 2023/3/16 + */ +public class AGV_03_DOWN implements IMessageBody, Serializable { + + private static final long serialVersionUID = 6922520599887812372L; + + @Override + public byte[] writeToBytes() { + return new byte[0]; + } + + @Override + public void readFromBytes(byte[] messageBodyBytes) { + + } + +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/AGV_80_DOWN.java b/app/src/main/java/com/example/agvcontroller/protocol/AGV_80_DOWN.java new file mode 100644 index 0000000..e59a7ff --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/AGV_80_DOWN.java @@ -0,0 +1,24 @@ +package com.example.agvcontroller.protocol; + + +import java.io.Serializable; + +/** + * 婵�娲诲寘 + * Created by vincent on 2023/3/16 + */ +public class AGV_80_DOWN implements IMessageBody, Serializable { + + private static final long serialVersionUID = -8289774377582991418L; + + @Override + public byte[] writeToBytes() { + return new byte[0]; + } + + @Override + public void readFromBytes(byte[] messageBodyBytes) { + + } + +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/AGV_A1_DOWN.java b/app/src/main/java/com/example/agvcontroller/protocol/AGV_A1_DOWN.java new file mode 100644 index 0000000..7cdfe5d --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/AGV_A1_DOWN.java @@ -0,0 +1,28 @@ +package com.example.agvcontroller.protocol; + + +import java.io.Serializable; + +/** + * 搴旂瓟姝e父鍖� + * Created by vincent on 2023/3/16 + */ +public class AGV_A1_DOWN implements IMessageBody, Serializable { + + private static final long serialVersionUID = -1744072958515341165L; + + @Override + public byte[] writeToBytes() { + return Utils.merge(ackSign, Utils.reverse(temp)); + } + + @Override + public void readFromBytes(byte[] messageBodyBytes) { + + } + + private byte ackSign = 0x11; + + private byte[] temp = new byte[4]; + +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/AGV_F0_DOWN.java b/app/src/main/java/com/example/agvcontroller/protocol/AGV_F0_DOWN.java new file mode 100644 index 0000000..d3fff92 --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/AGV_F0_DOWN.java @@ -0,0 +1,32 @@ +package com.example.agvcontroller.protocol; + + +import java.io.Serializable; + +/** + * Created by vincent on 2023/3/15 + */ +public class AGV_F0_DOWN implements IMessageBody, Serializable { + + private static final long serialVersionUID = 5769100530170067014L; + + @Override + public byte[] writeToBytes() { + Utils.reverse(sysMark); + Utils.reverse(sysVersion); + Utils.reverse(temp); + return Utils.merge(sysMark, sysVersion, temp); + } + + @Override + public void readFromBytes(byte[] bytes) { + } + + private byte[] sysMark = new byte[] {0x00, 0x01}; + + private byte[] sysVersion = new byte[] {0x01, 0x01}; + + private byte[] temp = new byte[4]; + +} + diff --git a/app/src/main/java/com/example/agvcontroller/protocol/AgvPackage.java b/app/src/main/java/com/example/agvcontroller/protocol/AgvPackage.java index e50fa82..39f001f 100644 --- a/app/src/main/java/com/example/agvcontroller/protocol/AgvPackage.java +++ b/app/src/main/java/com/example/agvcontroller/protocol/AgvPackage.java @@ -1,8 +1,5 @@ -package com.zy.acs.gateway.domain; +package com.example.agvcontroller.protocol; -import com.core.common.Cools; -import com.zy.acs.gateway.constant.AgvConstant; -import com.zy.acs.gateway.constant.PacErrorType; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; @@ -12,7 +9,7 @@ * AGV鎶ユ枃妯″瀷 * Created by vincent on 2023/3/10 */ -public class AgvPackage implements PackageSupport { +public class AgvPackage { /** * 婧愭暟鎹寘缂撳啿鍖�(寮曠敤) @@ -58,14 +55,14 @@ return agvPackage; } - public String toLogString() { - StringBuilder pacSb = new StringBuilder(); - pacSb.append("AGV(").append(getHeader().getUniqueNo()).append(")") - .append("涓婁紶") - .append("[").append(getHeader().getProtocolType().getDes()).append("]娑堟伅.") - .append("鍘熷娑堟伅[").append(getSourceHexStr()).append("]"); - return pacSb.toString(); - } + //public String toLogString() { + // StringBuilder pacSb = new StringBuilder(); + // pacSb.append("AGV(").append(getHeader().getUniqueNo()).append(")") + // .append("涓婁紶") + // .append("[").append(getHeader().getProtocolType().getDes()).append("]娑堟伅.") + // .append("鍘熷娑堟伅[").append(getSourceHexStr()).append("]"); + // return pacSb.toString(); + //} public ByteBuf getSourceBuff() { // 鍥犱负澶勭悊buffer鏃讹紝鎬绘槸甯屾湜鎷垮埌涓�涓噸缃簡readerIndex鐨刡uf @@ -139,19 +136,19 @@ return this; } - public ByteBuf convert(ByteBuf byteBuf){ - byteBuf.writeByte(this.getHeader().getStartSymbol()) - .writeShort(this.getHeader().getContentLength()) - .writeByte(this.getHeader().getProtocolType().getCode()) - .writeBytes(this.getHeader().getUniqueNo().getBytes(AgvConstant.CHARSET_GBK)) - .writeByte(this.getHeader().getEncryptType().getCode()) - .writeBytes(this.getBody().getContent()) - .writeByte(this.getValidCode()); - // 璁$畻骞惰缃牎楠岀爜 -// this.setValidCode(ValidUtil.caculateValidByteFromBuff(byteBuf)); - byteBuf.resetReaderIndex(); - byteBuf.writerIndex(byteBuf.readableBytes() - 1).writeByte(this.getValidCode()); - return byteBuf; - } +// public ByteBuf convert(ByteBuf byteBuf){ +// byteBuf.writeByte(this.getHeader().getStartSymbol()) +// .writeShort(this.getHeader().getContentLength()) +// .writeByte(this.getHeader().getProtocolType().getCode()) +// .writeBytes(this.getHeader().getUniqueNo().getBytes(AgvConstant.CHARSET_GBK)) +// .writeByte(this.getHeader().getEncryptType().getCode()) +// .writeBytes(this.getBody().getContent()) +// .writeByte(this.getValidCode()); +// // 璁$畻骞惰缃牎楠岀爜 +//// this.setValidCode(ValidUtil.caculateValidByteFromBuff(byteBuf)); +// byteBuf.resetReaderIndex(); +// byteBuf.writerIndex(byteBuf.readableBytes() - 1).writeByte(this.getValidCode()); +// return byteBuf; +// } } diff --git a/app/src/main/java/com/example/agvcontroller/protocol/AgvProtocol.java b/app/src/main/java/com/example/agvcontroller/protocol/AgvProtocol.java new file mode 100644 index 0000000..9914c0f --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/AgvProtocol.java @@ -0,0 +1,37 @@ +package com.example.agvcontroller.protocol; + + +import java.io.Serializable; + +/** + * Created by vincent on 2023/3/23 + */ +//@Data +public class AgvProtocol implements Serializable { + + private static final long serialVersionUID = 8858385758421567504L; + + private String agvNo; + + private int timestamp; + + private IMessageBody messageBody; + + public static AgvProtocol build(String agvNo) { + AgvProtocol protocol = new AgvProtocol(); + protocol.setAgvNo(agvNo); + //protocol.setTimestamp((int) (System.currentTimeMillis()/1000)); + return protocol; + } + + public AgvProtocol setAgvNo(String agvNo) { + this.agvNo = agvNo; + return this; + } + + public AgvProtocol setMessageBody(IMessageBody messageBody) { + this.messageBody = messageBody; + return this; + } + +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/CRCUtils.java b/app/src/main/java/com/example/agvcontroller/protocol/CRCUtils.java index a5047c8..22c6368 100644 --- a/app/src/main/java/com/example/agvcontroller/protocol/CRCUtils.java +++ b/app/src/main/java/com/example/agvcontroller/protocol/CRCUtils.java @@ -1,4 +1,4 @@ -package com.zy.acs.gateway.utils; +package com.example.agvcontroller.protocol; /** * CRC16寰幆鍐椾綑鏍¢獙鐮� diff --git a/app/src/main/java/com/example/agvcontroller/protocol/Cools.java b/app/src/main/java/com/example/agvcontroller/protocol/Cools.java new file mode 100644 index 0000000..be634d7 --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/Cools.java @@ -0,0 +1,367 @@ +package com.example.agvcontroller.protocol; + + +import java.lang.reflect.*; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Created by vincent on 2019-06-09 + */ +public class Cools { + + public static boolean isEmpty(Object... objects) { + for (Object obj : objects){ + if (isEmpty(obj)){ + return true; + } + } + return false; + } + + @SuppressWarnings("rawtypes") + public static boolean isEmpty(Object o) { + if (o == null) { + return true; + } + if (o instanceof String) { + if (o.toString().trim().equals("")) { + return true; + } + } else if (o instanceof List) { + if (((List) o).size() == 0) { + return true; + } + } else if (o instanceof Map) { + if (((Map) o).size() == 0) { + return true; + } + } else if (o instanceof Set) { + if (((Set) o).size() == 0) { + return true; + } + } else if (o instanceof Object[]) { + if (((Object[]) o).length == 0) { + return true; + } + } else if (o instanceof int[]) { + if (((int[]) o).length == 0) { + return true; + } + } else if (o instanceof long[]) { + if (((long[]) o).length == 0) { + return true; + } + } + return false; + } + + public static int sqlLimitIndex(Integer pageIndex, Integer pageSize){ + return (pageIndex - 1) * pageSize; + } + + //public static String enToken(String username, String password){ + // return AesUtils.encrypt(username, zerofill(password, 16)); + //} + + //public static String deTokn(String token, String password){ + // //return AesUtils.decrypt(token, zerofill(password, 16)); + //} + + public static String zerofill(String msg, Integer count){ + if (msg.length() == count){ + return msg; + } else if (msg.length() > count){ + return msg.substring(0, 16); + } else { + StringBuilder msgBuilder = new StringBuilder(msg); + for (int i = 0; i<count-msg.length(); i++){ + msgBuilder.append("0"); + } + return msgBuilder.toString(); + } + } + + /** + * 鎴彇瀛楃涓�(榛樿end=true) + * @param str 琚埅瀛楃涓� + * @param end true:鏈�鍚庝竴涓瓧绗� / false:绗竴涓瓧绗� + */ + public static String deleteChar(String str, boolean end){ + if (isEmpty(str)){ + return ""; + } + if (end){ + return str.substring(0, str.length()-1); + } else { + return str.substring(1); + } + } + + public static String deleteChar(String str){ + return deleteChar(str, true); + } + + + /** + * map 杞� 瀵硅薄 + */ + public static <T> T conver(Map<? extends String, ?> map, Class<T> cls){ + T instance = null; + try { + Constructor<T> constructor = cls.getDeclaredConstructor(); + boolean constructorAccessible = constructor.isAccessible(); + constructor.setAccessible(true); + instance = constructor.newInstance(); + constructor.setAccessible(constructorAccessible); + } catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { + e.printStackTrace(); + } + Class<?> prototype = cls; + do { + for (Field field : prototype.getDeclaredFields()){ + if (Modifier.isFinal(field.getModifiers()) + || Modifier.isStatic(field.getModifiers()) + || Modifier.isTransient(field.getModifiers())){ + continue; + } + String fieldName = field.getName(); + Object val = null; + if (map.containsKey(fieldName)){ + val = map.get(fieldName); + } + if (val != null){ + boolean fieldAccessible = field.isAccessible(); + field.setAccessible(true); + Class<?> type = field.getType(); + try { + Constructor<?> constructor = type.getDeclaredConstructor(String.class); + boolean constructorAccessible = constructor.isAccessible(); + constructor.setAccessible(true); + field.set(instance, constructor.newInstance(String.valueOf(val))); + constructor.setAccessible(constructorAccessible); + } catch (IllegalAccessException + | InstantiationException + | InvocationTargetException + | NoSuchMethodException e) { + System.err.println("convert error ===> Class["+prototype+"],Field:["+fieldName+"],Type:["+type+"],Value:["+val+"]"); + } + field.setAccessible(fieldAccessible); + } + } + prototype = prototype.getSuperclass(); + } while (!Object.class.equals(prototype)); + return instance; + } + + /** + * 瀵硅薄 杞� map + */ + public static Map<String, Object> conver(Object obj){ + Class<?> cls = obj.getClass(); + Field[] fields = getAllFields(cls); + Map<String, Object> map = new HashMap<>(); + for (Field field : fields) { + if (Modifier.isFinal(field.getModifiers()) + || Modifier.isStatic(field.getModifiers()) + || Modifier.isTransient(field.getModifiers())){ + continue; + } + String key = field.getName(); + boolean flag = field.isAccessible(); + field.setAccessible(true); + Object val = null; + try { + val = field.get(obj); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + field.setAccessible(flag); + if (val != null){ + map.put(key, val); + } + } + return map; + } + + /** + * 鑾峰彇鎸囧畾Class锛堝強鍏禨uperClass锛夌殑鎴愬憳鍙橀噺 + */ + public static Field[] getAllFields(Class<?> cls){ + return getAllFields(cls, null); + } + + /** + * 閫掑綊鍚堝苟鍩虹被Field + */ + private static Field[] getAllFields(Class<?> cls, Field[] params){ + Field[] fields = (params == null) ? cls.getDeclaredFields() : params; + Class<?> superCls = cls.getSuperclass(); + if (superCls == null || superCls == Object.class){ + return fields; + } + Field[] superClsFields = superCls.getDeclaredFields(); + fields = addAll(fields, superClsFields); + return getAllFields(superCls, fields); + } + + /** + * 鏍规嵁fieldName鑾峰彇Field瀵硅薄 + */ + public static Field getField(Class<?> cls, String fieldName) { + Field[] allFields = getAllFields(cls); + for (Field field : allFields) { + if (field.getName().equals(fieldName)) { + return field; + } + } + return null; + } + + /** + * 鑾峰彇瀵硅薄涓煇涓狥ield鐨勫�� + */ + public static Object getFieldValue(Object obj, Field field) { + if (null == field) { + return null; + } else { + if (obj instanceof Class) { + obj = null; + } + if (!field.isAccessible()) { + field.setAccessible(true); + } + try { + return field.get(obj); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + } + + /** + * 鏁扮粍鍙犲姞 + */ + @SuppressWarnings("unchecked") + public static <T> T[] addAll(T[] array1, T... array2) { + if (array1 == null) { + return clone(array2); + } else if (array2 == null) { + return clone(array1); + } else { + Class<?> cls = array1.getClass().getComponentType(); + T[] joinedArray = (T[]) Array.newInstance(cls, array1.length + array2.length); + System.arraycopy(array1, 0, joinedArray, 0, array1.length); + + try { + System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); + return joinedArray; + } catch (ArrayStoreException e) { + Class<?> type2 = array2.getClass().getComponentType(); + if (!cls.isAssignableFrom(type2)) { + throw new RuntimeException("Cannot store " + type2.getName() + " in an array of " + cls.getName(), e); + } else { + throw e; + } + } + } + } + + /** + * 鍏嬮殕 + */ + private static <T> T[] clone(T[] array) { + return array == null ? null : array.clone(); + } + + /** + * map鎿嶄綔 + */ + public static CoolMap add(String key,Object value){ + CoolMap map = new CoolMap(); + map.put(key, value); + return map; + } + + public static class CoolMap extends HashMap<String, Object>{ + + public CoolMap add(String key,Object value){ + this.put(key, value); + return this; + } + + public CoolMap $(String key,Object value){ + this.put(key, value); + return this; + } + + } + + public static String md5(String string){ + try{ + MessageDigest md5 = MessageDigest.getInstance("MD5"); + byte[] bytes = md5.digest(string.getBytes(StandardCharsets.UTF_8)); + char[] chars = new char[bytes.length * 2]; + for (int i = 0; i < bytes.length; i++) { + int b = bytes[i]; + chars[i * 2] = hexDigits[(b & 0xF0) >> 4]; + chars[i * 2 + 1] = hexDigits[b & 0x0F]; + } + return new String(chars).toLowerCase(); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("md5鍔犲瘑澶辫触,str=".concat(string)); + } + } + + private static char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + + //public static Map<String, Object> translate(Object obj){ + // Class<?> cls = obj.getClass(); + // Field[] fields = getAllFields(cls); + // Map<String, Object> map = new HashMap<>(); + // for (Field field : fields) { + // String key = field.getName(); + // if (field.isAnnotationPresent(CoolTranslate.class)){ + // CoolTranslate annotation = field.getAnnotation(CoolTranslate.class); + // if (!isEmpty(annotation.value())) { + // key = annotation.value(); + // } + // } + // boolean flag = field.isAccessible(); + // field.setAccessible(true); + // Object val = null; + // try { + // val = field.get(obj); + // } catch (IllegalAccessException e) { + // e.printStackTrace(); + // } + // field.setAccessible(flag); + // if (val != null){ + // map.put(key, val); + // } + // } + // return map; + //} + + public static boolean eq(String str, String str0) { + if (Cools.isEmpty(str) && Cools.isEmpty(str0)) { + return true; + } + if (Cools.isEmpty(str) && !Cools.isEmpty(str0)) { + return false; + } + if (Cools.isEmpty(str0) && !Cools.isEmpty(str)) { + return false; + } + if (str.equals(str0)) { + return true; + } + return false; + } + +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/DirectionType.java b/app/src/main/java/com/example/agvcontroller/protocol/DirectionType.java new file mode 100644 index 0000000..cbac087 --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/DirectionType.java @@ -0,0 +1,9 @@ +package com.example.agvcontroller.protocol; + +public enum DirectionType { + + DOWN, + UP, + ; + +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/EncryType.java b/app/src/main/java/com/example/agvcontroller/protocol/EncryType.java new file mode 100644 index 0000000..5722bf5 --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/EncryType.java @@ -0,0 +1,66 @@ +package com.example.agvcontroller.protocol; + +/** + * 鍔犲瘑鏍囪瘑鏋氫妇 + * Created by vincent on 2019-04-02 + */ +public enum EncryType implements ICodedStatus { + + /** + * 鏁版嵁涓嶅姞瀵� + */ + ENCRY_EMPTY(0x01), + /** + * RSA绠楁硶鍔犲瘑 + */ + ENCRY_RSA(0x02), + /** + * AES128浣嶇畻娉曞姞瀵� + */ + ENCRY_AES128(0x03), + /** + * 寮傚父 + */ + ENCRY_EXCEPTION(0xFE), + /** + * 鏃犳晥 + */ + ENCRY_INVALID(0xFF); + + /** + * 鍏朵粬鏁板�间负棰勭暀 + */ + + private int code; + EncryType(int code) { + this.code = code; + } + + + public static EncryType getByCode(int code) { + for (EncryType e : + EncryType.values()) { + if (e.getCode() == code) { + return e; + } + } + return null; + } + + @Override + public int getCode() { + return code; + } + + // 棰勬湡鐨勬湡鏈涘�硷紝閫楀彿鍒嗛殧 + public static String expectedVals() { + StringBuilder valSb = new StringBuilder(); + for (EncryType c : + EncryType.values()) { + valSb.append(c.getCode()).append(","); + } + // 鍒犻櫎鏈�鍚庝竴涓�楀彿 + valSb.deleteCharAt(valSb.length() - 1); + return valSb.toString(); + } +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/ICodedStatus.java b/app/src/main/java/com/example/agvcontroller/protocol/ICodedStatus.java new file mode 100644 index 0000000..794f0f1 --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/ICodedStatus.java @@ -0,0 +1,9 @@ +package com.example.agvcontroller.protocol; + +/** + * 棰勭暀鎺ュ彛锛屽弽灏勬椂璋冪敤 + * Created by vincent on 2019-04-03 + */ +public interface ICodedStatus { + int getCode(); +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/ICommandBody.java b/app/src/main/java/com/example/agvcontroller/protocol/ICommandBody.java new file mode 100644 index 0000000..188c970 --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/ICommandBody.java @@ -0,0 +1,12 @@ +package com.example.agvcontroller.protocol; + +/** + * 鍛戒护鍙傛暟 + */ +public interface ICommandBody { + + byte[] writeToBytes(); + + void readFromBytes(byte[] messageBodyBytes); + +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/IMessageBody.java b/app/src/main/java/com/example/agvcontroller/protocol/IMessageBody.java index a6a6d90..35efd0c 100644 --- a/app/src/main/java/com/example/agvcontroller/protocol/IMessageBody.java +++ b/app/src/main/java/com/example/agvcontroller/protocol/IMessageBody.java @@ -1,4 +1,4 @@ -package com.zy.acs.common.domain.protocol; +package com.example.agvcontroller.protocol; import java.io.Serializable; diff --git a/app/src/main/java/com/example/agvcontroller/protocol/PacBody.java b/app/src/main/java/com/example/agvcontroller/protocol/PacBody.java new file mode 100644 index 0000000..bdcd9ed --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/PacBody.java @@ -0,0 +1,38 @@ +package com.example.agvcontroller.protocol; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; + +/** + * 鍗忚浣� + * Created by vincent on 2019-04-03 + */ +public class PacBody { + + private ByteBuf content; + + private IMessageBody messageBody; + + public ByteBuf getContent() { + return content; + } + + public PacBody setContent(ByteBuf content) { + this.content = content.duplicate(); + return this; + } + + public IMessageBody getMessageBody() { + return messageBody; + } + + public void setMessageBody(IMessageBody messageBody) { + this.messageBody = messageBody; + } + + @Override + public String toString() { + return ByteBufUtil.hexDump(content); + } + +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/PacErrorType.java b/app/src/main/java/com/example/agvcontroller/protocol/PacErrorType.java new file mode 100644 index 0000000..6eec109 --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/PacErrorType.java @@ -0,0 +1,48 @@ +package com.example.agvcontroller.protocol; + + +public enum PacErrorType implements ICodedStatus { + + /** + * 瑙f瀽寮傚父 + */ + PAC_ANALYZE_ERROR(0x01, "鎶ユ枃瑙f瀽寮傚父"), + + /** + * 鎶ユ枃闀垮害楠岃瘉寮傚父 + */ + PAC_LENGTH_ERROR(0x02, "鎶ユ枃闀垮害楠岃瘉寮傚父"), + + /** + * 鎶ユ枃鏍¢獙寮傚父 + */ + PAC_VALID_ERROR(0x023, "鎶ユ枃闀垮害楠岃瘉寮傚父"), + + ; + + private int code; // 缂栫爜 + private String des; // 鎻忚堪 + + PacErrorType(int code, String des) { + this.code = code; + this.des = des; + } + + public int getCode() { + return code; + } + + public String getDes() { + return des; + } + + public static PacErrorType getByCode(int code) { + for (PacErrorType c : + PacErrorType.values()) { + if (c.getCode() == code) { + return c; + } + } + return null; + } +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/PacHeader.java b/app/src/main/java/com/example/agvcontroller/protocol/PacHeader.java index a41be16..2912ac4 100644 --- a/app/src/main/java/com/example/agvcontroller/protocol/PacHeader.java +++ b/app/src/main/java/com/example/agvcontroller/protocol/PacHeader.java @@ -1,15 +1,10 @@ -package com.zy.acs.gateway.domain; +package com.example.agvcontroller.protocol; -import com.zy.acs.gateway.constant.EncryType; -import com.zy.acs.gateway.constant.ProtocolType; -import lombok.Data; -import org.apache.commons.lang.builder.ToStringBuilder; /** * 鍗忚澶� * Created by vincent on 2019-04-03 */ -@Data public class PacHeader { /** @@ -84,15 +79,15 @@ return this; } - @Override - public String toString() { - return new ToStringBuilder(this) - .append("startSymbol", startSymbol) - .append("contentLength", contentLength) - .append("uniqueNo", uniqueNo) - .append("timestamp", timestamp) - .append("protocolType", protocolType.getDes()) - .append("serialNum", serialNum) - .toString(); - } + //@Override + //public String toString() { + // return new ToStringBuilder(this) + // .append("startSymbol", startSymbol) + // .append("contentLength", contentLength) + // .append("uniqueNo", uniqueNo) + // .append("timestamp", timestamp) + // .append("protocolType", protocolType.getDes()) + // .append("serialNum", serialNum) + // .toString(); + //} } diff --git a/app/src/main/java/com/example/agvcontroller/protocol/PackagePart.java b/app/src/main/java/com/example/agvcontroller/protocol/PackagePart.java new file mode 100644 index 0000000..679690b --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/PackagePart.java @@ -0,0 +1,87 @@ +package com.example.agvcontroller.protocol; + +/** + * 鎶ユ枃鏍囪瘑鏋氫妇 + * 涓嬫爣1: 璧峰绱㈠紩 + * 涓嬫爣2: 闀垮害 + * 涓嬫爣3: 鎻忚堪 + * Created by vincent on 2019-04-03 + */ +public enum PackagePart { + + /** + * 璧峰绗� + */ + START_SYMBOL(0, 1, "璧峰绗�"), + + /** + * 鏁版嵁鍗曞厓闀垮害 + */ + CONTENT_LENGTH(1, 2, "鏁版嵁鍗曞厓闀垮害"), + + + /** + * 鍞竴鏍囪瘑鐮� + */ + UNIQUENO(3, 4, "鍞竴鏍囪瘑鐮�"), + + /** + * 鏃堕棿鎴� + */ + TIMESTAMP(7, 4, "鏃堕棿鎴�"), + + /** + * 鍛戒护鏍囪瘑 + */ + COMMAND_MARK(11, 1, "鍛戒护鏍囪瘑"), + + /** + * 鏁版嵁鍗曞厓 + */ + CONTENT(12, -1, "鏁版嵁鍗曞厓"), + + /** + * 鏍¢獙鐮� + */ + VALIDE_CODE( -1, 2, "鏍¢獙鐮�"), + + /** + * 搴旂瓟鏍囧織 + */ + ACK_MARK(3, 1, "搴旂瓟鏍囧織"), + + /** + * 鏁版嵁鍗曞厓鍔犲瘑鏂瑰紡 + */ + ENCRYPT_TYPE(21, 1, "鏁版嵁鍗曞厓鍔犲瘑鏂瑰紡"), + + ; + + // 瀛楄妭娈靛紑濮嬬储寮� + private Integer startIndex; + + // 瀛楄妭娈垫�诲瓧鑺傛暟 + private Integer len; + + // 鎻忚堪 + private String des; + + PackagePart(int startIndex, int len, String des) { + this.startIndex = startIndex; + this.len = len; + this.des = des; + } + + public Integer getStartIndex() { + return startIndex; + } + + public Integer getLen() { + return len; + } + + public String getDes() { + return des; + } + +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/ProtocolEncoder.java b/app/src/main/java/com/example/agvcontroller/protocol/ProtocolEncoder.java index 04a396a..ddc38e0 100644 --- a/app/src/main/java/com/example/agvcontroller/protocol/ProtocolEncoder.java +++ b/app/src/main/java/com/example/agvcontroller/protocol/ProtocolEncoder.java @@ -1,20 +1,12 @@ -package com.zy.acs.gateway.handler.coder; +package com.example.agvcontroller.protocol; -import com.core.common.RadixTools; -import com.zy.acs.common.utils.Utils; -import com.zy.acs.gateway.config.SystemProperties; -import com.zy.acs.gateway.constant.PackagePart; -import com.zy.acs.gateway.domain.AgvPackage; -import com.zy.acs.gateway.utils.ValidUtil; +import java.util.logging.Logger; + import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; /** * 缂栫爜鍣� @@ -22,13 +14,13 @@ * 澶勭悊鏂瑰紡: 寮傛垨鍜� * Created by vincent on 2019-04-02 */ -@Component +//@Component @ChannelHandler.Sharable public class ProtocolEncoder extends MessageToByteEncoder<Object> { - private static final Logger log = LoggerFactory.getLogger(ProtocolEncoder.class); + //private static final Logger log = LoggerFactory.getLogger(ProtocolEncoder.class); - @Autowired + //@Autowired private SystemProperties systemProperties; @Override @@ -48,9 +40,9 @@ byte[] bodyBytes = pac.getBody().getMessageBody().writeToBytes(); // body - String uniqueNo = pac.getHeader().getUniqueNo(); + //String uniqueNo = pac.getHeader().getUniqueNo(); - byte[] uniquenoBytes = RadixTools.intToBytes(Integer.parseInt(pac.getHeader().getUniqueNo())); // uniqueno + //byte[] uniquenoBytes = RadixTools.intToBytes(Integer.parseInt(pac.getHeader().getUniqueNo())); // uniqueno int len = PackagePart.UNIQUENO.getLen() // len @@ -58,14 +50,14 @@ + PackagePart.COMMAND_MARK.getLen() + bodyBytes.length; - out.writeByte(pac.getHeader().getStartSymbol()) // symbol - .writeShortLE(len) - .writeBytes(Utils.reverse(uniquenoBytes)) // uniqueno - .writeIntLE((int) (System.currentTimeMillis() / 1000)) // timestamp - .writeByte(pac.getHeader().getProtocolType().getCode()) // type - .writeBytes(bodyBytes) // body - .writeShort(pac.getValidCode()) // valid - ; + //out.writeByte(pac.getHeader().getStartSymbol()) // symbol + // .writeShortLE(len) + // .writeBytes(Utils.reverse(uniquenoBytes)) // uniqueno + // .writeIntLE((int) (System.currentTimeMillis() / 1000)) // timestamp + // .writeByte(pac.getHeader().getProtocolType().getCode()) // type + // .writeBytes(bodyBytes) // body + // .writeShort(pac.getValidCode()) // valid + //; pac.setValidCode(ValidUtil.calculateValidByteFromBuff(out)); out.resetReaderIndex(); @@ -75,7 +67,7 @@ if (systemProperties.isPrintPacLog()){ - log.info("Agv [{}] 涓嬭 [{}] >>> {}", uniqueNo, pac.getHeader().getProtocolType().getDes(), ByteBufUtil.hexDump(out).toUpperCase()); + //log.info("Agv [{}] 涓嬭 [{}] >>> {}", uniqueNo, pac.getHeader().getProtocolType().getDes(), ByteBufUtil.hexDump(out).toUpperCase()); } } diff --git a/app/src/main/java/com/example/agvcontroller/protocol/ProtocolPojoType.java b/app/src/main/java/com/example/agvcontroller/protocol/ProtocolPojoType.java new file mode 100644 index 0000000..301b5cf --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/ProtocolPojoType.java @@ -0,0 +1,54 @@ +package com.example.agvcontroller.protocol; + + +/** + * 搴旂瓟瀹炰綋鏋氫妇 + * Created by vincent on 2019-04-02 + */ +public enum ProtocolPojoType { + + PATH_COMMAND(ProtocolType.PATH_COMMAND, AGV_01_DOWN.class), + + ACTION_COMMAND(ProtocolType.ACTION_COMMAND, AGV_02_DOWN.class), + + LOGIN_ACK(ProtocolType.LOGIN_ACK, AGV_F0_DOWN.class), + + ACTION_SUCCESS_ACK(ProtocolType.ACTION_SUCCESS_ACK, AGV_A1_DOWN.class), + + ACTIVATION_COMMAND(ProtocolType.ACTIVATION_COMMAND, AGV_80_DOWN.class), + + HEARTBEAT_COMMAND(ProtocolType.HEARTBEAT_COMMAND, AGV_03_DOWN.class), + + ; + + + public final ProtocolType protocolType; + + + public final Class<? extends IMessageBody> clazz; + + ProtocolPojoType(ProtocolType protocolType, Class<? extends IMessageBody> clazz) { + this.protocolType = protocolType; + this.clazz = clazz; + } + + + public static ProtocolPojoType query(ProtocolType protocolType) { + for (ProtocolPojoType type : ProtocolPojoType.values()) { + if (protocolType.equals(type.protocolType)) { + return type; + } + } + return null; + } + + public static ProtocolPojoType query(Class<? extends IMessageBody> clazz) { + for (ProtocolPojoType type : ProtocolPojoType.values()) { + if (clazz.equals(type.clazz)) { + return type; + } + } + return null; + } + +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/ProtocolType.java b/app/src/main/java/com/example/agvcontroller/protocol/ProtocolType.java new file mode 100644 index 0000000..1e0e8d3 --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/ProtocolType.java @@ -0,0 +1,82 @@ +package com.example.agvcontroller.protocol; + +/** + * 鏍囪瘑鏋氫妇 + * Created by vincent on 2019-04-02 + */ +public enum ProtocolType implements ICodedStatus { + + // 涓嬭 ------------------------------------------------------------------- + + PATH_COMMAND(0x01, "璺緞鏁版嵁鍖�", DirectionType.DOWN), + + ACTION_COMMAND(0x02, "鍔ㄤ綔鍛戒护鍖�", DirectionType.DOWN), + + HEARTBEAT_COMMAND(0x03, "蹇冭烦鍖�", DirectionType.DOWN), + + FAULT_CLEAR_COMMAND(0x04, "鏁呴殰鍛戒护鍖�", DirectionType.DOWN), + + ACTIVATION_COMMAND(0x80, "婵�娲诲寘", DirectionType.DOWN), + + LOGIN_ACK(0xF0, "鐧诲綍搴旂瓟鍖�", DirectionType.DOWN), + + ACTION_SUCCESS_ACK(0xA1, "鍔ㄤ綔瀹屾垚鎴愬姛搴旂瓟", DirectionType.DOWN), + + ACTION_FAIL_ACK(0xA0, "鍔ㄤ綔瀹屾垚澶辫触搴旂瓟", DirectionType.DOWN), + + // 涓婅 ------------------------------------------------------------------- + + PATH_ACK(0x01, "璺緞搴旂瓟鍖�", DirectionType.UP), + + COMMAND_ACK(0x02, "鍛戒护搴旂瓟鍖�", DirectionType.UP), + + ACTION_COMPLETE(0x11, "鍔ㄤ綔瀹屾垚鍖�", DirectionType.UP), + + DATA_CODE_REPORT(0x12, "鏈夌爜瀹炴椂鏁版嵁鍖�", DirectionType.UP), + + DATA_WITHOUT_CODE_REPORT(0x13, "鏃犵爜瀹炴椂鏁版嵁鍖�", DirectionType.UP), + + HEARTBEAT_REPORT(0x03, "蹇冭烦鍖�", DirectionType.UP), + + FAULT_REPORT(0x04, "鏁呴殰鏁版嵁鍖�", DirectionType.UP), + + HANDLE_FALUT_ACK(0x14, "鏁呴殰娓呴櫎搴旂瓟鍖�", DirectionType.UP), + + SILO_REPORT(0x70, "鏂欎粨淇℃伅鍖�", DirectionType.UP), + + LOGIN_REPORT(0xF0, "鏈哄櫒浜虹櫥闄嗘暟鎹寘", DirectionType.UP), + + ; + + private final int code; // 缂栫爜 + private final String des; // 鎻忚堪 + private final DirectionType direction; + + ProtocolType(int code, String des, DirectionType direction) { + this.code = code; + this.des = des; + this.direction = direction; + } + + public int getCode() { + return code; + } + + public String getDes() { + return des; + } + + public DirectionType getDirection() { + return direction; + } + + public static ProtocolType getByCode(int code, DirectionType direction) { + for (ProtocolType type : ProtocolType.values()) { + if (type.getCode() == code && type.getDirection() == direction) { + return type; + } + } + return null; + } + +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/ProtocolUtils.java b/app/src/main/java/com/example/agvcontroller/protocol/ProtocolUtils.java index 2ea178b..8f35911 100644 --- a/app/src/main/java/com/example/agvcontroller/protocol/ProtocolUtils.java +++ b/app/src/main/java/com/example/agvcontroller/protocol/ProtocolUtils.java @@ -1,38 +1,33 @@ -package com.zy.acs.gateway.utils; +package com.example.agvcontroller.protocol; -import com.zy.acs.common.domain.AgvProtocol; -import com.zy.acs.common.domain.protocol.IMessageBody; -import com.zy.acs.gateway.constant.ProtocolPojoType; -import com.zy.acs.gateway.constant.ProtocolType; -import com.zy.acs.gateway.domain.AgvPackage; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + +import java.util.logging.Logger; /** * Created by vincent on 2019-04-03 */ public class ProtocolUtils { - private static final Logger log = LoggerFactory.getLogger(ProtocolUtils.class); + //private static final Logger log = LoggerFactory.getLogger(ProtocolUtils.class); // 涓嬭鍗忚鎶ユ枃缁勮 public static AgvPackage installDownProtocol(AgvProtocol protocol){ - ProtocolPojoType protocolPojo = ProtocolPojoType.query(protocol.getMessageBody().getClass()); + //ProtocolPojoType protocolPojo = ProtocolPojoType.query(protocol.getMessageBody().getClass()); - assert protocolPojo != null; + //assert protocolPojo != null; AgvPackage ackPackage = AgvPackage.valueOfEmpty(); ackPackage.getHeader() .setStartSymbol((byte)0xEE) .setContentLength(0) - .setUniqueNo(protocol.getAgvNo()) - .setTimestamp(protocol.getTimestamp()) - .setProtocolType(protocolPojo.protocolType) + //.setUniqueNo(protocol.getAgvNo()) + //.setTimestamp(protocol.getTimestamp()) + //.setProtocolType(protocolPojo.protocolType) ; - ackPackage.getBody().setMessageBody(protocol.getMessageBody()); + //ackPackage.getBody().setMessageBody(protocol.getMessageBody()); return ackPackage; @@ -65,9 +60,9 @@ } catch (ReflectiveOperationException reflectiveOperationException) { - log.error("MessageBodyHandler ReflectiveOperationException", reflectiveOperationException); + //log.error("MessageBodyHandler ReflectiveOperationException", reflectiveOperationException); - log.error("java鍙嶅皠鍒涘缓瀹炰緥澶辫触锛歿}", protocolPojo.clazz.getName()); + //log.error("java鍙嶅皠鍒涘缓瀹炰緥澶辫触锛歿}", protocolPojo.clazz.getName()); return null; } diff --git a/app/src/main/java/com/example/agvcontroller/protocol/SystemProperties.java b/app/src/main/java/com/example/agvcontroller/protocol/SystemProperties.java new file mode 100644 index 0000000..0ac9ddf --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/SystemProperties.java @@ -0,0 +1,94 @@ +package com.example.agvcontroller.protocol; + + +import java.net.InetAddress; +import java.net.UnknownHostException; + +/** + * vc绯荤粺閰嶇疆 + * Created by luxiaotao on 2018/10/15 + */ +//@Configuration +//@ConfigurationProperties(prefix = "agv-tcp") +public class SystemProperties { + + public static String HOST_NAME; + + static { + try { + HOST_NAME = InetAddress.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + System.err.println("find hostname err"); + } + } + + private int port; + + private int heartSeconds; + + private int backlog; + + private boolean keepAlive; + + private int sndbuf; + + private int rcvbuf; + + private boolean printPacLog; + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public int getHeartSeconds() { + return heartSeconds; + } + + public void setHeartSeconds(int heartSeconds) { + this.heartSeconds = heartSeconds; + } + + public int getBacklog() { + return backlog; + } + + public void setBacklog(int backlog) { + this.backlog = backlog; + } + + public boolean isKeepAlive() { + return keepAlive; + } + + public void setKeepAlive(boolean keepAlive) { + this.keepAlive = keepAlive; + } + + public int getSndbuf() { + return sndbuf; + } + + public void setSndbuf(int sndbuf) { + this.sndbuf = sndbuf; + } + + public int getRcvbuf() { + return rcvbuf; + } + + public void setRcvbuf(int rcvbuf) { + this.rcvbuf = rcvbuf; + } + + public boolean isPrintPacLog() { + return printPacLog; + } + + public void setPrintPacLog(final boolean printPacLog) { + this.printPacLog = printPacLog; + } +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/Utils.java b/app/src/main/java/com/example/agvcontroller/protocol/Utils.java new file mode 100644 index 0000000..29d3654 --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/Utils.java @@ -0,0 +1,239 @@ +package com.example.agvcontroller.protocol; + + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +/** + * Created by vincent on 2023/3/14 + */ +public class Utils { + + /** + * 鏁扮粍鍊掑簭 + * @param bytes + * @param <T> + */ + public static <T> byte[] reverse(byte[] bytes) { + if (bytes == null) return null; + for (int start = 0, end = bytes.length - 1; start < end; start++, end--) { + byte temp = bytes[end]; + bytes[end] = bytes[start]; + bytes[start] = temp; + } + return bytes; + } + + /** + * 鎴彇鏁扮粍 + * @param bytes 鍘熸暟缁� + * @param pos 瀹氫綅锛堟埅鍙栧悗鍖呭惈瀹氫綅鐐规暟鎹級 + * @param len 闀垮害 + * @return new arr + */ + public static byte[] slice(byte[] bytes, int pos, int len) { + if (bytes == null || bytes.length == 0 || len == 0) { + return new byte[0]; + } + if (pos + len > bytes.length) { + throw new RuntimeException("com.zy.acs.common.utils ArrayIndexOutOfBoundsException\n" + + "鍘熸暟缁� bytes 闀垮害涓� " + bytes.length + "锛屾埅鍙栭暱搴﹁秴杩囧師鏁扮粍锛�"); + } + byte[] arr = new byte[len]; + System.arraycopy(bytes, pos, arr, 0, len); + return arr; + } + + public static byte[] sliceWithReverse(byte[] bytes, int pos, int len) { + byte[] slice = slice(bytes, pos, len); + reverse(slice); + return slice; + } + + public static byte[] merge(Object... objects) { + int len = 0; + for (Object object : objects) { + if (object instanceof byte[]) { + byte[] bytes = (byte[]) object; + len += bytes.length; + } + if (object instanceof Byte) { + len++; + } + } + byte[] arr = new byte[len]; + int idx = 0; + for (Object object : objects) { + if (object instanceof byte[]) { + byte[] bytes = (byte[]) object; + System.arraycopy(bytes, 0, arr, idx, bytes.length); + idx += bytes.length; + } + if (object instanceof Byte) { + byte[] bytes = new byte[] {(Byte) object}; + System.arraycopy(bytes, 0, arr, idx, bytes.length); + idx += bytes.length; + } + } + return arr; + } + + public static <T> String join(T[] array, String seq) { + StringBuilder sb = new StringBuilder(); + if (array != null) { + for (int i = 0; i < array.length; i++) { + sb.append(array[i]); + if (i < array.length - 1) { + sb.append(seq); + } + } + } + return sb.toString(); + } + + public static String zeroFill(String msg, Integer len) { + len = Optional.ofNullable(len).orElse(16); + if (msg.length() == len){ + return msg; + } else if (msg.length() > len){ + return msg.substring(0, 16); + } else { + StringBuilder msgBuilder = new StringBuilder(msg); + for (int i = 0; i<len-msg.length(); i++){ + msgBuilder.insert(0,"0"); + } + return msgBuilder.toString(); + } + } + + public static String removePrefix(String str, String prefix) { + if (!Cools.isEmpty(str) && !Cools.isEmpty(prefix)) { + if (str.startsWith(prefix)) { + return str.substring(prefix.length()); + } else { + return str; + } + } else { + return str; + } + } + + public static String removeSuffix(String str, String suffix) { + if (!Cools.isEmpty(str) && !Cools.isEmpty(suffix)) { + if (str.endsWith(suffix)) { + return str.substring(0, str.indexOf(suffix)); + } else { + return str; + } + } else { + return str; + } + } + + /** + * 澶ч┘宄� 杞� symbol灏忛┘宄� + */ + public static String toSymbolCase(String str, char symbol) { + if (str == null) { + return null; + } else { + int length = str.length(); + StringBuilder sb = new StringBuilder(); + + for(int i = 0; i < length; ++i) { + char c = str.charAt(i); + if (Character.isUpperCase(c)) { + Character preChar = i > 0 ? str.charAt(i - 1) : null; + Character nextChar = i < str.length() - 1 ? str.charAt(i + 1) : null; + if (null != preChar) { + if (symbol == preChar) { + if (null == nextChar || Character.isLowerCase(nextChar)) { + c = Character.toLowerCase(c); + } + } else if (Character.isLowerCase(preChar)) { + sb.append(symbol); + if (null == nextChar || Character.isLowerCase(nextChar)) { + c = Character.toLowerCase(c); + } + } else if (null == nextChar || Character.isLowerCase(nextChar)) { + sb.append(symbol); + c = Character.toLowerCase(c); + } + } else if (null == nextChar || Character.isLowerCase(nextChar)) { + c = Character.toLowerCase(c); + } + } + + sb.append(c); + } + + return sb.toString(); + } + } + + public static String toCamelCase(CharSequence name) { + if (null == name) { + return null; + } else { + String name2 = name.toString(); + if (name2.contains("_")) { + int length = name2.length(); + StringBuilder sb = new StringBuilder(length); + boolean upperCase = false; + + for(int i = 0; i < length; ++i) { + char c = name2.charAt(i); + if (c == '_') { + upperCase = true; + } else if (upperCase) { + sb.append(Character.toUpperCase(c)); + upperCase = false; + } else { + sb.append(Character.toLowerCase(c)); + } + } + + return sb.toString(); + } else { + return name2; + } + } + } + + public static String sub(String str, Integer maxLen) { + if (str.length() > maxLen) { + return str.substring(0, maxLen); + } + return str; + } + + public static String generateSeqNum(String lastSeqNum) { + if (Cools.isEmpty(lastSeqNum)) { + return zeroFill("1", 4); + } else { + int i = Integer.parseInt(lastSeqNum); + if (i >= 9999) { + return zeroFill("1", 4); + } else { + return zeroFill(String.valueOf(i+1), 4); + } + } + } + + // pos start in 0 + public static boolean getBit(byte b, int position) { + if (position < 0 || position > 7) { + throw new IllegalArgumentException("Bit position must be between 0 and 7"); + } + int mask = 1 << position; + return (b & mask) != 0; + } + + public static <T> List<T> singletonList(T o) { + List<T> list = new ArrayList<>(); + list.add(o); + return list; + } + +} diff --git a/app/src/main/java/com/example/agvcontroller/protocol/ValidUtil.java b/app/src/main/java/com/example/agvcontroller/protocol/ValidUtil.java new file mode 100644 index 0000000..d889df9 --- /dev/null +++ b/app/src/main/java/com/example/agvcontroller/protocol/ValidUtil.java @@ -0,0 +1,55 @@ +package com.example.agvcontroller.protocol; + +import io.netty.buffer.ByteBuf; + +/** + * 鏍¢獙宸ュ叿绫� + * Created by vincent on 2019-04-03 + */ +public class ValidUtil { + + public static final Integer MAX_DEFAULT = 65531; + + /** + * 鏍¢獙娑堟伅涓暟鍊兼槸鍚﹀湪瀵瑰簲鍏佽鐨勮寖鍥村唴 + * @param item 琚牎楠屾暟鎹�� + * @param min 鍏佽鐨勬渶灏忓�� + * @param max 鍏佽鐨勬渶澶у�� + */ + public static boolean rangeValid(int item, int min, int max) { + return !(item < min || item > max); + } + + /** + * 寮傛垨鏍¢獙鍖呭畬鏁� + * 浠庡懡浠ゅ崟鍏冿紙绗笁涓瓧鑺傦級寮�濮嬶紝涓�娆′笌鍚庝竴涓瓧鑺傚紓鎴栵紝鐩磋嚦鍊掓暟绗簩涓瓧鑺傘�� + * 灏嗗紓鎴栫殑缁撴灉涓庢姤鏂囦腑鏈�鍚庝竴涓瓧鑺傛瘮杈冦�� + * -鐩哥瓑锛氭姤鏂囧畬鏁� + * -涓嶇浉绛夛細鎶ユ枃涓嶅畬鏁� + */ + public static boolean validPac(AgvPackage pac) { + ByteBuf buf = pac.getSourceBuff(); + return getValdByteInMsg(pac) == calculateValidByteFromBuff(buf); + } + + /** + * 浠庡綋鍓嶆暟鎹寘璁$畻鏍¢獙鐮� + */ + public static int calculateValidByteFromBuff(ByteBuf buf) { + buf.resetReaderIndex(); + byte[] bytes = new byte[buf.readableBytes() - PackagePart.VALIDE_CODE.getLen()]; + buf.readBytes(bytes); + int currValidByte = CRCUtils.crc16(bytes); + // 閲嶇疆璇绘寚閽� + buf.resetReaderIndex(); + return currValidByte; + } + + /** + * 鑾峰彇褰撳墠鏁版嵁鍖呬腑鐨勬牎楠岀爜(鏈�鍚庝竴浣嶅瓧鑺�) + */ + public static int getValdByteInMsg(AgvPackage pac) { + return pac.getValidCode(); + } + +} -- Gitblit v1.9.1