private static void performSensorprocess() {
    /**
     * ************************************************ Here we start the main simulation loop that
     * will continuously look for control messages *************************************************
     */
    messageWindow.WriteMessage("Motion Sensor off");

    while (!Done) {
      // postArmStatus

      try {
        queue = messageManager.GetMessageQueue(); // get messages from message manager
        sendHeartBeat(messageManager, "22", "MotionSensor", "This device detectes motion");
      } catch (Exception e) {
        messageWindow.WriteMessage("Error getting message queue::" + e);
      }

      int qlen = queue.GetSize();

      for (int i = 0; i < qlen; i++) {
        Msg = queue.GetMessage();

        if (Msg.GetMessageId() == MOTION_SENSOR_ID) {
          handleMotionSensorControllerMessage(Msg);
        }

        if (Msg.GetMessageId() == HALT_SECURITY_ID) {
          handleExitMessage();
        }
        if (MotionSensorState) {
          if (Msg.GetMessageId() == MOTION_SIMULATE_ID) {
            if (SIMULATE_ON.equals(Msg.GetMessage())) {
              simulate = true;
              messageWindow.WriteMessage("Send MOTION_DETECTED");
              sendMessageToMessageManager(MOTION_DETECTED, MOTION_SENSE_MSG_ID);
            } else {
              simulate = false;
              messageWindow.WriteMessage("Send STOP_ALARM");
              sendMessageToMessageManager(STOP_ALARM, MOTION_SENSE_MSG_ID);
            }
          }
        }
      }
      if (MotionSensorState) {
        if (simulate) {
          messageWindow.WriteMessage("Send MOTION_DETECTED");
          sendMessageToMessageManager(MOTION_DETECTED, MOTION_SENSE_MSG_ID);
        } else {
          messageWindow.WriteMessage("Send OK");
          sendMessageToMessageManager(OK, MOTION_SENSE_MSG_ID);
        }
      }

      try {
        Thread.sleep(Delay);
      } catch (Exception e) {
        System.out.println("Sleep error:: " + e);
      }
    }
  }
  private static void handleMotionSensorControllerMessage(Message Msg) {

    if (Msg.GetMessage().equalsIgnoreCase(MOTION_SENSOR_ON)) // window break Sensor on
    {
      messageWindow.WriteMessage("Motion Sensor on");
      MotionSensorState = true;
    }

    if (Msg.GetMessage().equalsIgnoreCase(MOTION_SENSOR_OFF)) // window break Sensor off
    {
      messageWindow.WriteMessage("Motion Sensor off");
      MotionSensorState = false;
    }
  }
Exemple #3
0
  public static void main(String args[]) {
    MessageManagerInterface em = null;
    String MsgMgrIP; // Message Manager IP address
    Message Msg = null; // Message object
    MessageQueue eq = null; // Message Queue
    int MsgId = 0; // User specified message ID
    int Delay = 2500; // The loop delay (2.5 seconds)
    boolean Done = false; // Loop termination flag

    /////////////////////////////////////////////////////////////////////////////////
    // Get the IP address of the message manager
    /////////////////////////////////////////////////////////////////////////////////

    if (args.length == 0) {
      // message manager is on the local system

      System.out.println("\n\nAttempting to register on the local machine...");

      try {
        // Here we create an message manager interface object. This assumes
        // that the message manager is on the local machine

        em = new MessageManagerInterface();
      } catch (Exception e) {
        System.out.println("Error instantiating message manager interface: " + e);
      } // catch

    } else {

      // message manager is not on the local system

      MsgMgrIP = args[0];

      System.out.println("\n\nAttempting to register on the machine:: " + MsgMgrIP);

      try {
        // Here we create an message manager interface object. This assumes
        // that the message manager is NOT on the local machine

        em = new MessageManagerInterface(MsgMgrIP);
      } catch (Exception e) {
        System.out.println("Error instantiating message manager interface: " + e);
      } // catch
    } // if

    // Here we check to see if registration worked. If ef is null then the
    // message manager interface was not properly created.

    if (em != null) {
      float WinPosX = 0.5f; // This is the X position of the message window in terms
      // of a percentage of the screen height
      float WinPosY = 0.60f; // This is the Y position of the message window in terms
      // of a percentage of the screen height

      MotionSensor sensor = new MotionSensor("Montion Sensor", WinPosX, WinPosY, em);
      sensor.getMessageWindow().WriteMessage("Registered with the message manager.");
      sensor.getMessageWindow().WriteMessage("\nBeginning Simulation...");

      while (!Done) {
        // Get the message queue

        try {
          eq = em.GetMessageQueue();

        } // try
        catch (Exception e) {
          sensor.getMessageWindow().WriteMessage("Error getting message queue::" + e);
        } // catch

        int qlen = eq.GetSize();

        for (int i = 0; i < qlen; i++) {
          Msg = eq.GetMessage();

          if (Msg.GetMessageId() == 99) {
            Done = true;
            sensor.getMessageWindow().WriteMessage("Received End Message");
            try {
              em.UnRegister();

            } // try
            catch (Exception e) {
              sensor.getMessageWindow().WriteMessage("Error unregistering: " + e);
            } // catch

            sensor.getMessageWindow().WriteMessage("\n\nSimulation Stopped. \n");
          } // if
        } // for

        // Here we wait for a 2.5 seconds before we start the next sample

        try {
          Thread.sleep(Delay);

        } // try
        catch (Exception e) {
          sensor.getMessageWindow().WriteMessage("Sleep error:: " + e);
        } // catch
      } // while
    } else {

      System.out.println("Unable to register with the message manager.\n\n");
    } // if
  } // main