use of qz.communication.DeviceIO in project tray by qzind.
the class UsbUtilities method setupUsbStream.
// shared by usb and hid streaming
public static void setupUsbStream(final Session session, String UID, SocketConnection connection, final DeviceOptions dOpts, final StreamEvent.Stream streamType) {
final DeviceIO usb = connection.getDevice(dOpts);
if (usb != null) {
if (!usb.isStreaming()) {
usb.setStreaming(true);
new Thread() {
@Override
public void run() {
int interval = dOpts.getInterval();
int size = dOpts.getResponseSize();
Byte endpoint = dOpts.getEndpoint();
StreamEvent event = new StreamEvent(streamType, StreamEvent.Type.RECEIVE).withData("vendorId", usb.getVendorId()).withData("productId", usb.getProductId());
try {
while (usb.isOpen() && usb.isStreaming()) {
byte[] response = usb.readData(size, endpoint);
JSONArray hex = new JSONArray();
for (byte b : response) {
hex.put(UsbUtil.toHexString(b));
}
PrintSocketClient.sendStream(session, event.withData("output", hex));
try {
Thread.sleep(interval);
} catch (Exception ignore) {
}
}
} catch (WebSocketException e) {
usb.setStreaming(false);
log.error("USB stream error", e);
} catch (DeviceException e) {
usb.setStreaming(false);
log.error("USB stream error", e);
StreamEvent eventErr = new StreamEvent(streamType, StreamEvent.Type.ERROR).withException(e).withData("vendorId", usb.getVendorId()).withData("productId", usb.getProductId());
PrintSocketClient.sendStream(session, eventErr);
}
}
}.start();
PrintSocketClient.sendResult(session, UID, null);
} else {
PrintSocketClient.sendError(session, UID, String.format("USB Device [v:%s p:%s] is already streaming data.", dOpts.getVendorId(), dOpts.getProductId()));
}
} else {
PrintSocketClient.sendError(session, UID, String.format("USB Device [v:%s p:%s] must be claimed first.", dOpts.getVendorId(), dOpts.getProductId()));
}
}
Aggregations