Skip to content

Commit 22675f7

Browse files
committed
add getter/setter to ShellyThingConfiguration
Signed-off-by: Markus Michels <markus7017@gmail.com>
1 parent 1bbe7fd commit 22675f7

9 files changed

Lines changed: 50 additions & 39 deletions

File tree

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api1/Shelly1CoapHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public synchronized void start() throws ShellyApiException {
127127
String ps = substringAfter(profile.coiotEndpoint, ":");
128128
coiotPort = Integer.parseInt(ps);
129129
}
130-
coapServer.start(config.localIp, coiotPort, this);
130+
coapServer.start(config.getLocalIp(), coiotPort, this);
131131
statusClient = new CoapClient(completeUrl(config.getDeviceIp(), coiotPort, COLOIT_URI_DEVSTATUS))
132132
.setTimeout((long) SHELLY_API_TIMEOUT_MS).useNONs().setEndpoint(coapServer.getEndpoint());
133133
@Nullable

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api1/Shelly1HttpApi.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -608,21 +608,16 @@ private void setEventUrls(Integer index) throws ShellyApiException {
608608
}
609609

610610
private void setEventUrl(boolean enabled, String... eventTypes) throws ShellyApiException {
611-
ShellyThingConfiguration config;
612-
synchronized (this) {
613-
config = this.config;
614-
}
615-
616-
if (config.localIp.isEmpty()) {
611+
if (config.getLocalIp().isEmpty()) {
617612
throw new ShellyApiException(thingName + ": Local IP address was not detected, can't build Callback URL");
618613
}
619614
for (String eventType : eventTypes) {
620615
if (profile.containsEventUrl(eventType)) {
621616
// H&T adds the type=xx to report_url itself, so we need to ommit here
622617
String eclass = profile.isSensor ? EVENT_TYPE_SENSORDATA : eventType;
623618
String urlParm = eventType.contains("temp") || profile.isHT ? "" : "?type=" + eventType;
624-
String callBackUrl = "http://" + config.localIp + ":" + config.localPort + SHELLY1_CALLBACK_URI + "/"
625-
+ profile.thingName + "/" + eclass + urlParm;
619+
String callBackUrl = "http://" + config.getLocalIp() + ":" + config.getLocalPort()
620+
+ SHELLY1_CALLBACK_URI + "/" + profile.thingName + "/" + eclass + urlParm;
626621
String newUrl = enabled ? callBackUrl : SHELLY_NULL_URL;
627622
String testUrl = "\"" + mkEventUrl(eventType) + "\":\"" + newUrl + "\"";
628623
if (!enabled && !profile.settingsJson.contains(testUrl)) {
@@ -643,8 +638,9 @@ private void setEventUrl(String deviceClass, Integer index, boolean enabled, Str
643638
throws ShellyApiException {
644639
for (String eventType : eventTypes) {
645640
if (profile.containsEventUrl(eventType)) {
646-
String callBackUrl = "http://" + config.localIp + ":" + config.localPort + SHELLY1_CALLBACK_URI + "/"
647-
+ profile.thingName + "/" + deviceClass + "/" + index + "?type=" + eventType;
641+
String callBackUrl = "http://" + config.getLocalIp() + ":" + config.getLocalPort()
642+
+ SHELLY1_CALLBACK_URI + "/" + profile.thingName + "/" + deviceClass + "/" + index + "?type="
643+
+ eventType;
648644
String newUrl = enabled ? callBackUrl : SHELLY_NULL_URL;
649645
String test = "\"" + mkEventUrl(eventType) + "\":\"" + callBackUrl + "\"";
650646
if (!enabled && !profile.settingsJson.contains(test)) {

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ public ShellyDeviceProfile getDeviceProfile(ThingTypeUID thingTypeUID, @Nullable
260260
profile.hasRelays = profile.numRelays > 0 || profile.numRollers > 0;
261261

262262
ShellySettingsDevice device = profile.device;
263-
if (config.realm.isBlank()) {
264-
config.realm = getString(profile.device.hostname);
265-
logger.trace("{}: {} is used as realm", thingName, config.realm);
263+
if (config.getRealm().isBlank()) {
264+
config.setRealm(getString(profile.device.hostname));
265+
logger.trace("{}: {} is used as realm", thingName, config.getRealm());
266266
}
267267
profile.settings.fw = getString(device.fw);
268268
profile.fwDate = substringBefore(substringBefore(device.fw, "/"), "-");
@@ -1305,7 +1305,7 @@ protected Shelly2RpcBaseMessage buildRequest(String method, @Nullable Object par
13051305
String uid = thing.getThing().getUID().getAsString();
13061306
suffix = substringAfterLast(uid, ":");
13071307
} else {
1308-
suffix = config.localIp; // use a unique identifier;
1308+
suffix = config.getLocalIp(); // use a unique identifier;
13091309
}
13101310

13111311
Shelly2RpcBaseMessage request = new Shelly2RpcBaseMessage();

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiRpc.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ public ShellyDeviceProfile getDeviceProfile(ThingTypeUID thingTypeUID, @Nullable
219219
profile.hasRelays = profile.numRelays > 0 || profile.numRollers > 0;
220220

221221
ShellySettingsDevice device = profile.device;
222-
if (config.realm.isBlank()) {
223-
config.realm = getString(profile.device.hostname);
224-
logger.trace("{}: {} is used as realm", thingName, config.realm);
222+
if (config.getRealm().isBlank()) {
223+
config.setRealm(getString(profile.device.hostname));
224+
logger.trace("{}: {} is used as realm", thingName, config.getRealm());
225225
}
226226
profile.settings.fw = getString(device.fw);
227227
profile.fwDate = substringBefore(substringBefore(device.fw, "/"), "-");
@@ -384,8 +384,8 @@ public ShellyDeviceProfile getDeviceProfile(ThingTypeUID thingTypeUID, @Nullable
384384

385385
private void checkSetWsCallback() throws ShellyApiException {
386386
Shelly2ConfigParms wsConfig = apiRequest(SHELLYRPC_METHOD_WSGETCONFIG, null, Shelly2ConfigParms.class);
387-
String url = "ws://" + config.localIp + ":" + config.localPort + "/shelly/wsevent";
388-
if (!config.localIp.isEmpty() && !getBool(wsConfig.enable)
387+
String url = "ws://" + config.getLocalIp() + ":" + config.getLocalPort() + "/shelly/wsevent";
388+
if (!config.getLocalIp().isEmpty() && !getBool(wsConfig.enable)
389389
|| !url.equalsIgnoreCase(getString(wsConfig.server))) {
390390
logger.debug("{}: A battery device was detected without correct callback, fix it", thingName);
391391
wsConfig.enable = true;
@@ -1057,7 +1057,7 @@ public ShellySettingsLogin getLoginSettings() throws ShellyApiException {
10571057
public ShellySettingsLogin setLoginCredentials(String user, String password) throws ShellyApiException {
10581058
Shelly2RpcRequestParams params = new Shelly2RpcRequestParams();
10591059
params.user = "admin";
1060-
params.realm = config.realm;
1060+
params.realm = config.getRealm();
10611061
params.ha1 = sha256(params.user + ":" + params.realm + ":" + password);
10621062
apiRequest(SHELLYRPC_METHOD_AUTHSET, params, String.class);
10631063

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/ShellyBluApi.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void setConfig(String thingName, ShellyThingConfiguration config) {
9898
@Override
9999
public ShellySettingsDevice getDeviceInfo() throws ShellyApiException {
100100
ShellySettingsDevice info = new ShellySettingsDevice();
101-
info.hostname = !config.realm.isEmpty() ? config.realm : "";
101+
info.hostname = !config.getRealm().isEmpty() ? config.getRealm() : "";
102102
info.fw = "";
103103
info.type = "BLU";
104104
info.mac = config.getDeviceAddress();
@@ -125,8 +125,8 @@ public ShellyDeviceProfile getDeviceProfile(ThingTypeUID thingTypeUID, @Nullable
125125
}
126126

127127
profile.device = getDeviceInfo();
128-
if (config.realm.isEmpty()) {
129-
config.realm = getString(profile.device.hostname);
128+
if (config.getRealm().isEmpty()) {
129+
config.setRealm(getString(profile.device.hostname));
130130
}
131131

132132
// for now we have no API to get this information

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/config/ShellyThingBasicConfig.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,4 @@ public synchronized boolean getEventsSensorReport() {
125125
public synchronized boolean getEventsCoIoT() {
126126
return eventsCoIoT;
127127
}
128-
129128
}

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/config/ShellyThingConfiguration.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Locale;
2222

2323
import org.eclipse.jdt.annotation.NonNullByDefault;
24-
import org.openhab.binding.shelly.internal.handler.ShellyBaseHandler;
2524
import org.slf4j.Logger;
2625
import org.slf4j.LoggerFactory;
2726

@@ -32,13 +31,13 @@
3231
*/
3332
@NonNullByDefault
3433
public class ShellyThingConfiguration extends ShellyThingBasicConfig {
35-
protected final Logger logger = LoggerFactory.getLogger(ShellyBaseHandler.class);
34+
protected final Logger logger = LoggerFactory.getLogger(ShellyThingConfiguration.class);
3635

3736
// All access must be guarded by "this"
38-
public String realm;
37+
private String realm;
3938

40-
public final String localIp; // local ip addresses used to create callback url
41-
public final String localPort;
39+
private final String localIp; // local ip addresses used to create callback url
40+
private final String localPort;
4241

4342
public ShellyThingConfiguration(String thingName, ShellyThingBasicConfig basicConfig,
4443
ShellyBindingConfiguration bindingConfig, String realm, boolean gen2) {
@@ -120,6 +119,22 @@ public synchronized void disableGen1Events() {
120119
eventsSensorReport = false;
121120
}
122121

122+
public synchronized String getLocalIp() {
123+
return localIp;
124+
}
125+
126+
public synchronized String getLocalPort() {
127+
return localPort;
128+
}
129+
130+
public synchronized String getRealm() {
131+
return realm;
132+
}
133+
134+
public synchronized void setRealm(String realm) {
135+
this.realm = realm;
136+
}
137+
123138
@Override
124139
public String toString() {
125140
return "Device address=" + deviceAddress + ", HTTP user/password=" + userId + "/"

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/discovery/ShellyBasicDiscoveryService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ public void deactivate() {
139139
model = getString(devInfo.type);
140140
auth = getBool(devInfo.auth);
141141
if (name.isEmpty() || name.startsWith(SERVICE_NAME_SHELLYPLUSRANGE_PREFIX)) {
142-
config.realm = name = getString(devInfo.hostname);
142+
name = getString(devInfo.hostname);
143+
config.setRealm(name);
143144
}
144145

145146
thingType = name.contains("-") ? substringBeforeLast(name, "-") : name;

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/handler/ShellyBaseHandler.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public ShellyBaseHandler(final Thing thing, final ShellyTranslationProvider tran
176176
@Override
177177
public boolean checkRepresentation(String key) {
178178
return key.equalsIgnoreCase(getUID()) || key.equalsIgnoreCase(config.getDeviceAddress())
179-
|| key.equalsIgnoreCase(config.realm) || key.equalsIgnoreCase(getThingName());
179+
|| key.equalsIgnoreCase(config.getRealm()) || key.equalsIgnoreCase(getThingName());
180180
}
181181

182182
/**
@@ -328,8 +328,8 @@ thingName, getThing().getLabel(), thingType, config.getDeviceAddress().toUpperCa
328328
setThingOfflineAndDisconnect(ThingStatusDetail.CONFIGURATION_ERROR, "offline.conf-error-no-credentials");
329329
return false;
330330
}
331-
if (config.realm.isEmpty()) {
332-
config.realm = getString(device.hostname).toLowerCase(Locale.ROOT);
331+
if (config.getRealm().isEmpty()) {
332+
config.setRealm(getString(device.hostname).toLowerCase(Locale.ROOT));
333333
api.setConfig(thingName, config); // update config
334334
}
335335

@@ -884,7 +884,7 @@ public boolean isUpdateScheduled() {
884884
public boolean onEvent(String address, String deviceName, String deviceIndex, String type,
885885
Map<String, String> parameters) {
886886
if (thingName.equalsIgnoreCase(deviceName) || config.getDeviceAddress().equals(address)
887-
|| config.realm.equals(deviceName)) {
887+
|| config.getRealm().equals(deviceName)) {
888888
logger.debug("{}: Event received: class={}, index={}, parameters={}", deviceName, type, deviceIndex,
889889
parameters);
890890
int idx = !deviceIndex.isEmpty() ? Integer.parseInt(deviceIndex) : 1;
@@ -1025,9 +1025,9 @@ protected boolean initializeThingConfig() {
10251025
return false;
10261026
}
10271027

1028-
if (config.localIp.startsWith("169.254")) {
1028+
if (config.getLocalIp().startsWith("169.254")) {
10291029
setThingOfflineAndDisconnect(ThingStatusDetail.COMMUNICATION_ERROR, "config-status.error.network-config",
1030-
config.localIp);
1030+
config.getLocalIp());
10311031
return false;
10321032
}
10331033

@@ -1092,7 +1092,7 @@ public void startCoap(ShellyThingConfiguration config, ShellyDeviceProfile profi
10921092
}
10931093
if (profile.settings.coiot != null && profile.settings.coiot.enabled != null) {
10941094
String devpeer = getString(profile.settings.coiot.peer);
1095-
String ourpeer = config.localIp + ":" + Shelly1CoapJSonDTO.COIOT_PORT;
1095+
String ourpeer = config.getLocalIp() + ":" + Shelly1CoapJSonDTO.COIOT_PORT;
10961096
if (!profile.settings.coiot.enabled || (profile.isMotion && devpeer.isEmpty())) {
10971097
try {
10981098
api.setCoIoTPeer(ourpeer);
@@ -1344,7 +1344,7 @@ public boolean areChannelsCreated() {
13441344
public void updateProperties(ShellyDeviceProfile profile, ShellySettingsStatus status) {
13451345
Map<String, Object> properties = fillDeviceProperties(profile);
13461346
String deviceName = getString(profile.settings.name);
1347-
properties.put(PROPERTY_SERVICE_NAME, config.realm);
1347+
properties.put(PROPERTY_SERVICE_NAME, config.getRealm());
13481348
properties.put(PROPERTY_DEV_AUTH, getBool(profile.device.auth) ? "yes" : "no");
13491349
if (!deviceName.isEmpty()) {
13501350
properties.put(PROPERTY_DEV_NAME, deviceName);

0 commit comments

Comments
 (0)