Replaced zwave lib with OpenZwave

This commit is contained in:
Ziver Koc 2019-10-24 19:36:23 +02:00 committed by Ziver Koc
parent 12673437da
commit 7777972d97
5 changed files with 298 additions and 216 deletions

View file

@ -1,190 +1,353 @@
package se.hal.plugin.zwave;
import com.whizzosoftware.wzwave.controller.ZWaveController;
import com.whizzosoftware.wzwave.controller.ZWaveControllerListener;
import com.whizzosoftware.wzwave.controller.netty.NettyZWaveController;
import com.whizzosoftware.wzwave.node.NodeInfo;
import com.whizzosoftware.wzwave.node.ZWaveEndpoint;
import org.zwave4j.*;
import se.hal.HalContext;
import se.hal.intf.*;
import se.hal.plugin.tellstick.TellstickDevice;
import zutil.log.LogUtil;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
/**
*
* @author zagumennikov
*/
public class HalZWaveController implements HalSensorController, HalEventController,
HalAutoScannableController, ZWaveControllerListener {
public static final Logger logger = LogUtil.getLogger();
private ZWaveController controller;
public class HalZWaveController implements HalSensorController, HalEventController, HalAutoScannableController, NotificationWatcher{
private static final Logger logger = LogUtil.getLogger();
public static final String CONFIG_ZWAVE_PORT = "zwave.com_port";
public static final String CONFIG_ZWAVE_CFG_PATH = "zwave.cfg_path";
private String serialPort;
private long homeId;
private Options options;
private Manager manager;
private HalSensorReportListener sensorListener;
private HalEventReportListener eventListener;
private List<TellstickDevice> registeredDevices;
public static void main(String[] args) {
NettyZWaveController zwave = new NettyZWaveController("COM3", new HashMapPersistentStore());
zwave.setListener(new ZWaveControllerListener(){
@Override
public void onZWaveNodeAdded(ZWaveEndpoint node) {
System.out.println("onZWaveNodeAdded: "+ node);
}
public static void main(String[] args) throws IOException {
HalZWaveController controller = new HalZWaveController();
controller.initialize(
"/dev/serial/by-id/usb-0658_0200-if00",
"./");
@Override
public void onZWaveNodeUpdated(ZWaveEndpoint node) {
System.out.println("onZWaveNodeUpdated: "+ node);
}
@Override
public void onZWaveConnectionFailure(Throwable t) {
System.out.println("onZWaveConnectionFailure: "+ t);
}
@Override
public void onZWaveControllerInfo(String libraryVersion, Integer homeId, Byte nodeId) {
System.out.println("onZWaveControllerInfo: "+ libraryVersion+" "+homeId+" "+nodeId);
}
@Override
public void onZWaveInclusionStarted() {
System.out.println("onZWaveInclusionStarted");
}
@Override
public void onZWaveInclusion(NodeInfo nodeInfo, boolean success) {
System.out.println("onZWaveInclusion: "+ nodeInfo + " "+success);
}
@Override
public void onZWaveInclusionStopped() {
System.out.println("onZWaveInclusionStopped");
}
@Override
public void onZWaveExclusionStarted() {
System.out.println("onZWaveExclusionStarted");
}
@Override
public void onZWaveExclusion(NodeInfo nodeInfo, boolean success) {
System.out.println("onZWaveExclusion: "+ nodeInfo + " "+success);
}
@Override
public void onZWaveExclusionStopped() {
System.out.println("onZWaveExclusionStopped");
}
});
zwave.start();
System.in.read();
}
public HalZWaveController() {
NativeLibraryLoader.loadLibrary(ZWave4j.LIBRARY_NAME, ZWave4j.class);
}
@Override
public boolean isAvailable() {
return HalContext.getStringProperty("zwave.com_port") != null;
return HalContext.getStringProperty(CONFIG_ZWAVE_PORT) != null &&
HalContext.getStringProperty(CONFIG_ZWAVE_CFG_PATH) != null;
}
@Override
public void initialize() {
controller = new NettyZWaveController(
HalContext.getStringProperty("zwave.com_port"),
new HashMapPersistentStore());
controller.setListener(this);
controller.start();
initialize(HalContext.getStringProperty(CONFIG_ZWAVE_PORT),
HalContext.getStringProperty(CONFIG_ZWAVE_CFG_PATH));
}
public void initialize(String comPort, String configDir) {
options = Options.create(configDir, "", "");
options.addOptionBool("ConsoleOutput", false);
options.lock();
logger.info("Creating OpenZWave Manager...");
serialPort = comPort;
manager = Manager.create();
manager.addWatcher(this, null);
logger.info("Connecting to com port... ("+ serialPort +")");
manager.addDriver(serialPort);
}
@Override
public void close() {
controller.stop();
controller = null;
manager.removeWatcher(this, null);
manager.removeDriver(serialPort);
manager.destroy();
Options.destroy();
}
////////////// Z-WAVE CODE ////////////////////////
@Override
public void onZWaveNodeAdded(ZWaveEndpoint node) {
logger.finest("onZWaveNodeAdded: "+ node);
public void onNotification(Notification notification, Object context) {
switch (notification.getType()) {
case DRIVER_READY:
System.out.println(String.format("Driver ready\n" +
"\thome id: %d",
notification.getHomeId()
));
homeId = notification.getHomeId();
break;
case DRIVER_FAILED:
System.out.println("Driver failed");
break;
case DRIVER_RESET:
System.out.println("Driver reset");
break;
case AWAKE_NODES_QUERIED:
System.out.println("Awake nodes queried");
break;
case ALL_NODES_QUERIED_SOME_DEAD:
System.out.println("Some Nodes are dead");
case ALL_NODES_QUERIED:
System.out.println("Finished querying nodes");
manager.writeConfig(homeId);
// Controller is done initializing
break;
case POLLING_ENABLED:
System.out.println("Polling enabled");
break;
case POLLING_DISABLED:
System.out.println("Polling disabled");
break;
case NODE_NEW:
System.out.println(String.format("Node new\n" +
"\tnode id: %d",
notification.getNodeId()
));
break;
case NODE_ADDED:
System.out.println(String.format("Node added\n" +
"\tnode id: %d",
notification.getNodeId()
));
break;
case NODE_REMOVED:
System.out.println(String.format("Node removed\n" +
"\tnode id: %d",
notification.getNodeId()
));
break;
case ESSENTIAL_NODE_QUERIES_COMPLETE:
System.out.println(String.format("Node essential queries complete\n" +
"\tnode id: %d",
notification.getNodeId()
));
break;
case NODE_QUERIES_COMPLETE:
System.out.println(String.format("Node queries complete\n" +
"\tnode id: %d",
notification.getNodeId()
));
break;
case NODE_EVENT:
System.out.println(String.format("Node event\n" +
"\tnode id: %d\n" +
"\tevent id: %d",
notification.getNodeId(),
notification.getEvent()
));
break;
case NODE_NAMING:
System.out.println(String.format("Node naming\n" +
"\tnode id: %d",
notification.getNodeId()
));
break;
case NODE_PROTOCOL_INFO:
System.out.println(String.format("Node protocol info\n" +
"\tnode id: %d\n" +
"\ttype: %s",
notification.getNodeId(),
manager.getNodeType(notification.getHomeId(), notification.getNodeId())
));
break;
case VALUE_ADDED:
System.out.println(String.format("Value added\n" +
"\tnode id: %d\n" +
"\tcommand class: %d\n" +
"\tinstance: %d\n" +
"\tindex: %d\n" +
"\tgenre: %s\n" +
"\ttype: %s\n" +
"\tlabel: %s\n" +
"\tvalue: %s",
notification.getNodeId(),
notification.getValueId().getCommandClassId(),
notification.getValueId().getInstance(),
notification.getValueId().getIndex(),
notification.getValueId().getGenre().name(),
notification.getValueId().getType().name(),
manager.getValueLabel(notification.getValueId()),
getValue(notification.getValueId())
));
break;
case VALUE_REMOVED:
System.out.println(String.format("Value removed\n" +
"\tnode id: %d\n" +
"\tcommand class: %d\n" +
"\tinstance: %d\n" +
"\tindex: %d",
notification.getNodeId(),
notification.getValueId().getCommandClassId(),
notification.getValueId().getInstance(),
notification.getValueId().getIndex()
));
break;
case VALUE_CHANGED:
System.out.println(String.format("Value changed\n" +
"\tnode id: %d\n" +
"\tcommand class: %d\n" +
"\tinstance: %d\n" +
"\tindex: %d\n" +
"\tvalue: %s",
notification.getNodeId(),
notification.getValueId().getCommandClassId(),
notification.getValueId().getInstance(),
notification.getValueId().getIndex(),
getValue(notification.getValueId())
));
break;
case VALUE_REFRESHED:
System.out.println(String.format("Value refreshed\n" +
"\tnode id: %d\n" +
"\tcommand class: %d\n" +
"\tinstance: %d\n" +
"\tindex: %d" +
"\tvalue: %s",
notification.getNodeId(),
notification.getValueId().getCommandClassId(),
notification.getValueId().getInstance(),
notification.getValueId().getIndex(),
getValue(notification.getValueId())
));
break;
case GROUP:
System.out.println(String.format("Group\n" +
"\tnode id: %d\n" +
"\tgroup id: %d",
notification.getNodeId(),
notification.getGroupIdx()
));
break;
case SCENE_EVENT:
System.out.println(String.format("Scene event\n" +
"\tscene id: %d",
notification.getSceneId()
));
break;
case CREATE_BUTTON:
System.out.println(String.format("Button create\n" +
"\tbutton id: %d",
notification.getButtonId()
));
break;
case DELETE_BUTTON:
System.out.println(String.format("Button delete\n" +
"\tbutton id: %d",
notification.getButtonId()
));
break;
case BUTTON_ON:
System.out.println(String.format("Button on\n" +
"\tbutton id: %d",
notification.getButtonId()
));
break;
case BUTTON_OFF:
System.out.println(String.format("Button off\n" +
"\tbutton id: %d",
notification.getButtonId()
));
break;
case NOTIFICATION:
System.out.println("Notification");
break;
default:
System.out.println(notification.getType().name());
break;
}
}
@Override
public void onZWaveNodeUpdated(ZWaveEndpoint node) {
logger.finest("onZWaveNodeUpdated: "+ node);
private static Object getValue(ValueId valueId) {
switch (valueId.getType()) {
case BOOL:
AtomicReference<Boolean> b = new AtomicReference<>();
Manager.get().getValueAsBool(valueId, b);
return b.get();
case BYTE:
AtomicReference<Short> bb = new AtomicReference<>();
Manager.get().getValueAsByte(valueId, bb);
return bb.get();
case DECIMAL:
AtomicReference<Float> f = new AtomicReference<>();
Manager.get().getValueAsFloat(valueId, f);
return f.get();
case INT:
AtomicReference<Integer> i = new AtomicReference<>();
Manager.get().getValueAsInt(valueId, i);
return i.get();
case LIST:
return null;
case SCHEDULE:
return null;
case SHORT:
AtomicReference<Short> s = new AtomicReference<>();
Manager.get().getValueAsShort(valueId, s);
return s.get();
case STRING:
AtomicReference<String> ss = new AtomicReference<>();
Manager.get().getValueAsString(valueId, ss);
return ss.get();
case BUTTON:
return null;
case RAW:
AtomicReference<short[]> sss = new AtomicReference<>();
Manager.get().getValueAsRaw(valueId, sss);
return sss.get();
default:
return null;
}
}
@Override
public void onZWaveConnectionFailure(Throwable t) {
logger.finest("onZWaveConnectionFailure: "+ t);
}
@Override
public void onZWaveControllerInfo(String libraryVersion, Integer homeId, Byte nodeId) {
logger.finest("onZWaveControllerInfo: "+ libraryVersion+" "+homeId+" "+nodeId);
}
@Override
public void onZWaveInclusionStarted() {
logger.finest("onZWaveInclusionStarted");
}
@Override
public void onZWaveInclusion(NodeInfo nodeInfo, boolean success) {
logger.finest("onZWaveInclusion: "+ nodeInfo + " "+success);
}
@Override
public void onZWaveInclusionStopped() {
logger.finest("onZWaveInclusionStopped");
}
@Override
public void onZWaveExclusionStarted() {
logger.finest("onZWaveExclusionStarted");
}
@Override
public void onZWaveExclusion(NodeInfo nodeInfo, boolean success) {
logger.finest("onZWaveExclusion: "+ nodeInfo + " "+success);
}
@Override
public void onZWaveExclusionStopped() {
logger.finest("onZWaveExclusionStopped");
}
////////////// HAL CODE ////////////////////////
@Override
public void register(HalSensorConfig sensor) {
}
@Override
public void register(HalEventConfig event) {
}
@Override
public void deregister(HalSensorConfig sensor) {
public void register(HalSensorConfig sensor) {
}
@Override
public void deregister(HalEventConfig event) {
registeredDevices.remove(event);
}
@Override
public void deregister(HalSensorConfig sensor) {
registeredDevices.remove(sensor);
}
@Override
public int size() {
return 0;
return registeredDevices.size();
}
@Override
public void setListener(HalEventReportListener listener) {
}
@Override
public void setListener(HalSensorReportListener listener) {
}
@Override
public void send(HalEventConfig eventConfig, HalEventData eventData) {
}
@Override
public void setListener(HalEventReportListener listener) {
eventListener = listener;
}
@Override
public void setListener(HalSensorReportListener listener) {
sensorListener = listener;
}
}

View file

@ -1,81 +0,0 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package se.hal.plugin.zwave;
import com.whizzosoftware.wzwave.node.NodeCreationException;
import com.whizzosoftware.wzwave.node.NodeListener;
import com.whizzosoftware.wzwave.node.ZWaveNode;
import com.whizzosoftware.wzwave.node.ZWaveNodeFactory;
import com.whizzosoftware.wzwave.persist.PersistenceContext;
import com.whizzosoftware.wzwave.persist.PersistentStore;
import java.util.HashMap;
import java.util.Map;
/**
* A HashMap implementation of PersistentStore.
*
* @author Ziver Koc
*/
public class HashMapPersistentStore implements PersistentStore, PersistenceContext {
private HashMap<String, HashMap<String,Object>> db;
public HashMapPersistentStore() {
db = new HashMap<>();
}
@Override
public ZWaveNode getNode(byte nodeId, NodeListener listener) throws NodeCreationException {
return ZWaveNodeFactory.createNode(this, nodeId, listener);
}
@Override
public void saveNode(ZWaveNode node) {
node.save(this);
}
@Override
public void close() { }
@Override
public Map<String, Object> getNodeMap(int nodeId) {
return get("" + nodeId);
}
@Override
public Map<String, Object> getCommandClassMap(int nodeId, int commandClassId) {
return get("" + nodeId + "." + commandClassId);
}
private Map<String, Object> get(String key) {
if (!db.containsKey(key))
db.put(key, new HashMap<String, Object>());
return db.get(key);
}
}