⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 collectserver.java

📁 Contiki是一个开源
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    // Setup menu    JMenuBar menuBar = new JMenuBar();    JMenu fileMenu = new JMenu("File");    fileMenu.setMnemonic(KeyEvent.VK_F);    menuBar.add(fileMenu);    serialItem = new JMenuItem("Connect to serial");    serialItem.addActionListener(new SerialItemHandler());    fileMenu.add(serialItem);    JMenuItem item = new JMenuItem("Program Sky nodes...");    item.addActionListener(new ProgramItemHandler());    fileMenu.add(item);    fileMenu.addSeparator();    final JMenuItem clearMapItem = new JMenuItem("Remove Map Background");    clearMapItem.addActionListener(new ActionListener() {      public void actionPerformed(ActionEvent e) {        mapPanel.setMapBackground(null);        clearMapItem.setEnabled(false);        configTable.remove("collect.mapimage");      }    });    clearMapItem.setEnabled(mapPanel.getMapBackground() != null);    item = new JMenuItem("Select Map Background...");    item.addActionListener(new ActionListener() {      public void actionPerformed(ActionEvent e) {        if (fileChooser == null) {          fileChooser = new JFileChooser();          int reply = fileChooser.showOpenDialog(window);          if (reply == JFileChooser.APPROVE_OPTION) {            File file = fileChooser.getSelectedFile();            String name = file.getAbsolutePath();            configTable.put("collect.mapimage", name);            if (!mapPanel.setMapBackground(file.getAbsolutePath())) {              JOptionPane.showMessageDialog(window, "Failed to set background image", "Error", JOptionPane.ERROR_MESSAGE);            }            clearMapItem.setEnabled(mapPanel.getMapBackground() != null);            saveConfig(configTable, configFile);          }        }      }    });    fileMenu.add(item);    fileMenu.add(clearMapItem);    fileMenu.addSeparator();    item = new JMenuItem("Clear Sensor Data...");    item.addActionListener(new ActionListener() {      public void actionPerformed(ActionEvent e) {        int reply = JOptionPane.showConfirmDialog(window, "Also clear the sensor data log file?");        if (reply == JOptionPane.YES_OPTION) {          // Clear data from both memory and sensor log file          clearSensorDataLog();          clearSensorData();        } else if (reply == JOptionPane.NO_OPTION) {          // Only clear data from memory          clearSensorData();        }      }    });    fileMenu.add(item);    fileMenu.addSeparator();    item = new JMenuItem("Exit", KeyEvent.VK_X);    item.addActionListener(new ActionListener() {      public void actionPerformed(ActionEvent e) {        exit();      }    });    fileMenu.add(item);    JMenu toolsMenu = new JMenu("Tools");    toolsMenu.setMnemonic(KeyEvent.VK_T);    menuBar.add(toolsMenu);    runInitScriptItem = new JMenuItem("Run Init Script");    runInitScriptItem.addActionListener(new ActionListener() {      public void actionPerformed(ActionEvent e) {        mainPanel.setSelectedComponent(serialConsole.getPanel());        if (serialConnection != null && serialConnection.isOpen()) {          runInitScript();        } else {          JOptionPane.showMessageDialog(mainPanel, "No serial port connection", "No connected node", JOptionPane.ERROR_MESSAGE);        }      }    });    runInitScriptItem.setEnabled(false);    toolsMenu.add(runInitScriptItem);    toolsMenu.addSeparator();    final JCheckBoxMenuItem scrollItem = new JCheckBoxMenuItem("Scroll Layout");    scrollItem.addActionListener(new ActionListener() {      public void actionPerformed(ActionEvent e) {        if (scrollItem.getState()) {          mainPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);        } else {          mainPanel.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);        }      }    });    toolsMenu.add(scrollItem);    window.setJMenuBar(menuBar);    window.pack();    String bounds = configTable.getProperty("collect.bounds");    if (bounds != null) {      String[] b = bounds.split(",");      if (b.length == 4) {        window.setBounds(Integer.parseInt(b[0]), Integer.parseInt(b[1]),            Integer.parseInt(b[2]), Integer.parseInt(b[3]));      }    }    for(Object key: configTable.keySet()) {      String property = key.toString();      if (!property.startsWith("collect")) {        getNode(property, true);      }    }    initSensorData();    SwingUtilities.invokeLater(new Runnable() {      public void run() {        window.setVisible(true);      }    });    serialConnection = new SerialConnection() {      private boolean hasOpened;      private boolean hasSentInit;      @Override      protected void serialOpened() {        serialConsole.addSerialData("*** Serial console listening on port: " + getComPort() + " ***");        hasOpened = true;        // Remember the last selected serial port        configTable.put("collect.serialport", getComPort());        setSystemMessage("connected to " + getComPort());        // Send any initial commands        if (!hasSentInit) {          hasSentInit = true;          if (hasInitScript()) {            // Wait a short time before running the init script            sleep(3000);            runInitScript();          }        }      }      @Override      protected void serialClosed() {        String comPort = getComPort();        String prefix;        if (hasOpened) {          serialConsole.addSerialData("*** Serial connection terminated ***");          prefix = "Serial connection terminated.\n";          hasOpened = false;          setSystemMessage("not connected");        } else {          prefix = "Failed to connect to " + getComPort() + '\n';        }        if (!isClosed) {          String options[] = {"Retry", "Search for connected nodes", "Cancel"};          int value = JOptionPane.showOptionDialog(window,              prefix + "Do you want to retry or search for connected nodes?",              "Reconnect to serial port?",              JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,              null, options, options[0]);          if (value == JOptionPane.CLOSED_OPTION || value == 2) {//          exit();          } else {            if (value == 1) {              // Select new serial port              comPort = MoteFinder.selectComPort(window);              if (comPort == null) {//              exit();              }            }            // Try to open com port again            if (comPort != null) {              open(comPort);            }          }        }      }      @Override      protected void serialData(String line) {        parseIncomingLine(System.currentTimeMillis(), line);      }    };    if (comPort != null) {      serialConnection.setComPort(comPort);    }    connectToSerial();  }  protected void connectToSerial() {    if (!serialConnection.isOpen()) {      String comPort = serialConnection.getComPort();      if (comPort == null) {        comPort = MoteFinder.selectComPort(window);      }      if (comPort != null) {        serialConnection.open(comPort);      }    }  }  private void exit() {    /* TODO Clean up resources */    if (configFile != null) {      configTable.setProperty("collect.bounds", "" + window.getX() + ',' + window.getY() + ',' + window.getWidth() + ',' + window.getHeight());      saveConfig(configTable, configFile);    }    if (serialConnection != null) {      serialConnection.close();    }    PrintWriter output = this.sensorDataOutput;    if (output != null) {      output.close();    }    System.exit(0);  }  private void sleep(long delay) {    try {      Thread.sleep(delay);    } catch (InterruptedException e1) {      // Ignore    }  }  protected boolean hasInitScript() {    return initScript != null && new File(initScript).canRead();  }  protected void runInitScript() {    if (initScript != null) {      runScript(initScript);    }  }  protected void runScript(final String scriptFileName) {    new Thread("scripter") {      public void run() {        try {          BufferedReader in = new BufferedReader(new FileReader(scriptFileName));          String line;          while ((line = in.readLine()) != null) {            if (line.length() == 0 || line.charAt(0) == '#') {              // Ignore empty lines and comments            } else if (line.startsWith("echo ")) {              line = line.substring(5).trim();              if (line.indexOf('%') >= 0) {                line = line.replace("%TIME%", "" + (System.currentTimeMillis() / 1000));              }              sendToNode(line);            } else if (line.startsWith("sleep ")) {              long delay = Integer.parseInt(line.substring(6).trim());              Thread.sleep(delay * 1000);            } else {              System.err.println("Unknown script command: " + line);              break;            }          }          in.close();        } catch (Exception e) {          System.err.println("Failed to run script: " + scriptFileName);          e.printStackTrace();        }      }    }.start();  }  public String getConfig(String property) {    return getConfig(property, null);  }  public String getConfig(String property, String defaultValue) {    return configTable.getProperty(property, config.getProperty(property, defaultValue));  }  protected void setSystemMessage(final String message) {    SwingUtilities.invokeLater(new Runnable() {      public void run() {        boolean isOpen = serialConnection.isOpen();        if (message == null) {          window.setTitle(WINDOW_TITLE);        } else {          window.setTitle(WINDOW_TITLE + " (" + message + ')');        }        serialItem.setText(isOpen ? "Disconnect from serial" : "Connect to serial");        runInitScriptItem.setEnabled(isOpen && hasInitScript());      }    });  }  // -------------------------------------------------------------------  // Node Handling  // -------------------------------------------------------------------  public synchronized Node[] getNodes() {    if (nodeCache == null) {      Node[] tmp = nodeTable.values().toArray(new Node[nodeTable.size()]);      Arrays.sort(tmp);      nodeCache = tmp;    }    return nodeCache;  }  public Node addNode(String nodeID) {    return getNode(nodeID, true);  }  private Node getNode(final String nodeID, boolean notify) {    Node node = nodeTable.get(nodeID);    if (node == null) {      node = new Node(nodeID);      nodeTable.put(nodeID, node);      updateNodeLocation(node);      synchronized (this) {        nodeCache = null;      }      if (notify) {        final Node newNode = node;        SwingUtilities.invokeLater(new Runnable() {            public void run() {              // Insert the node sorted by name              String nodeName = newNode.getName();              boolean added = false;              for (int i = 0, n = nodeModel.size(); i < n; i++) {                int cmp = nodeName.compareTo(((Node) nodeModel.get(i)).getName());                if (cmp < 0) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -