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);
}
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);
}
}
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);
}
}
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()));
}
}
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);
}
Aggregations