public void stopAction() { collecting = false; jsonDataCollector.getListeners().stream().forEach(RecordListener::stop); toggleButtons(); jsonDataCollector.getListeners().stream().forEach(s -> s.dump("Data")); startButton.setDefaultButton(true); stopButton.setDefaultButton(false); }
public void startAction() { collecting = true; jsonDataCollector.getListeners().stream().forEach(RecordListener::start); startButton.setDefaultButton(false); stopButton.setDefaultButton(true); toggleButtons(); // todo must be killed by stop action new Thread( () -> { while (collecting) { hub.run(100); } System.out.println("collecting = " + collecting); }) .start(); }
public void connectMyo() { try { LOGGER.info("Connecting to Myo"); updateMyoStatus("Attempting to find a Myo..."); Myo myo = hub.waitForMyo(10000); if (myo == null) { LOGGER.error("Unable to connect to Myo"); throw new RuntimeException("Unable to find a Myo!"); } myo.setStreamEmg(StreamEmgType.STREAM_EMG_ENABLED); LOGGER.info("EMG Stream enabled"); updateMyoStatus("Connected to a Myo armband!"); jsonDataCollector = new JsonDataCollector(); jsonDataCollector.addListsner(new FileMyoDataCollector("c:\\tmp\\myo")); jsonDataCollector.addListsner(new SocketServerCollector()); hub.addListener(jsonDataCollector); LOGGER.info("Listener added"); indicatorMyo.setIndicatorStyle(SimpleIndicator.IndicatorStyle.GREEN); startButton.setDisable(false); startButton.setDefaultButton(true); Task<ObservableList<String>> task = new Task<ObservableList<String>>() { @Override protected ObservableList<String> call() throws Exception { while (true) { List<RecordListener> collect = jsonDataCollector .getListeners() .stream() .filter(s -> s instanceof SocketServerCollector) .collect(Collectors.toList()); List<String> connections = collect .stream() .map(s -> ((SocketServerCollector) s).channels) .flatMap(c -> c.stream().map(f -> mapRemoteAddress(f))) .collect(Collectors.toList()); updateValue(FXCollections.observableList(connections)); Thread.sleep(10); } } }; Task<Boolean> booleanTask = new Task<Boolean>() { @Override protected Boolean call() throws Exception { while (true) { List<RecordListener> collect = jsonDataCollector .getListeners() .stream() .filter(s -> s instanceof SocketServerCollector) .collect(Collectors.toList()); long count = collect .stream() .map(s -> ((SocketServerCollector) s).channels) .flatMap(Collection::stream) .filter(AsynchronousSocketChannel::isOpen) .count(); updateValue(count > 0 ? Boolean.TRUE : Boolean.FALSE); Thread.sleep(10); } } }; Task<SimpleIndicator.IndicatorStyle> stringTask = new Task<SimpleIndicator.IndicatorStyle>() { @Override protected SimpleIndicator.IndicatorStyle call() throws Exception { while (true) { List<RecordListener> collect = jsonDataCollector .getListeners() .stream() .filter(s -> s instanceof SocketServerCollector) .collect(Collectors.toList()); long count = collect .stream() .map(s -> ((SocketServerCollector) s).channels) .flatMap(Collection::stream) .filter(AsynchronousSocketChannel::isOpen) .count(); updateValue( count > 0 ? SimpleIndicator.IndicatorStyle.RED : SimpleIndicator.IndicatorStyle.GREEN); Thread.sleep(10); } } }; inboundConnections.itemsProperty().bind(task.valueProperty()); indicatorServer.onProperty().bind(booleanTask.valueProperty()); // try { // indicatorServer.indicatorStyleProperty().bind(stringTask.valueProperty()); // } catch (Throwable e) { // //??Throws a null ppointer but it works?? // } Thread thread = new Thread(task); thread.setDaemon(true); thread.start(); Thread thread2 = new Thread(booleanTask); thread2.setDaemon(true); thread2.start(); Thread thread3 = new Thread(stringTask); thread3.setDaemon(true); thread3.start(); } catch (Exception e) { e.printStackTrace(); updateMyoStatus("Error: " + e.getMessage()); } }