/* (non-Javadoc)
   * @see ensemble.Reasoning#newSense(ensemble.Sensor, double, double)
   */
  @Override
  public void newSense(Sensor sourceSensor, double instant, double duration) throws Exception {

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

      System.out.println("Entrei no newSense()");

      Mapping map = mappings.get(sourceSensor.getComponentName());
      double[] buf = (double[]) map.ehMemory.readMemory(instant, duration, TimeUnit.SECONDS);
      byte[] b = AudioTools.convertDoubleByte(buf, 0, buf.length);
      ((FileOutputStream) map.stream).write(b);
    }
  }
  /* (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());
      }
    }
  }