Search in sources :

Example 1 with StringFieldValue

use of io.openems.common.types.StringFieldValue in project openems by OpenEMS.

the class FeneconPersistence method addChannelValueToQueue.

/**
 * Add a channel value to the send queue
 *
 * @param channel
 * @param valueOpt
 */
private void addChannelValueToQueue(Channel channel, Optional<?> valueOpt) {
    // Ignore anything that is not a ReadChannel
    if (!(channel instanceof ReadChannel<?>)) {
        return;
    }
    ReadChannel<?> readChannel = (ReadChannel<?>) channel;
    // Ignore channels that shall not be persisted
    if (readChannel.isDoNotPersist()) {
        return;
    }
    // Read and format value from channel
    FieldValue<?> fieldValue;
    if (!valueOpt.isPresent()) {
        fieldValue = new NullFieldValue();
    } else {
        Object value = valueOpt.get();
        if (value instanceof Number) {
            fieldValue = new NumberFieldValue((Number) value);
        } else if (value instanceof String) {
            fieldValue = new StringFieldValue((String) value);
        } else if (value instanceof Inet4Address) {
            fieldValue = new StringFieldValue(((Inet4Address) value).getHostAddress());
        } else if (value instanceof Boolean) {
            fieldValue = new NumberFieldValue(((Boolean) value) ? 1 : 0);
        } else if (value instanceof ChannelEnum) {
            fieldValue = new NumberFieldValue(((ChannelEnum) value).getValue());
        } else if (value instanceof DeviceNature || value instanceof JsonElement || value instanceof Map || value instanceof Set || value instanceof List || value instanceof ThingMap) {
            // ignore
            return;
        } else {
            log.warn("FENECON Persistence for value type [" + value.getClass().getName() + "] of channel [" + channel.address() + "] is not implemented.");
            return;
        }
    }
    // Add timestamp + value to queue
    synchronized (queue) {
        queue.put(readChannel.address(), fieldValue);
    }
}
Also used : Inet4Address(java.net.Inet4Address) Set(java.util.Set) NumberFieldValue(io.openems.common.types.NumberFieldValue) ReadChannel(io.openems.api.channel.ReadChannel) NullFieldValue(io.openems.common.types.NullFieldValue) StringFieldValue(io.openems.common.types.StringFieldValue) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) DeviceNature(io.openems.api.device.nature.DeviceNature) List(java.util.List) ThingMap(io.openems.api.controller.ThingMap) ChannelEnum(io.openems.common.types.ChannelEnum) HashMap(java.util.HashMap) Map(java.util.Map) ThingMap(io.openems.api.controller.ThingMap)

Example 2 with StringFieldValue

use of io.openems.common.types.StringFieldValue in project openems by OpenEMS.

the class DefaultMessages method timestampedData.

/**
 * <pre>
 *	{
 *		timedata: {
 *			timestamp (Long): {
 *				channel: String,
 *				value: String | Number
 *			}
 *		}
 *	}
 * </pre>
 *
 * @param token
 * @return
 */
public static JsonObject timestampedData(long timestamp, HashMap<ChannelAddress, FieldValue<?>> queue) {
    JsonObject jTimestamp = new JsonObject();
    for (Entry<ChannelAddress, FieldValue<?>> entry : queue.entrySet()) {
        String address = entry.getKey().toString();
        FieldValue<?> fieldValue = entry.getValue();
        if (fieldValue instanceof NumberFieldValue) {
            jTimestamp.addProperty(address, ((NumberFieldValue) fieldValue).value);
        } else if (fieldValue instanceof StringFieldValue) {
            jTimestamp.addProperty(address, ((StringFieldValue) fieldValue).value);
        }
    }
    JsonObject jTimedata = new JsonObject();
    jTimedata.add(String.valueOf(timestamp), jTimestamp);
    JsonObject j = new JsonObject();
    j.add("timedata", jTimedata);
    return j;
}
Also used : StringFieldValue(io.openems.common.types.StringFieldValue) JsonObject(com.google.gson.JsonObject) ChannelAddress(io.openems.common.types.ChannelAddress) NumberFieldValue(io.openems.common.types.NumberFieldValue) NumberFieldValue(io.openems.common.types.NumberFieldValue) FieldValue(io.openems.common.types.FieldValue) StringFieldValue(io.openems.common.types.StringFieldValue)

Aggregations

JsonObject (com.google.gson.JsonObject)2 NumberFieldValue (io.openems.common.types.NumberFieldValue)2 StringFieldValue (io.openems.common.types.StringFieldValue)2 JsonElement (com.google.gson.JsonElement)1 ReadChannel (io.openems.api.channel.ReadChannel)1 ThingMap (io.openems.api.controller.ThingMap)1 DeviceNature (io.openems.api.device.nature.DeviceNature)1 ChannelAddress (io.openems.common.types.ChannelAddress)1 ChannelEnum (io.openems.common.types.ChannelEnum)1 FieldValue (io.openems.common.types.FieldValue)1 NullFieldValue (io.openems.common.types.NullFieldValue)1 Inet4Address (java.net.Inet4Address)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1