这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public boolean processMessage(Message message) {
// devices
device = new Circle(((RoleCallResponseMessage) message).getNodeMAC(), stick,
((RoleCallResponseMessage) message).getNodeMAC());
stick.plugwiseDeviceCache.add(device);
stick.addDevice(device);
logger.debug("Added a Circle with MAC {} to the cache", device.getMAC());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private void setupNonStickDevices(Dictionary<String, ?> config) {
PlugwiseDevice device = createPlugwiseDevice(deviceType, MAC, deviceName);

if (device != null) {
stick.plugwiseDeviceCache.add(device);
stick.addDevice(device);
}
}

Expand Down Expand Up @@ -479,7 +479,7 @@ private void scheduleJobs(Scheduler scheduler) {
if (!cp.getMAC().equals(element.getId())) {
// a circleplus has been added/detected and it is not what is in the binding config
PlugwiseDevice device = new Circle(element.getId(), stick, element.getId());
stick.plugwiseDeviceCache.add(device);
stick.addDevice(device);
logger.debug("Plugwise added Circle with MAC address: {}", element.getId());
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.openhab.binding.plugwise.internal;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Caches all {@link PlugwiseDevice} instances and allows for querying them by mac, name and device class.
*
* @author Wouter Born
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add @since 1.9.0

* @since 1.9.0
*/
public class PlugwiseDeviceCache {

private Map<String, PlugwiseDevice> macDeviceMapping = new ConcurrentHashMap<String, PlugwiseDevice>();
private Map<String, PlugwiseDevice> nameDeviceMapping = new ConcurrentHashMap<String, PlugwiseDevice>();

public void add(PlugwiseDevice device) {
macDeviceMapping.put(device.getMAC(), device);
nameDeviceMapping.put(device.getName(), device);
}

public void clear() {
macDeviceMapping.clear();
nameDeviceMapping.clear();
}

public PlugwiseDevice get(String id) {
PlugwiseDevice device = getByMAC(id);
if (device == null) {
return getByName(id);
} else {
return device;
}
}

public PlugwiseDevice getByMAC(String mac) {
return macDeviceMapping.get(mac);
}

public PlugwiseDevice getByName(String name) {
return nameDeviceMapping.get(name);
}

@SuppressWarnings("unchecked")
public <T> List<T> getByClass(Class<T> deviceClass) {

List<T> result = new ArrayList<T>();
for (PlugwiseDevice device : macDeviceMapping.values()) {
if (deviceClass.isAssignableFrom(device.getClass())) {
result.add((T) device);
}
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -111,7 +109,7 @@ public class Stick extends PlugwiseDevice implements SerialPortEventListener {

// Stick fields
private boolean initialised = false;
protected List<PlugwiseDevice> plugwiseDeviceCache = Collections.synchronizedList(new ArrayList<PlugwiseDevice>());
private final PlugwiseDeviceCache plugwiseDeviceCache = new PlugwiseDeviceCache();
private PlugwiseBinding binding;
/** default interval for sending messages on the ZigBee network */
private int interval = 50;
Expand All @@ -135,44 +133,24 @@ public Stick(String port, PlugwiseBinding binding) {
}
}

protected void addDevice(PlugwiseDevice device) {
plugwiseDeviceCache.add(device);
}

protected PlugwiseDevice getDevice(String id) {
PlugwiseDevice someDevice = getDeviceByMAC(id);
if (someDevice == null) {
return getDeviceByName(id);
} else {
return someDevice;
}
return plugwiseDeviceCache.get(id);
}

protected PlugwiseDevice getDeviceByMAC(String MAC) {
for (PlugwiseDevice device : plugwiseDeviceCache) {
if (MAC.equals(device.getMAC())) {
return device;
}
}
return null;
protected PlugwiseDevice getDeviceByMAC(String mac) {
return plugwiseDeviceCache.getByMAC(mac);
}

protected PlugwiseDevice getDeviceByName(String name) {
for (PlugwiseDevice device : plugwiseDeviceCache) {
if (name.equals(device.getName())) {
return device;
}
}
return null;
return plugwiseDeviceCache.getByName(name);
}

@SuppressWarnings("unchecked")
protected <T> List<T> getDevicesByClass(Class<T> deviceClass) {

List<T> result = new ArrayList<T>();
for (PlugwiseDevice device : plugwiseDeviceCache) {
if (deviceClass.isAssignableFrom(device.getClass())) {
result.add((T) device);
}
}

return result;
return plugwiseDeviceCache.getByClass(deviceClass);
}

public String getPort() {
Expand Down Expand Up @@ -200,9 +178,7 @@ public boolean isInitialised() {
private void initialize() throws PlugwiseInitializationException {

// Flush the deviceCache
if (this.plugwiseDeviceCache != null) {
plugwiseDeviceCache = Collections.synchronizedList(new ArrayList<PlugwiseDevice>());
}
plugwiseDeviceCache.clear();

// parse ports and if the default port is found, initialized the reader
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
Expand Down