📄 collectserver.java
字号:
nodeModel.insertElementAt(newNode, i); added = true; break; } else if (cmp == 0) { // node already added added = true; break; } } if (!added) { nodeModel.addElement(newNode); } if (visualizers != null) { for (int i = 0, n = visualizers.length; i < n; i++) { visualizers[i].nodeAdded(newNode); } } } }); } } return node; } public void selectNodes(Node[] nodes) { selectNodes(nodes, true); } private void selectNodes(Node[] nodes, boolean updateList) { if (nodes != selectedNodes) { selectedNodes = nodes; if (updateList) { nodeList.clearSelection(); if (selectedNodes != null) { for (int i = 0, n = selectedNodes.length; i < n; i++) { int index = nodeModel.indexOf(selectedNodes[i]); if (index >= 0) { nodeList.addSelectionInterval(index, index); } } } } if (visualizers != null) { for (int i = 0, n = visualizers.length; i < n; i++) { visualizers[i].nodesSelected(nodes); } } } } // ------------------------------------------------------------------- // Node location handling // ------------------------------------------------------------------- public boolean updateNodeLocation(Node node) { String id = node.getID(); if (node.hasLocation()) { String location = "" + node.getX() + ',' + node.getY(); if (!location.equals(configTable.get(id))) { configTable.put(id, location); } return false; } String location = configTable.getProperty(id); if (location != null) { try { String[] pos = location.split(","); node.setLocation(Integer.parseInt(pos[0].trim()), Integer.parseInt(pos[1].trim())); return true; } catch (Exception e) { System.err.println("could not parse node location: " + location); e.printStackTrace(); } } return false; } private boolean loadConfig(Properties properties, String configFile) { try { BufferedInputStream input = new BufferedInputStream(new FileInputStream(configFile)); try { properties.load(input); } finally { input.close(); } return true; } catch (FileNotFoundException e) { // No configuration file exists. } catch (IOException e) { System.err.println("Failed to read configuration file: " + configFile); e.printStackTrace(); } return false; } private void saveConfig(Properties properties, String configFile) { try { File fp = new File(configFile); if (fp.exists()) { File targetFp = new File(configFile + ".bak"); if (targetFp.exists()) { targetFp.delete(); } fp.renameTo(targetFp); } FileOutputStream output = new FileOutputStream(configFile); try { properties.store(output, "Configuration for Collect"); } finally { output.close(); } } catch (IOException e) { System.err.println("failed to save configuration to " + configFile); e.printStackTrace(); } } // ------------------------------------------------------------------- // Serial communication // ------------------------------------------------------------------- public boolean sendToNode(String data) { if (serialConnection != null && serialConnection.isOpen()) { serialConsole.addSerialData("SEND: " + data); serialConnection.writeSerialData(data); return true; } return false; } protected void parseIncomingLine(long systemTime, String line) { SensorData sensorData = SensorData.parseSensorData(this, line, systemTime); if (sensorData != null) { // Sensor data received handleSensorData(sensorData); return; } System.out.println("SERIAL: " + line); serialConsole.addSerialData(line); } // ------------------------------------------------------------------- // SensorData handling // ------------------------------------------------------------------- public int getSensorDataCount() { return sensorDataList.size(); } public SensorData getSensorData(int i) { return sensorDataList.get(i); } private void handleSensorData(final SensorData sensorData) { System.out.println("SENSOR DATA: " + sensorData); if (sensorData.getNode().addSensorData(sensorData)) { sensorDataList.add(sensorData); saveSensorData(sensorData); handleLinks(sensorData); if (visualizers != null) { SwingUtilities.invokeLater(new Runnable() { public void run() { for (int i = 0, n = visualizers.length; i < n; i++) { visualizers[i].nodeDataReceived(sensorData); } } }); } } } private void handleLinks(SensorData sensorData) { String nodeID = sensorData.getBestNeighborID(); if (nodeID != null) { Node neighbor = addNode(nodeID); Node source = sensorData.getNode(); Link link = source.getLink(neighbor); link.setETX(sensorData.getBestNeighborETX()); link.setLastActive(sensorData.getNodeTime()); } } private void initSensorData() { loadSensorData(SENSORDATA_FILE); } private boolean loadSensorData(String filename) { File fp = new File(filename); if (fp.exists() && fp.canRead()) { BufferedReader in = null; try { in = new BufferedReader(new FileReader(fp)); String line; int no = 0; while ((line = in.readLine()) != null) { no++; if (line.length() == 0 || line.charAt(0) == '#') { // Ignore empty lines and comments } else { SensorData data = SensorData.parseSensorData(this, line); if (data != null) { if (data.getNode().addSensorData(data)) { sensorDataList.add(data); handleLinks(data); } } else { // TODO exit here? System.err.println("Failed to parse sensor data from log line " + no + ": " + line); } } } in.close(); } catch (IOException e) { System.err.println("Failed to read sensor data log from " + fp.getAbsolutePath()); e.printStackTrace(); return false; } } return true; } private void saveSensorData(SensorData data) { PrintWriter output = this.sensorDataOutput; if (output == null) { try { output = sensorDataOutput = new PrintWriter(new FileWriter(SENSORDATA_FILE, true)); } catch (IOException e) { System.err.println("Failed to add sensor data to log '" + SENSORDATA_FILE + '\''); e.printStackTrace(); } } if (output != null) { output.println(data.toString()); output.flush(); } } private void clearSensorData() { sensorDataList.clear(); Node[] nodes = getNodes(); if (nodes != null) { for(Node node : nodes) { node.removeAllSensorData(); } } if (visualizers != null) { for(Visualizer v : visualizers) { v.clearNodeData(); } } } private void clearSensorDataLog() { PrintWriter output = this.sensorDataOutput; if (output != null) { output.close(); } // Remove the sensor data log new File(SENSORDATA_FILE).delete(); this.sensorDataOutput = null; } protected class SerialItemHandler implements ActionListener, Runnable { private boolean isRunning; public void actionPerformed(ActionEvent e) { if (!isRunning) { isRunning = true; new Thread(this, "serial").start(); } } public void run() { try { if (serialConnection != null && serialConnection.isOpen()) { serialConnection.close(); } else { connectToSerial(); } } finally { isRunning = false; } } } protected class ProgramItemHandler implements ActionListener, Runnable { private boolean isRunning = false; public void actionPerformed(ActionEvent e) { if (!isRunning) { isRunning = true; new Thread(this, "program thread").start(); } } @Override public void run() { try { MoteProgrammer mp = new MoteProgrammer(); mp.setParentComponent(window); mp.setFirmwareFile(FIRMWARE_FILE); mp.searchForMotes(); String[] motes = mp.getMotes(); if (motes == null || motes.length == 0) { JOptionPane.showMessageDialog(window, "Could not find any connected Sky nodes", "Error", JOptionPane.ERROR_MESSAGE); return; } int reply = JOptionPane.showConfirmDialog(window, "Found " + motes.length + " connected Sky nodes.\n" + "Do you want to upload the firmware " + FIRMWARE_FILE + '?'); if (reply == JFileChooser.APPROVE_OPTION) { boolean wasOpen = serialConnection.isOpen(); serialConnection.close(); if (wasOpen) { Thread.sleep(1000); } mp.programMotes(); mp.waitForProcess(); if (wasOpen) { connectToSerial(); } } } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(window, "Programming failed: " + e, "Error", JOptionPane.ERROR_MESSAGE); } finally { isRunning = false; } } } // ------------------------------------------------------------------- // Main // ------------------------------------------------------------------- public static void main(String[] args) { String comPort = null; if (args.length > 0) { if (args.length > 1 || args[0].startsWith("-h")) { System.err.println("Usage: java CollectServer COMPORT"); System.exit(1); } comPort = args[0]; } new CollectServer(comPort); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -