12 changed files with 367 additions and 154 deletions
-
8app/src/main/java/com/iflytop/profilometer/MainActivity.java
-
31app/src/main/java/com/iflytop/profilometer/api/ble/BleApi.java
-
15app/src/main/java/com/iflytop/profilometer/api/ws/BleWebsocketManager.java
-
58app/src/main/java/com/iflytop/profilometer/api/ws/DeviceStateWebsocketManager.java
-
37app/src/main/java/com/iflytop/profilometer/core/bluetooth/BleDeviceListener.java
-
11app/src/main/java/com/iflytop/profilometer/core/bluetooth/BleDeviceOnReportListener.java
-
10app/src/main/java/com/iflytop/profilometer/core/bluetooth/BleManager.java
-
29app/src/main/java/com/iflytop/profilometer/core/migration/channel/BleDeviceUartChannel.java
-
77app/src/main/java/com/iflytop/profilometer/core/system/DeviceState.java
-
5app/src/main/java/com/iflytop/profilometer/core/system/SystemState.java
@ -0,0 +1,58 @@ |
|||
package com.iflytop.profilometer.api.ws; |
|||
|
|||
import android.annotation.SuppressLint; |
|||
import android.content.Context; |
|||
|
|||
import com.iflytop.profilometer.core.system.DeviceState; |
|||
import com.iflytop.profilometer.core.system.SystemState; |
|||
import com.iflytop.profilometer.core.websocket.WebSocketManager; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
import java.util.concurrent.Executors; |
|||
import java.util.concurrent.ScheduledExecutorService; |
|||
import java.util.concurrent.ScheduledFuture; |
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
import cn.hutool.json.JSONUtil; |
|||
|
|||
public class DeviceStateWebsocketManager { |
|||
private static DeviceStateWebsocketManager instance; |
|||
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); |
|||
private ScheduledFuture<?> scheduledTask; |
|||
|
|||
private DeviceStateWebsocketManager() { |
|||
} |
|||
|
|||
public static synchronized DeviceStateWebsocketManager getInstance() { |
|||
if (instance == null) { |
|||
instance = new DeviceStateWebsocketManager(); |
|||
} |
|||
return instance; |
|||
} |
|||
|
|||
/** |
|||
* 开始定时任务 |
|||
*/ |
|||
@SuppressLint("MissingPermission") |
|||
public void startWsPush() { |
|||
stopWsPush(); |
|||
scheduledTask = scheduler.scheduleWithFixedDelay(() -> { |
|||
DeviceState deviceState = SystemState.getInstance().getDeviceState(); |
|||
Map<String, Object> map = new HashMap<>(); |
|||
map.put("type", "peripheral-status"); |
|||
map.put("data", deviceState); |
|||
WebSocketManager.send(JSONUtil.toJsonStr(map)); |
|||
}, 0, 1000, TimeUnit.MILLISECONDS); |
|||
} |
|||
|
|||
/** |
|||
* 结束定时任务 |
|||
*/ |
|||
public void stopWsPush() { |
|||
if (scheduledTask != null && !scheduledTask.isCancelled()) { |
|||
scheduledTask.cancel(false); |
|||
} |
|||
} |
|||
|
|||
} |
@ -0,0 +1,37 @@ |
|||
package com.iflytop.profilometer.core.bluetooth; |
|||
|
|||
import android.util.Log; |
|||
|
|||
import com.iflytop.profilometer.core.migration.type.protocol.TPMIBasicReport; |
|||
import com.iflytop.profilometer.core.migration.type.protocol.TPMIPacket; |
|||
import com.iflytop.profilometer.core.migration.type.protocol.TPMIPosReport; |
|||
import com.iflytop.profilometer.core.system.DeviceState; |
|||
import com.iflytop.profilometer.core.system.SystemState; |
|||
|
|||
public class BleDeviceListener implements BleDeviceOnReportListener { |
|||
private static final String TAG = "MyBleDeviceListener"; |
|||
|
|||
@Override |
|||
public void onBasicReport(TPMIBasicReport report) { |
|||
Log.i(TAG, "基础上报包:" + report); |
|||
DeviceState deviceState = SystemState.getInstance().getDeviceState(); |
|||
deviceState.setTemperature(report.getTemperature()); |
|||
deviceState.setInclinatorX(report.getInclinatorX()); |
|||
deviceState.setInclinatorY(report.getInclinatorY()); |
|||
deviceState.setPower(report.getPower()); |
|||
deviceState.setFlag(report.getFlag()); |
|||
deviceState.setState(report.getState()); |
|||
} |
|||
|
|||
@Override |
|||
public void onPosReport(TPMIPosReport report) { |
|||
Log.i(TAG, "位置信息上报:"); |
|||
Log.i(TAG, "ARM0角度 " + report.getArm1Angle()); |
|||
Log.i(TAG, "ARM1角度" + report.getArm2Angle()); |
|||
} |
|||
|
|||
@Override |
|||
public void onUnknownReport(TPMIPacket raw) { |
|||
Log.w(TAG, "收到未知类型数据包: CMD=" + raw.getCommand() + " LEN=" + raw.getPacketLen()); |
|||
} |
|||
} |
@ -0,0 +1,11 @@ |
|||
package com.iflytop.profilometer.core.bluetooth; |
|||
|
|||
import com.iflytop.profilometer.core.migration.type.protocol.TPMIBasicReport; |
|||
import com.iflytop.profilometer.core.migration.type.protocol.TPMIPacket; |
|||
import com.iflytop.profilometer.core.migration.type.protocol.TPMIPosReport; |
|||
|
|||
public interface BleDeviceOnReportListener { |
|||
default void onBasicReport(TPMIBasicReport report) {} |
|||
default void onPosReport(TPMIPosReport report) {} |
|||
default void onUnknownReport(TPMIPacket raw) {} |
|||
} |
@ -0,0 +1,77 @@ |
|||
package com.iflytop.profilometer.core.system; |
|||
|
|||
public class DeviceState { |
|||
private Boolean isConnected; |
|||
private Integer power; |
|||
private Integer state; |
|||
private Integer flag; |
|||
private Double inclinatorX; |
|||
private Double inclinatorY; |
|||
private Double temperature; |
|||
|
|||
public DeviceState() { |
|||
isConnected = false; |
|||
power = 0; |
|||
state = 0; |
|||
flag = 0; |
|||
inclinatorX = 0.0; |
|||
inclinatorY = 0.0; |
|||
temperature = 0.0; |
|||
} |
|||
|
|||
public Boolean getConnected() { |
|||
return isConnected; |
|||
} |
|||
|
|||
public void setConnected(Boolean connected) { |
|||
isConnected = connected; |
|||
} |
|||
|
|||
public Integer getPower() { |
|||
return power; |
|||
} |
|||
|
|||
public void setPower(Integer power) { |
|||
this.power = power; |
|||
} |
|||
|
|||
public Integer getState() { |
|||
return state; |
|||
} |
|||
|
|||
public void setState(Integer state) { |
|||
this.state = state; |
|||
} |
|||
|
|||
public Integer getFlag() { |
|||
return flag; |
|||
} |
|||
|
|||
public void setFlag(Integer flag) { |
|||
this.flag = flag; |
|||
} |
|||
|
|||
public Double getInclinatorX() { |
|||
return inclinatorX; |
|||
} |
|||
|
|||
public void setInclinatorX(Double inclinatorX) { |
|||
this.inclinatorX = inclinatorX; |
|||
} |
|||
|
|||
public Double getInclinatorY() { |
|||
return inclinatorY; |
|||
} |
|||
|
|||
public void setInclinatorY(Double inclinatorY) { |
|||
this.inclinatorY = inclinatorY; |
|||
} |
|||
|
|||
public Double getTemperature() { |
|||
return temperature; |
|||
} |
|||
|
|||
public void setTemperature(Double temperature) { |
|||
this.temperature = temperature; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue