Search in sources :

Example 1 with DeviceIO

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()));
    }
}
Also used : WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) StreamEvent(qz.ws.StreamEvent) JSONArray(org.codehaus.jettison.json.JSONArray) DeviceIO(qz.communication.DeviceIO) DeviceException(qz.communication.DeviceException) WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) JSONException(org.codehaus.jettison.json.JSONException) DeviceException(qz.communication.DeviceException)

Aggregations

JSONArray (org.codehaus.jettison.json.JSONArray)1 JSONException (org.codehaus.jettison.json.JSONException)1 WebSocketException (org.eclipse.jetty.websocket.api.WebSocketException)1 DeviceException (qz.communication.DeviceException)1 DeviceIO (qz.communication.DeviceIO)1 StreamEvent (qz.ws.StreamEvent)1