Skip to content

Commit

Permalink
Implement discovery service
Browse files Browse the repository at this point in the history
  • Loading branch information
pbogut committed Aug 23, 2020
1 parent c7567ff commit 9fd755e
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
package org.openhab.binding.ewpesmart.internal;

import java.util.Collections;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.thing.ThingTypeUID;

Expand All @@ -28,6 +31,7 @@ public class EWPESmartBindingConstants {

// List of all Thing Type UIDs
public static final ThingTypeUID THING_TYPE_AIRCON = new ThingTypeUID(BINDING_ID, "EWPEAirCon");
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE_AIRCON);

// List of all Config options
public static final String CONFIG_BROADCAST_IP = "broadcastIp";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class EWPEDevice {
private final static HashMap<String, HashMap<String,Integer>> tempRanges = createTempRangeMap();
private Boolean mIsBound = false;
private InetAddress mAddress;
private InetAddress mBroadcast;
private int mPort = 0;
private String mKey;
private EWPEScanResponse4Gson mScanResponseGson = null;
Expand All @@ -79,6 +80,14 @@ public void setAddress(InetAddress address) {
this.mAddress = address;
}

public InetAddress getBroadcast() {
return mBroadcast;
}

public void setBroadcast(InetAddress address) {
this.mBroadcast = address;
}

public int getPort() {
return mPort;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public void Scan(DatagramSocket clientSocket) throws IOException, Exception {
EWPEDevice newDevice = new EWPEDevice();
newDevice.setAddress(remoteAddress);
newDevice.setPort(remotePort);
newDevice.setBroadcast(mIPAddress);
newDevice.setScanResponseGson(scanResponseGson);

AddDevice(newDevice);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* 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.ewpesmart.internal.discovery;

import static org.openhab.binding.ewpesmart.internal.EWPESmartBindingConstants.*;

import org.openhab.binding.ewpesmart.internal.device.EWPEDevice;
import org.openhab.binding.ewpesmart.internal.device.EWPEDeviceFinder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.net.DatagramSocket;
import java.net.InetAddress;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.discovery.AbstractDiscoveryService;
import org.eclipse.smarthome.config.discovery.DiscoveryService;
import org.eclipse.smarthome.config.discovery.DiscoveryResult;
import org.eclipse.smarthome.config.discovery.DiscoveryResultBuilder;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Iterator;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;

/**
* The {@link EWPESmartDiscovery} is responsible for discovering supported
* things.
*
* @author Pawel Bogut - Initial contribution
*/
@NonNullByDefault
@Component(service = DiscoveryService.class, immediate = true, configurationPid = "discovery.ewpesmart")
public class EWPESmartDiscovery extends AbstractDiscoveryService {
private final Logger logger = LoggerFactory.getLogger(EWPESmartDiscovery.class);

private static final int SEARCH_TIME = 10;

private @Nullable EWPEDeviceFinder deviceFinder = null;
private @Nullable DatagramSocket clientSocket = null;

@Activate
public EWPESmartDiscovery () {
super(SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME, true);
try {
clientSocket = new DatagramSocket();
clientSocket.setSoTimeout(DATAGRAM_SOCKET_TIMEOUT);
} catch (Exception e ) {
logger.error("EWPESmart: Could not create clientSocket {}", e.getMessage());
}
}

@Override
public Set<ThingTypeUID> getSupportedThingTypes() {
return SUPPORTED_THING_TYPES_UIDS;
}

@Override
public void startScan() {
search();
}

private void search() {
try {
// Create a new Datagram socket with a specified timeout
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();

while (en.hasMoreElements()) {
NetworkInterface ni = en.nextElement();
logger.trace("EWPESmart: Get {} network interface", ni.getDisplayName());

List<InterfaceAddress> list = ni.getInterfaceAddresses();
Iterator<InterfaceAddress> it = list.iterator();

while (it.hasNext()) {
InterfaceAddress ia = it.next();
if (ia.getBroadcast() == null) {
continue;
}
logger.trace("EWPESmart: Scan for devices on {} broadcast address", ia.getBroadcast());
searchNetwork(ia.getBroadcast());
}
}
} catch (Exception e) {
logger.error("EWPESmart: Error while scanning for devices {}", e.getMessage());
}
}

private void searchNetwork(InetAddress broadcastIp) throws Exception {
// Firstly, lets find all Gree Airconditioners on the network
deviceFinder = new EWPEDeviceFinder(broadcastIp);
deviceFinder.Scan(clientSocket);

for (HashMap.Entry<String, EWPEDevice> e : deviceFinder.GetDevices().entrySet()) {
EWPEDevice device = e.getValue();
DiscoveryResult discoveryResult = buildDiscoveryResult(device);

logger.trace("EWPESmart: Discovered thing {}", getDeviceName(device));
thingDiscovered(discoveryResult);
}
}

private DiscoveryResult buildDiscoveryResult(EWPEDevice device) {
Map<String, Object> properties = new HashMap<>();

properties.put("broadcastIp", (device.getBroadcast() + "").replace("/", ""));
properties.put("ipAddress", (device.getAddress() + "").replace("/", ""));
properties.put("refresh", 2);

DiscoveryResult discoveryResult = DiscoveryResultBuilder
.create(getThingUID(device))
.withThingType(THING_TYPE_AIRCON)
.withProperties(properties)
.withRepresentationProperty("ipAddress")
.withLabel(getDeviceName(device))
.build();

return discoveryResult;
}

private String getDeviceName(EWPEDevice device) {
return ("EWPESmart AirCon " + device.getName()
+ " - " + device.getAddress()).replace("/", "");
}

private ThingUID getThingUID(EWPEDevice device) {
return new ThingUID("ewpesmart:EWPEAirCon:" + device.getId());
}
}

0 comments on commit 9fd755e

Please sign in to comment.