Browse Source

调整蓝牙设备链接逻辑

master
白凤吉 4 months ago
parent
commit
45e0bb3990
  1. 4
      app/build.gradle
  2. 4
      app/src/main/assets/web/asset-manifest.json
  3. 2
      app/src/main/assets/web/index.html
  4. 4
      app/src/main/assets/web/static/js/main.6d38614b.js
  5. 0
      app/src/main/assets/web/static/js/main.6d38614b.js.LICENSE.txt
  6. 2
      app/src/main/java/com/iflytop/profilometer/ProfilometerApplication.java
  7. 6
      app/src/main/java/com/iflytop/profilometer/api/ble/BleApi.java
  8. 44
      app/src/main/java/com/iflytop/profilometer/api/system/SystemApi.java
  9. 3
      app/src/main/java/com/iflytop/profilometer/api/system/SystemRoutes.kt
  10. 1
      app/src/main/java/com/iflytop/profilometer/common/constant/SystemConfigType.java
  11. 23
      app/src/main/java/com/iflytop/profilometer/core/bluetooth/BleDeviceDriver.java
  12. 24
      app/src/main/java/com/iflytop/profilometer/core/bluetooth/BleManager.java
  13. 5
      app/src/main/java/com/iflytop/profilometer/core/migration/channel/BleDeviceUartChannel.java
  14. 648
      app/src/main/java/com/iflytop/profilometer/core/migration/factory/TPMIPacketFactory.java
  15. 505
      app/src/main/java/com/iflytop/profilometer/core/migration/type/protocol/TPMIPacket.java
  16. 1
      app/src/main/java/com/iflytop/profilometer/model/bo/RailProfileMeasureTaskState.java
  17. 9
      app/src/main/java/com/iflytop/profilometer/model/vo/SystemConfigVO.java
  18. 3
      app/src/main/java/com/iflytop/profilometer/service/RailProfileDrawerService.java

4
app/build.gradle

@ -17,9 +17,9 @@ android {
versionName "1.0"
//
versionCode 2
versionCode 5
//
versionName "1.1.2"
versionName "1.1.5"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

4
app/src/main/assets/web/asset-manifest.json

@ -1,7 +1,7 @@
{
"files": {
"main.css": "./static/css/main.3543332f.css",
"main.js": "./static/js/main.7ec89011.js",
"main.js": "./static/js/main.6d38614b.js",
"static/js/206.d71bd05f.chunk.js": "./static/js/206.d71bd05f.chunk.js",
"static/media/icon_tab3_u.svg": "./static/media/icon_tab3_u.6d18cbd7c4dc6a54f745fdfcc7716050.svg",
"static/media/icon_tab3_s.svg": "./static/media/icon_tab3_s.b0a9cbf5ecee59829c9de63a2e9de169.svg",
@ -22,6 +22,6 @@
},
"entrypoints": [
"static/css/main.3543332f.css",
"static/js/main.7ec89011.js"
"static/js/main.6d38614b.js"
]
}

2
app/src/main/assets/web/index.html

@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="./favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"/><title>廓形仪</title><script defer="defer" src="./static/js/main.7ec89011.js"></script><link href="./static/css/main.3543332f.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script>function setupWebViewJavascriptBridge(e){if(window.WebViewJavascriptBridge)return e(WebViewJavascriptBridge);if(/android/i.test(navigator.userAgent))document.addEventListener("WebViewJavascriptBridgeReady",(function(){e(WebViewJavascriptBridge)}),!1);else{if(window.WVJBCallbacks)return window.WVJBCallbacks.push(e);window.WVJBCallbacks=[e];var i=document.createElement("iframe");i.style.display="none",i.src="https://__bridge_loaded__",document.documentElement.appendChild(i),setTimeout((function(){document.documentElement.removeChild(i)}),0)}}setupWebViewJavascriptBridge((function(e){window.bridge=e}));var SyncBridgeJS={call:function(e,i){return prompt("sync/syncBridge/"+e,i)}}</script></body></html>
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="./favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"/><title>廓形仪</title><script defer="defer" src="./static/js/main.6d38614b.js"></script><link href="./static/css/main.3543332f.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script>function setupWebViewJavascriptBridge(e){if(window.WebViewJavascriptBridge)return e(WebViewJavascriptBridge);if(/android/i.test(navigator.userAgent))document.addEventListener("WebViewJavascriptBridgeReady",(function(){e(WebViewJavascriptBridge)}),!1);else{if(window.WVJBCallbacks)return window.WVJBCallbacks.push(e);window.WVJBCallbacks=[e];var i=document.createElement("iframe");i.style.display="none",i.src="https://__bridge_loaded__",document.documentElement.appendChild(i),setTimeout((function(){document.documentElement.removeChild(i)}),0)}}setupWebViewJavascriptBridge((function(e){window.bridge=e}));var SyncBridgeJS={call:function(e,i){return prompt("sync/syncBridge/"+e,i)}}</script></body></html>

4
app/src/main/assets/web/static/js/main.6d38614b.js
File diff suppressed because it is too large
View File

0
app/src/main/assets/web/static/js/main.7ec89011.js.LICENSE.txt → app/src/main/assets/web/static/js/main.6d38614b.js.LICENSE.txt

2
app/src/main/java/com/iflytop/profilometer/ProfilometerApplication.java

@ -119,7 +119,7 @@ public class ProfilometerApplication extends Application {
"\t\t},\n" +
"\t\t\"radius\": 300,\n" +
"\t\t\"color\": \"#000000\",\n" +
"\t\t\"side\": \"right\"\n" +
"\t\t\"side\": \"left\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"type\": \"arc\",\n" +

6
app/src/main/java/com/iflytop/profilometer/api/ble/BleApi.java

@ -59,12 +59,12 @@ public class BleApi {
BleWebsocketManager.getInstance().stopWsPush();
BleManager.getInstance().stopScan();//连接后停止扫描
BleManager.getInstance().connectToDevice(mac);
BlePingManager.getInstance().startPing();
// BlePingManager.getInstance().startPing();
return Result.success();
} catch (Exception e) {
BleWebsocketManager.getInstance().startWsPush();//链接失败后继续扫描并推送
BleManager.getInstance().startScan();
BlePingManager.getInstance().stopPing();
// BlePingManager.getInstance().stopPing();
Log.e(TAG, "链接蓝牙设备失败", e);
return Result.failed("链接蓝牙设备失败");
}
@ -76,7 +76,7 @@ public class BleApi {
public String disconnect() {
try {
BleManager.getInstance().disconnect();
BlePingManager.getInstance().stopPing();
// BlePingManager.getInstance().stopPing();
BleManager.getInstance().startScan();//断开连接后开始扫描
return Result.success();
} catch (Exception e) {

44
app/src/main/java/com/iflytop/profilometer/api/system/SystemApi.java

@ -1,9 +1,13 @@
package com.iflytop.profilometer.api.system;
import android.content.Context;
import android.util.Log;
import com.iflytop.profilometer.common.constant.SystemConfigType;
import com.iflytop.profilometer.common.result.Result;
import com.iflytop.profilometer.core.bluetooth.BleDeviceDriver;
import com.iflytop.profilometer.core.bluetooth.BleManager;
import com.iflytop.profilometer.core.system.SystemService;
import com.iflytop.profilometer.dao.SystemConfigDao;
import com.iflytop.profilometer.model.entity.SystemConfig;
import com.iflytop.profilometer.model.vo.SystemConfigVO;
@ -24,18 +28,46 @@ public class SystemApi {
SystemConfigVO systemConfigVO = new SystemConfigVO();
if (serverConfig != null) {
systemConfigVO.setServer(serverConfig.getConfigValue());
try {
if (BleManager.getInstance().isConnected()) {
BleDeviceDriver bleDeviceDriver = SystemService.getInstance().getBleDeviceDriver();
Integer standbyMinutesByDevice = bleDeviceDriver.getStandbyInactiveTime();
systemConfigVO.setStandbyMinutes(standbyMinutesByDevice / 60 / 1000);
}
} catch (Exception e) {
e.printStackTrace();
Log.e("SYSTEM API", "获取设备自动待机时间出错");
if (BleManager.getInstance().isConnected()) {
BleDeviceDriver bleDeviceDriver = SystemService.getInstance().getBleDeviceDriver();
Integer standbyMinutesByDevice = bleDeviceDriver.getStandbyInactiveTime();
systemConfigVO.setStandbyMinutes(standbyMinutesByDevice / 60 / 1000);
}
}
}
return Result.success(systemConfigVO);
}
public String save(String server) {
public String save(String server, Integer standbyMinutes) {
SystemConfigDao systemConfigDao = new SystemConfigDao(context);
SystemConfig systemConfig = new SystemConfig();
systemConfig.setConfigKey(SystemConfigType.SERVER);
systemConfig.setConfigValue(server);
int rows = systemConfigDao.updateSystemConfig(systemConfig);
SystemConfig serverConfig = new SystemConfig();
serverConfig.setConfigKey(SystemConfigType.SERVER);
serverConfig.setConfigValue(server);
int rows = systemConfigDao.updateSystemConfig(serverConfig);
if (rows < 1) {
systemConfigDao.insertSystemConfig(systemConfig);
systemConfigDao.insertSystemConfig(serverConfig);
}
try {
if (BleManager.getInstance().isConnected()) {
BleDeviceDriver bleDeviceDriver = SystemService.getInstance().getBleDeviceDriver();
bleDeviceDriver.setStandbyInactiveTime(standbyMinutes * 60 * 1000);
}
} catch (Exception e) {
e.printStackTrace();
Log.e("SYSTEM API", "保存设备自动待机时间出错");
if (BleManager.getInstance().isConnected()) {
BleDeviceDriver bleDeviceDriver = SystemService.getInstance().getBleDeviceDriver();
bleDeviceDriver.setStandbyInactiveTime(standbyMinutes * 60 * 1000);
}
}
return Result.success();
}

3
app/src/main/java/com/iflytop/profilometer/api/system/SystemRoutes.kt

@ -27,7 +27,8 @@ fun Routing.systemRoutes(context: Context) {
val requestBody = call.receiveText()
val jsonObj = JSONUtil.parseObj(requestBody)
val server = jsonObj.getStr("server")
val jsonResponse = api.save(server)
val standbyMinutes = jsonObj.getInt("standbyMinutes")
val jsonResponse = api.save(server, standbyMinutes)
call.respondText(jsonResponse, ContentType.Application.Json)
}
}

1
app/src/main/java/com/iflytop/profilometer/common/constant/SystemConfigType.java

@ -2,4 +2,5 @@ package com.iflytop.profilometer.common.constant;
public class SystemConfigType {
public static final String SERVER = "sync_server";
public static final String STANDBY_MINUTES = "standby_minutes";
}

23
app/src/main/java/com/iflytop/profilometer/core/bluetooth/BleDeviceDriver.java

@ -53,4 +53,27 @@ public class BleDeviceDriver {
return appearance;
}
public void unlockEEPROM() {
var packet = TPMIPacketFactory.buildUnlockEEPROMCmd();
uartChannel.sendCommand(packet);
}
public void lockEEPROM() {
var packet = TPMIPacketFactory.buildLockEEPROMCmd();
uartChannel.sendCommand(packet);
}
public synchronized void setStandbyInactiveTime(Integer time) {
unlockEEPROM();
var packet = TPMIPacketFactory.buildSetStandbyInactiveCmd(time);
uartChannel.sendCommand(packet);
lockEEPROM();
}
public Integer getStandbyInactiveTime() {
var packet = TPMIPacketFactory.buildGetStandbyInactiveTime();
var rx = uartChannel.sendCommand(packet);
return rx.getDataAsInt(0);
}
}

24
app/src/main/java/com/iflytop/profilometer/core/bluetooth/BleManager.java

@ -509,18 +509,22 @@ public class BleManager {
@SuppressLint("MissingPermission")
public void disconnect() {
if (bluetoothGatt != null) {
bluetoothGatt.disconnect();
bluetoothGatt.close();
bluetoothGatt = null;
DeviceState deviceState = SystemService.getInstance().getDeviceState();
deviceState.setConnected(false);
try{
bluetoothGatt.disconnect();
bluetoothGatt.close();
bluetoothGatt = null;
DeviceState deviceState = SystemService.getInstance().getDeviceState();
deviceState.setConnected(false);
SharedPreferences prefs = context.getSharedPreferences("profilometer", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.remove("mac_address");
editor.apply();
SharedPreferences prefs = context.getSharedPreferences("profilometer", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.remove("mac_address");
editor.apply();
Log.d(TAG, "当前蓝牙设备已断开连接");
Log.d(TAG, "当前蓝牙设备已断开连接");
}catch (Exception e){
e.printStackTrace();
}
}
}

5
app/src/main/java/com/iflytop/profilometer/core/migration/channel/BleDeviceUartChannel.java

@ -87,11 +87,10 @@ public class BleDeviceUartChannel {
BleManager.getInstance().sendMessage(tx.rawpacket);
try {
ctCxt.waitCxt(10000);
ctCxt.waitCxt(5000);
} catch (InterruptedException ignored) {
}
var rxPacket = ctCxt.clearCtCxt();
return rxPacket;
return ctCxt.clearCtCxt();
}
private void handleReceivedData(byte[] data) {

648
app/src/main/java/com/iflytop/profilometer/core/migration/factory/TPMIPacketFactory.java

@ -8,329 +8,379 @@ import com.iflytop.profilometer.core.migration.utils.ZByteUtils;
public class TPMIPacketFactory {
static public TPMIPacket buildDeviceDetectCmd() {return buildCommand(TPMIPacket.Command.DEVICE_DETECT);}
static final public Integer CMD_OVERTIME = 50;
static public TPMIPacket buildPingCmd() {return buildCommand(TPMIPacket.Command.PING);}
static public TPMIPacket buildDeviceDetectCmd() {
return buildCommand(TPMIPacket.Command.DEVICE_DETECT);
}
static public TPMIPacket buildGetDeviceInfoCmd() {return buildCommand(TPMIPacket.Command.GET_DEVICE_INFO);}
static public TPMIPacket buildPingCmd() {
return buildCommand(TPMIPacket.Command.PING);
}
static public TPMIPacket buildGetDeviceAppearanceCmd() {return buildCommand(TPMIPacket.Command.GET_DEVICE_APPEARANCE);}
static public TPMIPacket buildGetDeviceInfoCmd() {
return buildCommand(TPMIPacket.Command.GET_DEVICE_INFO);
}
static public TPMIPacket buildGetDeviceCalibrationInfoCmd() {return buildCommand(TPMIPacket.Command.GET_DEVICE_CALIBRATION_INFO);}
static public TPMIPacket buildGetDeviceAppearanceCmd() {
return buildCommand(TPMIPacket.Command.GET_DEVICE_APPEARANCE);
}
static public TPMIPacket buildUnlockEEPROMCmd() {return buildCommand(TPMIPacket.Command.UNLOCK_EERPOM);}
static public TPMIPacket buildGetStandbyInactiveTime() {
return buildCommand(TPMIPacket.Command.GET_STANDBY_INACTIVE_TIME);
}
static public TPMIPacket buildLockEEPROMCmd() {return buildCommand(TPMIPacket.Command.LOCK_EERPOM);}
;
static public TPMIPacket buildResetMCUCmd() {return buildCommand(TPMIPacket.Command.RESET_MCU);}
static public TPMIPacket buildGetDeviceCalibrationInfoCmd() {
return buildCommand(TPMIPacket.Command.GET_DEVICE_CALIBRATION_INFO);
}
static public TPMIPacket buildStartPosSampleCmd(Integer period) {return buildCommand(TPMIPacket.Command.START_POS_SAMPLE, period);}
static public TPMIPacket buildUnlockEEPROMCmd() {
return buildCommand(TPMIPacket.Command.UNLOCK_EERPOM);
}
static public TPMIPacket buildStopPosSampleCmd() {return buildCommand(TPMIPacket.Command.STOP_POS_SAMPLE);}
static public TPMIPacket buildLockEEPROMCmd() {
return buildCommand(TPMIPacket.Command.LOCK_EERPOM);
}
static public TPMIPacket buildResetMCUCmd() {
return buildCommand(TPMIPacket.Command.RESET_MCU);
}
// CALIBRATE_ENCODER1(18), // 校准编码器0位置
// CALIBRATE_ENCODER2(19), // 校准编码器1位置
// CALIBRATE_INCLINATOR_X(20), // 校准水平仪_X
// CALIBRATE_INCLINATOR_Y(21), // 校准水平仪_Y
// CALIBRATE_INCLINATOR_TEMPERATURE(22), // 校准水平仪温度传感器
static public TPMIPacket buildStartPosSampleCmd(Integer period) {
return buildCommand(TPMIPacket.Command.START_POS_SAMPLE, period);
}
// static public TPMIPacket buildCalibrateEncoderZeroPos() {return buildCommand(TPMIPacket.Command.CALIBRATE_ENCODER_ZERO_POS);}
// static public TPMIPacket buildCalibrateInclinatorZeroPos() {return buildCommand(TPMIPacket.Command.CALIBRATE_INCLINATOR_ZERO_POS);}
static public TPMIPacket buildStopPosSampleCmd() {
return buildCommand(TPMIPacket.Command.STOP_POS_SAMPLE);
}
static public TPMIPacket buildCalibrateEncoder1() {return buildCommand(TPMIPacket.Command.CALIBRATE_ENCODER1);}
static public TPMIPacket buildCalibrateEncoder2() {return buildCommand(TPMIPacket.Command.CALIBRATE_ENCODER2);}
// CALIBRATE_ENCODER1(18), // 校准编码器0位置
// CALIBRATE_ENCODER2(19), // 校准编码器1位置
// CALIBRATE_INCLINATOR_X(20), // 校准水平仪_X
// CALIBRATE_INCLINATOR_Y(21), // 校准水平仪_Y
// CALIBRATE_INCLINATOR_TEMPERATURE(22), // 校准水平仪温度传感器
static public TPMIPacket buildCalibrateInclinatorZeroPosX() {return buildCommand(TPMIPacket.Command.CALIBRATE_INCLINATOR_X);}
// static public TPMIPacket buildCalibrateEncoderZeroPos() {return buildCommand(TPMIPacket.Command.CALIBRATE_ENCODER_ZERO_POS);}
// static public TPMIPacket buildCalibrateInclinatorZeroPos() {return buildCommand(TPMIPacket.Command.CALIBRATE_INCLINATOR_ZERO_POS);}
static public TPMIPacket buildCalibrateInclinatorZeroPosY() {return buildCommand(TPMIPacket.Command.CALIBRATE_INCLINATOR_Y);}
static public TPMIPacket buildCalibrateEncoder1() {
return buildCommand(TPMIPacket.Command.CALIBRATE_ENCODER1);
}
static public TPMIPacket buildCalibrateInclinatorTemperature(Integer nowval) {return buildCommand(TPMIPacket.Command.CALIBRATE_INCLINATOR_TEMPERATURE, nowval);}
static public TPMIPacket buildCalibrateEncoder2() {
return buildCommand(TPMIPacket.Command.CALIBRATE_ENCODER2);
}
static public TPMIPacket buildCalibrateInclinatorZeroPosX() {
return buildCommand(TPMIPacket.Command.CALIBRATE_INCLINATOR_X);
}
static public TPMIPacket buildSetDeviceAppearanceCmd(Double pulleyRadius, Double profilometerHight, Double arm1Length, Double arm2Length) {
return buildCommand(TPMIPacket.Command.SET_DEVICE_APPEARANCE, (int) (1000 * pulleyRadius), (int) (1000 * profilometerHight), (int) (1000 * arm1Length), (int) (1000 * arm2Length));
}
static public TPMIPacket buildCalibrateInclinatorZeroPosY() {
return buildCommand(TPMIPacket.Command.CALIBRATE_INCLINATOR_Y);
}
static public TPMIPacket buildSetCalibrationTimeCmd(Integer year, Integer month, Integer day) {
return buildCommand(TPMIPacket.Command.SET_CALIBRATION_TIME, year, month, day);
}
// SET_ENCODER1_OFFSET(23), // 设置编码器1-零偏
// RESET_ENCODER1_OFFSET(24), // 重置编码器1-零偏
// SET_ENCODER2_OFFSET(25), // 设置编码器2-零偏
// RESET_ENCODER2_OFFSET(26), // 重置编码器2-零偏
static public TPMIPacket buildCalibrateInclinatorTemperature(Integer nowval) {
return buildCommand(TPMIPacket.Command.CALIBRATE_INCLINATOR_TEMPERATURE, nowval);
}
static public TPMIPacket buildSetStandbyInactiveCmd(int standbyInactiveTime) {
return buildCommand(TPMIPacket.Command.SET_STANDBY_INACTIVE_TIME, standbyInactiveTime);
}
static public TPMIPacket buildSetDeviceAppearanceCmd(Double pulleyRadius, Double profilometerHight, Double arm1Length, Double arm2Length) {
return buildCommand(TPMIPacket.Command.SET_DEVICE_APPEARANCE, (int) (1000 * pulleyRadius), (int) (1000 * profilometerHight), (int) (1000 * arm1Length), (int) (1000 * arm2Length));
}
static public TPMIPacket buildSetEncoder1ffsetCmd(Integer offset) {return buildCommand(TPMIPacket.Command.SET_ENCODER1_OFFSET, offset);}
static public TPMIPacket buildSetCalibrationTimeCmd(Integer year, Integer month, Integer day) {
return buildCommand(TPMIPacket.Command.SET_CALIBRATION_TIME, year, month, day);
}
// SET_ENCODER1_OFFSET(23), // 设置编码器1-零偏
// RESET_ENCODER1_OFFSET(24), // 重置编码器1-零偏
// SET_ENCODER2_OFFSET(25), // 设置编码器2-零偏
// RESET_ENCODER2_OFFSET(26), // 重置编码器2-零偏
static public TPMIPacket buildGetEncoder1ffsetCmd() {return buildCommand(TPMIPacket.Command.GET_ENCODER1_OFFSET);}
static public TPMIPacket buildSetEncoder1ffsetCmd(Integer offset) {
return buildCommand(TPMIPacket.Command.SET_ENCODER1_OFFSET, offset);
}
static public TPMIPacket buildGetEncoder1ffsetCmd() {
return buildCommand(TPMIPacket.Command.GET_ENCODER1_OFFSET);
}
static public TPMIPacket buildSetEncoder2ffsetCmd(Integer offset) {
return buildCommand(TPMIPacket.Command.SET_ENCODER2_OFFSET, offset);
}
static public TPMIPacket buildGetEncoder2ffsetCmd() {
return buildCommand(TPMIPacket.Command.GET_ENCODER2_OFFSET);
}
//
// PRIVETE
//
static public TPMIPacket buildSetEncoder2ffsetCmd(Integer offset) {return buildCommand(TPMIPacket.Command.SET_ENCODER2_OFFSET, offset);}
static public TPMIPacket buildGetEncoder2ffsetCmd() {return buildCommand(TPMIPacket.Command.GET_ENCODER2_OFFSET);}
//
// PRIVETE
//
public enum PacketAnalysisResult {
SUC,
TAIL_ERROR,
HEADER_ERROR,
CHECKSUM_ERROR,
PACKE_LEN_ERROR,
PACKE_NOT_FULLY_RECEIVED,
}
static public Byte Index = 0;
static public Byte reportIndex = 0;
static private synchronized Byte getIndex() {
Index++;
return Index;
}
static private synchronized Byte getReportIndex() {
reportIndex++;
return reportIndex;
}
static public TPMIPacket buildCommand(TPMIPacket.Command command, TPMIPacket.DataType dataType, byte[] data) {
byte[] rawpacket = new byte[data.length + TPMIPacket.FIX_PACKET_LEN];
rawpacket[TPMIPacket.P_HEADER_OFF] = TPMIPacket.P_HEADER;
rawpacket[TPMIPacket.P_DATA_LEN_OFF] = (byte) (data.length);
rawpacket[TPMIPacket.P_INDEX_OFF] = getIndex();
rawpacket[TPMIPacket.P_TYPE_OFF] = (byte) TPMIPacket.PacketType.REQUEST.ordinal();
rawpacket[TPMIPacket.P_CMD_OFF] = (byte) command.getIndex();
rawpacket[TPMIPacket.P_DATA_TYPE_OFF] = (byte) dataType.ordinal();
System.arraycopy(data, 0, rawpacket, TPMIPacket.P_DATA_OFF, data.length);
rawpacket[rawpacket.length - 1] = TPMIPacket.P_TAIL;
if (!dataType.equals(TPMIPacket.DataType.NONE)) {
System.arraycopy(data, 0, rawpacket, TPMIPacket.P_DATA_OFF, data.length);
}
byte checksum = 0;
for (int i = 0; i < rawpacket.length - 2; i++) {
checksum += rawpacket[i];
}
rawpacket[rawpacket.length - 2] = checksum;
return new TPMIPacket(rawpacket);
}
static public TPMIPacket buildReport(TPMIPacket.Command command, TPMIPacket.DataType dataType, byte[] data) {
byte[] rawpacket = new byte[data.length + TPMIPacket.FIX_PACKET_LEN];
rawpacket[TPMIPacket.P_HEADER_OFF] = TPMIPacket.P_HEADER;
rawpacket[TPMIPacket.P_DATA_LEN_OFF] = (byte) (data.length);
rawpacket[TPMIPacket.P_INDEX_OFF] = getReportIndex();
rawpacket[TPMIPacket.P_TYPE_OFF] = (byte) TPMIPacket.PacketType.REPORT.ordinal();
rawpacket[TPMIPacket.P_CMD_OFF] = (byte) command.ordinal();
rawpacket[TPMIPacket.P_DATA_TYPE_OFF] = (byte) dataType.ordinal();
System.arraycopy(data, 0, rawpacket, TPMIPacket.P_DATA_OFF, data.length);
rawpacket[rawpacket.length - 1] = TPMIPacket.P_TAIL;
if (!dataType.equals(TPMIPacket.DataType.NONE)) {
System.arraycopy(data, 0, rawpacket, TPMIPacket.P_DATA_OFF, data.length);
}
byte checksum = 0;
for (int i = 0; i < rawpacket.length - 2; i++) {
checksum += rawpacket[i];
}
rawpacket[rawpacket.length - 2] = checksum;
return new TPMIPacket(rawpacket);
}
static public TPMIPacket buildReceipt(TPMIPacket rx, TPMIPacket.DataType dataType, byte[] data) {
byte[] rawpacket = new byte[data.length + TPMIPacket.FIX_PACKET_LEN];
rawpacket[TPMIPacket.P_HEADER_OFF] = TPMIPacket.P_HEADER;
rawpacket[TPMIPacket.P_DATA_LEN_OFF] = (byte) (data.length);
rawpacket[TPMIPacket.P_INDEX_OFF] = rx.getIndex();
rawpacket[TPMIPacket.P_TYPE_OFF] = (byte) TPMIPacket.PacketType.RESPONSE.ordinal();
rawpacket[TPMIPacket.P_CMD_OFF] = (byte) rx.getCommand().getIndex();
rawpacket[TPMIPacket.P_DATA_TYPE_OFF] = (byte) dataType.ordinal();
System.arraycopy(data, 0, rawpacket, TPMIPacket.P_DATA_OFF, data.length);
rawpacket[rawpacket.length - 1] = TPMIPacket.P_TAIL;
if (!dataType.equals(TPMIPacket.DataType.NONE)) {
System.arraycopy(data, 0, rawpacket, 6, data.length);
}
byte checksum = 0;
for (int i = 0; i < rawpacket.length - 2; i++) {
checksum += rawpacket[i];
}
rawpacket[rawpacket.length - 2] = checksum;
return new TPMIPacket(rawpacket);
}
static private TPMIPacket buildCommand(TPMIPacket.Command command, Integer... data) {
byte[] intdata = new byte[data.length * 4];
for (int i = 0; i < data.length; i++) {
intdata[i * 4 + 3] = (byte) (data[i] >> 24);
intdata[i * 4 + 2] = (byte) (data[i] >> 16);
intdata[i * 4 + 1] = (byte) (data[i] >> 8);
intdata[i * 4 + 0] = (byte) (data[i].intValue());
}
return buildCommand(command, TPMIPacket.DataType.INT, intdata);
}
static private TPMIPacket buildCommand(TPMIPacket.Command command, byte[] rawdata) {
return buildCommand(command, TPMIPacket.DataType.BYTE, rawdata);
}
static private TPMIPacket buildCommand(TPMIPacket.Command command) {
return buildCommand(command, TPMIPacket.DataType.NONE, new byte[0]);
}
static private TPMIPacket buildCommand(TPMIPacket.Command command, String data) {
return buildCommand(command, TPMIPacket.DataType.STR, data.getBytes());
}
static public TPMIPacket buildSetSNCmd(Integer year, Integer week, Integer index) {
DeviceSN sn = new DeviceSN(year, week, index);
return buildCommand(TPMIPacket.Command.SET_SN, sn.toInteger());
}
static public TPMIPacket buildReport(TPMIPacket.Command command, String data) {
return buildReport(command, TPMIPacket.DataType.STR, data.getBytes());
}
static public TPMIPacket buildReport(TPMIPacket.Command command, byte[] rawdata) {
return buildReport(command, TPMIPacket.DataType.BYTE, rawdata);
}
static public TPMIPacket buildReport(TPMIPacket.Command command, Integer... data) {
byte[] intdata = new byte[data.length * 4];
for (int i = 0; i < data.length; i++) {
intdata[i * 4 + 3] = (byte) (data[i] >> 24);
intdata[i * 4 + 2] = (byte) (data[i] >> 16);
intdata[i * 4 + 1] = (byte) (data[i] >> 8);
intdata[i * 4] = (byte) (data[i].intValue());
}
return buildReport(command, TPMIPacket.DataType.INT, intdata);
}
static public TPMIPacket buildReport(TPMIPacket.Command command) {
return buildReport(command, TPMIPacket.DataType.NONE, new byte[0]);
}
static public TPMIPacket buildReceipt(TPMIPacket rx, String data) {
return buildReceipt(rx, TPMIPacket.DataType.STR, data.getBytes());
}
static public TPMIPacket buildReceipt(TPMIPacket rx, byte[] rawdata) {
return buildReceipt(rx, TPMIPacket.DataType.BYTE, rawdata);
}
static public TPMIPacket buildReceipt(TPMIPacket rx, Integer... data) {
return buildReceipt(rx, TPMIPacket.DataType.INT, ZByteUtils.integerToLittleEndianByte(data));
}
static public TPMIPacket buildReceipt(TPMIPacket rx) {
return buildReceipt(rx, TPMIPacket.DataType.NONE, new byte[0]);
}
synchronized static public TPMIPacket parseRawRxPacketToResponse(byte[] rawpacket, int length) {
// SubDevicePacketUtils
if (isValidatePacket(rawpacket, length, 0).equals(PacketAnalysisResult.SUC)) {
var newPacket = new TPMIPacket(rawpacket, 0, length);
if (newPacket.getPacketType().equals(TPMIPacket.PacketType.RESPONSE)) {
return newPacket;
}
}
return null;
}
public static class PacketAnalysisReport {
public PacketAnalysisResult result; // 分析结果
public TPMIPacket packet; // 分析出的数据包
public byte[] rawdata; // 分析出的数据包
}
/**
* 解析接收到的数据包
* @param indata 接收到的数据包
* @param indatalen 接收到数据总长
* @param startoff 分析的起始位置
* @return 分析结果
*/
public static PacketAnalysisReport analysisRawRxData(byte[] indata, int indatalen, int startoff) {
PacketAnalysisReport report = new PacketAnalysisReport();
report.result = isValidatePacket(indata, indatalen, startoff);
if (report.result.equals(PacketAnalysisResult.SUC)) {
byte packetLen = (byte) (indata[startoff + TPMIPacket.P_DATA_LEN_OFF] + TPMIPacket.FIX_PACKET_LEN);
report.rawdata = ZByteUtils.subBytes(indata, startoff, packetLen);
TPMIPacket.Command cmd = TPMIPacket.getCommand(report.rawdata);
switch (cmd) {
case SAMPLE_VAL_REPORT:
report.packet = new TPMIPosReport(report.rawdata);
break;
case BASIC_REPORT:
report.packet = new TPMIBasicReport(report.rawdata);
break;
default:
report.packet = new TPMIPacket(report.rawdata);
break;
}
} else if (report.result.equals(PacketAnalysisResult.CHECKSUM_ERROR)) {
byte packetLen = (byte) (indata[startoff + TPMIPacket.P_DATA_LEN_OFF] + TPMIPacket.FIX_PACKET_LEN);
report.rawdata = ZByteUtils.subBytes(indata, startoff, packetLen);
}
return report;
}
public static PacketAnalysisResult isValidatePacket(byte[] indata, int indatalen, int startoff) {
if (indata[startoff] != TPMIPacket.P_HEADER) {
return PacketAnalysisResult.HEADER_ERROR;
}
if ((indatalen - startoff) < TPMIPacket.FIX_PACKET_LEN) {
return PacketAnalysisResult.PACKE_NOT_FULLY_RECEIVED;
}
int packetLen = indata[startoff + TPMIPacket.P_DATA_LEN_OFF] + TPMIPacket.FIX_PACKET_LEN;
if (packetLen > (indatalen - startoff)) {
return PacketAnalysisResult.PACKE_NOT_FULLY_RECEIVED;
}
byte tail = indata[startoff + packetLen - 1];
if (tail != TPMIPacket.P_TAIL) {
return PacketAnalysisResult.TAIL_ERROR;
}
if (packetLen < TPMIPacket.FIX_PACKET_LEN) {
return PacketAnalysisResult.PACKE_LEN_ERROR;
}
byte checksum = 0;
for (int i = 0; i < packetLen - 2; i++) {
if (i == (packetLen - 2)) {
continue;
}
checksum += indata[startoff + i];
}
if (checksum != indata[startoff + packetLen - 2]) {
return PacketAnalysisResult.CHECKSUM_ERROR;
}
return PacketAnalysisResult.SUC;
}
public enum PacketAnalysisResult {
SUC,
TAIL_ERROR,
HEADER_ERROR,
CHECKSUM_ERROR,
PACKE_LEN_ERROR,
PACKE_NOT_FULLY_RECEIVED,
}
static public Byte Index = 0;
static public Byte reportIndex = 0;
static private synchronized Byte getIndex() {
Index++;
return Index;
}
static private synchronized Byte getReportIndex() {
reportIndex++;
return reportIndex;
}
static public TPMIPacket buildCommand(TPMIPacket.Command command, TPMIPacket.DataType dataType, byte[] data) {
byte[] rawpacket = new byte[data.length + TPMIPacket.FIX_PACKET_LEN];
rawpacket[TPMIPacket.P_HEADER_OFF] = TPMIPacket.P_HEADER;
rawpacket[TPMIPacket.P_DATA_LEN_OFF] = (byte) (data.length);
rawpacket[TPMIPacket.P_INDEX_OFF] = getIndex();
rawpacket[TPMIPacket.P_TYPE_OFF] = (byte) TPMIPacket.PacketType.REQUEST.ordinal();
rawpacket[TPMIPacket.P_CMD_OFF] = (byte) command.getIndex();
rawpacket[TPMIPacket.P_DATA_TYPE_OFF] = (byte) dataType.ordinal();
System.arraycopy(data, 0, rawpacket, TPMIPacket.P_DATA_OFF, data.length);
rawpacket[rawpacket.length - 1] = TPMIPacket.P_TAIL;
if (!dataType.equals(TPMIPacket.DataType.NONE)) {
System.arraycopy(data, 0, rawpacket, TPMIPacket.P_DATA_OFF, data.length);
}
byte checksum = 0;
for (int i = 0; i < rawpacket.length - 2; i++) {
checksum += rawpacket[i];
}
rawpacket[rawpacket.length - 2] = checksum;
return new TPMIPacket(rawpacket);
}
static public TPMIPacket buildReport(TPMIPacket.Command command, TPMIPacket.DataType dataType, byte[] data) {
byte[] rawpacket = new byte[data.length + TPMIPacket.FIX_PACKET_LEN];
rawpacket[TPMIPacket.P_HEADER_OFF] = TPMIPacket.P_HEADER;
rawpacket[TPMIPacket.P_DATA_LEN_OFF] = (byte) (data.length);
rawpacket[TPMIPacket.P_INDEX_OFF] = getReportIndex();
rawpacket[TPMIPacket.P_TYPE_OFF] = (byte) TPMIPacket.PacketType.REPORT.ordinal();
rawpacket[TPMIPacket.P_CMD_OFF] = (byte) command.ordinal();
rawpacket[TPMIPacket.P_DATA_TYPE_OFF] = (byte) dataType.ordinal();
System.arraycopy(data, 0, rawpacket, TPMIPacket.P_DATA_OFF, data.length);
rawpacket[rawpacket.length - 1] = TPMIPacket.P_TAIL;
if (!dataType.equals(TPMIPacket.DataType.NONE)) {
System.arraycopy(data, 0, rawpacket, TPMIPacket.P_DATA_OFF, data.length);
}
byte checksum = 0;
for (int i = 0; i < rawpacket.length - 2; i++) {
checksum += rawpacket[i];
}
rawpacket[rawpacket.length - 2] = checksum;
return new TPMIPacket(rawpacket);
}
static public TPMIPacket buildReceipt(TPMIPacket rx, TPMIPacket.DataType dataType, byte[] data) {
byte[] rawpacket = new byte[data.length + TPMIPacket.FIX_PACKET_LEN];
rawpacket[TPMIPacket.P_HEADER_OFF] = TPMIPacket.P_HEADER;
rawpacket[TPMIPacket.P_DATA_LEN_OFF] = (byte) (data.length);
rawpacket[TPMIPacket.P_INDEX_OFF] = rx.getIndex();
rawpacket[TPMIPacket.P_TYPE_OFF] = (byte) TPMIPacket.PacketType.RESPONSE.ordinal();
rawpacket[TPMIPacket.P_CMD_OFF] = (byte) rx.getCommand().getIndex();
rawpacket[TPMIPacket.P_DATA_TYPE_OFF] = (byte) dataType.ordinal();
System.arraycopy(data, 0, rawpacket, TPMIPacket.P_DATA_OFF, data.length);
rawpacket[rawpacket.length - 1] = TPMIPacket.P_TAIL;
if (!dataType.equals(TPMIPacket.DataType.NONE)) {
System.arraycopy(data, 0, rawpacket, 6, data.length);
}
byte checksum = 0;
for (int i = 0; i < rawpacket.length - 2; i++) {
checksum += rawpacket[i];
}
rawpacket[rawpacket.length - 2] = checksum;
return new TPMIPacket(rawpacket);
}
static private TPMIPacket buildCommand(TPMIPacket.Command command, Integer... data) {
byte[] intdata = new byte[data.length * 4];
for (int i = 0; i < data.length; i++) {
intdata[i * 4 + 3] = (byte) (data[i] >> 24);
intdata[i * 4 + 2] = (byte) (data[i] >> 16);
intdata[i * 4 + 1] = (byte) (data[i] >> 8);
intdata[i * 4 + 0] = (byte) (data[i].intValue());
}
return buildCommand(command, TPMIPacket.DataType.INT, intdata);
}
static private TPMIPacket buildCommand(TPMIPacket.Command command, byte[] rawdata) {
return buildCommand(command, TPMIPacket.DataType.BYTE, rawdata);
}
static private TPMIPacket buildCommand(TPMIPacket.Command command) {
return buildCommand(command, TPMIPacket.DataType.NONE, new byte[0]);
}
static private TPMIPacket buildCommand(TPMIPacket.Command command, String data) {
return buildCommand(command, TPMIPacket.DataType.STR, data.getBytes());
}
static public TPMIPacket buildSetSNCmd(Integer year, Integer week, Integer index) {
DeviceSN sn = new DeviceSN(year, week, index);
return buildCommand(TPMIPacket.Command.SET_SN, sn.toInteger());
}
static public TPMIPacket buildReport(TPMIPacket.Command command, String data) {
return buildReport(command, TPMIPacket.DataType.STR, data.getBytes());
}
static public TPMIPacket buildReport(TPMIPacket.Command command, byte[] rawdata) {
return buildReport(command, TPMIPacket.DataType.BYTE, rawdata);
}
static public TPMIPacket buildReport(TPMIPacket.Command command, Integer... data) {
byte[] intdata = new byte[data.length * 4];
for (int i = 0; i < data.length; i++) {
intdata[i * 4 + 3] = (byte) (data[i] >> 24);
intdata[i * 4 + 2] = (byte) (data[i] >> 16);
intdata[i * 4 + 1] = (byte) (data[i] >> 8);
intdata[i * 4] = (byte) (data[i].intValue());
}
return buildReport(command, TPMIPacket.DataType.INT, intdata);
}
static public TPMIPacket buildReport(TPMIPacket.Command command) {
return buildReport(command, TPMIPacket.DataType.NONE, new byte[0]);
}
static public TPMIPacket buildReceipt(TPMIPacket rx, String data) {
return buildReceipt(rx, TPMIPacket.DataType.STR, data.getBytes());
}
static public TPMIPacket buildReceipt(TPMIPacket rx, byte[] rawdata) {
return buildReceipt(rx, TPMIPacket.DataType.BYTE, rawdata);
}
static public TPMIPacket buildReceipt(TPMIPacket rx, Integer... data) {
return buildReceipt(rx, TPMIPacket.DataType.INT, ZByteUtils.integerToLittleEndianByte(data));
}
static public TPMIPacket buildReceipt(TPMIPacket rx) {
return buildReceipt(rx, TPMIPacket.DataType.NONE, new byte[0]);
}
synchronized static public TPMIPacket parseRawRxPacketToResponse(byte[] rawpacket, int length) {
// SubDevicePacketUtils
if (isValidatePacket(rawpacket, length, 0).equals(PacketAnalysisResult.SUC)) {
var newPacket = new TPMIPacket(rawpacket, 0, length);
if (newPacket.getPacketType().equals(TPMIPacket.PacketType.RESPONSE)) {
return newPacket;
}
}
return null;
}
public static class PacketAnalysisReport {
public PacketAnalysisResult result; // 分析结果
public TPMIPacket packet; // 分析出的数据包
public byte[] rawdata; // 分析出的数据包
}
/**
* 解析接收到的数据包
*
* @param indata 接收到的数据包
* @param indatalen 接收到数据总长
* @param startoff 分析的起始位置
* @return 分析结果
*/
public static PacketAnalysisReport analysisRawRxData(byte[] indata, int indatalen, int startoff) {
PacketAnalysisReport report = new PacketAnalysisReport();
report.result = isValidatePacket(indata, indatalen, startoff);
if (report.result.equals(PacketAnalysisResult.SUC)) {
byte packetLen = (byte) (indata[startoff + TPMIPacket.P_DATA_LEN_OFF] + TPMIPacket.FIX_PACKET_LEN);
report.rawdata = ZByteUtils.subBytes(indata, startoff, packetLen);
TPMIPacket.Command cmd = TPMIPacket.getCommand(report.rawdata);
switch (cmd) {
case SAMPLE_VAL_REPORT:
report.packet = new TPMIPosReport(report.rawdata);
break;
case BASIC_REPORT:
report.packet = new TPMIBasicReport(report.rawdata);
break;
default:
report.packet = new TPMIPacket(report.rawdata);
break;
}
} else if (report.result.equals(PacketAnalysisResult.CHECKSUM_ERROR)) {
byte packetLen = (byte) (indata[startoff + TPMIPacket.P_DATA_LEN_OFF] + TPMIPacket.FIX_PACKET_LEN);
report.rawdata = ZByteUtils.subBytes(indata, startoff, packetLen);
}
return report;
}
public static PacketAnalysisResult isValidatePacket(byte[] indata, int indatalen, int startoff) {
if (indata[startoff] != TPMIPacket.P_HEADER) {
return PacketAnalysisResult.HEADER_ERROR;
}
if ((indatalen - startoff) < TPMIPacket.FIX_PACKET_LEN) {
return PacketAnalysisResult.PACKE_NOT_FULLY_RECEIVED;
}
int packetLen = indata[startoff + TPMIPacket.P_DATA_LEN_OFF] + TPMIPacket.FIX_PACKET_LEN;
if (packetLen > (indatalen - startoff)) {
return PacketAnalysisResult.PACKE_NOT_FULLY_RECEIVED;
}
byte tail = indata[startoff + packetLen - 1];
if (tail != TPMIPacket.P_TAIL) {
return PacketAnalysisResult.TAIL_ERROR;
}
if (packetLen < TPMIPacket.FIX_PACKET_LEN) {
return PacketAnalysisResult.PACKE_LEN_ERROR;
}
byte checksum = 0;
for (int i = 0; i < packetLen - 2; i++) {
if (i == (packetLen - 2)) {
continue;
}
checksum += indata[startoff + i];
}
if (checksum != indata[startoff + packetLen - 2]) {
return PacketAnalysisResult.CHECKSUM_ERROR;
}
return PacketAnalysisResult.SUC;
}
}

505
app/src/main/java/com/iflytop/profilometer/core/migration/type/protocol/TPMIPacket.java

@ -9,256 +9,261 @@ import com.iflytop.profilometer.core.migration.factory.TPMIPacketFactory;
import com.iflytop.profilometer.core.migration.utils.ZByteUtils;
public class TPMIPacket {
public enum Command {
/**
* @brief
*
*
* DEVICE_DETECT, 设备探测
* Parameters : void
* Receipt(int32_t) : sn
*
* PING,PING
* Parameters : void
* Receipt(void) : void
*
* GET_DEVICE_INFO,获取设备信息
* Parameters : void
* Receipt(int32_t) : sn, sfw_version, hfw_version
*
* GET_DEVICE_APPEARANCE,获取设备信息
* Parameters : void
* Receipt(int32_t) : pulleyRadius, profilometerHight, arm0Length, arm1Length
*
* GET_DEVICE_CALIBRATION_INFO,获取设备信息
* Parameters : void
* Receipt(int32_t) : calibration_year, calibration_mouth, calibration_day
*
* UNLOCK_EERPOM,解锁EEPROM
* Parameters : void
* Receipt(void) : void
*
* LOCK_EERPOM,锁定EEPROM
* Parameters : void
* Receipt(void) : void
*
* RESET_MCU,复位MCU
* Parameters : void
* Receipt(void) : void
*
* START_POS_SAMPLE,开始采样
* Parameters : int32_t report_period
* Receipt(void) : void
*
* STOP_POS_SAMPLE,停止采样
* Parameters : void
* Receipt(void) : void
*
* SAMPLE_VAL_REPORT,采样上报
* Report(int32_t) : armEcoder0, armEcoder1
*
* BASIC_REPORT,基础状态上报
* Report(int32_t) : power, state, flag, inclinatorX, inclinatorY, temperature
*
* CALIBRATE_ENCODER1,校准编码器0位置
* (void)->(void)
*
* CALIBRATE_ENCODER2,校准编码器1位置
* (void)->(void)
*
* CALIBRATE_INCLINATOR_X,校准水平仪_X
* (void)->(void)
*
* CALIBRATE_INCLINATOR_Y,校准水平仪_Y
* (void)->(void)
*
* CALIBRATE_INCLINATOR_TEMPERATURE,校准水平仪温度传感器
* Parameters : int32_t realTemperature
*
*
* SET_DEVICE_APPEARANCE,设置设备信息
* Parameters : pulley_radius, profilometer_hight, arm0Length, arm1Length
*
* SET_CALIBRATION_TIME,设置设备信息
* Parameters : calibrate_year, calibrate_mouth, calibrate_day
*
* SET_SN,设置SN
* Parameters : SN
*
*/
DEVICE_DETECT(0), // 设备探测
PING(1), // PING
GET_DEVICE_INFO(2), // 获取设备信息
GET_DEVICE_APPEARANCE(3), // 获取设备外形信息
GET_DEVICE_CALIBRATION_INFO(4), // 获取设备校验时间
UNLOCK_EERPOM(7), // 解锁EEPROM
LOCK_EERPOM(8), // 锁定EEPROM
RESET_MCU(9), // 复位MCU
START_POS_SAMPLE(10), // 开始采样
STOP_POS_SAMPLE(11), // 停止采样
SAMPLE_VAL_REPORT(12), // 采样上报
BASIC_REPORT(15), // 基础状态上报
CALIBRATE_ENCODER1(18), // 校准编码器0位置
CALIBRATE_ENCODER2(19), // 校准编码器1位置
CALIBRATE_INCLINATOR_X(20), // 校准水平仪_X
CALIBRATE_INCLINATOR_Y(21), // 校准水平仪_Y
CALIBRATE_INCLINATOR_TEMPERATURE(22), // 校准水平仪温度传感器
SET_ENCODER1_OFFSET(23), // 设置编码器1-零偏
GET_ENCODER1_OFFSET(24), // 获得编码器1-零偏
SET_ENCODER2_OFFSET(25), // 设置编码器2-零偏
GET_ENCODER2_OFFSET(26), // 获得编码器2-零偏
SET_DEVICE_APPEARANCE(50), // 设置设备外形信息
SET_CALIBRATION_TIME(51), // 设置校验时间
SET_SN(52); // 设置SN
byte index = 0;
Command(Integer index) {
this.index = index.byteValue();
}
public byte getIndex() {
return index;
}
public static Command of(byte index) {
for (Command command : Command.values()) {
if (command.index == index) {
return command;
}
}
public enum Command {
/**
* @brief DEVICE_DETECT, 设备探测
* Parameters : void
* Receipt(int32_t) : sn
* <p>
* PING,PING
* Parameters : void
* Receipt(void) : void
* <p>
* GET_DEVICE_INFO,获取设备信息
* Parameters : void
* Receipt(int32_t) : sn, sfw_version, hfw_version
* <p>
* GET_DEVICE_APPEARANCE,获取设备信息
* Parameters : void
* Receipt(int32_t) : pulleyRadius, profilometerHight, arm0Length, arm1Length
* <p>
* GET_STANDBY_INACTIVE_TIME,获取无操作进入待机时间
* Parameters : void
* Receipt(int32_t) : standbyInactiveTime
* <p>
* GET_DEVICE_CALIBRATION_INFO,获取设备信息
* Parameters : void
* Receipt(int32_t) : calibration_year, calibration_mouth, calibration_day
* <p>
* UNLOCK_EERPOM,解锁EEPROM
* Parameters : void
* Receipt(void) : void
* <p>
* LOCK_EERPOM,锁定EEPROM
* Parameters : void
* Receipt(void) : void
* <p>
* RESET_MCU,复位MCU
* Parameters : void
* Receipt(void) : void
* <p>
* START_POS_SAMPLE,开始采样
* Parameters : int32_t report_period
* Receipt(void) : void
* <p>
* STOP_POS_SAMPLE,停止采样
* Parameters : void
* Receipt(void) : void
* <p>
* SAMPLE_VAL_REPORT,采样上报
* Report(int32_t) : armEcoder0, armEcoder1
* <p>
* BASIC_REPORT,基础状态上报
* Report(int32_t) : power, state, flag, inclinatorX, inclinatorY, temperature
* <p>
* CALIBRATE_ENCODER1,校准编码器0位置
* (void)->(void)
* <p>
* CALIBRATE_ENCODER2,校准编码器1位置
* (void)->(void)
* <p>
* CALIBRATE_INCLINATOR_X,校准水平仪_X
* (void)->(void)
* <p>
* CALIBRATE_INCLINATOR_Y,校准水平仪_Y
* (void)->(void)
* <p>
* CALIBRATE_INCLINATOR_TEMPERATURE,校准水平仪温度传感器
* Parameters : int32_t realTemperature
* <p>
* <p>
* SET_DEVICE_APPEARANCE,设置设备信息
* Parameters : pulley_radius, profilometer_hight, arm0Length, arm1Length
* <p>
* SET_CALIBRATION_TIME,设置设备信息
* Parameters : calibrate_year, calibrate_mouth, calibrate_day
* <p>
* SET_SN,设置SN
* Parameters : SN
* <p>
* SET_STANDBY_INACTIVE_TIME,设置无操作进入待机时间
* Parameters : standbyInactiveTime
*/
DEVICE_DETECT(0), // 设备探测
PING(1), // PING
GET_DEVICE_INFO(2), // 获取设备信息
GET_DEVICE_APPEARANCE(3), // 获取设备外形信息
GET_STANDBY_INACTIVE_TIME(5),
GET_DEVICE_CALIBRATION_INFO(4), // 获取设备校验时间
UNLOCK_EERPOM(7), // 解锁EEPROM
LOCK_EERPOM(8), // 锁定EEPROM
RESET_MCU(9), // 复位MCU
START_POS_SAMPLE(10), // 开始采样
STOP_POS_SAMPLE(11), // 停止采样
SAMPLE_VAL_REPORT(12), // 采样上报
BASIC_REPORT(15), // 基础状态上报
CALIBRATE_ENCODER1(18), // 校准编码器0位置
CALIBRATE_ENCODER2(19), // 校准编码器1位置
CALIBRATE_INCLINATOR_X(20), // 校准水平仪_X
CALIBRATE_INCLINATOR_Y(21), // 校准水平仪_Y
CALIBRATE_INCLINATOR_TEMPERATURE(22), // 校准水平仪温度传感器
SET_ENCODER1_OFFSET(23), // 设置编码器1-零偏
GET_ENCODER1_OFFSET(24), // 获得编码器1-零偏
SET_ENCODER2_OFFSET(25), // 设置编码器2-零偏
GET_ENCODER2_OFFSET(26), // 获得编码器2-零偏
SET_DEVICE_APPEARANCE(50), // 设置设备外形信息
SET_CALIBRATION_TIME(51), // 设置校验时间
SET_SN(52),// 设置SN
SET_STANDBY_INACTIVE_TIME(53); // 设置无操作进入待机时间
byte index = 0;
Command(Integer index) {
this.index = index.byteValue();
}
public byte getIndex() {
return index;
}
public static Command of(byte index) {
for (Command command : Command.values()) {
if (command.index == index) {
return command;
}
}
// log.error("Unknown Command index: {}", index);
return null;
}
}
public enum PacketType {
REQUEST, //0
RESPONSE,//1
REPORT //2
}
public enum DataType {
STR,//0
BYTE, //1
INT, //2
ERROR_CODE, //3
NONE, //4
}
;
/*
*
* 均为有符号数
*
* FIX LEN = 7
*
* HEADER: 0x5A
* TAIL : 0xA6
* LEN :
* CMD :
*
* 0 1 2 3 4 5 n 6 7
* HEADDER PACKET_TYPE CMD INDEX DATA_TYPE DATA_LEN CHECKSUM TAIL
*/
public static byte FIX_PACKET_LEN = 8;
public static byte P_HEADER = 90; //5A
public static byte P_TAIL = -90;//A6
public static byte P_HEADER_OFF = 0;
public static byte P_TYPE_OFF = 1;
public static byte P_CMD_OFF = 2;
public static byte P_INDEX_OFF = 3;
public static byte P_DATA_TYPE_OFF = 4;
public static byte P_DATA_LEN_OFF = 5;
public static byte P_DATA_OFF = 6;
public byte[] rawpacket;
public TPMIPacket(byte[] rawpacket) {
this.rawpacket = new byte[rawpacket.length];
System.arraycopy(rawpacket, 0, this.rawpacket, 0, rawpacket.length);
}
public TPMIPacket(byte[] rawpacket, int off, int len) {
this.rawpacket = new byte[len];
System.arraycopy(rawpacket, off, this.rawpacket, 0, len);
}
public byte getPacketLen() {
return (byte) (rawpacket[P_DATA_LEN_OFF] + FIX_PACKET_LEN);
}
public byte getIndex() {
return rawpacket[P_INDEX_OFF];
}
public PacketType getPacketType() {
return PacketType.values()[rawpacket[P_TYPE_OFF]];
}
public Command getCommand() {
return Command.of(rawpacket[P_CMD_OFF]);
}
static public Command getCommand(byte[] raw) {
return Command.of(raw[P_CMD_OFF]);
}
public DataType getDataType() {
return DataType.values()[rawpacket[P_DATA_TYPE_OFF]];
}
public String getPayloadAsString() {
return new String(rawpacket, 6, rawpacket.length - FIX_PACKET_LEN);
}
public Integer getDataAsInt(Integer interOff) {
int val = 0;
val |= (rawpacket[P_DATA_OFF + interOff * 4 + 3] << 24) & 0xff000000;
val |= (rawpacket[P_DATA_OFF + interOff * 4 + 2] << 16) & 0x00ff0000;
val |= (rawpacket[P_DATA_OFF + interOff * 4 + 1] << 8) & 0x0000ff00;
val |= (rawpacket[P_DATA_OFF + interOff * 4 + 0]) & 0x000000FF;
return val;
}
public Byte getDataAsByte(Integer off) {
return rawpacket[P_DATA_OFF + off];
}
public byte[] getData() {
byte[] data = new byte[rawpacket.length - FIX_PACKET_LEN];
System.arraycopy(rawpacket, P_DATA_OFF, data, 0, data.length);
return data;
}
@NonNull
@SuppressLint("DefaultLocale")
return null;
}
}
public enum PacketType {
REQUEST, //0
RESPONSE,//1
REPORT //2
}
public enum DataType {
STR,//0
BYTE, //1
INT, //2
ERROR_CODE, //3
NONE, //4
}
;
/*
*
* 均为有符号数
*
* FIX LEN = 7
*
* HEADER: 0x5A
* TAIL : 0xA6
* LEN :
* CMD :
*
* 0 1 2 3 4 5 n 6 7
* HEADDER PACKET_TYPE CMD INDEX DATA_TYPE DATA_LEN CHECKSUM TAIL
*/
public static byte FIX_PACKET_LEN = 8;
public static byte P_HEADER = 90; //5A
public static byte P_TAIL = -90;//A6
public static byte P_HEADER_OFF = 0;
public static byte P_TYPE_OFF = 1;
public static byte P_CMD_OFF = 2;
public static byte P_INDEX_OFF = 3;
public static byte P_DATA_TYPE_OFF = 4;
public static byte P_DATA_LEN_OFF = 5;
public static byte P_DATA_OFF = 6;
public byte[] rawpacket;
public TPMIPacket(byte[] rawpacket) {
this.rawpacket = new byte[rawpacket.length];
System.arraycopy(rawpacket, 0, this.rawpacket, 0, rawpacket.length);
}
public TPMIPacket(byte[] rawpacket, int off, int len) {
this.rawpacket = new byte[len];
System.arraycopy(rawpacket, off, this.rawpacket, 0, len);
}
public byte getPacketLen() {
return (byte) (rawpacket[P_DATA_LEN_OFF] + FIX_PACKET_LEN);
}
public byte getIndex() {
return rawpacket[P_INDEX_OFF];
}
public PacketType getPacketType() {
return PacketType.values()[rawpacket[P_TYPE_OFF]];
}
public Command getCommand() {
return Command.of(rawpacket[P_CMD_OFF]);
}
static public Command getCommand(byte[] raw) {
return Command.of(raw[P_CMD_OFF]);
}
public DataType getDataType() {
return DataType.values()[rawpacket[P_DATA_TYPE_OFF]];
}
public String getPayloadAsString() {
return new String(rawpacket, 6, rawpacket.length - FIX_PACKET_LEN);
}
public Integer getDataAsInt(Integer interOff) {
int val = 0;
val |= (rawpacket[P_DATA_OFF + interOff * 4 + 3] << 24) & 0xff000000;
val |= (rawpacket[P_DATA_OFF + interOff * 4 + 2] << 16) & 0x00ff0000;
val |= (rawpacket[P_DATA_OFF + interOff * 4 + 1] << 8) & 0x0000ff00;
val |= (rawpacket[P_DATA_OFF + interOff * 4 + 0]) & 0x000000FF;
return val;
}
public Byte getDataAsByte(Integer off) {
return rawpacket[P_DATA_OFF + off];
}
public byte[] getData() {
byte[] data = new byte[rawpacket.length - FIX_PACKET_LEN];
System.arraycopy(rawpacket, P_DATA_OFF, data, 0, data.length);
return data;
}
@NonNull
@SuppressLint("DefaultLocale")
public String toString() {
return String.format("[INDEX:%03d CMD: %-35s,%s]", getIndex(), getCommand(), ZByteUtils.bytesToHexString(rawpacket, rawpacket.length));
// return String.format("H:%02X L:%02X I:%02X T:%02X C:%02X DT:%02X %s C:%02X END:%02X",
// rawpacket[P_HEADER_OFF], rawpacket[P_LEN_OFF], rawpacket[P_INDEX_OFF], rawpacket[P_TYPE_OFF], rawpacket[P_CMD_OFF], rawpacket[P_DATA_TYPE_OFF],
// getDataAsString(), rawpacket[rawpacket.length - 2], rawpacket[rawpacket.length - 1]);
}
public static void main(String[] args) {
byte[] packet = new byte[]{P_HEADER, 0x08, 0x00, 0x00, 0x00, 0x00, 0x08, P_TAIL};
System.out.println(TPMIPacketFactory.isValidatePacket(packet, 0, packet.length));
}
return String.format("[INDEX:%03d CMD: %-35s,%s]", getIndex(), getCommand(), ZByteUtils.bytesToHexString(rawpacket, rawpacket.length));
// return String.format("H:%02X L:%02X I:%02X T:%02X C:%02X DT:%02X %s C:%02X END:%02X",
// rawpacket[P_HEADER_OFF], rawpacket[P_LEN_OFF], rawpacket[P_INDEX_OFF], rawpacket[P_TYPE_OFF], rawpacket[P_CMD_OFF], rawpacket[P_DATA_TYPE_OFF],
// getDataAsString(), rawpacket[rawpacket.length - 2], rawpacket[rawpacket.length - 1]);
}
public static void main(String[] args) {
byte[] packet = new byte[]{P_HEADER, 0x08, 0x00, 0x00, 0x00, 0x00, 0x08, P_TAIL};
System.out.println(TPMIPacketFactory.isValidatePacket(packet, 0, packet.length));
}
}

1
app/src/main/java/com/iflytop/profilometer/model/bo/RailProfileMeasureTaskState.java

@ -54,6 +54,7 @@ public class RailProfileMeasureTaskState {
*/
public void reset() {
this.leftPointSetList.clear();
this.rightPointSetList.clear();
this.taskStatus = RailProfileMeasureTaskStatus.IDLE;
this.measureSideCnt = 0;
this.isMeasuringLeftEnd = false;

9
app/src/main/java/com/iflytop/profilometer/model/vo/SystemConfigVO.java

@ -2,6 +2,7 @@ package com.iflytop.profilometer.model.vo;
public class SystemConfigVO {
private String server;
private Integer standbyMinutes;
public String getServer() {
return server;
@ -10,4 +11,12 @@ public class SystemConfigVO {
public void setServer(String server) {
this.server = server;
}
public Integer getStandbyMinutes() {
return standbyMinutes;
}
public void setStandbyMinutes(Integer standbyMinutes) {
this.standbyMinutes = standbyMinutes;
}
}

3
app/src/main/java/com/iflytop/profilometer/service/RailProfileDrawerService.java

@ -102,9 +102,6 @@ public class RailProfileDrawerService implements RailProfileDrawerListener {
if (side == MeasureSide.LEFT) {
map.put("data", RailProfileMeasureTaskEvent.FINISH_RECORD_LEFT);
taskState.setMeasuringLeftEnd(true);
//左侧测量完毕后将数据反转
// List<XYPoint> outlinePointSetList = taskState.getOutlinePointSetList();
// Collections.reverse(outlinePointSetList);
} else if (side == MeasureSide.RIGHT) {
map.put("data", RailProfileMeasureTaskEvent.FINISH_RECORD_RIGHT);
taskState.setMeasuringRightEnd(true);

Loading…
Cancel
Save