Exemplo n.º 1
0
  /**
   * verifies response on availability of data
   *
   * @param response of heat pump
   * @param request request defined for heat pump response
   * @return Map of Strings with name and values
   */
  public Map<String, String> parseRecords(final byte[] response, Request request)
      throws StiebelHeatPumpException {

    Map<String, String> map = new HashMap<String, String>();

    logger.debug("Parse bytes: {}", DataParser.bytesToHex(response));

    if (response.length < 2) {
      logger.error(
          "response does not have a valid length of bytes: {}", DataParser.bytesToHex(response));
      return map;
    }

    // parse response and fill map
    for (RecordDefinition recordDefinition : request.getRecordDefinitions()) {
      try {
        String value = parseRecord(response, recordDefinition);
        logger.debug(
            "Parsed value {} -> {} with pos: {} , len: {}",
            recordDefinition.getName(),
            value,
            recordDefinition.getPosition(),
            recordDefinition.getLength());
        map.put(recordDefinition.getName(), value);
      } catch (StiebelHeatPumpException e) {
        continue;
      }
    }
    return map;
  }
Exemplo n.º 2
0
  /**
   * parses a single record
   *
   * @param response of heat pump
   * @param RecordDefinition that shall be used for parsing the heat pump response
   * @return string value of the parse response
   * @throws StiebelHeatPumpException
   */
  public String parseRecord(byte[] response, RecordDefinition recordDefinition)
      throws StiebelHeatPumpException {
    try {
      if (response.length < 2) {
        logger.error(
            "response does not have a valid length of bytes: {}", DataParser.bytesToHex(response));
        throw new StiebelHeatPumpException();
      }
      ByteBuffer buffer = ByteBuffer.wrap(response);
      short number = 0;
      byte[] bytes = null;

      switch (recordDefinition.getLength()) {
        case 1:
          bytes = new byte[1];
          System.arraycopy(response, recordDefinition.getPosition(), bytes, 0, 1);
          number = Byte.valueOf(buffer.get(recordDefinition.getPosition()));
          break;
        case 2:
          bytes = new byte[2];
          System.arraycopy(response, recordDefinition.getPosition(), bytes, 0, 2);
          number = buffer.getShort(recordDefinition.getPosition());
          break;
      }

      if (recordDefinition.getBitPosition() > 0) {

        int returnValue = getBit(bytes, recordDefinition.getBitPosition());
        return String.valueOf(returnValue);
      }

      if (recordDefinition.getScale() != 1.0) {
        double myDoubleNumber = number * recordDefinition.getScale();
        myDoubleNumber = Math.round(myDoubleNumber * 100.0) / 100.0;
        String returnString = String.format("%s", myDoubleNumber);
        return returnString;
      }

      return String.valueOf(number);
    } catch (Exception e) {
      logger.error(
          "response {} could not be parsed for record definition {} ",
          DataParser.bytesToHex(response),
          recordDefinition.getName());
      throw new StiebelHeatPumpException();
    }
  }
  @Test
  public void testParseByteToHex() throws StiebelHeatPumpException {
    byte[] response =
        new byte[] {
          (byte) 0x01,
          (byte) 0x00,
          (byte) 0xb5,
          (byte) 0xfd,
          (byte) 0x01,
          (byte) 0xb6,
          (byte) 0xff,
          (byte) 0x10,
          (byte) 0x03
        };

    String hex = DataParser.bytesToHex(response);
    Assert.assertEquals("(00)01 00 B5 FD (04)01 B6 FF 10 (08)03 ", hex);
  }