Skip to content

Commit

Permalink
[modbus][sunspec] Support for SunSpec meters (openhab#7441)
Browse files Browse the repository at this point in the history
* [sunspec] Support for SunSpec meters
This commit adds support for SunSpec compatible meters. They are auto discovered
and handled as separate things even if they are part of another device.
This is due to the nature of the SunSpec format wich also handles
the different report types as different blocks.
* [sunspec] fixed file endings and missing unit declaration
* [sunspec] meter block dto refactored
This way we could get rid of ~250 loc.
* [sunspec] make sure we don't fail if the address/block size contains decimal dots
Some older installations of OpenHAB returned this values incorrectly

Signed-off-by: Nagy Attila Gabor <mrbig@sneaker.hu>
Signed-off-by: CSchlipp <christian@schlipp.de>
  • Loading branch information
mrbig authored and CSchlipp committed Jul 26, 2020
1 parent 32ab0d5 commit a6160a0
Show file tree
Hide file tree
Showing 9 changed files with 955 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ public class SunSpecConstants {
"inverter-split-phase");
public static final ThingTypeUID THING_TYPE_INVERTER_THREE_PHASE = new ThingTypeUID(BINDING_ID,
"inverter-three-phase");
public static final ThingTypeUID THING_TYPE_METER_SINGLE_PHASE = new ThingTypeUID(BINDING_ID, "meter-single-phase");
public static final ThingTypeUID THING_TYPE_METER_SPLIT_PHASE = new ThingTypeUID(BINDING_ID, "meter-split-phase");
public static final ThingTypeUID THING_TYPE_METER_WYE_PHASE = new ThingTypeUID(BINDING_ID, "meter-wye-phase");
public static final ThingTypeUID THING_TYPE_METER_DELTA_PHASE = new ThingTypeUID(BINDING_ID, "meter-delta-phase");

// Block types
public static final int COMMON_BLOCK = 1;
public static final int INVERTER_SINGLE_PHASE = 101;
public static final int INVERTER_SPLIT_PHASE = 102;
public static final int INVERTER_THREE_PHASE = 103;
public static final int METER_SINGLE_PHASE = 201;
public static final int METER_SPLIT_PHASE = 202;
public static final int METER_WYE_PHASE = 203;
public static final int METER_DELTA_PHASE = 204;
public static final int FINAL_BLOCK = 0xffff;

/**
Expand All @@ -53,6 +61,10 @@ public class SunSpecConstants {
SUPPORTED_THING_TYPES_UIDS.put(INVERTER_SINGLE_PHASE, THING_TYPE_INVERTER_SINGLE_PHASE);
SUPPORTED_THING_TYPES_UIDS.put(INVERTER_SPLIT_PHASE, THING_TYPE_INVERTER_SPLIT_PHASE);
SUPPORTED_THING_TYPES_UIDS.put(INVERTER_THREE_PHASE, THING_TYPE_INVERTER_THREE_PHASE);
SUPPORTED_THING_TYPES_UIDS.put(METER_SINGLE_PHASE, THING_TYPE_METER_SINGLE_PHASE);
SUPPORTED_THING_TYPES_UIDS.put(METER_SPLIT_PHASE, THING_TYPE_METER_SPLIT_PHASE);
SUPPORTED_THING_TYPES_UIDS.put(METER_WYE_PHASE, THING_TYPE_METER_WYE_PHASE);
SUPPORTED_THING_TYPES_UIDS.put(METER_DELTA_PHASE, THING_TYPE_METER_DELTA_PHASE);
}

// properties
Expand Down Expand Up @@ -90,6 +102,22 @@ public class SunSpecConstants {
public static final String CHANNEL_AC_POWER_FACTOR = "ac-power-factor";
public static final String CHANNEL_AC_LIFETIME_ENERGY = "ac-lifetime-energy";

// List of channels ids in AC general group for meter
public static final String CHANNEL_AC_AVERAGE_VOLTAGE_TO_N = "ac-average-voltage-to-n";
public static final String CHANNEL_AC_AVERAGE_VOLTAGE_TO_NEXT = "ac-average-voltage-to-next";
public static final String CHANNEL_AC_TOTAL_REAL_POWER = "ac-total-real-power";
public static final String CHANNEL_AC_TOTAL_APPARENT_POWER = "ac-total-apparent-power";
public static final String CHANNEL_AC_TOTAL_REACTIVE_POWER = "ac-total-reactive-power";
public static final String CHANNEL_AC_AVERAGE_POWER_FACTOR = "ac-average-power-factor";
public static final String CHANNEL_AC_TOTAL_EXPORTED_REAL_ENERGY = "ac-total-exported-real-energy";
public static final String CHANNEL_AC_TOTAL_IMPORTED_REAL_ENERGY = "ac-total-imported-real-energy";
public static final String CHANNEL_AC_TOTAL_EXPORTED_APPARENT_ENERGY = "ac-total-exported-apparent-energy";
public static final String CHANNEL_AC_TOTAL_IMPORTED_APPARENT_ENERGY = "ac-total-imported-apparent-energy";
public static final String CHANNEL_AC_TOTAL_IMPORTED_REACTIVE_ENERGY_Q1 = "ac-total-imported-reactive-energy-q1";
public static final String CHANNEL_AC_TOTAL_IMPORTED_REACTIVE_ENERGY_Q2 = "ac-total-imported-reactive-energy-q2";
public static final String CHANNEL_AC_TOTAL_EXPORTED_REACTIVE_ENERGY_Q3 = "ac-total-exported-reactive-energy-q3";
public static final String CHANNEL_AC_TOTAL_EXPORTED_REACTIVE_ENERGY_Q4 = "ac-total-exported-reactive-energy-q4";

// List of channel ids in AC phase group for inverter
public static final String CHANNEL_AC_PHASE_CURRENT = "ac-phase-current";
public static final String CHANNEL_AC_VOLTAGE_TO_NEXT = "ac-voltage-to-next";
Expand All @@ -100,6 +128,17 @@ public class SunSpecConstants {
public static final String CHANNEL_DC_VOLTAGE = "dc-voltage";
public static final String CHANNEL_DC_POWER = "dc-power";

// List of channel ids in AC phase group for meter
public static final String CHANNEL_AC_REAL_POWER = "ac-real-power";
public static final String CHANNEL_AC_EXPORTED_REAL_ENERGY = "ac-exported-real-energy";
public static final String CHANNEL_AC_IMPORTED_REAL_ENERGY = "ac-imported-real-energy";
public static final String CHANNEL_AC_EXPORTED_APPARENT_ENERGY = "ac-exported-apparent-energy";
public static final String CHANNEL_AC_IMPORTED_APPARENT_ENERGY = "ac-imported-apparent-energy";
public static final String CHANNEL_AC_IMPORTED_REACTIVE_ENERGY_Q1 = "ac-imported-reactive-energy-q1";
public static final String CHANNEL_AC_IMPORTED_REACTIVE_ENERGY_Q2 = "ac-imported-reactive-energy-q2";
public static final String CHANNEL_AC_EXPORTED_REACTIVE_ENERGY_Q3 = "ac-exported-reactive-energy-q3";
public static final String CHANNEL_AC_EXPORTED_REACTIVE_ENERGY_Q4 = "ac-exported-reactive-energy-q4";

// Expected SunSpec ID This is a magic constant to distinguish SunSpec compatible
// devices from other modbus devices
public static final long SUNSPEC_ID = 0x53756e53;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory;
import org.openhab.binding.modbus.sunspec.internal.handler.InverterHandler;
import org.openhab.binding.modbus.sunspec.internal.handler.MeterHandler;
import org.openhab.io.transport.modbus.ModbusManager;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
Expand Down Expand Up @@ -75,6 +76,11 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
|| thingTypeUID.equals(THING_TYPE_INVERTER_THREE_PHASE)) {
logger.debug("New InverterHandler created");
return new InverterHandler(thing, manager);
} else if (thingTypeUID.equals(THING_TYPE_METER_SINGLE_PHASE)
|| thingTypeUID.equals(THING_TYPE_METER_SPLIT_PHASE) || thingTypeUID.equals(THING_TYPE_METER_WYE_PHASE)
|| thingTypeUID.equals(THING_TYPE_METER_DELTA_PHASE)) {
logger.debug("New MeterHandler created");
return new MeterHandler(thing, manager);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.modbus.sunspec.internal.dto;

import java.util.Optional;

/**
*
* Data object for the parsed information from a sunspec meter
*
* @author Nagy Attila Gabor - Initial contribution
*
*/
public class MeterModelBlock {

/**
* Sunspec device type id
*/
public Integer sunspecDID;

/**
* Block length
*/
public Integer length;

/**
* AC Total Current value
*/
public Short acCurrentTotal;

/**
* Descriptors for phase A
*/
public PhaseBlock phaseA = new PhaseBlock();

/**
* Descriptors for phase B
*/
public PhaseBlock phaseB = new PhaseBlock();

/**
* Descriptors for phase C
*/
public PhaseBlock phaseC = new PhaseBlock();

/**
* AC Current scale factor
*/
public Short acCurrentSF;

/**
* AC Voltage Line to line value
*/
public Optional<Short> acVoltageLineToNAverage;

/**
* AC Voltage Line to N value
*/
public Optional<Short> acVoltageLineToLineAverage;

/**
* AC Voltage scale factor
*/
public Short acVoltageSF;

/**
* AC Frequency value
*/
public Short acFrequency;

/**
* AC Frequency scale factor
*/
public Optional<Short> acFrequencySF;

/**
* Total real power
*/
public Short acRealPowerTotal;

/**
* AC Real Power Scale Factor
*/
public Short acRealPowerSF;

/**
* Total apparent power
*/
public Optional<Short> acApparentPowerTotal;

/**
* AC Apparent Power Scale Factor
*/
public Optional<Short> acApparentPowerSF;

/**
* Total reactive power
*/
public Optional<Short> acReactivePowerTotal;

/**
* AC Reactive Power Scale Factor
*/
public Optional<Short> acReactivePowerSF;

/**
* Power factor
*/
public Optional<Short> acPowerFactor;

/**
* Power factor scale factor
*/
public Optional<Short> acPowerFactorSF;

/**
* Total exported real energy
*/
public Optional<Long> acExportedRealEnergyTotal;

/**
* Total imported real energy
*/
public Long acImportedRealEnergyTotal;

/**
* Real Energy Scale Factor
*/
public Short acRealEnergySF;

/**
* Total exported apparent energy
*/
public Optional<Long> acExportedApparentEnergyTotal;

/**
* Total imported apparent energy
*/
public Optional<Long> acImportedApparentEnergyTotal;

/**
* Apparent Energy Scale Factor
*/
public Optional<Short> acApparentEnergySF;

/**
* Quadrant 1: Total imported reactive energy
*/
public Optional<Long> acImportedReactiveEnergyQ1Total;

/**
* Quadrant 2: Total imported reactive energy
*/
public Optional<Long> acImportedReactiveEnergyQ2Total;

/**
* Quadrant 3: Total exported reactive energy
*/
public Optional<Long> acExportedReactiveEnergyQ3Total;

/**
* Quadrant 4: Total exported reactive energy
*/
public Optional<Long> acExportedReactiveEnergyQ4Total;

/**
* Reactive Energy Scale Factor
*/
public Optional<Short> acReactiveEnergySF;

/**
* This subclass is used to store raw data for a single phase in
* multi phase meters.
*/
public static class PhaseBlock {
/**
* AC Phase A Current value
*/
public Optional<Short> acPhaseCurrent;

/**
* AC Voltage Phase Phase to N value
*/
public Optional<Short> acVoltageToN;

/**
* AC Voltage Phase Line to next Line value
*/
public Optional<Short> acVoltageToNext;

/**
* Phase A AC real power
*/
public Optional<Short> acRealPower;

/**
* Phase A AC apparent power
*/
public Optional<Short> acApparentPower;

/**
* Phase A AC reactive power
*/
public Optional<Short> acReactivePower;

/**
* Phase A Power factor
*/
public Optional<Short> acPowerFactor;

/**
* Phase A exported real energy
*/
public Optional<Long> acExportedRealEnergy;

/**
* Phase A imported real energy
*/
public Optional<Long> acImportedRealEnergy;

/**
* Phase A exported apparent energy
*/
public Optional<Long> acExportedApparentEnergy;

/**
* Phase A imported apparent energy
*/
public Optional<Long> acImportedApparentEnergy;

/**
* Quadrant 1: Phase A imported reactive energy
*/
public Optional<Long> acImportedReactiveEnergyQ1;

/**
* Quadrant 2: Phase A imported reactive energy
*/
public Optional<Long> acImportedReactiveEnergyQ2;

/**
* Quadrant 3: Phase A exported reactive energy
*/
public Optional<Long> acExportedReactiveEnergyQ3;

/**
* Quadrant 4: Phase A exported reactive energy
*/
public Optional<Long> acExportedReactiveEnergyQ4;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ private void startUp() {
}
try {
ModelBlock block = new ModelBlock();
block.address = Integer.parseInt(thing.getProperties().get(PROPERTY_BLOCK_ADDRESS));
block.length = Integer.parseInt(thing.getProperties().get(PROPERTY_BLOCK_LENGTH));
block.address = (int) Double.parseDouble(thing.getProperties().get(PROPERTY_BLOCK_ADDRESS));
block.length = (int) Double.parseDouble(thing.getProperties().get(PROPERTY_BLOCK_LENGTH));
return block;
} catch (NumberFormatException ex) {
logger.debug("Could not parse address and length properties, error: {}", ex.getMessage());
Expand Down
Loading

0 comments on commit a6160a0

Please sign in to comment.