Search in sources :

Example 1 with StreamEvent

use of qz.ws.StreamEvent in project tray by qzind.

the class FileIO method fileChanged.

public void fileChanged(String fileName, String type, String fileData) {
    StreamEvent evt = new StreamEvent(StreamEvent.Stream.FILE, StreamEvent.Type.ACTION).withData("file", getOriginalPath().resolve(fileName)).withData("eventType", type);
    if (fileData != null) {
        evt.withData("fileData", fileData);
    }
    PrintSocketClient.sendStream(session, evt);
}
Also used : StreamEvent(qz.ws.StreamEvent)

Example 2 with StreamEvent

use of qz.ws.StreamEvent in project tray by qzind.

the class SerialUtilities method setupSerialPort.

public static void setupSerialPort(final Session session, String UID, SocketConnection connection, JSONObject params) throws JSONException {
    final String portName = params.getString("port");
    if (connection.getSerialPort(portName) != null) {
        PrintSocketClient.sendError(session, UID, String.format("Serial port [%s] is already open.", portName));
        return;
    }
    try {
        SerialOptions props = new SerialOptions(params.optJSONObject("options"), true);
        final SerialIO serial = new SerialIO(portName);
        if (serial.open(props)) {
            connection.addSerialPort(portName, serial);
            // apply listener here, so we can send all replies to the browser
            serial.applyPortListener(spe -> {
                String output = serial.processSerialEvent(spe);
                if (output != null) {
                    log.debug("Received serial output: {}", output);
                    StreamEvent event = new StreamEvent(StreamEvent.Stream.SERIAL, StreamEvent.Type.RECEIVE).withData("portName", portName).withData("output", output);
                    PrintSocketClient.sendStream(session, event);
                }
            });
            PrintSocketClient.sendResult(session, UID, null);
        } else {
            PrintSocketClient.sendError(session, UID, String.format("Unable to open serial port [%s]", portName));
        }
    } catch (SerialPortException e) {
        PrintSocketClient.sendError(session, UID, e);
    }
}
Also used : StreamEvent(qz.ws.StreamEvent) SerialPortException(jssc.SerialPortException) SerialOptions(qz.communication.SerialOptions) SerialIO(qz.communication.SerialIO)

Example 3 with StreamEvent

use of qz.ws.StreamEvent in project tray by qzind.

the class SocketUtilities method setupSocket.

public static void setupSocket(final Session session, String UID, SocketConnection connection, JSONObject params) throws JSONException {
    final String host = params.getString("host");
    final int port = params.getInt("port");
    final String location = String.format("%s:%s", host, port);
    if (connection.getNetworkSocket(location) != null) {
        PrintSocketClient.sendError(session, UID, String.format("Socket [%s] is already open", location));
        return;
    }
    // TODO - move to dedicated options class?
    Charset encoding = StandardCharsets.UTF_8;
    if (!params.isNull("encoding")) {
        try {
            encoding = Charset.forName(params.getString("encoding"));
        } catch (JSONException e) {
            LoggerUtilities.optionWarn(log, "string", "encoding", params.opt("encoding"));
        }
    }
    try {
        final SocketIO socket = new SocketIO(host, port, encoding);
        if (socket.open()) {
            connection.addNetworkSocket(location, socket);
            new Thread(() -> {
                StreamEvent event = new StreamEvent(StreamEvent.Stream.SOCKET, StreamEvent.Type.RECEIVE).withData("host", host).withData("port", port);
                try {
                    while (socket.isOpen()) {
                        String response = socket.processSocketResponse();
                        if (response != null) {
                            log.debug("Received socket response: {}", response);
                            PrintSocketClient.sendStream(session, event.withData("response", response));
                        }
                    }
                } catch (IOException e) {
                    StreamEvent eventErr = new StreamEvent(StreamEvent.Stream.SOCKET, StreamEvent.Type.ERROR).withData("host", host).withData("port", port).withException(e);
                    PrintSocketClient.sendStream(session, eventErr);
                }
                try {
                    Thread.sleep(100);
                } catch (Exception ignore) {
                }
            }).start();
            PrintSocketClient.sendResult(session, UID, null);
        } else {
            PrintSocketClient.sendError(session, UID, String.format("Unable to open socket [%s]", location));
        }
    } catch (IOException e) {
        PrintSocketClient.sendError(session, UID, e);
    }
}
Also used : SocketIO(qz.communication.SocketIO) StreamEvent(qz.ws.StreamEvent) Charset(java.nio.charset.Charset) JSONException(org.codehaus.jettison.json.JSONException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) IOException(java.io.IOException)

Example 4 with StreamEvent

use of qz.ws.StreamEvent 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)

Example 5 with StreamEvent

use of qz.ws.StreamEvent in project tray by qzind.

the class FileIO method sendError.

public void sendError(String message) {
    StreamEvent eventErr = new StreamEvent(StreamEvent.Stream.FILE, StreamEvent.Type.ERROR).withData("message", message);
    PrintSocketClient.sendStream(session, eventErr);
}
Also used : StreamEvent(qz.ws.StreamEvent)

Aggregations

StreamEvent (qz.ws.StreamEvent)5 JSONException (org.codehaus.jettison.json.JSONException)2 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1 SerialPortException (jssc.SerialPortException)1 JSONArray (org.codehaus.jettison.json.JSONArray)1 WebSocketException (org.eclipse.jetty.websocket.api.WebSocketException)1 DeviceException (qz.communication.DeviceException)1 DeviceIO (qz.communication.DeviceIO)1 SerialIO (qz.communication.SerialIO)1 SerialOptions (qz.communication.SerialOptions)1 SocketIO (qz.communication.SocketIO)1