Search in sources :

Example 6 with DateTimeType

use of org.openhab.core.library.types.DateTimeType in project openhab1-addons by openhab.

the class KNXCoreTypeMapper method toDPTValue.

/*
     * (non-Javadoc)
     *
     * @see org.openhab.binding.knx.config.KNXTypeMapper#toDPTValue(org.openhab.core.types.Type, java.lang.String)
     */
@Override
public String toDPTValue(Type type, String dptID) {
    DPT dpt;
    int mainNumber = getMainNumber(dptID);
    if (mainNumber == -1) {
        logger.error("toDPTValue couldn't identify mainnumber in dptID: {}", dptID);
        return null;
    }
    try {
        DPTXlator translator = TranslatorTypes.createTranslator(mainNumber, dptID);
        dpt = translator.getType();
    } catch (KNXException e) {
        e.printStackTrace();
        return null;
    }
    // check for HSBType first, because it extends PercentType as well
    if (type instanceof HSBType) {
        Color color = ((HSBType) type).toColor();
        return "r:" + Integer.toString(color.getRed()) + " g:" + Integer.toString(color.getGreen()) + " b:" + Integer.toString(color.getBlue());
    } else if (type instanceof OnOffType) {
        return type.equals(OnOffType.OFF) ? dpt.getLowerValue() : dpt.getUpperValue();
    } else if (type instanceof UpDownType) {
        return type.equals(UpDownType.UP) ? dpt.getLowerValue() : dpt.getUpperValue();
    } else if (type instanceof IncreaseDecreaseType) {
        DPT valueDPT = ((DPTXlator3BitControlled.DPT3BitControlled) dpt).getControlDPT();
        return type.equals(IncreaseDecreaseType.DECREASE) ? valueDPT.getLowerValue() + " 5" : valueDPT.getUpperValue() + " 5";
    } else if (type instanceof OpenClosedType) {
        return type.equals(OpenClosedType.CLOSED) ? dpt.getLowerValue() : dpt.getUpperValue();
    } else if (type instanceof StopMoveType) {
        return type.equals(StopMoveType.STOP) ? dpt.getLowerValue() : dpt.getUpperValue();
    } else if (type instanceof PercentType) {
        return type.toString();
    } else if (type instanceof DecimalType) {
        switch(mainNumber) {
            case 2:
                DPT valueDPT = ((DPTXlator1BitControlled.DPT1BitControlled) dpt).getValueDPT();
                switch(((DecimalType) type).intValue()) {
                    case 0:
                        return "0 " + valueDPT.getLowerValue();
                    case 1:
                        return "0 " + valueDPT.getUpperValue();
                    case 2:
                        return "1 " + valueDPT.getLowerValue();
                    default:
                        return "1 " + valueDPT.getUpperValue();
                }
            case 18:
                int intVal = ((DecimalType) type).intValue();
                if (intVal > 63) {
                    return "learn " + (intVal - 0x80);
                } else {
                    return "activate " + intVal;
                }
            default:
                return type.toString();
        }
    } else if (type instanceof StringType) {
        return type.toString();
    } else if (type instanceof DateTimeType) {
        return formatDateTime((DateTimeType) type, dptID);
    }
    logger.debug("toDPTValue: Couldn't get value for {} dpt id {} (no mapping).", type, dptID);
    return null;
}
Also used : KNXException(tuwien.auto.calimero.exception.KNXException) StringType(org.openhab.core.library.types.StringType) Color(java.awt.Color) DPT(tuwien.auto.calimero.dptxlator.DPT) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) Datapoint(tuwien.auto.calimero.datapoint.Datapoint) StopMoveType(org.openhab.core.library.types.StopMoveType) DateTimeType(org.openhab.core.library.types.DateTimeType) DPTXlator(tuwien.auto.calimero.dptxlator.DPTXlator) OnOffType(org.openhab.core.library.types.OnOffType) OpenClosedType(org.openhab.core.library.types.OpenClosedType) DPTXlator3BitControlled(tuwien.auto.calimero.dptxlator.DPTXlator3BitControlled) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) DPTXlator1BitControlled(tuwien.auto.calimero.dptxlator.DPTXlator1BitControlled) HSBType(org.openhab.core.library.types.HSBType)

Example 7 with DateTimeType

use of org.openhab.core.library.types.DateTimeType in project openhab1-addons by openhab.

the class KM200Comm method sendProvidersState.

/**
     * This function sets the state of a service on the device
     *
     */
public byte[] sendProvidersState(KM200BindingProvider provider, String item, Command command) {
    synchronized (device) {
        String type = null;
        String dataToSend = null;
        KM200CommObject object = null;
        Class<? extends Item> itemType = provider.getItemType(item);
        String service = checkParameterReplacement(provider, item);
        logger.debug("Prepare item for send: {} type: {} item: {}", service, type, itemType.getName());
        if (device.blacklistMap.contains(service)) {
            logger.debug("Service on blacklist: {}", service);
            return null;
        }
        if (device.serviceMap.containsKey(service)) {
            if (device.serviceMap.get(service).getWriteable() == 0) {
                logger.error("Service is listed as read-only: {}", service);
                return null;
            }
            object = device.serviceMap.get(service);
            type = object.getServiceType();
        } else {
            logger.error("Service is not in the determined device service list: {}", service);
            return null;
        }
        /* The service is availible, set now the values depeding on the item and binding type */
        logger.debug("state of: {} type: {}", command, type);
        /* Binding is a NumberItem */
        if (itemType.isAssignableFrom(NumberItem.class)) {
            BigDecimal bdVal = ((DecimalType) command).toBigDecimal();
            /* Check the capabilities of this service */
            if (object.getValueParameter() != null) {
                @SuppressWarnings("unchecked") List<BigDecimal> valParas = (List<BigDecimal>) object.getValueParameter();
                BigDecimal minVal = valParas.get(0);
                BigDecimal maxVal = valParas.get(1);
                if (bdVal.compareTo(minVal) < 0) {
                    bdVal = minVal;
                }
                if (bdVal.compareTo(maxVal) > 0) {
                    bdVal = maxVal;
                }
            }
            if (type.equals("floatValue")) {
                dataToSend = new JSONObject().put("value", bdVal).toString();
            } else if (type.equals("stringValue")) {
                dataToSend = new JSONObject().put("value", bdVal.toString()).toString();
            } else if (type.equals("switchProgram") && object.getVirtual() == 1) {
                /* A switchProgram as NumberItem is always virtual */
                dataToSend = sendVirtualState(object, itemType, service, command);
            } else if (type.equals("errorList") && object.getVirtual() == 1) {
                /* A errorList as NumberItem is always virtual */
                dataToSend = sendVirtualState(object, itemType, service, command);
            } else {
                logger.warn("Not supported type for numberItem: {}", type);
            }
        /* Binding is a StringItem */
        } else if (itemType.isAssignableFrom(StringItem.class)) {
            String val = ((StringType) command).toString();
            /* Check the capabilities of this service */
            if (object.getValueParameter() != null) {
                @SuppressWarnings("unchecked") List<String> valParas = (List<String>) object.getValueParameter();
                if (!valParas.contains(val)) {
                    logger.warn("Parameter is not in the service parameterlist: {}", val);
                    return null;
                }
            }
            if (type.equals("stringValue")) {
                dataToSend = new JSONObject().put("value", val).toString();
            } else if (type.equals("floatValue")) {
                dataToSend = new JSONObject().put("value", Float.parseFloat(val)).toString();
            } else if (type.equals("switchProgram")) {
                if (object.getVirtual() == 1) {
                    dataToSend = sendVirtualState(object, itemType, service, command);
                } else {
                    /* The JSONArray of switch items can be sended directly */
                    try {
                        /* Check whether ths input string is a valid JSONArray */
                        JSONArray userArray = new JSONArray(val);
                        dataToSend = userArray.toString();
                    } catch (Exception e) {
                        logger.warn("The input for the switchProgram is not a valid JSONArray : {}", e.getMessage());
                        return null;
                    }
                }
            } else {
                logger.warn("Not supported type for stringItem: {}", type);
            }
        /* Binding is a DateTimeItem */
        } else if (itemType.isAssignableFrom(DateTimeItem.class)) {
            String val = ((DateTimeType) command).toString();
            if (type.equals("stringValue")) {
                dataToSend = new JSONObject().put("value", val).toString();
            } else if (type.equals("switchProgram")) {
                dataToSend = sendVirtualState(object, itemType, service, command);
            } else {
                logger.warn("Not supported type for dateTimeItem: {}", type);
            }
        /* Binding is a SwitchItem */
        } else if (itemType.isAssignableFrom(SwitchItem.class)) {
            String val = null;
            if (provider.getParameter(item).containsKey("on")) {
                if (command == OnOffType.OFF) {
                    val = provider.getParameter(item).get("off");
                } else if (command == OnOffType.ON) {
                    val = provider.getParameter(item).get("on");
                }
            } else {
                logger.warn("Switch-Item only on configured on/off string values {}", command);
                return null;
            }
            if (type.equals("stringValue")) {
                dataToSend = new JSONObject().put("value", val).toString();
            } else {
                logger.warn("Not supported type for SwitchItem:{}", type);
            }
        } else {
            logger.warn("Bindingtype not supported: {}", itemType.getClass());
            return null;
        }
        /* If some data is availible then we have to send it to device */
        if (dataToSend != null) {
            /* base64 + encoding */
            logger.debug("Encoding: {}", dataToSend);
            byte[] encData = encodeMessage(dataToSend);
            if (encData == null) {
                logger.error("Couldn't encrypt data");
                return null;
            }
            return encData;
        } else {
            return null;
        }
    }
}
Also used : JSONArray(org.json.JSONArray) StringItem(org.openhab.core.library.items.StringItem) BigDecimal(java.math.BigDecimal) JSONException(org.json.JSONException) GeneralSecurityException(java.security.GeneralSecurityException) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DateTimeType(org.openhab.core.library.types.DateTimeType) JSONObject(org.json.JSONObject) DecimalType(org.openhab.core.library.types.DecimalType) ArrayList(java.util.ArrayList) List(java.util.List) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 8 with DateTimeType

use of org.openhab.core.library.types.DateTimeType in project openhab1-addons by openhab.

the class KM200Comm method sendVirtualState.

/**
     * This function sets the state of a virtual service
     *
     */
public String sendVirtualState(KM200CommObject object, Class<? extends Item> itemType, String service, Command command) {
    String dataToSend = null;
    String type = null;
    logger.debug("Check virtual state of: {} type: {} item: {}", service, type, itemType.getName());
    object = device.serviceMap.get(service);
    KM200CommObject parObject = device.serviceMap.get(object.getParent());
    type = object.getServiceType();
    /* Binding is a StringItem */
    if (itemType.isAssignableFrom(StringItem.class)) {
        String val = ((StringType) command).toString();
        switch(type) {
            case "switchProgram":
                KM200SwitchProgramService sPService = ((KM200SwitchProgramService) parObject.getValueParameter());
                String[] servicePath = service.split("/");
                String virtService = servicePath[servicePath.length - 1];
                if (virtService.equals("weekday")) {
                    /* Only parameter changing without communication to device */
                    sPService.setActiveDay(val);
                }
                break;
        }
    /* Binding is a NumberItem */
    } else if (itemType.isAssignableFrom(NumberItem.class)) {
        Integer val = ((DecimalType) command).intValue();
        switch(type) {
            case "switchProgram":
                KM200SwitchProgramService sPService = ((KM200SwitchProgramService) parObject.getValueParameter());
                String[] servicePath = service.split("/");
                String virtService = servicePath[servicePath.length - 1];
                if (virtService.equals("cycle")) {
                    /* Only parameter changing without communication to device */
                    sPService.setActiveCycle(val);
                } else if (virtService.equals(sPService.getPositiveSwitch())) {
                    sPService.setActivePositiveSwitch(val);
                    /* Create a JSON Array from current switch configuration */
                    dataToSend = sPService.getUpdatedJSONData(parObject);
                } else if (virtService.equals(sPService.getNegativeSwitch())) {
                    sPService.setActiveNegativeSwitch(val);
                    /* Create a JSON Array from current switch configuration */
                    dataToSend = sPService.getUpdatedJSONData(parObject);
                }
                break;
            case "errorList":
                KM200ErrorService eService = ((KM200ErrorService) device.serviceMap.get(object.getParent()).getValueParameter());
                String[] nServicePath = service.split("/");
                String nVirtService = nServicePath[nServicePath.length - 1];
                if (nVirtService.equals("error")) {
                    /* Only parameter changing without communication to device */
                    eService.setActiveError(val);
                }
                break;
        }
    } else if (itemType.isAssignableFrom(DateTimeItem.class)) {
        Calendar cal = ((DateTimeType) command).getCalendar();
        KM200SwitchProgramService sPService = ((KM200SwitchProgramService) parObject.getValueParameter());
        String[] servicePath = service.split("/");
        String virtService = servicePath[servicePath.length - 1];
        Integer minutes;
        if (virtService.equals(sPService.getPositiveSwitch())) {
            minutes = cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE);
            minutes = (minutes % sPService.getSwitchPointTimeRaster()) * sPService.getSwitchPointTimeRaster();
            sPService.setActivePositiveSwitch(minutes);
            /* Create a JSON Array from current switch configuration */
            dataToSend = sPService.getUpdatedJSONData(parObject);
        }
        if (virtService.equals(sPService.getNegativeSwitch())) {
            minutes = cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE);
            minutes = (minutes % sPService.getSwitchPointTimeRaster()) * sPService.getSwitchPointTimeRaster();
            sPService.setActiveNegativeSwitch(minutes);
            /* Create a JSON Array from current switch configuration */
            dataToSend = sPService.getUpdatedJSONData(parObject);
        }
    }
    return dataToSend;
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) StringType(org.openhab.core.library.types.StringType) Calendar(java.util.Calendar)

Example 9 with DateTimeType

use of org.openhab.core.library.types.DateTimeType in project openhab1-addons by openhab.

the class JpaHistoricItem method fromPersistedItem.

/**
     * Converts the string value of the persisted item to the state of a HistoricItem.
     * 
     * @param pItem the persisted JpaPersistentItem
     * @param item the source reference Item
     * @return historic item
     */
public static HistoricItem fromPersistedItem(JpaPersistentItem pItem, Item item) {
    State state;
    if (item instanceof NumberItem) {
        state = new DecimalType(Double.valueOf(pItem.getValue()));
    } else if (item instanceof DimmerItem) {
        state = new PercentType(Integer.valueOf(pItem.getValue()));
    } else if (item instanceof SwitchItem) {
        state = OnOffType.valueOf(pItem.getValue());
    } else if (item instanceof ContactItem) {
        state = OpenClosedType.valueOf(pItem.getValue());
    } else if (item instanceof RollershutterItem) {
        state = PercentType.valueOf(pItem.getValue());
    } else if (item instanceof ColorItem) {
        state = new HSBType(pItem.getValue());
    } else if (item instanceof DateTimeItem) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(Long.valueOf(pItem.getValue())));
        state = new DateTimeType(cal);
    } else if (item instanceof LocationItem) {
        PointType pType = null;
        String[] comps = pItem.getValue().split(";");
        if (comps.length >= 2) {
            pType = new PointType(new DecimalType(comps[0]), new DecimalType(comps[1]));
            if (comps.length == 3) {
                pType.setAltitude(new DecimalType(comps[2]));
            }
        }
        state = pType;
    } else if (item instanceof CallItem) {
        state = new CallType(pItem.getValue());
    } else {
        state = new StringType(pItem.getValue());
    }
    return new JpaHistoricItem(item.getName(), state, pItem.getTimestamp());
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) StringType(org.openhab.core.library.types.StringType) ContactItem(org.openhab.core.library.items.ContactItem) Calendar(java.util.Calendar) CallType(org.openhab.library.tel.types.CallType) ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) DateTimeItem(org.openhab.core.library.items.DateTimeItem) Date(java.util.Date) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DecimalType(org.openhab.core.library.types.DecimalType) PointType(org.openhab.core.library.types.PointType) CallItem(org.openhab.library.tel.items.CallItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 10 with DateTimeType

use of org.openhab.core.library.types.DateTimeType in project openhab1-addons by openhab.

the class AbstractDynamoDBItem method asHistoricItem.

/*
     * (non-Javadoc)
     *
     * @see org.openhab.persistence.dynamodb.internal.DynamoItem#asHistoricItem(org.openhab.core.items.Item)
     */
@Override
public HistoricItem asHistoricItem(final Item item) {
    final State[] state = new State[1];
    accept(new DynamoDBItemVisitor() {

        @Override
        public void visit(DynamoDBStringItem dynamoStringItem) {
            if (item instanceof ColorItem) {
                state[0] = new HSBType(dynamoStringItem.getState());
            } else if (item instanceof LocationItem) {
                state[0] = new PointType(dynamoStringItem.getState());
            } else if (item instanceof DateTimeItem) {
                Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                try {
                    cal.setTime(DATEFORMATTER.parse(dynamoStringItem.getState()));
                } catch (ParseException e) {
                    LOGGER.error("Failed to parse {} as date. Outputting UNDEF instead", dynamoStringItem.getState());
                    state[0] = UnDefType.UNDEF;
                }
                state[0] = new DateTimeType(cal);
            } else if (dynamoStringItem.getState().equals(UNDEFINED_PLACEHOLDER)) {
                state[0] = UnDefType.UNDEF;
            } else if (item instanceof CallItem) {
                String parts = dynamoStringItem.getState();
                String[] strings = parts.split("##");
                String dest = strings[0];
                String orig = strings[1];
                state[0] = new CallType(orig, dest);
            } else {
                state[0] = new StringType(dynamoStringItem.getState());
            }
        }

        @Override
        public void visit(DynamoDBBigDecimalItem dynamoBigDecimalItem) {
            if (item instanceof NumberItem) {
                state[0] = new DecimalType(dynamoBigDecimalItem.getState());
            } else if (item instanceof DimmerItem) {
                state[0] = new PercentType(dynamoBigDecimalItem.getState());
            } else if (item instanceof SwitchItem) {
                state[0] = dynamoBigDecimalItem.getState().compareTo(BigDecimal.ONE) == 0 ? OnOffType.ON : OnOffType.OFF;
            } else if (item instanceof ContactItem) {
                state[0] = dynamoBigDecimalItem.getState().compareTo(BigDecimal.ONE) == 0 ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
            } else if (item instanceof RollershutterItem) {
                state[0] = new PercentType(dynamoBigDecimalItem.getState());
            } else {
                LOGGER.warn("Not sure how to convert big decimal item {} to type {}. Using StringType as fallback", dynamoBigDecimalItem.getName(), item.getClass());
                state[0] = new StringType(dynamoBigDecimalItem.getState().toString());
            }
        }
    });
    return new DynamoDBHistoricItem(getName(), state[0], getTime());
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) StringType(org.openhab.core.library.types.StringType) CallType(org.openhab.library.tel.types.CallType) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem) Calendar(java.util.Calendar) ContactItem(org.openhab.core.library.items.ContactItem) PercentType(org.openhab.core.library.types.PercentType) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) PointType(org.openhab.core.library.types.PointType) DecimalType(org.openhab.core.library.types.DecimalType) CallItem(org.openhab.library.tel.items.CallItem) ParseException(java.text.ParseException)

Aggregations

DateTimeType (org.openhab.core.library.types.DateTimeType)49 Calendar (java.util.Calendar)37 DecimalType (org.openhab.core.library.types.DecimalType)30 StringType (org.openhab.core.library.types.StringType)29 State (org.openhab.core.types.State)16 DateTimeItem (org.openhab.core.library.items.DateTimeItem)12 PercentType (org.openhab.core.library.types.PercentType)12 NumberItem (org.openhab.core.library.items.NumberItem)11 BigDecimal (java.math.BigDecimal)10 OnOffType (org.openhab.core.library.types.OnOffType)9 ContactItem (org.openhab.core.library.items.ContactItem)8 DimmerItem (org.openhab.core.library.items.DimmerItem)8 StringItem (org.openhab.core.library.items.StringItem)8 HSBType (org.openhab.core.library.types.HSBType)8 Test (org.junit.Test)7 SwitchItem (org.openhab.core.library.items.SwitchItem)7 Date (java.util.Date)6 ColorItem (org.openhab.core.library.items.ColorItem)6 RollershutterItem (org.openhab.core.library.items.RollershutterItem)6 ArrayList (java.util.ArrayList)5