示例#1
0
  /**
   * composes the new value of a record definition into a updated set command that can be send back
   * to heat pump
   *
   * @param response of heat pump that should be updated with new value
   * @param RecordDefinition that shall be used for compose the new value into the heat pump set
   *     command
   * @param string value to be compose
   * @return byte[] ready to send to heat pump
   * @throws StiebelHeatPumpException
   */
  public byte[] composeRecord(String value, byte[] response, RecordDefinition recordDefinition)
      throws StiebelHeatPumpException {
    short newValue = 0;

    if (recordDefinition.getDataType() != Type.Settings) {
      logger.warn(
          "The record {} can not be set as it is not a setable value!", recordDefinition.getName());
      throw new StiebelHeatPumpException("record is not a setting!");
    }

    double number = Double.parseDouble(value);

    if (number > recordDefinition.getMax() || number < recordDefinition.getMin()) {
      logger.warn(
          "The record {} can not be set to value {} as allowed range is {}<-->{} !",
          recordDefinition.getName(),
          value,
          recordDefinition.getMax(),
          recordDefinition.getMin());
      throw new StiebelHeatPumpException("invalid value !");
    }

    // change response byte to setting command
    response[1] = SET;

    // reverse the scale
    if (recordDefinition.getScale() != 1.0) {
      number = number / recordDefinition.getScale();
      newValue = (short) number;
    }

    // set new bit values in a byte
    if (recordDefinition.getBitPosition() > 0) {

      byte[] abyte = new byte[] {response[recordDefinition.getPosition()]};
      abyte = setBit(abyte, recordDefinition.getBitPosition(), newValue);
      response[recordDefinition.getPosition()] = abyte[0];
      return response;
    }

    // create byte values for single and double byte values
    // and update response
    switch (recordDefinition.getLength()) {
      case 1:
        byte newByteValue = (byte) number;
        response[recordDefinition.getPosition()] = newByteValue;
        break;
      case 2:
        byte[] newByteValues = shortToByte(newValue);
        int position = recordDefinition.getPosition();
        response[position] = newByteValues[1];
        response[position + 1] = newByteValues[0];
        break;
    }

    response[2] = this.calculateChecksum(response);
    response = this.addDuplicatedBytes(response);
    logger.debug(
        "Updated record {} at position {} to value {}.",
        recordDefinition.getName(),
        recordDefinition.getPosition(),
        value);
    return response;
  }