mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-04-06 02:01:58 +02:00
Clean packages
This commit is contained in:
@@ -16,13 +16,12 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import dev.slimevr.bridge.Bridge;
|
||||
import dev.slimevr.bridge.ProtobufBridge;
|
||||
import dev.slimevr.platform.windows.WindowsNamedPipeBridge;
|
||||
import dev.slimevr.bridge.VMCBridge;
|
||||
import dev.slimevr.poserecorder.BVHRecorder;
|
||||
import dev.slimevr.serial.SerialHandler;
|
||||
import dev.slimevr.vr.trackers.*;
|
||||
import dev.slimevr.websocketapi.ProtocolAPI;
|
||||
import dev.slimevr.gui.protocol.ProtocolAPI;
|
||||
import dev.slimevr.websocketapi.WebSocketVRBridge;
|
||||
import dev.slimevr.util.ann.VRServerThread;
|
||||
import dev.slimevr.vr.processor.HumanPoseProcessor;
|
||||
@@ -35,8 +34,6 @@ import io.eiren.util.collections.FastList;
|
||||
import io.eiren.yaml.YamlException;
|
||||
import io.eiren.yaml.YamlFile;
|
||||
import io.eiren.yaml.YamlNode;
|
||||
import slimevr_protocol.datatypes.DeviceId;
|
||||
import slimevr_protocol.datatypes.TrackerId;
|
||||
import slimevr_protocol.datatypes.TrackerIdT;
|
||||
|
||||
public class VRServer extends Thread {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package dev.slimevr.websocketapi;
|
||||
package dev.slimevr.gui.protocol;
|
||||
|
||||
import slimevr_protocol.data_feed.DataFeedConfigT;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package dev.slimevr.websocketapi;
|
||||
package dev.slimevr.gui.protocol;
|
||||
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
import com.jme3.math.Quaternion;
|
||||
@@ -1,4 +1,4 @@
|
||||
package dev.slimevr.websocketapi;
|
||||
package dev.slimevr.gui.protocol;
|
||||
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
import io.eiren.util.logging.LogManager;
|
||||
@@ -1,4 +1,4 @@
|
||||
package dev.slimevr.websocketapi;
|
||||
package dev.slimevr.gui.protocol;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.UUID;
|
||||
@@ -1,9 +1,7 @@
|
||||
package dev.slimevr.websocketapi;
|
||||
package dev.slimevr.gui.protocol;
|
||||
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
import dev.slimevr.VRServer;
|
||||
import slimevr_protocol.MessageBundle;
|
||||
import slimevr_protocol.data_feed.DataFeedMessage;
|
||||
import slimevr_protocol.data_feed.DataFeedMessageHeader;
|
||||
import slimevr_protocol.rpc.RpcMessageHeader;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package dev.slimevr.websocketapi;
|
||||
package dev.slimevr.gui.protocol;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package dev.slimevr.websocketapi;
|
||||
package dev.slimevr.gui.protocol;
|
||||
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
import slimevr_protocol.MessageBundle;
|
||||
@@ -1,7 +1,6 @@
|
||||
package dev.slimevr.websocketapi;
|
||||
package dev.slimevr.gui.protocol;
|
||||
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
import dev.slimevr.VRServer;
|
||||
import dev.slimevr.vr.processor.HumanPoseProcessor;
|
||||
import dev.slimevr.vr.processor.skeleton.SkeletonConfigValue;
|
||||
import slimevr_protocol.rpc.SkeletonConfigResponse;
|
||||
@@ -1,9 +1,8 @@
|
||||
package dev.slimevr.websocketapi;
|
||||
package dev.slimevr.gui.protocol;
|
||||
|
||||
import com.fazecast.jSerialComm.SerialPort;
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
import dev.slimevr.platform.windows.WindowsNamedPipeBridge;
|
||||
import dev.slimevr.serial.SerialHandler;
|
||||
import dev.slimevr.serial.SerialListener;
|
||||
import dev.slimevr.vr.processor.skeleton.SkeletonConfigValue;
|
||||
import dev.slimevr.vr.trackers.*;
|
||||
@@ -12,7 +11,6 @@ import slimevr_protocol.MessageBundle;
|
||||
import slimevr_protocol.datatypes.TransactionId;
|
||||
import slimevr_protocol.rpc.*;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public class RPCHandler extends ProtocolHandler<RpcMessageHeader> implements SerialListener {
|
||||
@@ -63,7 +61,7 @@ public class RPCHandler extends ProtocolHandler<RpcMessageHeader> implements Ser
|
||||
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(32);
|
||||
SerialUpdateResponse.startSerialUpdateResponse(fbb);
|
||||
SerialUpdateResponse.addClosed(fbb, false);
|
||||
SerialUpdateResponse.addClosed(fbb, !this.api.server.getSerialHandler().isConnected());
|
||||
int update = SerialUpdateResponse.endSerialUpdateResponse(fbb);
|
||||
int outbound = this.createRPCMessage(fbb, RpcMessage.SerialUpdateResponse, update);
|
||||
fbb.finish(outbound);
|
||||
@@ -80,7 +78,7 @@ public class RPCHandler extends ProtocolHandler<RpcMessageHeader> implements Ser
|
||||
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(32);
|
||||
SerialUpdateResponse.startSerialUpdateResponse(fbb);
|
||||
SerialUpdateResponse.addClosed(fbb, false);
|
||||
SerialUpdateResponse.addClosed(fbb, !this.api.server.getSerialHandler().isConnected());
|
||||
int update = SerialUpdateResponse.endSerialUpdateResponse(fbb);
|
||||
int outbound = this.createRPCMessage(fbb, RpcMessage.SerialUpdateResponse, update);
|
||||
fbb.finish(outbound);
|
||||
@@ -1,20 +0,0 @@
|
||||
package dev.slimevr.websocketapi;
|
||||
|
||||
public class APIApplication {
|
||||
|
||||
public int applicationType;
|
||||
|
||||
public boolean listeningToSerial;
|
||||
|
||||
public APIApplication() {
|
||||
this.listeningToSerial = false;
|
||||
}
|
||||
|
||||
public void setListeningToSerial(boolean serial) {
|
||||
this.listeningToSerial = serial;
|
||||
}
|
||||
|
||||
public boolean isListeningToSerial() {
|
||||
return listeningToSerial;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
package dev.slimevr.websocketapi;
|
||||
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
import com.google.flatbuffers.Table;
|
||||
import dev.slimevr.VRServer;
|
||||
import dev.slimevr.gui.protocol.GenericConnection;
|
||||
import dev.slimevr.gui.protocol.ProtocolAPI;
|
||||
import dev.slimevr.gui.protocol.ProtocolAPIServer;
|
||||
import io.eiren.util.logging.LogManager;
|
||||
import org.java_websocket.WebSocket;
|
||||
import org.java_websocket.drafts.Draft;
|
||||
|
||||
@@ -1,467 +0,0 @@
|
||||
package dev.slimevr.websocketapi;
|
||||
|
||||
import com.fazecast.jSerialComm.SerialPort;
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
import com.jme3.math.Quaternion;
|
||||
import com.jme3.math.Vector3f;
|
||||
import dev.slimevr.VRServer;
|
||||
import dev.slimevr.platform.windows.WindowsNamedPipeBridge;
|
||||
import dev.slimevr.serial.SerialListener;
|
||||
import dev.slimevr.vr.processor.skeleton.SkeletonConfigValue;
|
||||
import dev.slimevr.vr.trackers.*;
|
||||
import dev.slimevr.vr.trackers.TrackerPosition;
|
||||
import dev.slimevr.vr.trackers.TrackerRole;
|
||||
import io.eiren.util.ann.ThreadSafe;
|
||||
import io.eiren.util.logging.LogManager;
|
||||
import org.java_websocket.WebSocket;
|
||||
import org.java_websocket.drafts.Draft;
|
||||
import org.java_websocket.drafts.Draft_6455;
|
||||
import org.java_websocket.handshake.ClientHandshake;
|
||||
import org.java_websocket.server.WebSocketServer;
|
||||
import slimevr_protocol.InboundPacket;
|
||||
import slimevr_protocol.InboundUnion;
|
||||
import slimevr_protocol.OutboundUnion;
|
||||
import slimevr_protocol.datatypes.Acknowledgement;
|
||||
import slimevr_protocol.server.*;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
public class WebsocketAPI extends WebSocketServer implements SerialListener {
|
||||
|
||||
public VRServer server;
|
||||
|
||||
private final HashMap<Integer, APIApplication> applications;
|
||||
|
||||
private long lastMillis = 0;
|
||||
|
||||
private int outbountPacketCount = 0;
|
||||
|
||||
public WebsocketAPI(VRServer server) {
|
||||
super(new InetSocketAddress(21110), Collections.<Draft>singletonList(new Draft_6455()));
|
||||
this.server = server;
|
||||
this.applications = new HashMap<>();
|
||||
this.server.getSerialHandler().addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpen(WebSocket conn, ClientHandshake handshake) {
|
||||
LogManager.log.info("[WebSocketAPI] New connection from: " + conn.getRemoteSocketAddress().getAddress().getHostAddress());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
|
||||
LogManager.log.info("[WebSocketAPI] Disconnected: " + conn.getRemoteSocketAddress().getAddress().getHostAddress() + ", (" + code + ") " + reason + ". Remote: " + remote);
|
||||
this.applications.remove(conn.hashCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(WebSocket conn, String message) {}
|
||||
|
||||
@Override
|
||||
public void onMessage(WebSocket conn, ByteBuffer message) {
|
||||
InboundPacket inboundPacket = InboundPacket.getRootAsInboundPacket(message);
|
||||
|
||||
try {
|
||||
switch (inboundPacket.packetType()) {
|
||||
case InboundUnion.ConnectionRequest: {
|
||||
HandshakeRequest req = (HandshakeRequest) inboundPacket.packet(new HandshakeRequest());
|
||||
if (req != null) this.onHandshakeRequest(conn, req);
|
||||
break;
|
||||
}
|
||||
case InboundUnion.AssignTrackerRequest: {
|
||||
AssignTrackerRequest req = (AssignTrackerRequest) inboundPacket.packet(new AssignTrackerRequest());
|
||||
if (req != null) this.onAssignTrackerRequest(conn, req);
|
||||
break;
|
||||
}
|
||||
case InboundUnion.ResetRequest: {
|
||||
ResetRequest req = (ResetRequest) inboundPacket.packet(new ResetRequest());
|
||||
if (req != null) this.onResetRequest(conn, req);
|
||||
break;
|
||||
}
|
||||
case InboundUnion.SettingsRequest: {
|
||||
SettingsRequest req = (SettingsRequest) inboundPacket.packet(new SettingsRequest());
|
||||
if (req != null) this.onSettingsRequest(conn, req);
|
||||
break;
|
||||
}
|
||||
case InboundUnion.ChangeSettingsRequest: {
|
||||
ChangeSettingsRequest req = (ChangeSettingsRequest) inboundPacket.packet(new ChangeSettingsRequest());
|
||||
if (req != null) this.onChangeSettingsRequest(conn, req);
|
||||
break;
|
||||
}
|
||||
case InboundUnion.SkeletonConfigRequest: {
|
||||
SkeletonConfigRequest req = (SkeletonConfigRequest) inboundPacket.packet(new SkeletonConfigRequest());
|
||||
if (req != null) this.onSkeletonConfigRequest(conn, req);
|
||||
break;
|
||||
}
|
||||
case InboundUnion.ChangeSkeletonConfigRequest: {
|
||||
ChangeSkeletonConfigRequest req = (ChangeSkeletonConfigRequest) inboundPacket.packet(new ChangeSkeletonConfigRequest());
|
||||
if (req != null) this.onChangeSkeletonConfigRequest(conn, req);
|
||||
break;
|
||||
}
|
||||
case InboundUnion.SkeletonResetAllRequest: {
|
||||
SkeletonResetAllRequest req = (SkeletonResetAllRequest) inboundPacket.packet(new SkeletonResetAllRequest());
|
||||
if (req != null) this.onSkeletonResetAllRequest(conn, req);
|
||||
break;
|
||||
}
|
||||
case InboundUnion.RecordBVHRequest: {
|
||||
RecordBVHRequest req = (RecordBVHRequest) inboundPacket.packet(new RecordBVHRequest());
|
||||
if (req != null) this.onRecordBVHRequest(conn, req);
|
||||
break;
|
||||
}
|
||||
case InboundUnion.OpenSerialRequest: {
|
||||
OpenSerialRequest req = (OpenSerialRequest) inboundPacket.packet(new OpenSerialRequest());
|
||||
if (req != null) this.onOpenSerialRequest(conn, req);
|
||||
break;
|
||||
}
|
||||
case InboundUnion.CloseSerialRequest: {
|
||||
CloseSerialRequest req = (CloseSerialRequest) inboundPacket.packet(new CloseSerialRequest());
|
||||
if (req != null) this.onCloseSerialRequest(conn, req);
|
||||
break;
|
||||
}
|
||||
case InboundUnion.SetWifiRequest: {
|
||||
SetWifiRequest req = (SetWifiRequest) inboundPacket.packet(new SetWifiRequest());
|
||||
if (req != null) this.onSetWifiRequest(conn, req);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
LogManager.log.warning("[WebSocketAPI] Received unknown packet type: " + inboundPacket.packetType());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogManager.log.severe("[WebSocketAPI] Failed to parse packet: " + inboundPacket.packetType(), e);
|
||||
}
|
||||
|
||||
|
||||
if (inboundPacket.acknowledgeMe()) {
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(40);
|
||||
int acknowledgement = Acknowledgement.createAcknowledgement(fbb, inboundPacket.packetCounter());
|
||||
int outbound = this.createOutboundPacket(fbb, OutboundUnion.slimevr_protocol_misc_Acknowledgement, acknowledgement);
|
||||
fbb.finish(outbound);
|
||||
conn.send(fbb.dataBuffer());
|
||||
}
|
||||
}
|
||||
|
||||
public void onSetWifiRequest(WebSocket conn, SetWifiRequest req) {
|
||||
if (req.password() == null || req.ssid() == null || !this.server.getSerialHandler().isConnected())
|
||||
return;
|
||||
this.server.getSerialHandler().setWifi(req.ssid(), req.password());
|
||||
}
|
||||
|
||||
public void onOpenSerialRequest(WebSocket conn, OpenSerialRequest req) {
|
||||
APIApplication application = this.applications.get(conn.hashCode());
|
||||
if (application == null) return;
|
||||
application.setListeningToSerial(true);
|
||||
|
||||
this.server.getSerialHandler().openSerial();
|
||||
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(32);
|
||||
|
||||
SerialUpdateResponse.startSerialUpdateResponse(fbb);
|
||||
SerialUpdateResponse.addClosed(fbb, false);
|
||||
int update = SerialUpdateResponse.endSerialUpdateResponse(fbb);
|
||||
int outbound = this.createOutboundPacket(fbb, OutboundUnion.SerialUpdateResponse, update);
|
||||
fbb.finish(outbound);
|
||||
|
||||
ByteBuffer buf = fbb.dataBuffer();
|
||||
conn.send(buf);
|
||||
}
|
||||
|
||||
public void onCloseSerialRequest(WebSocket conn, CloseSerialRequest req) {
|
||||
APIApplication application = this.applications.get(conn.hashCode());
|
||||
if (application == null) return;
|
||||
application.setListeningToSerial(false);
|
||||
|
||||
this.server.getSerialHandler().closeSerial();
|
||||
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(32);
|
||||
|
||||
SerialUpdateResponse.startSerialUpdateResponse(fbb);
|
||||
SerialUpdateResponse.addClosed(fbb, false);
|
||||
int update = SerialUpdateResponse.endSerialUpdateResponse(fbb);
|
||||
int outbound = this.createOutboundPacket(fbb, OutboundUnion.SerialUpdateResponse, update);
|
||||
fbb.finish(outbound);
|
||||
|
||||
ByteBuffer buf = fbb.dataBuffer();
|
||||
conn.send(buf);
|
||||
}
|
||||
|
||||
|
||||
public void onRecordBVHRequest(WebSocket conn, RecordBVHRequest req) {
|
||||
|
||||
if (req.stop()) {
|
||||
if (this.server.getBvhRecorder().isRecording())
|
||||
this.server.getBvhRecorder().endRecording();
|
||||
} else {
|
||||
if (!this.server.getBvhRecorder().isRecording())
|
||||
this.server.getBvhRecorder().startRecording();
|
||||
}
|
||||
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(40);
|
||||
int status = RecordBVHStatus.createRecordBVHStatus(fbb, this.server.getBvhRecorder().isRecording());
|
||||
int outbound = this.createOutboundPacket(fbb, OutboundUnion.RecordBVHStatus, status);
|
||||
fbb.finish(outbound);
|
||||
conn.send(fbb.dataBuffer());
|
||||
}
|
||||
|
||||
public void onSkeletonResetAllRequest(WebSocket conn, SkeletonResetAllRequest req) {
|
||||
server.humanPoseProcessor.getSkeletonConfig().resetConfigs();
|
||||
server.saveConfig();
|
||||
// might not be a good idea maybe let the client ask again
|
||||
this.onSkeletonConfigRequest(conn, new SkeletonConfigRequest());
|
||||
}
|
||||
|
||||
|
||||
public void onSkeletonConfigRequest(WebSocket conn, SkeletonConfigRequest req) {
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(300);
|
||||
|
||||
int[] partsOffsets = new int[SkeletonConfigValue.values().length];
|
||||
|
||||
|
||||
for (int index = 0; index < SkeletonConfigValue.values().length; index++) {
|
||||
SkeletonConfigValue val = SkeletonConfigValue.values[index];
|
||||
int part = SkeletonPart.createSkeletonPart(fbb, val.id, this.server.humanPoseProcessor.getSkeletonConfig(val));
|
||||
partsOffsets[index] = part;
|
||||
}
|
||||
|
||||
|
||||
int parts = SkeletonConfigResponse.createSkeletonPartsVector(fbb, partsOffsets);
|
||||
int config = SkeletonConfigResponse.createSkeletonConfigResponse(fbb, parts);
|
||||
int outbound = this.createOutboundPacket(fbb, OutboundUnion.SkeletonConfigResponse, config);
|
||||
fbb.finish(outbound);
|
||||
conn.send(fbb.dataBuffer());
|
||||
}
|
||||
|
||||
public void onChangeSkeletonConfigRequest(WebSocket conn, ChangeSkeletonConfigRequest req) {
|
||||
SkeletonConfigValue joint = SkeletonConfigValue.getById(req.id());
|
||||
|
||||
server.humanPoseProcessor.setSkeletonConfig(joint, req.value());
|
||||
server.humanPoseProcessor.getSkeletonConfig().saveToConfig(server.config);
|
||||
server.saveConfig();
|
||||
}
|
||||
|
||||
public void onSettingsRequest(WebSocket conn, SettingsRequest req) {
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(32);
|
||||
|
||||
WindowsNamedPipeBridge bridge = this.server.getVRBridge(WindowsNamedPipeBridge.class);
|
||||
|
||||
int steamvrTrackerSettings = SteamVRTrackersSetting.createSteamVRTrackersSetting(fbb,
|
||||
bridge.getShareSetting(TrackerRole.WAIST),
|
||||
bridge.getShareSetting(TrackerRole.CHEST),
|
||||
bridge.getShareSetting(TrackerRole.LEFT_FOOT) && bridge.getShareSetting(TrackerRole.RIGHT_FOOT),
|
||||
bridge.getShareSetting(TrackerRole.LEFT_KNEE) &&bridge.getShareSetting(TrackerRole.RIGHT_KNEE),
|
||||
bridge.getShareSetting(TrackerRole.LEFT_ELBOW) && bridge.getShareSetting(TrackerRole.RIGHT_ELBOW)
|
||||
);
|
||||
|
||||
int filterSettings = FilteringSettings.createFilteringSettings(
|
||||
fbb,
|
||||
TrackerFilters.valueOf(this.server.config.getString("filters.type", "NONE")).id,
|
||||
(int)(server.config.getFloat("filters.amount", 0.3f) * 100),
|
||||
server.config.getInt("filters.tickCount", 2)
|
||||
);
|
||||
|
||||
int settings = SettingsResponse.createSettingsResponse(fbb, steamvrTrackerSettings, filterSettings);
|
||||
int outbound = this.createOutboundPacket(fbb, OutboundUnion.SettingsResponse, settings);
|
||||
fbb.finish(outbound);
|
||||
conn.send(fbb.dataBuffer());
|
||||
}
|
||||
|
||||
public void onChangeSettingsRequest(WebSocket conn, ChangeSettingsRequest req) {
|
||||
if (req.steamVrTrackers() != null) {
|
||||
W;lp.
|
||||
}
|
||||
|
||||
if (req.filtering() != null) {
|
||||
TrackerFilters type = TrackerFilters.fromId(req.filtering().type());
|
||||
if (type != null) {
|
||||
this.server.updateTrackersFilters(type, (float)req.filtering().intensity() / 100.0f, req.filtering().ticks());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onResetRequest(WebSocket con, ResetRequest req) {
|
||||
if (req.quick()) {
|
||||
this.server.resetTrackersYaw();
|
||||
} else {
|
||||
this.server.resetTrackers();
|
||||
}
|
||||
LogManager.log.severe("[WebSocketAPI] Reset performed");
|
||||
}
|
||||
|
||||
public void onAssignTrackerRequest(WebSocket con, AssignTrackerRequest req) {
|
||||
Tracker tracker = this.server.getAllTrackers().get(req.id());
|
||||
if (tracker == null)
|
||||
return ;
|
||||
|
||||
tracker.setBodyPosition(TrackerPosition.getById(req.mountingPosition()));
|
||||
if (tracker instanceof IMUTracker) {
|
||||
IMUTracker imu = (IMUTracker) tracker;
|
||||
TrackerMountingRotation rot = TrackerMountingRotation.fromAngle(req.mountingRotation());
|
||||
if (rot != null)
|
||||
imu.setMountingRotation(rot);
|
||||
}
|
||||
this.server.trackerUpdated(tracker);
|
||||
}
|
||||
|
||||
public void onHandshakeRequest(WebSocket con, HandshakeRequest req) {
|
||||
APIApplication app = new APIApplication();
|
||||
app.applicationType = req.applicationType();
|
||||
this.applications.put(con.hashCode(), app);
|
||||
}
|
||||
|
||||
@ThreadSafe
|
||||
public void updateTrackersList() {
|
||||
if (System.currentTimeMillis() - lastMillis <= 100) {
|
||||
return;
|
||||
}
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(300);
|
||||
int[] trackersIds = new int[this.server.getTrackersCount()];
|
||||
|
||||
for (int index = 0; index < this.server.getTrackersCount(); index++) {
|
||||
Tracker tracker = this.server.getAllTrackers().get(index);
|
||||
|
||||
if(tracker instanceof ReferenceAdjustedTracker)
|
||||
tracker = ((ReferenceAdjustedTracker<? extends Tracker>) tracker).getTracker();
|
||||
|
||||
int trackerName = fbb.createString(tracker.getName());
|
||||
|
||||
Vector3f pos3f = new Vector3f();
|
||||
Quaternion quatRot = new Quaternion();
|
||||
tracker.getPosition(pos3f);
|
||||
tracker.getRotation(quatRot);
|
||||
|
||||
DeviceStatus.startDeviceStatus(fbb);
|
||||
DeviceStatus.addId(fbb, index); // or tacker id ?
|
||||
DeviceStatus.addName(fbb, trackerName);
|
||||
DeviceStatus.addPosition(fbb, Vec3f.createVec3f(fbb, pos3f.x, pos3f.y, pos3f.z));
|
||||
DeviceStatus.addRotation(fbb, Quat.createQuat(fbb, quatRot.getX(), quatRot.getY(), quatRot.getZ(), quatRot.getW()));
|
||||
DeviceStatus.addComputed(fbb, tracker.isComputed());
|
||||
DeviceStatus.addEditable(fbb, tracker.userEditable());
|
||||
|
||||
if (tracker.getBodyPosition() != null) {
|
||||
DeviceStatus.addMountingPosition(fbb, tracker.getBodyPosition().id);
|
||||
}
|
||||
|
||||
if (tracker instanceof IMUTracker) {
|
||||
IMUTracker imu = (IMUTracker) tracker;
|
||||
if (imu.getMountingRotation() != null)
|
||||
DeviceStatus.addMountingRotation(fbb, (short)imu.getMountingRotation().angle);
|
||||
// put back to 0 - 256 so we can fit it in a byte instead of float
|
||||
DeviceStatus.addBattery(fbb, (int)((imu.getBatteryLevel() / 100) * 255));
|
||||
DeviceStatus.addPing(fbb, imu.ping);
|
||||
DeviceStatus.addStatus(fbb, imu.getStatus().id + 1);
|
||||
DeviceStatus.addSignal(fbb, (short)imu.signalStrength);
|
||||
DeviceStatus.addTps(fbb, (int)Math.floor(imu.getTPS()));
|
||||
}
|
||||
trackersIds[index] = DeviceStatus.endDeviceStatus(fbb);
|
||||
}
|
||||
|
||||
int trackers = TrackersList.createTrackersVector(fbb, trackersIds);
|
||||
|
||||
TrackersList.startTrackersList(fbb);
|
||||
TrackersList.addTrackers(fbb, trackers);
|
||||
int list = TrackersList.endTrackersList(fbb);
|
||||
|
||||
int outbound = this.createOutboundPacket(fbb, OutboundUnion.TrackersList, list);
|
||||
fbb.finish(outbound);
|
||||
|
||||
ByteBuffer buf = fbb.dataBuffer();
|
||||
this.sendToApps(buf);
|
||||
|
||||
lastMillis = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
|
||||
public int createOutboundPacket(FlatBufferBuilder fbb, byte packetType, int packetOffset) {
|
||||
return OutboundPacket.createOutboundPacket(fbb, this.outbountPacketCount++, false, packetType, packetOffset);
|
||||
}
|
||||
|
||||
|
||||
public void sendToApps(ByteBuffer buf) {
|
||||
this.getConnections()
|
||||
.stream()
|
||||
.filter((conn) -> this.applications.containsKey(conn.hashCode()))
|
||||
.forEach((conn) -> conn.send(buf));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onError(WebSocket conn, Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
LogManager.log.info("[WebSocketAPI] Web Socket API started on port " + getPort());
|
||||
setConnectionLostTimeout(0);
|
||||
|
||||
this.server.addOnTick(this::updateTrackersList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSerialConnected(SerialPort port) {
|
||||
this.applications.entrySet().stream()
|
||||
.filter((app) -> app.getValue().isListeningToSerial())
|
||||
.map((app) -> this.getConnections().stream().filter((conn) -> conn.hashCode() == app.getKey()).findFirst().orElse(null))
|
||||
.filter(Objects::nonNull)
|
||||
.forEach((conn) -> {
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(32);
|
||||
|
||||
SerialUpdateResponse.startSerialUpdateResponse(fbb);
|
||||
SerialUpdateResponse.addClosed(fbb, false);
|
||||
int update = SerialUpdateResponse.endSerialUpdateResponse(fbb);
|
||||
int outbound = this.createOutboundPacket(fbb, OutboundUnion.SerialUpdateResponse, update);
|
||||
fbb.finish(outbound);
|
||||
|
||||
ByteBuffer buf = fbb.dataBuffer();
|
||||
conn.send(buf);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSerialDisconnected() {
|
||||
this.applications.entrySet().stream()
|
||||
.filter((app) -> app.getValue().isListeningToSerial())
|
||||
.map((app) -> this.getConnections().stream().filter((conn) -> conn.hashCode() == app.getKey()).findFirst().orElse(null))
|
||||
.filter(Objects::nonNull)
|
||||
.forEach((conn) -> {
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(32);
|
||||
|
||||
SerialUpdateResponse.startSerialUpdateResponse(fbb);
|
||||
SerialUpdateResponse.addClosed(fbb, true);
|
||||
int update = SerialUpdateResponse.endSerialUpdateResponse(fbb);
|
||||
int outbound = this.createOutboundPacket(fbb, OutboundUnion.SerialUpdateResponse, update);
|
||||
fbb.finish(outbound);
|
||||
|
||||
ByteBuffer buf = fbb.dataBuffer();
|
||||
conn.send(buf);
|
||||
|
||||
this.applications.get(conn.hashCode()).setListeningToSerial(false);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSerialLog(String str) {
|
||||
this.applications.entrySet().stream()
|
||||
.filter((app) -> app.getValue().isListeningToSerial())
|
||||
.map((app) -> this.getConnections().stream().filter((conn) -> conn.hashCode() == app.getKey()).findFirst().orElse(null))
|
||||
.filter(Objects::nonNull)
|
||||
.forEach((conn) -> {
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(32);
|
||||
|
||||
int logOffset = fbb.createString(str);
|
||||
|
||||
SerialUpdateResponse.startSerialUpdateResponse(fbb);
|
||||
SerialUpdateResponse.addLog(fbb, logOffset);
|
||||
int update = SerialUpdateResponse.endSerialUpdateResponse(fbb);
|
||||
int outbound = this.createOutboundPacket(fbb, OutboundUnion.SerialUpdateResponse, update);
|
||||
fbb.finish(outbound);
|
||||
|
||||
ByteBuffer buf = fbb.dataBuffer();
|
||||
conn.send(buf);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package dev.slimevr.websocketapi;
|
||||
|
||||
import dev.slimevr.gui.protocol.ConnectionContext;
|
||||
import dev.slimevr.gui.protocol.GenericConnection;
|
||||
import org.java_websocket.WebSocket;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -24,7 +26,8 @@ public class WebsocketConnection implements GenericConnection {
|
||||
|
||||
@Override
|
||||
public void send(ByteBuffer bytes) {
|
||||
this.conn.send(bytes);
|
||||
if (this.conn.isOpen())
|
||||
this.conn.send(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,236 +0,0 @@
|
||||
include "commons/misc.fbs";
|
||||
include "commons/datatypes.fbs";
|
||||
include "commons/hardware_info.fbs";
|
||||
|
||||
namespace slimevr_protocol.server;
|
||||
|
||||
// Inbound is from the outside to the server
|
||||
union InboundUnion {
|
||||
ConnectionRequest,
|
||||
ResetRequest,
|
||||
AssignTrackerRequest,
|
||||
ChangeSettingsRequest,
|
||||
SettingsRequest,
|
||||
SkeletonConfigRequest,
|
||||
ChangeSkeletonConfigRequest,
|
||||
SkeletonResetAllRequest,
|
||||
RecordBVHRequest,
|
||||
OpenSerialRequest,
|
||||
CloseSerialRequest,
|
||||
SetWifiRequest,
|
||||
slimevr_protocol.misc.Acknowledgement,
|
||||
}
|
||||
|
||||
// outbound is from the server to the outside
|
||||
union OutboundUnion {
|
||||
TrackersList,
|
||||
SettingsResponse,
|
||||
SkeletonConfigResponse,
|
||||
RecordBVHStatus,
|
||||
SerialUpdateResponse,
|
||||
slimevr_protocol.misc.Acknowledgement
|
||||
}
|
||||
|
||||
|
||||
table InboundPacket {
|
||||
packet_counter: uint32;
|
||||
// If `true`, receipt of this packet should be acknowledged
|
||||
acknowledge_me: bool = false;
|
||||
packet: InboundUnion;
|
||||
}
|
||||
|
||||
table OutboundPacket {
|
||||
packet_counter: uint32;
|
||||
// If `true`, receipt of this packet should be acknowledged
|
||||
acknowledge_me: bool = false;
|
||||
packet: OutboundUnion;
|
||||
}
|
||||
|
||||
|
||||
table ResetRequest {
|
||||
quick: bool;
|
||||
}
|
||||
|
||||
|
||||
enum ApplicationType: uint8 {
|
||||
Other = 0,
|
||||
UI,
|
||||
OVERLAY,
|
||||
}
|
||||
|
||||
table ConnectionRequest {
|
||||
application_type: ApplicationType;
|
||||
|
||||
tracker_list: [TrackerListFeatures];
|
||||
}
|
||||
|
||||
enum TrackerStatus: uint8 {
|
||||
NONE = 0,
|
||||
DISCONNECTED = 1,
|
||||
OK = 2,
|
||||
BUSY = 3,
|
||||
ERROR = 4,
|
||||
OCCLUDED = 5,
|
||||
}
|
||||
|
||||
enum TrackerRole: uint8 {
|
||||
NONE = 0,
|
||||
WAIST = 1,
|
||||
LEFT_FOOT = 2,
|
||||
RIGHT_FOOT = 3,
|
||||
CHEST = 4,
|
||||
LEFT_KNEE = 5,
|
||||
RIGHT_KNEE = 6,
|
||||
LEFT_ELBOW = 7,
|
||||
RIGHT_ELBOW = 8,
|
||||
LEFT_SHOULDER = 9,
|
||||
RIGHT_SHOULDER = 10,
|
||||
LEFT_HAND = 11,
|
||||
RIGHT_HAND = 12,
|
||||
LEFT_CONTROLLER = 13,
|
||||
RIGHT_CONTROLLER = 14,
|
||||
HEAD = 15,
|
||||
NECK = 16,
|
||||
CAMERA = 17,
|
||||
KEYBOARD = 18,
|
||||
HMD = 19,
|
||||
BEACON = 20,
|
||||
GENERIC_CONTROLLER = 21
|
||||
}
|
||||
|
||||
enum TrackerPosition: uint8 {
|
||||
NONE = 0,
|
||||
HMD = 1,
|
||||
CHEST = 2,
|
||||
WAIST = 3,
|
||||
HIP = 4,
|
||||
LEFT_LEG = 5,
|
||||
RIGHT_LEG = 6,
|
||||
LEFT_ANKLE = 7,
|
||||
RIGHT_ANKLE = 8,
|
||||
LEFT_FOOT = 9,
|
||||
RIGHT_FOOT = 10,
|
||||
LEFT_CONTROLLER = 11,
|
||||
RIGHT_CONTROLLER = 12,
|
||||
LEFT_FOREARM = 13,
|
||||
RIGHT_FOREARM = 14,
|
||||
LEFT_UPPER_ARM = 15,
|
||||
RIGHT_UPPER_ARM = 16
|
||||
}
|
||||
|
||||
|
||||
|
||||
table TrackerListFeatures {
|
||||
interval: uint16;
|
||||
complete: bool;
|
||||
device_features: DeviceStatusFeatures;
|
||||
}
|
||||
|
||||
table DeviceStatusFeatures {
|
||||
name: bool;
|
||||
role: bool;
|
||||
mounting_rotation: bool;
|
||||
rotation: bool;
|
||||
position: bool;
|
||||
status: bool;
|
||||
battery: bool;
|
||||
computed: bool;
|
||||
signal: bool;
|
||||
tps: bool;
|
||||
ping: bool;
|
||||
editable: bool;
|
||||
}
|
||||
|
||||
table DeviceStatus {
|
||||
id: uint8;
|
||||
name: string;
|
||||
mounting_position: TrackerPosition;
|
||||
mounting_rotation: uint16;
|
||||
rotation: slimevr_protocol.datatypes.Quat;
|
||||
position: slimevr_protocol.datatypes.Vec3f;
|
||||
status: TrackerStatus;
|
||||
battery: uint8;
|
||||
computed: bool;
|
||||
signal: int16;
|
||||
tps: uint8;
|
||||
ping: uint16;
|
||||
editable: bool;
|
||||
}
|
||||
|
||||
table TrackersList {
|
||||
trackers: [DeviceStatus];
|
||||
}
|
||||
|
||||
|
||||
table AssignTrackerRequest {
|
||||
id: uint8;
|
||||
mounting_position: TrackerPosition;
|
||||
mounting_rotation: uint16;
|
||||
}
|
||||
|
||||
table SteamVRTrackersSetting {
|
||||
waist: bool;
|
||||
chest: bool;
|
||||
legs: bool;
|
||||
knees: bool;
|
||||
elbows: bool;
|
||||
}
|
||||
|
||||
|
||||
|
||||
enum SkeletonConfigValue: uint8 {
|
||||
NONE = 0,
|
||||
HEAD = 1,
|
||||
NECK = 2,
|
||||
TORSO = 3,
|
||||
CHEST = 4,
|
||||
WAIST = 5,
|
||||
HIP_OFFSET = 6,
|
||||
HIPS_WIDTH = 7,
|
||||
LEGS_LENGTH = 8,
|
||||
KNEE_HEIGHT = 9,
|
||||
FOOT_LENGTH = 10,
|
||||
FOOT_OFFSET = 11,
|
||||
SKELETON_OFFSET = 12,
|
||||
CONTROLLER_DISTANCE_Z = 13,
|
||||
CONTROLLER_DISTANCE_Y = 14,
|
||||
ELBOW_DISTANCE = 15,
|
||||
UPPER_ARM_DISTANCE = 16
|
||||
}
|
||||
|
||||
table SkeletonPart {
|
||||
id: SkeletonConfigValue;
|
||||
value: float;
|
||||
}
|
||||
|
||||
table SkeletonConfigRequest {}
|
||||
table SkeletonConfigResponse {
|
||||
skeleton_parts: [SkeletonPart];
|
||||
}
|
||||
|
||||
table SkeletonResetAllRequest {}
|
||||
|
||||
table ChangeSkeletonConfigRequest {
|
||||
id: SkeletonConfigValue;
|
||||
value: float;
|
||||
}
|
||||
|
||||
table RecordBVHRequest {
|
||||
stop: bool;
|
||||
}
|
||||
|
||||
table RecordBVHStatus {
|
||||
recording: bool;
|
||||
}
|
||||
|
||||
|
||||
table OpenSerialRequest {}
|
||||
table CloseSerialRequest {}
|
||||
table SetWifiRequest {
|
||||
ssid: string;
|
||||
password: string;
|
||||
}
|
||||
table SerialUpdateResponse {
|
||||
log: string;
|
||||
closed: bool;
|
||||
}
|
||||
Reference in New Issue
Block a user