Esempio n. 1
0
  public void paintAfterMotes(Graphics g) {
    FontMetrics fm = g.getFontMetrics();
    g.setColor(Color.BLACK);

    /* Paint last output below motes */
    Mote[] allMotes = simulation.getMotes();
    for (Mote mote : allMotes) {
      String msg = null;
      {
        IPAddress ipAddr = mote.getInterfaces().getIPAddress();
        if (ipAddr != null) {
          msg = ipAddr.getIPString();
        }
      }

      if (msg == null) {
        RimeAddress rimeAddr = mote.getInterfaces().getRimeAddress();
        if (rimeAddr != null) {
          msg = rimeAddr.getAddressString();
        }
      }

      if (msg == null) {
        continue;
      }

      Position pos = mote.getInterfaces().getPosition();
      Point pixel = visualizer.transformPositionToPixel(pos);

      int msgWidth = fm.stringWidth(msg);
      g.drawString(msg, pixel.x - msgWidth / 2, pixel.y + 2 * Visualizer.MOTE_RADIUS + 3);
    }
  }
Esempio n. 2
0
 public void setInactive() {
   simulation.deleteObserver(simObserver);
   for (Mote mote : simulation.getMotes()) {
     IPAddress ipAddr = mote.getInterfaces().getIPAddress();
     if (ipAddr != null) {
       ipAddr.deleteObserver(addrObserver);
     }
     RimeAddress rimeAddr = mote.getInterfaces().getRimeAddress();
     if (rimeAddr != null) {
       rimeAddr.deleteObserver(addrObserver);
     }
   }
 }
Esempio n. 3
0
        public void update(Observable obs, Object obj) {

          /* Observe Rime and IP addresses */
          for (Mote mote : simulation.getMotes()) {
            IPAddress ipAddr = mote.getInterfaces().getIPAddress();
            if (ipAddr != null) {
              ipAddr.addObserver(addrObserver);
            }
            RimeAddress rimeAddr = mote.getInterfaces().getRimeAddress();
            if (rimeAddr != null) {
              rimeAddr.addObserver(addrObserver);
            }
          }
          visualizer.repaint();
        }
  private MoteTracker createMoteTracker(Mote mote) {
    final Radio moteRadio = mote.getInterfaces().getRadio();
    if (moteRadio == null) {
      return null;
    }

    /* Radio observer */
    MoteTracker tracker = new MoteTracker(mote);
    tracker.update(null, null);
    return tracker;
  }
    public MoteTracker(Mote mote) {
      this.simulation = mote.getSimulation();
      this.mote = mote;
      this.radio = mote.getInterfaces().getRadio();

      radioWasOn = radio.isRadioOn();
      if (radio.isTransmitting()) {
        lastRadioState = RadioState.TRANSMITTING;
      } else if (radio.isReceiving()) {
        lastRadioState = RadioState.RECEIVING;
      } else if (radio.isInterfered()) {
        lastRadioState = RadioState.INTERFERED;
      } else {
        lastRadioState = RadioState.IDLE;
      }
      lastUpdateTime = simulation.getSimulationTime();

      radio.addObserver(this);
    }
  public GISOO_Simulink_Plugin(Mote mote, Simulation simulation, final GUI gui) {

    super("GISOO-Simulink Plugin (" + mote + ")", gui, false);
    this.mote = mote;
    skyMote = (SkyMote) mote;
    sim = skyMote.getSimulation();
    LISTEN_PORT = 18000 + mote.getID();

    if (GUI.isVisualized()) {
      this.getContentPane().setSize(100, 100);
      Box northBox = Box.createHorizontalBox();
      northBox.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
      JPanel smallPanel = new JPanel(new BorderLayout());
      JLabel label = new JLabel("Listening on port: " + LISTEN_PORT);
      label.setPreferredSize(new Dimension(LABEL_WIDTH, 20));
      smallPanel.add(BorderLayout.CENTER, label);
      northBox.add(smallPanel);

      Box mainBox = Box.createHorizontalBox();
      mainBox.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
      JPanel smallPane = new JPanel(new BorderLayout());
      JLabel simulinkPort = new JLabel("Sending data to port: " + SIMULINK_PORT);
      simulinkPort.setPreferredSize(new Dimension(LABEL_WIDTH, LABEL_HEIGHT));
      smallPane.add(BorderLayout.CENTER, simulinkPort);
      mainBox.add(smallPane);

      Box southernBox = Box.createHorizontalBox();
      southernBox.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
      JPanel smallPane2 = new JPanel(new BorderLayout());
      serialDataShouldRCV = new JCheckBox("Serial reply should be received after(ms): ");
      CPUDellay = new JTextField("1");
      CPUDellay.setPreferredSize(new Dimension(LABEL_WIDTH, 25));
      smallPane2.add(BorderLayout.WEST, serialDataShouldRCV);
      smallPane2.add(BorderLayout.CENTER, CPUDellay);
      southernBox.add(smallPane2);

      getContentPane().add(BorderLayout.NORTH, northBox);
      getContentPane().add(BorderLayout.CENTER, mainBox);
      getContentPane().add(BorderLayout.SOUTH, southernBox);
      pack();
    }

    /* Mote serial port */
    serialPort = (SerialPort) mote.getInterfaces().getLog();
    if (serialPort == null) {
      throw new RuntimeException("No mote serial port");
    }
    for (byte b : serialPayload) {
      b = 0;
    }

    logger.info("Listening on port: " + LISTEN_PORT);

    /* Observe serial port for outgoing data */
    serialPort.addSerialDataObserver(
        serialDataObserver =
            new Observer() {
              public void update(Observable obs, Object obj) {
                serialSendFlag = false;
                /* Check the received message to find the moment that message follow the header format
                "0x 00 ff ff 00 00" which is equal to "0 -1 -1 0 0" in decimal. The next byte after
                this message is the message size*/

                lastByte = serialPort.getLastSerialData();
                logger.info("lastByte: " + lastByte);
                if (lastByte == 126 && nRCVedByte < 7) {
                  nRCVedByte = 1;
                  //  messageIndex=0;
                } else if (nRCVedByte > 0 && nRCVedByte < 7) {
                  nRCVedByte++;
                } else if (nRCVedByte == 7) {
                  sizeOfPayload = lastByte;
                  //                    logger.info("sizeOfPayload is: " + sizeOfPayload);
                  nRCVedByte++;
                } else if (nRCVedByte > 7 && nRCVedByte < 10) {
                  nRCVedByte++;
                } else if (nRCVedByte >= 10 && nRCVedByte < 10 + sizeOfPayload) {
                  //                    logger.info("nRCVedByte: " + nRCVedByte);
                  //                    logger.info("messageIndex: " + messageIndex);

                  serialPayload[messageIndex] = lastByte;
                  //                    logger.info("messageIndex: " + messageIndex + "   lastByte:
                  // " + lastByte);
                  if (nRCVedByte == (10 + sizeOfPayload - 1)) {
                    messageIndex = 0;
                    serialSendFlag = true;
                  } else {
                    messageIndex++;
                  }
                  nRCVedByte++;
                } else {
                  nRCVedByte = 0;
                }

                if (serialSendFlag) {

                  simulationTime = (int) sim.getSimulationTimeMillis();
                  serialPinNumber =
                      (skyMote.getID() * 100)
                          + 18; // 18 = pinNumber for Sending data to the serial port in Simulink
                  int u = 0; // value;
                  byte[] serialDataMSG =
                      serialMessageCreator(simulationTime, serialPinNumber, serialPayload);
                  fileWriter(serialPinNumber, serialPayload);
                  int q = serialMsgSender(serialDataMSG, dSocket);
                  serialSendFlag = false;
                  if (serialDataShouldRCV
                      .isSelected()) // If the second scenario has been selected (It should check
                                     // the second scenario CheckBox )
                  {
                    //                        logger.info("Simulation Time is : " +
                    // sim.getSimulationTime());
                    TimeEvent delayedEvent =
                        new TimeEvent(0) {
                          public void execute(long t) {
                            //                                logger.info("  Simulation Time is : "
                            // + sim.getSimulationTime());

                            simulationTime = (int) sim.getSimulationTimeMillis();
                            //                              logger.info("When recoded Simulation
                            // Time is : " + sim.getSimulationTime());
                            serialPinNumber = (skyMote.getID() * 100) + 19; // SerialDataRequest;
                            //                            logger.info("SerialPinNumber is : " +
                            // serialPinNumber);

                            byte[] serialRequestMesg =
                                serialRequestMsgCreator(simulationTime, serialPinNumber);
                            serialRequestSender(serialRequestMesg, dSocket);
                            fileWriter(serialPinNumber, serialRequestMesg);
                          }
                        };

                    sim.scheduleEvent(
                        delayedEvent,
                        sim.getSimulationTime()
                            + (Long.parseLong(CPUDellay.getText()) * (sim.MILLISECOND)));
                  }
                }
              }
            });

    try {
      dSocket = new DatagramSocket(LISTEN_PORT); // (18000 + skyMote.getID());

    } catch (SocketException ex) {
      System.out.println("Errore in dSocket creation");
    }
    IOUnit adc = skyMote.getCPU().getIOUnit("ADC12");
    if (adc instanceof ADC12) {
      ((ADC12) adc).setADCInput(0, new ADCConnector(0));
      ((ADC12) adc).setADCInput(1, new ADCConnector(1));
      ((ADC12) adc).setADCInput(2, new ADCConnector(2));
      ((ADC12) adc).setADCInput(3, new ADCConnector(3));
      ((ADC12) adc).setADCInput(4, new ADCConnector(4));
      ((ADC12) adc).setADCInput(5, new ADCConnector(5));
      ((ADC12) adc).setADCInput(6, new ADCConnector(6));
      ((ADC12) adc).setADCInput(7, new ADCConnector(7));
      ((ADC12) adc).setADCInput(8, new ADCConnector(8));
      ((ADC12) adc).setADCInput(9, new ADCConnector(9));
      ((ADC12) adc).setADCInput(10, new ADCConnector(10));
      ((ADC12) adc).setADCInput(11, new ADCConnector(11));
      ((ADC12) adc).setADCInput(12, new ADCConnector(12));
      ((ADC12) adc).setADCInput(13, new ADCConnector(13));
      ((ADC12) adc).setADCInput(14, new ADCConnector(14));
      ((ADC12) adc).setADCInput(15, new ADCConnector(15));
    }
    ((DAC12) skyMote.getCPU().getIOUnit("DAC12")).setDACOutput(0, new DACConnector(16));
    ((DAC12) skyMote.getCPU().getIOUnit("DAC12")).setDACOutput(1, new DACConnector(17));
  }
  public SerialSocketServer(Mote mote, Simulation simulation, final GUI gui) {
    super("Serial Socket (SERVER) (" + mote + ")", gui, false);
    this.mote = mote;

    LISTEN_PORT = 60000 + mote.getID();

    /* GUI components */
    if (GUI.isVisualized()) {
      Box northBox = Box.createHorizontalBox();
      northBox.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      statusLabel = configureLabel(northBox, "", "");

      Box mainBox = Box.createHorizontalBox();
      mainBox.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
      inLabel = configureLabel(mainBox, "socket -> mote:", "0 bytes");
      outLabel = configureLabel(mainBox, "mote -> socket", "0 bytes");

      getContentPane().add(BorderLayout.NORTH, northBox);
      getContentPane().add(BorderLayout.CENTER, mainBox);
      pack();
    }

    /* Mote serial port */
    serialPort = (SerialPort) mote.getInterfaces().getLog();
    if (serialPort == null) {
      throw new RuntimeException("No mote serial port");
    }

    try {
      logger.info("Listening on port: " + LISTEN_PORT);
      if (GUI.isVisualized()) {
        statusLabel.setText("Listening on port: " + LISTEN_PORT);
      }
      server = new ServerSocket(LISTEN_PORT);
      new Thread() {
        public void run() {
          while (server != null) {
            try {
              client = server.accept();
              in = new DataInputStream(client.getInputStream());
              out = new DataOutputStream(client.getOutputStream());
              out.flush();

              startSocketReadThread(in);
              if (GUI.isVisualized()) {
                statusLabel.setText("Client connected: " + client.getInetAddress());
              }
            } catch (IOException e) {
              logger.fatal("Listening thread shut down: " + e.getMessage());
              server = null;
              cleanupClient();
              break;
            }
          }
        }
      }.start();
    } catch (Exception e) {
      throw (RuntimeException)
          new RuntimeException("Connection error: " + e.getMessage()).initCause(e);
    }

    /* Observe serial port for outgoing data */
    serialPort.addSerialDataObserver(
        serialDataObserver =
            new Observer() {
              public void update(Observable obs, Object obj) {
                try {
                  if (out == null) {
                    /*logger.debug("out is null");*/
                    return;
                  }
                  out.write(serialPort.getLastSerialData());
                  out.flush();
                  outBytes++;
                  if (GUI.isVisualized()) {
                    outLabel.setText(outBytes + " bytes");
                  }
                } catch (IOException e) {
                  cleanupClient();
                }
              }
            });
  }
  public MoteSerialSocketConnection(Mote mote, String server, int serverPort) throws IOException {
    isConnected = true;

    /* Simulated -> socket */
    motePort = (SerialPort) mote.getInterfaces().getLog();
    motePort.addSerialDataObserver(
        moteObserver =
            new Observer() {
              public void update(Observable obs, Object obj) {
                try {
                  if (socketOut == null) {
                    return;
                  }
                  socketOut.write(motePort.getLastSerialData());
                  socketOut.flush();
                  toSocket++;

                  if (listeners != null) {
                    for (MoteSerialSocketConnectionListener dl : listeners) {
                      dl.dataTransferred(
                          MoteSerialSocketConnection.this,
                          toMote,
                          toSocket,
                          motePort.getLastSerialData());
                    }
                  }
                } catch (IOException e) {
                  e.printStackTrace();
                  logger.fatal("Write to socket error: " + e.getMessage(), e);
                  cleanup();
                }
              }
            });

    /* Socket -> simulated */
    socket = new Socket(server, serverPort);
    socketIn = new DataInputStream(socket.getInputStream());
    socketOut = new DataOutputStream(socket.getOutputStream());
    socketOut.flush();
    Thread socketThread =
        new Thread(
            new Runnable() {
              public void run() {
                int numRead = 0;
                byte[] data = new byte[1024];
                while (true) {
                  numRead = -1;
                  try {
                    numRead = socketIn.read(data);
                  } catch (IOException e) {
                    e.printStackTrace();
                    return;
                  }

                  if (numRead >= 0) {
                    for (int i = 0; i < numRead; i++) {
                      toMote++;
                      motePort.writeByte(data[i]);
                      if (listeners != null) {
                        for (MoteSerialSocketConnectionListener dl : listeners) {
                          dl.dataTransferred(
                              MoteSerialSocketConnection.this, toMote, toSocket, data[i]);
                        }
                      }
                    }

                  } else {
                    logger.fatal("Incoming data thread shut down");
                    cleanup();
                    break;
                  }
                }
              }
            });
    socketThread.start();
  }
Esempio n. 9
0
 public Position getPosition() {
   return mote.getInterfaces().getPosition();
 }