/* (non-Javadoc)
  * @see ensemble.Reasoning#eventHandlerRegistered(ensemble.EventHandler)
  */
 @Override
 protected void eventHandlerRegistered(EventHandler evtHdl) throws Exception {
   if (evtHdl instanceof Sensor && evtHdl.getEventType().equals(AudioConstants.EVT_TYPE_AUDIO)) {
     // Stores sensor's memory
     Sensor sensor = (Sensor) evtHdl;
     sensor.registerListener(this);
     sensorMemories.put(
         sensor.getComponentName(), getAgent().getKB().getMemory(sensor.getComponentName()));
   } else if (evtHdl instanceof Actuator
       && evtHdl.getEventType().equals(AudioConstants.EVT_TYPE_AUDIO)) {
     // Stores actuator's memory
     Actuator actuator = (Actuator) evtHdl;
     actuator.registerListener(this);
     actuatorMemories.put(
         actuator.getComponentName(), getAgent().getKB().getMemory(actuator.getComponentName()));
     chunk_size = Integer.parseInt(actuator.getParameter(Constants.PARAM_CHUNK_SIZE, "0"));
   }
 }
  /* (non-Javadoc)
   * @see ensemble.Reasoning#needAction(ensemble.Actuator, double, double)
   */
  @Override
  public void needAction(Actuator sourceActuator, double instant, double duration)
      throws Exception {

    System.out.println(
        "Status: " + state.toString() + " time:" + (System.currentTimeMillis() - this.dt));
    if (state != ReasoningState.PLAYING) {

      if ((System.currentTimeMillis() - this.dt) >= 5000 && state == ReasoningState.NOT_DEFINED) {
        Command cmd =
            new Command(
                getAddress(),
                "/" + Constants.FRAMEWORK_NAME + "/" + getAgent().getAgentName() + "/RecReasoning",
                "RECORD");
        cmd.addParameter("SENSOR", "EAR");
        cmd.addParameter("FILE", "TEST");
        System.out.println("Iniciou REC ");

        sendCommand(cmd);
      } else if ((System.currentTimeMillis() - this.dt) >= 15000) {
        if (state != ReasoningState.STOPPED) {
          Command cmd =
              new Command(
                  getAddress(),
                  "/"
                      + Constants.FRAMEWORK_NAME
                      + "/"
                      + getAgent().getAgentName()
                      + "/RecReasoning",
                  "STOP");
          if (state == ReasoningState.RECORDING) {
            cmd.addParameter("EH", "EAR");
          } else cmd.addParameter("EH", "MOUTH");

          sendCommand(cmd);
        } else if ((System.currentTimeMillis() - this.dt) >= 18000) {
          Command cmd =
              new Command(
                  getAddress(),
                  "/"
                      + Constants.FRAMEWORK_NAME
                      + "/"
                      + getAgent().getAgentName()
                      + "/RecReasoning",
                  "PLAY");
          cmd.addParameter("ACTUATOR", "MOUTH");
          cmd.addParameter("FILE", "TEST");
          sendCommand(cmd);
        }
      }
    }

    // Checks if there's a mapping for this actuator
    if (mappings.containsKey(sourceActuator.getComponentName())) {

      Mapping map = mappings.get(sourceActuator.getComponentName());
      byte[] b = new byte[2 * chunk_size];
      int res = ((FileInputStream) map.stream).read(b);
      try {
        map.ehMemory.writeMemory(
            AudioTools.convertByteDouble(b, 0, b.length), instant, duration, TimeUnit.SECONDS);
        sourceActuator.act();

      } catch (MemoryException e1) {
        e1.printStackTrace();
      }

      sourceActuator.act();

      if (res == -1) {
        System.out.println("Fim do arquivo!");
        mappings.remove(sourceActuator.getComponentName());
      }
    }
  }