Search in sources :

Example 1 with Request

use of org.openhab.binding.stiebelheatpump.protocol.Request in project openhab1-addons by openhab.

the class CommunicationService method setData.

/**
     * This method updates the parameter item of a heat pump request
     * 
     * @param value
     *            the new value of the item
     * @param parameter
     *            to be update in the heat pump
     */
public Map<String, String> setData(String value, String parameter) throws StiebelHeatPumpException {
    Request updateRequest = null;
    RecordDefinition updateRecord = null;
    Map<String, String> data = new HashMap<String, String>();
    // be updated
    if (parameter != null) {
        for (Request request : heatPumpSettingConfiguration) {
            for (RecordDefinition record : request.getRecordDefinitions()) {
                if (record.getName().equalsIgnoreCase(parameter)) {
                    updateRecord = record;
                    updateRequest = request;
                    logger.debug("Found valid record definition {} in request {}:{}", record.getName(), request.getName(), request.getDescription());
                    break;
                }
            }
        }
    }
    if (updateRecord == null || updateRequest == null) {
        // did not find any valid record, do nothing
        logger.warn("Could not find valid record definition for {}", parameter);
        return data;
    }
    try {
        // get actual value for the corresponding request
        // as we do no have individual requests for each settings we need to
        // decode the new value
        // into a current response , the response is available in the
        // connector object
        byte[] requestMessage = createRequestMessage(updateRequest);
        byte[] response = getData(requestMessage);
        data = parser.parseRecords(response, updateRequest);
        // lookup parameter value in the data
        String currentState = data.get(updateRecord.getName());
        if (currentState.equals(value)) {
            // current State is already same as new values!
            logger.debug("Current State for {} is already {}.", parameter, value);
            return data;
        }
        // create new set request out from the existing read response
        byte[] requestUpdateMessage = parser.composeRecord(value, response, updateRecord);
        logger.debug("Setting new value [{}] for parameter [{}]", value, parameter);
        Thread.sleep(WAITING_TIME_BETWEEN_REQUESTS);
        response = setData(requestUpdateMessage);
        if (parser.setDataCheck(response)) {
            logger.debug("Updated parameter {} successfully.", parameter);
        } else {
            logger.debug("Update for parameter {} failed!", parameter);
        }
    } catch (StiebelHeatPumpException e) {
        logger.error("Stiebel heat pump communication error during update of value! " + e.toString());
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
    }
    return data;
}
Also used : HashMap(java.util.HashMap) Request(org.openhab.binding.stiebelheatpump.protocol.Request) RecordDefinition(org.openhab.binding.stiebelheatpump.protocol.RecordDefinition)

Example 2 with Request

use of org.openhab.binding.stiebelheatpump.protocol.Request in project openhab1-addons by openhab.

the class CommunicationService method getStatus.

/**
     * This method reads all status values defined in the heat pump
     * configuration from the heat pump
     * 
     * @return map of heat pump status values
     */
public Map<String, String> getStatus() throws StiebelHeatPumpException {
    logger.debug("Loading Status");
    Map<String, String> data = new HashMap<String, String>();
    for (Request request : heatPumpStatusConfiguration) {
        logger.debug("Loading data for request {} ...", request.getName());
        try {
            Map<String, String> newData = readData(request);
            data.putAll(newData);
            Thread.sleep(WAITING_TIME_BETWEEN_REQUESTS);
        } catch (InterruptedException e) {
            throw new StiebelHeatPumpException(e.toString());
        }
    }
    return data;
}
Also used : HashMap(java.util.HashMap) Request(org.openhab.binding.stiebelheatpump.protocol.Request)

Example 3 with Request

use of org.openhab.binding.stiebelheatpump.protocol.Request in project openhab1-addons by openhab.

the class CommunicationService method setTime.

/**
     * This method set the time of the heat pump to the current time
     * 
     * @return true if time has been updated
     */
public Map<String, String> setTime() throws StiebelHeatPumpException {
    startCommunication();
    Map<String, String> data = new HashMap<String, String>();
    Request timeRequest = null;
    for (Request request : heatPumpSettingConfiguration) {
        if (request.getName().equals("Time")) {
            timeRequest = request;
            break;
        }
    }
    if (timeRequest == null) {
        logger.warn("Could not find request definition for time settings! Skip setting time.");
        return data;
    }
    logger.debug("Loading current time data ...");
    try {
        // get time from heat pump
        byte[] requestMessage = createRequestMessage(timeRequest);
        byte[] response = getData(requestMessage);
        // get current time from local machine
        DateTime dt = DateTime.now();
        logger.debug("Current time is : {}", dt.toString());
        String weekday = Integer.toString(dt.getDayOfWeek() - 1);
        String day = Integer.toString(dt.getDayOfMonth());
        String month = Integer.toString(dt.getMonthOfYear());
        String year = Integer.toString(dt.getYearOfCentury());
        String seconds = Integer.toString(dt.getSecondOfMinute());
        String hours = Integer.toString(dt.getHourOfDay());
        String minutes = Integer.toString(dt.getMinuteOfHour());
        data = parser.parseRecords(response, timeRequest);
        boolean updateRequired = false;
        for (Map.Entry<String, String> entry : data.entrySet()) {
            String entryName = entry.getKey();
            String entryValue = entry.getValue();
            RecordDefinition currentRecord = null;
            for (RecordDefinition record : timeRequest.getRecordDefinitions()) {
                if (record.getName().equals(entryName)) {
                    currentRecord = record;
                    break;
                }
            }
            if (entryName.equals("WeekDay") && !entryValue.equals(weekday)) {
                updateRequired = true;
                response = parser.composeRecord(weekday, response, currentRecord);
                logger.debug("WeekDay needs update from {} to {}", entryValue, weekday);
                continue;
            }
            if (entryName.equals("Hours") && !entryValue.equals(hours)) {
                updateRequired = true;
                response = parser.composeRecord(hours, response, currentRecord);
                logger.debug("Hours needs update from {} to {}", entryValue, hours);
                continue;
            }
            if (entryName.equals("Minutes") && !entryValue.equals(minutes)) {
                updateRequired = true;
                response = parser.composeRecord(minutes, response, currentRecord);
                logger.debug("Minutes needs update from {} to {}", entryValue, minutes);
                continue;
            }
            if (entryName.equals("Seconds") && !entryValue.equals(seconds)) {
                updateRequired = true;
                response = parser.composeRecord(seconds, response, currentRecord);
                logger.debug("Seconds needs update from {} to {}", entryValue, seconds);
                continue;
            }
            if (entryName.equals("Year") && !entryValue.equals(year)) {
                updateRequired = true;
                response = parser.composeRecord(year, response, currentRecord);
                logger.debug("Year needs update from {} to {}", entryValue, year);
                continue;
            }
            if (entryName.equals("Month") && !entryValue.equals(month)) {
                updateRequired = true;
                response = parser.composeRecord(month, response, currentRecord);
                logger.debug("Month needs update from {} to {}", entryValue, month);
                continue;
            }
            if (entryName.equals("Day") && !entryValue.equals(day)) {
                updateRequired = true;
                response = parser.composeRecord(day, response, currentRecord);
                logger.debug("Day needs update from {} to {}", entryValue, day);
                continue;
            }
        }
        if (updateRequired) {
            Thread.sleep(WAITING_TIME_BETWEEN_REQUESTS);
            logger.info("Time need update. Set time to " + dt.toString());
            setData(response);
            Thread.sleep(WAITING_TIME_BETWEEN_REQUESTS);
            response = getData(requestMessage);
            data = parser.parseRecords(response, timeRequest);
            dt = DateTime.now();
            logger.debug("Current time is : {}", dt.toString());
        }
        return data;
    } catch (InterruptedException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
}
Also used : HashMap(java.util.HashMap) Request(org.openhab.binding.stiebelheatpump.protocol.Request) HashMap(java.util.HashMap) Map(java.util.Map) DateTime(org.joda.time.DateTime) RecordDefinition(org.openhab.binding.stiebelheatpump.protocol.RecordDefinition)

Example 4 with Request

use of org.openhab.binding.stiebelheatpump.protocol.Request in project openhab1-addons by openhab.

the class CommunicationService method getSensors.

/**
     * This method reads all sensor values defined in the heat pump
     * configuration from the heat pump
     * 
     * @return map of heat pump sensor values
     */
public Map<String, String> getSensors() throws StiebelHeatPumpException {
    logger.debug("Loading Sensors");
    Map<String, String> data = new HashMap<String, String>();
    for (Request request : heatPumpSensorConfiguration) {
        logger.debug("Loading data for request {} ...", request.getName());
        try {
            Map<String, String> newData = readData(request);
            data.putAll(newData);
            Thread.sleep(WAITING_TIME_BETWEEN_REQUESTS);
        } catch (InterruptedException e) {
            throw new StiebelHeatPumpException(e.toString());
        }
    }
    return data;
}
Also used : HashMap(java.util.HashMap) Request(org.openhab.binding.stiebelheatpump.protocol.Request)

Example 5 with Request

use of org.openhab.binding.stiebelheatpump.protocol.Request in project openhab1-addons by openhab.

the class ConfigLocatorTest method LoadResourceFiles.

@Test
public void LoadResourceFiles() throws StiebelHeatPumpException {
    String workingDir = System.getProperty("user.dir");
    String FILEDUMP = "itemDump.txt";
    FILEDUMP = workingDir + File.separator + FILEDUMP;
    String GROUPPREFIX = "gEnergieWaermepumpe_";
    String PARENTGROUP = "gEnergieWaermepumpe";
    ConfigLocator configLocator = new ConfigLocator("2.06.xml");
    List<Request> configuration = configLocator.getConfig();
    Request firstRequest = configuration.get(0);
    Assert.assertEquals("Version", firstRequest.getName());
    Assert.assertEquals((byte) 0xfd, firstRequest.getRequestByte());
    Assert.assertEquals((byte) 0x05, configuration.get(7).getRequestByte());
    FileWriter dumpFile;
    try {
        dumpFile = new FileWriter(FILEDUMP);
        String newLine = System.getProperty("line.separator");
        // dump items definition
        for (Request request : configuration) {
            // create group item
            String groupItem = String.format("Group %s%s \"%s\" (%s) ", GROUPPREFIX, request.getName(), request.getName(), PARENTGROUP);
            dumpFile.write(groupItem + newLine);
            // add item definition per record
            for (RecordDefinition record : request.getRecordDefinitions()) {
                String itemType;
                String itemFormat;
                if (record.getDataType() == Type.Status) {
                    if (record.getUnit() == "") {
                        itemType = "String";
                        itemFormat = "[%s]";
                    } else if (record.getScale() != 1.0) {
                        itemType = "Number";
                        itemFormat = "[%.1f " + record.getUnit() + "]";
                    } else {
                        itemType = "Number";
                        itemFormat = "[%d " + record.getUnit() + "]";
                    }
                } else {
                    itemType = "Number";
                    if (record.getScale() != 1.0) {
                        itemFormat = "[%.1f " + record.getUnit() + "]";
                    } else {
                        itemFormat = "[%d " + record.getUnit() + "]";
                    }
                }
                // { stiebelheatpump="OutputElectricalHeatingStage1" }
                String itemDefinition = String.format("%s %s \"%s %s\" (%s,%s) { stiebelheatpump=\"%s\" }", itemType, record.getName(), record.getName(), itemFormat, GROUPPREFIX + request.getName(), PARENTGROUP, record.getName());
                dumpFile.write(itemDefinition + newLine);
            }
            // add site item definition per record
            for (RecordDefinition record : request.getRecordDefinitions()) {
                String itemType = "";
                String itemFormat = "";
                Type dataType = record.getDataType();
                switch(dataType) {
                    case Settings:
                        itemType = "Setpoint item=";
                        itemFormat = String.format("%s minValue=%s maxValue=%s step=%s", record.getName(), record.getMin(), record.getMax(), record.getStep());
                        break;
                    case Status:
                    case Sensor:
                        itemType = "Text item=";
                        itemFormat = record.getName();
                        break;
                }
                dumpFile.write(itemType + itemFormat + newLine);
            }
            dumpFile.write(newLine);
        }
        for (Request request : configuration) {
            for (RecordDefinition record : request.getRecordDefinitions()) {
                if (record.getDataType() == Type.Settings) {
                    dumpFile.write(record.getName() + ",");
                }
            }
        }
        dumpFile.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : Type(org.openhab.binding.stiebelheatpump.protocol.RecordDefinition.Type) FileWriter(java.io.FileWriter) Request(org.openhab.binding.stiebelheatpump.protocol.Request) IOException(java.io.IOException) RecordDefinition(org.openhab.binding.stiebelheatpump.protocol.RecordDefinition) Test(org.junit.Test)

Aggregations

Request (org.openhab.binding.stiebelheatpump.protocol.Request)12 HashMap (java.util.HashMap)5 RecordDefinition (org.openhab.binding.stiebelheatpump.protocol.RecordDefinition)5 Test (org.junit.Test)3 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBException (javax.xml.bind.JAXBException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 DateTime (org.joda.time.DateTime)1 Type (org.openhab.binding.stiebelheatpump.protocol.RecordDefinition.Type)1 Requests (org.openhab.binding.stiebelheatpump.protocol.Requests)1