Search in sources :

Example 1 with ChannelEnum

use of io.openems.common.types.ChannelEnum 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 ChannelEnum

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

the class InfluxdbPersistence method channelUpdated.

/*
	 * Methods
	 */
/**
 * Receives events for all {@link ReadChannel}s, excluding {@link ConfigChannel}s via the {@link Databus}.
 */
@Override
public void channelUpdated(Channel channel, Optional<?> newValue) {
    if (!(channel instanceof ReadChannel<?>)) {
        return;
    }
    ReadChannel<?> readChannel = (ReadChannel<?>) channel;
    if (!newValue.isPresent()) {
        return;
    }
    Object value = newValue.get();
    String field = readChannel.address().toString();
    FieldValue<?> fieldValue;
    // TODO merge this with io.openems.backend.timedata.influx.addChannelToBuilder()
    if (value instanceof Number) {
        fieldValue = new NumberFieldValue(field, (Number) value);
    } else if (value instanceof String) {
        fieldValue = new StringFieldValue(field, (String) value);
    } else if (value instanceof ChannelEnum) {
        fieldValue = new NumberFieldValue(field, ((ChannelEnum) value).getValue());
    } else {
        return;
    }
    // Round time to Cycle-Time
    int cycleTime = this.getCycleTime();
    Long timestamp = System.currentTimeMillis() / cycleTime * cycleTime;
    synchronized (queue) {
        queue.put(timestamp, fieldValue);
    }
}
Also used : JsonObject(com.google.gson.JsonObject) ChannelEnum(io.openems.common.types.ChannelEnum) Point(org.influxdb.dto.Point) ReadChannel(io.openems.api.channel.ReadChannel)

Aggregations

JsonObject (com.google.gson.JsonObject)2 ReadChannel (io.openems.api.channel.ReadChannel)2 ChannelEnum (io.openems.common.types.ChannelEnum)2 JsonElement (com.google.gson.JsonElement)1 ThingMap (io.openems.api.controller.ThingMap)1 DeviceNature (io.openems.api.device.nature.DeviceNature)1 NullFieldValue (io.openems.common.types.NullFieldValue)1 NumberFieldValue (io.openems.common.types.NumberFieldValue)1 StringFieldValue (io.openems.common.types.StringFieldValue)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 Point (org.influxdb.dto.Point)1