use of io.openems.api.channel.Channel 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;
}
use of io.openems.api.channel.Channel in project openems by OpenEMS.
the class ThingRepository method addThing.
/**
* Add a Thing to the Repository and cache its Channels and other information for later usage.
*
* @param thing
*/
public synchronized void addThing(Thing thing) {
if (thingIds.containsValue(thing)) {
// Thing was already added
return;
}
// Add to thingIds
thingIds.forcePut(thing.id(), thing);
// Add to thingClasses
thingClasses.put(thing.getClass(), thing);
// Add to bridges
if (thing instanceof Bridge) {
bridges.add((Bridge) thing);
}
// Add to schedulers
if (thing instanceof Scheduler) {
schedulers.add((Scheduler) thing);
}
// Add to persistences
if (thing instanceof Persistence) {
persistences.add((Persistence) thing);
}
// Add to queryablePersistences
if (thing instanceof QueryablePersistence) {
queryablePersistences.add((QueryablePersistence) thing);
}
// Add to device natures
if (thing instanceof DeviceNature) {
deviceNatures.add((DeviceNature) thing);
}
// Add Listener
thing.addListener(this);
// Apply channel annotation (this happens now and again after initializing the thing via init()
this.applyChannelAnnotation(thing);
// Add Channels thingConfigChannels
ThingDoc thingDoc = classRepository.getThingDoc(thing.getClass());
for (ChannelDoc channelDoc : thingDoc.getChannelDocs()) {
Member member = channelDoc.getMember();
try {
List<Channel> channels = new ArrayList<>();
java.util.function.Consumer<Channel> addToChannels = (c) -> {
if (c == null) {
// TODO this error is not handled properly
// log.error(
// "Channel is returning null! Thing [" + thing.id() + "], Member [" + member.getName() + "]");
} else {
channels.add(c);
}
};
if (member instanceof Method) {
if (((Method) member).getReturnType().isArray()) {
Channel[] ch = (Channel[]) ((Method) member).invoke(thing);
for (Channel c : ch) {
addToChannels.accept(c);
}
} else {
// It's a Method with ReturnType Channel
Channel c = (Channel) ((Method) member).invoke(thing);
addToChannels.accept(c);
if (c instanceof ThingStateChannels) {
ThingStateChannels tsc = (ThingStateChannels) c;
for (ThingStateChannel fc : tsc.getFaultChannels()) {
addToChannels.accept(fc);
}
for (ThingStateChannel wc : tsc.getWarningChannels()) {
addToChannels.accept(wc);
}
}
}
} else if (member instanceof Field) {
// It's a Field with Type Channel
Channel c = (Channel) ((Field) member).get(thing);
addToChannels.accept(c);
} else {
continue;
}
if (channels.isEmpty()) {
continue;
}
for (Channel channel : channels) {
// Add Channel to thingChannels
thingChannels.put(thing, channel.id(), channel);
if (channel instanceof ConfigChannel) {
// Add Channel to configChannels
thingConfigChannels.put(thing, (ConfigChannel<?>) channel);
}
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
log.warn("Unable to add Channel. Member [" + member.getName() + "]", e);
}
}
for (ThingsChangedListener listener : thingListeners) {
listener.thingChanged(thing, Action.ADD);
}
}
use of io.openems.api.channel.Channel 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);
}
}
}
use of io.openems.api.channel.Channel in project openems by OpenEMS.
the class SunnyIsland6Ess method defineModbusProtocol.
@Override
protected ModbusProtocol defineModbusProtocol() throws ConfigException {
ModbusProtocol protokol = new ModbusProtocol(new ModbusRegisterRange(30201, new UnsignedDoublewordElement(30201, systemState = new ModbusReadLongChannel("SystemState", this).label(35, "Fehler").label(303, "Aus").label(307, "OK").label(455, "Warnung")), new UnsignedDoublewordElement(30203, maxPower = new ModbusReadLongChannel("MaxPower", this).unit("W"))), new //
ModbusRegisterRange(//
30775, new //
SignedDoublewordElement(//
30775, activePower = new ModbusReadLongChannel("ActivePower", this).unit("W")), new DummyElement(30777, 30802), new //
UnsignedDoublewordElement(//
30803, frequency = new ModbusReadLongChannel("Frequency", this).unit("mHz").multiplier(1)), new //
SignedDoublewordElement(//
30805, reactivePower = new ModbusReadLongChannel("ReactivePower", this).unit("var").negate())), new //
ModbusRegisterRange(//
30843, new //
SignedDoublewordElement(//
30843, batteryCurrent = new ModbusReadLongChannel("BatteryCurrent", this).unit("mA")), new //
UnsignedDoublewordElement(//
30845, soc = new ModbusReadLongChannel("Soc", this).unit("%").interval(0, 100)), new DummyElement(30847, 30848), new //
SignedDoublewordElement(//
30849, batteryTemperature = new ModbusReadLongChannel("BatteryTemperature", this).unit("°C").multiplier(-1)), new //
UnsignedDoublewordElement(//
30851, batteryVoltage = new ModbusReadLongChannel("BatteryVoltage", this).unit("mV").multiplier(1))), new ModbusRegisterRange(40189, new //
UnsignedDoublewordElement(//
40189, allowedCharge = new ModbusReadLongChannel("AllowedCharge", this).unit("W").negate()), new //
UnsignedDoublewordElement(//
40191, allowedDischarge = new ModbusReadLongChannel("AllowedDischarge", this).unit("W"))), new //
WriteableModbusRegisterRange(//
40149, new SignedDoublewordElement(40149, //
setActivePower = new ModbusWriteLongChannel("SetActivePower", this).unit("W")), new UnsignedDoublewordElement(40151, setControlMode = new ModbusWriteLongChannel("SetControlMode", this).label(802, START).label(803, //
STOP)), new SignedDoublewordElement(40153, setReactivePower = new ModbusWriteLongChannel("SetReactivePower", this).unit("Var"))), new WriteableModbusRegisterRange(40705, new UnsignedDoublewordElement(40705, //
minSocPowerOn = new ModbusWriteLongChannel("MinSocPowerOn", this)), new UnsignedDoublewordElement(40707, //
minSocPowerOff = new ModbusWriteLongChannel("MinSocPowerOff", this))), new WriteableModbusRegisterRange(41187, new UnsignedDoublewordElement(41187, meterSetting = new ModbusWriteLongChannel("MeterSetting", this).label(3053, "SMA Energy Meter").label(3547, "Wechselrichter"))));
this.power = new SymmetricPowerImpl(40000, setActivePower, setReactivePower, getParent().getBridge());
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;
}
use of io.openems.api.channel.Channel in project openems by OpenEMS.
the class ChannelRestlet method handle.
@Override
public void handle(Request request, Response response) {
super.handle(request, response);
// check general permission
if (isAuthenticatedAsRole(request, Role.GUEST)) {
// pfff... it's only a "GUEST"! Deny anything but GET requests
if (!request.getMethod().equals(Method.GET)) {
throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED);
}
}
// get request attributes
Map<String, Object> attributes = request.getAttributes();
String thingId = (String) attributes.get("thing");
String channelId = (String) attributes.get("channel");
// get channel
Channel channel;
Optional<Channel> channelOptional = thingRepository.getChannel(thingId, channelId);
if (channelOptional.isPresent()) {
// get channel value
channel = channelOptional.get();
} else {
// Channel not found
throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND);
}
// call handler methods
if (request.getMethod().equals(Method.GET)) {
// check read permission
assertAllowed(request, channel.readRoles());
Representation entity = getValue(channel);
response.setEntity(entity);
} else if (request.getMethod().equals(Method.POST)) {
// check write permissions
assertAllowed(request, channel.writeRoles());
JsonParser parser = new JsonParser();
String httpPost = request.getEntityAsText();
JsonObject jHttpPost = parser.parse(httpPost).getAsJsonObject();
setValue(channel, jHttpPost);
}
}
Aggregations