Search in sources :

Example 6 with ReadChannel

use of io.openems.api.channel.ReadChannel in project openems by OpenEMS.

the class FeneconProPvMeter method defineModbusProtocol.

@Override
protected ModbusProtocol defineModbusProtocol() throws ConfigException {
    ModbusProtocol protocol = new // 
    ModbusProtocol(new ModbusRegisterRange(121, new // 
    UnsignedWordElement(// 
    121, voltageL1 = new ModbusReadLongChannel("VoltageL1", this).unit("mV").multiplier(2)), new // 
    UnsignedWordElement(// 
    122, voltageL2 = new ModbusReadLongChannel("VoltageL2", this).unit("mV").multiplier(2)), new // 
    UnsignedWordElement(// 
    123, voltageL3 = new ModbusReadLongChannel("VoltageL3", this).unit("mV").multiplier(2))), new // 
    ModbusRegisterRange(// 
    2035, new // 
    UnsignedDoublewordElement(// 
    2035, activeEnergyL1 = new ModbusReadLongChannel("ActiveEnergyL1", this).unit("Wh").multiplier(2)), new DummyElement(2037, 2065), new // 
    UnsignedWordElement(// 
    2066, orginalActivePowerL1 = new ModbusReadLongChannel("OriginalActivePowerL1", this).unit("W").delta(10000L))), new // 
    ModbusRegisterRange(// 
    2135, new // 
    UnsignedDoublewordElement(// 
    2135, activeEnergyL2 = new ModbusReadLongChannel("ActiveEnergyL2", this).unit("Wh").multiplier(2)), new DummyElement(2137, 2165), new // 
    UnsignedWordElement(// 
    2166, orginalActivePowerL2 = new ModbusReadLongChannel("OriginalActivePowerL2", this).unit("W").delta(10000L))), new // 
    ModbusRegisterRange(// 
    2235, new // 
    UnsignedDoublewordElement(// 
    2235, activeEnergyL3 = new ModbusReadLongChannel("ActiveEnergyL3", this).unit("Wh").multiplier(2)), new DummyElement(2237, 2265), new // 
    UnsignedWordElement(// 
    2266, orginalActivePowerL3 = new ModbusReadLongChannel("OriginalActivePowerL3", this).unit("W").delta(10000L))));
    activePowerL1 = new FunctionalReadChannel<Long>("ActivePowerL1", this, (channels) -> {
        ReadChannel<Long> power = channels[0];
        try {
            if (power.value() >= 0) {
                this.negativePowerL1.setValue(false);
                return power.value();
            } else {
                this.negativePowerL1.setValue(true);
                return 0L;
            }
        } catch (InvalidValueException e) {
            return null;
        }
    }, orginalActivePowerL1);
    activePowerL2 = new FunctionalReadChannel<Long>("ActivePowerL2", this, (channels) -> {
        ReadChannel<Long> power = channels[0];
        try {
            if (power.value() >= 0) {
                this.negativePowerL2.setValue(false);
                return power.value();
            } else {
                this.negativePowerL2.setValue(true);
                return 0L;
            }
        } catch (InvalidValueException e) {
            return null;
        }
    }, orginalActivePowerL2);
    activePowerL3 = new FunctionalReadChannel<Long>("ActivePowerL3", this, (channels) -> {
        ReadChannel<Long> power = channels[0];
        try {
            if (power.value() >= 0) {
                this.negativePowerL3.setValue(false);
                return power.value();
            } else {
                this.negativePowerL3.setValue(true);
                return 0L;
            }
        } catch (InvalidValueException e) {
            return null;
        }
    }, orginalActivePowerL3);
    activePower = new FunctionalReadChannel<Long>("ActivePower", this, (channels) -> {
        ReadChannel<Long> L1 = channels[0];
        ReadChannel<Long> L2 = channels[1];
        ReadChannel<Long> L3 = channels[2];
        try {
            return L1.value() + L2.value() + L3.value();
        } catch (InvalidValueException e) {
            return null;
        }
    }, activePowerL1, activePowerL2, activePowerL3);
    reactivePower = new FunctionalReadChannel<Long>("ReactivePower", this, (channels) -> {
        ReadChannel<Long> L1 = channels[0];
        ReadChannel<Long> L2 = channels[1];
        ReadChannel<Long> L3 = channels[2];
        try {
            return L1.value() + L2.value() + L3.value();
        } catch (InvalidValueException e) {
            return null;
        }
    }, reactivePowerL1, reactivePowerL2, reactivePowerL3);
    return protocol;
}
Also used : ReadChannel(io.openems.api.channel.ReadChannel) ModbusRegisterRange(io.openems.impl.protocol.modbus.internal.range.ModbusRegisterRange) ThingInfo(io.openems.api.doc.ThingInfo) ConfigException(io.openems.api.exception.ConfigException) StaticThingStateChannel(io.openems.api.channel.StaticThingStateChannel) FunctionalReadChannel(io.openems.api.channel.FunctionalReadChannel) UnsignedDoublewordElement(io.openems.impl.protocol.modbus.internal.UnsignedDoublewordElement) ConfigChannel(io.openems.api.channel.ConfigChannel) Device(io.openems.api.device.Device) ModbusProtocol(io.openems.impl.protocol.modbus.internal.ModbusProtocol) ThingStateChannels(io.openems.api.channel.thingstate.ThingStateChannels) SymmetricMeterNature(io.openems.api.device.nature.meter.SymmetricMeterNature) ModbusReadChannel(io.openems.impl.protocol.modbus.ModbusReadChannel) AsymmetricMeterNature(io.openems.api.device.nature.meter.AsymmetricMeterNature) InvalidValueException(io.openems.api.exception.InvalidValueException) ModbusDeviceNature(io.openems.impl.protocol.modbus.ModbusDeviceNature) ModbusReadLongChannel(io.openems.impl.protocol.modbus.ModbusReadLongChannel) DummyElement(io.openems.impl.protocol.modbus.internal.DummyElement) UnsignedWordElement(io.openems.impl.protocol.modbus.internal.UnsignedWordElement) InvalidValueException(io.openems.api.exception.InvalidValueException) ModbusReadLongChannel(io.openems.impl.protocol.modbus.ModbusReadLongChannel) DummyElement(io.openems.impl.protocol.modbus.internal.DummyElement) UnsignedWordElement(io.openems.impl.protocol.modbus.internal.UnsignedWordElement) UnsignedDoublewordElement(io.openems.impl.protocol.modbus.internal.UnsignedDoublewordElement) ModbusProtocol(io.openems.impl.protocol.modbus.internal.ModbusProtocol) ModbusRegisterRange(io.openems.impl.protocol.modbus.internal.range.ModbusRegisterRange) ReadChannel(io.openems.api.channel.ReadChannel) FunctionalReadChannel(io.openems.api.channel.FunctionalReadChannel) ModbusReadChannel(io.openems.impl.protocol.modbus.ModbusReadChannel)

Example 7 with ReadChannel

use of io.openems.api.channel.ReadChannel in project openems by OpenEMS.

the class FeneconMiniEss method defineModbusProtocol.

/*
	 * Methods
	 */
@Override
protected ModbusProtocol defineModbusProtocol() throws ConfigException {
    ModbusProtocol protokol = new ModbusProtocol(new // 
    ModbusRegisterRange(// 
    100, new // 
    UnsignedWordElement(// 
    100, systemState = // 
    new ModbusReadLongChannel("SystemState", this).label(0, // 
    STANDBY).label(1, // 
    "Start Off-Grid").label(2, // 
    START).label(3, // 
    FAULT).label(4, "Off-grid PV")), new // 
    UnsignedWordElement(// 
    101, controlMode = // 
    new ModbusReadLongChannel("ControlMode", this).label(1, // 
    "Remote").label(2, // 
    "Local")), // 
    new DummyElement(102, 103), new // 
    UnsignedDoublewordElement(// 
    104, totalBatteryChargeEnergy = new ModbusReadLongChannel("TotalBatteryChargeEnergy", this).unit(// 
    "Wh")), new // 
    UnsignedDoublewordElement(// 
    106, totalBatteryDischargeEnergy = new ModbusReadLongChannel("TotalBatteryDischargeEnergy", this).unit(// 
    "Wh")), new // 
    UnsignedWordElement(// 
    108, batteryGroupState = // 
    new ModbusReadLongChannel("BatteryGroupState", this).label(0, // 
    "Initial").label(1, // 
    "Stop").label(2, // 
    "Starting").label(3, // 
    "Running").label(4, // 
    "Stopping").label(5, "Fail")), new // 
    UnsignedWordElement(// 
    109, soc = new ModbusReadLongChannel("Soc", this).unit("%").interval(0, 100)), new // 
    UnsignedWordElement(// 
    110, batteryVoltage = new ModbusReadLongChannel("BatteryVoltage", this).unit("mV").multiplier(2)), new // 
    SignedWordElement(// 
    111, batteryCurrent = new ModbusReadLongChannel("BatteryCurrent", this).unit("mA").multiplier(2)), new // 
    SignedWordElement(// 
    112, batteryPower = new ModbusReadLongChannel("BatteryPower", this).unit("W")), new // 
    UnsignedWordElement(// 
    113, // 
    new ModbusBitWrappingChannel("BatteryGroupAlarm", this, this.thingState).warningBit(0, // 
    WarningEss.FailTheSystemShouldBeStopped).warningBit(1, // 
    WarningEss.CommonLowVoltageAlarm).warningBit(2, // 
    WarningEss.CommonHighVoltageAlarm).warningBit(3, // 
    WarningEss.ChargingOverCurrentAlarm).warningBit(4, // 
    WarningEss.DischargingOverCurrentAlarm).warningBit(5, // 
    WarningEss.OverTemperatureAlarm).warningBit(6, // 
    WarningEss.InteralCommunicationAbnormal)), new // 
    UnsignedWordElement(// 
    114, pcsOperationState = new ModbusReadLongChannel("PcsOperationState", this).label(0, // 
    "Self-checking").label(1, // 
    "Standby").label(2, // 
    "Off grid PV").label(3, // 
    "Off grid").label(4, // 
    ON_GRID).label(5, // 
    "Fail").label(6, // 
    "bypass 1").label(7, "bypass 2")), // 
    new DummyElement(115, 117), new // 
    SignedWordElement(// 
    118, current = new ModbusReadLongChannel("Current", this).unit("mA").multiplier(2)), new DummyElement(119, 120), new // 
    UnsignedWordElement(// 
    121, voltage = new ModbusReadLongChannel("Voltage", this).unit("mV").multiplier(2)), new DummyElement(122, 123), new // 
    SignedWordElement(// 
    124, activePower = new ModbusReadLongChannel("ActivePower", this).unit("W")), new DummyElement(125, 126), new // 
    SignedWordElement(// 
    127, reactivePower = new ModbusReadLongChannel("ReactivePower", this).unit("var")), new DummyElement(128, 130), new // 
    UnsignedWordElement(// 
    131, frequency = new ModbusReadLongChannel("Frequency", this).unit("mHz").multiplier(1)), new DummyElement(132, 133), new // 
    UnsignedWordElement(// 
    134, phaseAllowedApparent = new ModbusReadLongChannel("PhaseAllowedApparentPower", this).unit("VA")), new DummyElement(135, 140), new // 
    UnsignedWordElement(// 
    141, allowedCharge = new ModbusReadLongChannel("AllowedCharge", this).unit("W").negate()), new // 
    UnsignedWordElement(// 
    142, allowedDischarge = new ModbusReadLongChannel("AllowedDischarge", this).unit("W")), new DummyElement(143, 149), new // 
    UnsignedWordElement(// 
    150, // 
    new ModbusBitWrappingChannel("PcsAlarm1", this, this.thingState).warningBit(0, // 
    WarningEss.GridUndervoltage).warningBit(1, // 
    WarningEss.GridOvervoltage).warningBit(2, // 
    WarningEss.GridUnderFrequency).warningBit(3, // 
    WarningEss.GridOverFrequency).warningBit(4, // 
    WarningEss.GridPowerSupplyOff).warningBit(5, // 
    WarningEss.GridConditionUnmeet).warningBit(6, // 
    WarningEss.DCUnderVoltage).warningBit(7, // 
    WarningEss.InputOverResistance).warningBit(8, // 
    WarningEss.CombinationError).warningBit(9, // 
    WarningEss.CommWithInverterError).warningBit(10, // 
    WarningEss.TmeError)), new UnsignedWordElement(151, // 
    new ModbusBitWrappingChannel("PcsAlarm2", this, this.thingState)), new UnsignedWordElement(152, // 
    new ModbusBitWrappingChannel("PcsFault1", this, this.thingState).faultBit(0, // 
    FaultEss.ControlCurrentOverload100Percent).faultBit(1, // 
    FaultEss.ControlCurrentOverload110Percent).faultBit(2, // 
    FaultEss.ControlCurrentOverload150Percent).faultBit(3, // 
    FaultEss.ControlCurrentOverload200Percent).faultBit(4, // 
    FaultEss.ControlCurrentOverload120Percent).faultBit(5, // 
    FaultEss.ControlCurrentOverload300Percent).faultBit(6, // 
    FaultEss.ControlTransientLoad300Percent).faultBit(7, // 
    FaultEss.GridOverCurrent).faultBit(8, // 
    FaultEss.LockingWaveformTooManyTimes).faultBit(9, // 
    FaultEss.InverterVoltageZeroDriftError).faultBit(10, // 
    FaultEss.GridVoltageZeroDriftError).faultBit(11, // 
    FaultEss.ControlCurrentZeroDriftError).faultBit(12, // 
    FaultEss.InverterCurrentZeroDriftError).faultBit(13, // 
    FaultEss.GridCurrentZeroDriftError).faultBit(14, // 
    FaultEss.PDPProtection).faultBit(15, // 
    FaultEss.HardwareControlCurrentProtection)), new // 
    UnsignedWordElement(// 
    153, // 
    new ModbusBitWrappingChannel("PcsFault2", this, this.thingState).faultBit(0, // 
    FaultEss.HardwareACVoltProtection).faultBit(1, // 
    FaultEss.HardwareDCCurrentProtection).faultBit(2, // 
    FaultEss.HardwareTemperatureProtection).faultBit(3, // 
    FaultEss.NoCapturingSignal).faultBit(4, // 
    FaultEss.DCOvervoltage).faultBit(5, // 
    FaultEss.DCDisconnected).faultBit(6, // 
    FaultEss.InverterUndervoltage).faultBit(7, // 
    FaultEss.InverterOvervoltage).faultBit(8, // 
    FaultEss.CurrentSensorFail).faultBit(9, // 
    FaultEss.VoltageSensorFail).faultBit(10, // 
    FaultEss.PowerUncontrollable).faultBit(11, // 
    FaultEss.CurrentUncontrollable).faultBit(12, // 
    FaultEss.FanError).faultBit(13, // 
    FaultEss.PhaseLack).faultBit(14, // 
    FaultEss.InverterRelayFault).faultBit(15, // 
    FaultEss.GridRelayFault)), new // 
    UnsignedWordElement(// 
    154, // 
    new ModbusBitWrappingChannel("PcsFault3", this, this.thingState).faultBit(0, // 
    FaultEss.ControlPanelOvertemp).faultBit(1, // 
    FaultEss.PowerPanelOvertemp).faultBit(2, // 
    FaultEss.DCInputOvercurrent).faultBit(3, // 
    FaultEss.CapacitorOvertemp).faultBit(4, // 
    FaultEss.RadiatorOvertemp).faultBit(5, // 
    FaultEss.TransformerOvertemp).faultBit(6, // 
    FaultEss.CombinationCommError).faultBit(7, // 
    FaultEss.EEPROMError).faultBit(8, // 
    FaultEss.LoadCurrentZeroDriftError).faultBit(9, // 
    FaultEss.CurrentLimitRError).faultBit(10, // 
    FaultEss.PhaseSyncError).faultBit(11, // 
    FaultEss.ExternalPVCurrentZeroDriftError).faultBit(12, // 
    FaultEss.ExternalGridCurrentZeroDriftError))), new // 
    WriteableModbusRegisterRange(// 
    200, new UnsignedWordElement(200, setWorkState = // 
    new ModbusWriteLongChannel("SetWorkState", this).label(0, // 
    "Local control").label(1, // "Remote control on grid starting"
    START).label(2, // 
    "Remote control off grid starting").label(3, // 
    STOP).label(4, "Emergency Stop"))), new // 
    WriteableModbusRegisterRange(// 
    201, new SignedWordElement(201, // 
    setActivePower = new ModbusWriteLongChannel("SetActivePower", this).unit("W")), new SignedWordElement(202, // 
    setReactivePower = new ModbusWriteLongChannel("SetReactivePower", this).unit("Var"))), new // 
    WriteableModbusRegisterRange(// 
    9014, new UnsignedWordElement(9014, rtcYear = new ModbusWriteLongChannel("Year", this)), new UnsignedWordElement(9015, rtcMonth = new ModbusWriteLongChannel("Month", this)), new UnsignedWordElement(9016, rtcDay = new ModbusWriteLongChannel("Day", this)), new UnsignedWordElement(9017, rtcHour = new ModbusWriteLongChannel("Hour", this)), new UnsignedWordElement(9018, rtcMinute = new ModbusWriteLongChannel("Minute", this)), new UnsignedWordElement(9019, rtcSecond = new ModbusWriteLongChannel("Second", this))), new WriteableModbusRegisterRange(30558, new UnsignedWordElement(30558, setSetupMode = new ModbusWriteLongChannel("SetSetupMode", this).label(0, EssNature.OFF).label(1, EssNature.ON))), new WriteableModbusRegisterRange(30559, new UnsignedWordElement(30559, setPcsMode = // 
    new ModbusWriteLongChannel("SetPcsMode", this).label(0, // 
    "Emergency").label(1, // 
    "ConsumersPeakPattern").label(2, // 
    "Economic").label(3, // 
    "Eco").label(4, // 
    "Debug").label(5, // 
    "SmoothPv").label(6, "Remote"))), new ModbusRegisterRange(30157, new UnsignedWordElement(30157, setupMode = // 
    new ModbusReadLongChannel("SetupMode", this).label(0, // 
    EssNature.OFF).label(1, EssNature.ON)), new UnsignedWordElement(30158, pcsMode = // 
    new ModbusReadLongChannel("PcsMode", this).label(0, // 
    "Emergency").label(1, // 
    "ConsumersPeakPattern").label(2, // 
    "Economic").label(3, // 
    "Eco").label(4, // 
    "Debug").label(5, // 
    "SmoothPv").label(6, "Remote"))));
    gridMode = new FunctionalReadChannel<Long>("GridMode", this, (channels) -> {
        ReadChannel<Long> state = channels[0];
        try {
            if (state.value() == 1L) {
                return 0L;
            } else {
                return 1L;
            }
        } catch (InvalidValueException e) {
            return null;
        }
    }, systemState).label(0L, OFF_GRID).label(1L, ON_GRID);
    apparentPower = new FunctionalReadChannel<Long>("ApparentPower", this, (channels) -> {
        ReadChannel<Long> activePower = channels[0];
        ReadChannel<Long> reactivePower = channels[1];
        try {
            return ControllerUtils.calculateApparentPower(activePower.value(), reactivePower.value());
        } catch (InvalidValueException e) {
            log.error("failed to calculate apparentPower. some value is missing.", e);
        }
        return 0l;
    }, activePower, reactivePower);
    this.power = new SymmetricPowerImpl(3000, setActivePower, setReactivePower, getParent().getBridge());
    this.allowedApparentLimit = new SMaxLimitation(power);
    this.allowedApparentLimit.setSMax(phaseAllowedApparent.valueOptional().orElse(0L) * 3, 0L, 0L);
    this.phaseAllowedApparent.addChangeListener(new ChannelChangeListener() {

        @Override
        public void channelChanged(Channel channel, Optional<?> newValue, Optional<?> oldValue) {
            allowedApparentLimit.setSMax(phaseAllowedApparent.valueOptional().orElse(0L) * 3, 0L, 0L);
        }
    });
    this.power.addStaticLimitation(this.allowedApparentLimit);
    this.allowedChargeLimit = new PGreaterEqualLimitation(power);
    this.allowedChargeLimit.setP(this.allowedCharge.valueOptional().orElse(0L));
    this.allowedCharge.addChangeListener(new ChannelChangeListener() {

        @Override
        public void channelChanged(Channel channel, Optional<?> newValue, Optional<?> oldValue) {
            allowedChargeLimit.setP(allowedCharge.valueOptional().orElse(0L));
        }
    });
    this.power.addStaticLimitation(this.allowedChargeLimit);
    this.allowedDischargeLimit = new PSmallerEqualLimitation(power);
    this.allowedDischargeLimit.setP(this.allowedDischarge.valueOptional().orElse(0L));
    this.allowedDischarge.addChangeListener(new ChannelChangeListener() {

        @Override
        public void channelChanged(Channel channel, Optional<?> newValue, Optional<?> oldValue) {
            allowedDischargeLimit.setP(allowedDischarge.valueOptional().orElse(0L));
        }
    });
    return protokol;
}
Also used : ReadChannel(io.openems.api.channel.ReadChannel) ModbusRegisterRange(io.openems.impl.protocol.modbus.internal.range.ModbusRegisterRange) ThingInfo(io.openems.api.doc.ThingInfo) PGreaterEqualLimitation(io.openems.core.utilities.power.symmetric.PGreaterEqualLimitation) LoggerFactory(org.slf4j.LoggerFactory) WriteChannel(io.openems.api.channel.WriteChannel) PSmallerEqualLimitation(io.openems.core.utilities.power.symmetric.PSmallerEqualLimitation) ConfigChannel(io.openems.api.channel.ConfigChannel) EssNature(io.openems.api.device.nature.ess.EssNature) ChannelChangeListener(io.openems.api.channel.ChannelChangeListener) ModbusProtocol(io.openems.impl.protocol.modbus.internal.ModbusProtocol) SMaxLimitation(io.openems.core.utilities.power.symmetric.SMaxLimitation) ModbusBitWrappingChannel(io.openems.impl.protocol.modbus.ModbusBitWrappingChannel) ModbusReadLongChannel(io.openems.impl.protocol.modbus.ModbusReadLongChannel) ModbusWriteLongChannel(io.openems.impl.protocol.modbus.ModbusWriteLongChannel) UnsignedWordElement(io.openems.impl.protocol.modbus.internal.UnsignedWordElement) WriteableModbusRegisterRange(io.openems.impl.protocol.modbus.internal.range.WriteableModbusRegisterRange) RealTimeClockNature(io.openems.api.device.nature.realtimeclock.RealTimeClockNature) SymmetricPowerImpl(io.openems.core.utilities.power.symmetric.SymmetricPowerImpl) Logger(org.slf4j.Logger) SymmetricEssNature(io.openems.api.device.nature.ess.SymmetricEssNature) StatusBitChannel(io.openems.api.channel.StatusBitChannel) ConfigException(io.openems.api.exception.ConfigException) FunctionalReadChannel(io.openems.api.channel.FunctionalReadChannel) UnsignedDoublewordElement(io.openems.impl.protocol.modbus.internal.UnsignedDoublewordElement) ControllerUtils(io.openems.core.utilities.ControllerUtils) Device(io.openems.api.device.Device) ThingStateChannels(io.openems.api.channel.thingstate.ThingStateChannels) InvalidValueException(io.openems.api.exception.InvalidValueException) SignedWordElement(io.openems.impl.protocol.modbus.internal.SignedWordElement) ModbusDeviceNature(io.openems.impl.protocol.modbus.ModbusDeviceNature) Optional(java.util.Optional) StaticValueChannel(io.openems.api.channel.StaticValueChannel) DummyElement(io.openems.impl.protocol.modbus.internal.DummyElement) Channel(io.openems.api.channel.Channel) SMaxLimitation(io.openems.core.utilities.power.symmetric.SMaxLimitation) ReadChannel(io.openems.api.channel.ReadChannel) FunctionalReadChannel(io.openems.api.channel.FunctionalReadChannel) InvalidValueException(io.openems.api.exception.InvalidValueException) PGreaterEqualLimitation(io.openems.core.utilities.power.symmetric.PGreaterEqualLimitation) ModbusBitWrappingChannel(io.openems.impl.protocol.modbus.ModbusBitWrappingChannel) UnsignedWordElement(io.openems.impl.protocol.modbus.internal.UnsignedWordElement) ChannelChangeListener(io.openems.api.channel.ChannelChangeListener) SignedWordElement(io.openems.impl.protocol.modbus.internal.SignedWordElement) PSmallerEqualLimitation(io.openems.core.utilities.power.symmetric.PSmallerEqualLimitation) ModbusReadLongChannel(io.openems.impl.protocol.modbus.ModbusReadLongChannel) WriteableModbusRegisterRange(io.openems.impl.protocol.modbus.internal.range.WriteableModbusRegisterRange) ReadChannel(io.openems.api.channel.ReadChannel) WriteChannel(io.openems.api.channel.WriteChannel) ConfigChannel(io.openems.api.channel.ConfigChannel) ModbusBitWrappingChannel(io.openems.impl.protocol.modbus.ModbusBitWrappingChannel) ModbusReadLongChannel(io.openems.impl.protocol.modbus.ModbusReadLongChannel) ModbusWriteLongChannel(io.openems.impl.protocol.modbus.ModbusWriteLongChannel) StatusBitChannel(io.openems.api.channel.StatusBitChannel) FunctionalReadChannel(io.openems.api.channel.FunctionalReadChannel) StaticValueChannel(io.openems.api.channel.StaticValueChannel) Channel(io.openems.api.channel.Channel) ModbusWriteLongChannel(io.openems.impl.protocol.modbus.ModbusWriteLongChannel) DummyElement(io.openems.impl.protocol.modbus.internal.DummyElement) SymmetricPowerImpl(io.openems.core.utilities.power.symmetric.SymmetricPowerImpl) UnsignedDoublewordElement(io.openems.impl.protocol.modbus.internal.UnsignedDoublewordElement) ModbusProtocol(io.openems.impl.protocol.modbus.internal.ModbusProtocol) ModbusRegisterRange(io.openems.impl.protocol.modbus.internal.range.ModbusRegisterRange) WriteableModbusRegisterRange(io.openems.impl.protocol.modbus.internal.range.WriteableModbusRegisterRange)

Example 8 with ReadChannel

use of io.openems.api.channel.ReadChannel in project openems by OpenEMS.

the class ThingRepository method thingChannelsUpdated.

@Override
public void thingChannelsUpdated(Thing thing) {
    // remove Channels from thingChannels, thingWriteChannels
    Databus databus = Databus.getInstance();
    Set<Entry<String, Channel>> thingRow = thingChannels.row(thing).entrySet();
    Iterator<Entry<String, Channel>> i = thingRow.iterator();
    while (i.hasNext()) {
        Entry<String, Channel> thingChannel = i.next();
        if (!(thingChannel.getValue() instanceof ConfigChannel)) {
            thingChannel.getValue().removeChangeListener(databus);
            thingChannel.getValue().removeUpdateListener(databus);
            i.remove();
        }
    }
    thingWriteChannels.removeAll(thing);
    // Add Channels to thingChannels, thingConfigChannels and thingWriteChannels
    ThingDoc thingDoc = classRepository.getThingDoc(thing.getClass());
    for (ChannelDoc channelDoc : thingDoc.getChannelDocs()) {
        Member member = channelDoc.getMember();
        try {
            List<Channel> channels = new ArrayList<>();
            boolean ignoreEmpty = false;
            if (member instanceof Method) {
                if (((Method) member).getReturnType().isArray()) {
                    // ignore e.g. if getFaultChannels is returning an empty array
                    ignoreEmpty = true;
                    Channel[] ch = (Channel[]) ((Method) member).invoke(thing);
                    for (Channel c : ch) {
                        channels.add(c);
                    }
                } else {
                    // It's a Method with ReturnType Channel
                    channels.add((Channel) ((Method) member).invoke(thing));
                }
            } else if (member instanceof Field) {
                // It's a Field with Type Channel
                channels.add((Channel) ((Field) member).get(thing));
            } else {
                continue;
            }
            if (!ignoreEmpty && channels.isEmpty()) {
                log.warn("Channel is returning null! Thing [" + thing.id() + "], Member [" + member.getName() + "]");
                continue;
            }
            for (Channel channel : channels) {
                if (channel != null) {
                    // Add Channel to thingChannels
                    thingChannels.put(thing, channel.id(), channel);
                    // Add Channel to writeChannels
                    if (channel instanceof WriteChannel) {
                        thingWriteChannels.put(thing, (WriteChannel<?>) channel);
                    }
                    // Register Databus as listener
                    if (channel instanceof ReadChannel) {
                        ((ReadChannel<?>) channel).addUpdateListener(databus);
                        ((ReadChannel<?>) channel).addChangeListener(databus);
                    }
                }
            }
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            log.warn("Unable to add Channel. Member [" + member.getName() + "]", e);
        }
    }
}
Also used : ConfigChannel(io.openems.api.channel.ConfigChannel) ReadChannel(io.openems.api.channel.ReadChannel) WriteChannel(io.openems.api.channel.WriteChannel) ConfigChannel(io.openems.api.channel.ConfigChannel) ThingStateChannel(io.openems.api.channel.ThingStateChannel) Channel(io.openems.api.channel.Channel) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ChannelDoc(io.openems.api.doc.ChannelDoc) InvocationTargetException(java.lang.reflect.InvocationTargetException) ReadChannel(io.openems.api.channel.ReadChannel) Field(java.lang.reflect.Field) Entry(java.util.Map.Entry) WriteChannel(io.openems.api.channel.WriteChannel) Member(java.lang.reflect.Member) ThingDoc(io.openems.api.doc.ThingDoc)

Example 9 with ReadChannel

use of io.openems.api.channel.ReadChannel 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)

Example 10 with ReadChannel

use of io.openems.api.channel.ReadChannel in project openems by OpenEMS.

the class StuderVs70Charger method defineStuderProtocol.

/*
	 * Methods
	 */
@Override
protected StuderProtocol defineStuderProtocol() throws ConfigException {
    StuderProtocol p = new StuderProtocol();
    FloatParameterObject batteryChargeCurrent = new FloatParameterObject(14217, "batteryChargeCurrent", "Adc", this);
    p.addObject(batteryChargeCurrent);
    batteryChargeCurrentValue = batteryChargeCurrent.value().channel();
    batteryChargeCurrentUnsavedValue = batteryChargeCurrent.unsavedValue().channel();
    IntParameterObject start = new IntParameterObject(14038, "start", "", this);
    p.addObject(start);
    setStart = start.value().channel();
    IntParameterObject stop = new IntParameterObject(14039, "stop", "", this);
    p.addObject(stop);
    setStop = stop.value().channel();
    FloatUserinfoObject vBatt = new FloatUserinfoObject(15000, "BatteryVoltage", "V", this);
    p.addObject(vBatt);
    @SuppressWarnings("unchecked") ReadChannel<Float> batteryVoltage = (ReadChannel<Float>) vBatt.value().channel();
    this.batteryVoltage = batteryVoltage;
    return p;
}
Also used : StuderProtocol(io.openems.impl.protocol.studer.internal.StuderProtocol) FloatUserinfoObject(io.openems.impl.protocol.studer.internal.object.FloatUserinfoObject) FloatParameterObject(io.openems.impl.protocol.studer.internal.object.FloatParameterObject) IntParameterObject(io.openems.impl.protocol.studer.internal.object.IntParameterObject) ReadChannel(io.openems.api.channel.ReadChannel)

Aggregations

ReadChannel (io.openems.api.channel.ReadChannel)10 ConfigChannel (io.openems.api.channel.ConfigChannel)5 FunctionalReadChannel (io.openems.api.channel.FunctionalReadChannel)5 Device (io.openems.api.device.Device)5 ModbusDeviceNature (io.openems.impl.protocol.modbus.ModbusDeviceNature)5 ModbusReadLongChannel (io.openems.impl.protocol.modbus.ModbusReadLongChannel)5 ModbusProtocol (io.openems.impl.protocol.modbus.internal.ModbusProtocol)5 UnsignedDoublewordElement (io.openems.impl.protocol.modbus.internal.UnsignedDoublewordElement)5 ModbusRegisterRange (io.openems.impl.protocol.modbus.internal.range.ModbusRegisterRange)5 WriteChannel (io.openems.api.channel.WriteChannel)4 ThingStateChannels (io.openems.api.channel.thingstate.ThingStateChannels)4 ThingInfo (io.openems.api.doc.ThingInfo)4 ConfigException (io.openems.api.exception.ConfigException)4 InvalidValueException (io.openems.api.exception.InvalidValueException)4 DummyElement (io.openems.impl.protocol.modbus.internal.DummyElement)4 UnsignedWordElement (io.openems.impl.protocol.modbus.internal.UnsignedWordElement)4 Channel (io.openems.api.channel.Channel)3 StaticValueChannel (io.openems.api.channel.StaticValueChannel)3 ModbusBitWrappingChannel (io.openems.impl.protocol.modbus.ModbusBitWrappingChannel)3 ModbusWriteLongChannel (io.openems.impl.protocol.modbus.ModbusWriteLongChannel)3