Added custom serial port implementation using JSerialComm
This commit is contained in:
parent
1c0340b9cf
commit
b60a7af9cd
6 changed files with 199 additions and 17 deletions
|
|
@ -1,4 +1,5 @@
|
|||
dependencies {
|
||||
implementation project(':hal-core')
|
||||
implementation 'com.fazecast:jSerialComm:1.3.11'
|
||||
|
||||
implementation 'com.fazecast:jSerialComm:2.6.2'
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,12 @@
|
|||
repositories {
|
||||
maven {
|
||||
url "https://dl.bintray.com/tlaukkan/bubblecloud"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation project(':hal-core')
|
||||
|
||||
implementation 'com.fazecast:jSerialComm:2.6.2'
|
||||
implementation 'org.bubblecloud.zigbee4java:zigbee-api:3.0.3'
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
package se.hal.plugin.zigbee;
|
||||
|
||||
import org.bubblecloud.zigbee.v3.SerialPort;
|
||||
import org.bubblecloud.zigbee.v3.ZigBeeApiDongleImpl;
|
||||
import org.bubblecloud.zigbee.v3.ZigBeeDevice;
|
||||
import org.bubblecloud.zigbee.v3.ZigBeeDongleTiCc2531Impl;
|
||||
import se.hal.HalContext;
|
||||
import se.hal.intf.*;
|
||||
import se.hal.struct.AbstractDevice;
|
||||
|
|
@ -14,7 +18,7 @@ import java.util.logging.Logger;
|
|||
public class HalZigbeeController implements HalSensorController, HalEventController, HalAutoScannableController {
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
private static final String CONFIG_ZIGBEE_PORT = "";
|
||||
private static final String CONFIG_ZIGBEE_PORT = "zigbee.com_port";
|
||||
|
||||
private SerialPort port;
|
||||
private ZigBeeApiDongleImpl zigbeeApi;
|
||||
|
|
@ -37,29 +41,21 @@ public class HalZigbeeController implements HalSensorController, HalEventControl
|
|||
}
|
||||
public void initialize(String comPort) {
|
||||
byte[] networkKey = null; // Default network key
|
||||
port = new SerialPortImpl(comPort);
|
||||
port = new SerialPortJSC(comPort);
|
||||
zigbeeApi = new ZigBeeApiDongleImpl(
|
||||
new ZigBeeDongleTiCc2531Impl(port, 4951, 11, networkKey, false),
|
||||
new ZigBeeDongleTiCc2531Impl(port, -6480, 11, networkKey, false),
|
||||
false);
|
||||
|
||||
zigbeeApi.startup();
|
||||
|
||||
ZigBeeDevice device = zigbeeApi.getZigBeeDevices().get(3);
|
||||
|
||||
zigbeeApi.on(device);
|
||||
Thread.sleep(1000);
|
||||
zigbeeApi.color(device, 1.0, 0.0, 0.0, 1.0);
|
||||
Thread.sleep(1000);
|
||||
zigbeeApi.color(device, 0.0, 1.0, 0.0, 1.0);
|
||||
Thread.sleep(1000);
|
||||
zigbeeApi.color(device, 0.0, 0.0, 1.0, 1.0);
|
||||
Thread.sleep(1000);
|
||||
zigbeeApi.off(device);
|
||||
for (ZigBeeDevice device : zigbeeApi.getDevices()) {
|
||||
System.out.println("Device: " + device.getLabel());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
logger.info("Shutting down Zigbee port...");
|
||||
logger.info("Shutting down Zigbee port.");
|
||||
|
||||
zigbeeApi.shutdown();
|
||||
port.close();
|
||||
|
|
|
|||
130
plugins/hal-zigbee/src/se/hal/plugin/zigbee/SerialPortJSC.java
Normal file
130
plugins/hal-zigbee/src/se/hal/plugin/zigbee/SerialPortJSC.java
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 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.zigbee;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import com.fazecast.jSerialComm.SerialPort;
|
||||
|
||||
/**
|
||||
* Wrapper for usage of JSerialComm library with Zigbee4Java
|
||||
*
|
||||
* @author Ziver Koc
|
||||
*/
|
||||
public class SerialPortJSC implements org.bubblecloud.zigbee.v3.SerialPort {
|
||||
private final static Logger LOGGER = LoggerFactory.getLogger(SerialPortJSC.class);
|
||||
|
||||
/**
|
||||
* The default baud rate.
|
||||
*/
|
||||
public static final int DEFAULT_BAUD_RATE = 38400;
|
||||
|
||||
private final String portName;
|
||||
private final int baudRate;
|
||||
|
||||
private SerialPort serialPort;
|
||||
private InputStream inputStream;
|
||||
private OutputStream outputStream;
|
||||
|
||||
/**
|
||||
* Constructor which sets port name to given value and baud rate to default.
|
||||
*/
|
||||
public SerialPortJSC(final String portName) {
|
||||
this(portName, DEFAULT_BAUD_RATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor setting port name and baud rate.
|
||||
* @param portName the port name
|
||||
* @param baudRate the baud rate
|
||||
*/
|
||||
public SerialPortJSC(final String portName, final int baudRate) {
|
||||
this.portName = portName;
|
||||
this.baudRate = baudRate;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean open() {
|
||||
if (serialPort != null) {
|
||||
throw new RuntimeException("Serial port already open.");
|
||||
}
|
||||
|
||||
try {
|
||||
LOGGER.info("Connecting to com port... (" + portName + ")");
|
||||
serialPort = SerialPort.getCommPort(portName);
|
||||
serialPort.setBaudRate(baudRate);
|
||||
serialPort.setComPortTimeouts(
|
||||
SerialPort.TIMEOUT_READ_BLOCKING, 0, 0);
|
||||
|
||||
if (!serialPort.openPort()) {
|
||||
LOGGER.error("Could not open port: " + portName);
|
||||
return false;
|
||||
}
|
||||
|
||||
outputStream = serialPort.getOutputStream();
|
||||
inputStream = serialPort.getInputStream();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LOGGER.warn("Unable to open serial port: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (serialPort == null)
|
||||
return;
|
||||
|
||||
try {
|
||||
inputStream.close();
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
serialPort.closePort();
|
||||
|
||||
LOGGER.info("Serial portName '" + portName + "' closed.");
|
||||
|
||||
serialPort = null;
|
||||
inputStream = null;
|
||||
outputStream = null;
|
||||
} catch (Exception e) {
|
||||
LOGGER.warn("Error closing portName portName: '" + portName + "'", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream getOutputStream() {
|
||||
return outputStream;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() {
|
||||
return inputStream;
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,6 @@
|
|||
"name": "Hal-Zigbee",
|
||||
"description": "A Zigbee plugin for directly connecting to a CC2531 device over serial port.",
|
||||
"interfaces": [
|
||||
{"se.hal.intf.HalAutoScannableController": "se.hal.plugin.zigbee.deconz.zigbee.HalDeconzZigbeeController"}
|
||||
{"se.hal.intf.HalAutoScannableController": "se.hal.plugin.zigbee.HalZigbeeController"}
|
||||
]
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 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.zigbee;
|
||||
|
||||
import zutil.log.CompactLogFormatter;
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class HalZigbeeControllerTest {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
LogUtil.setGlobalFormatter(new CompactLogFormatter());
|
||||
LogUtil.setGlobalLevel(Level.ALL);
|
||||
|
||||
HalZigbeeController controller = new HalZigbeeController();
|
||||
controller.initialize(
|
||||
"COM3");
|
||||
|
||||
System.out.println("Press ENTER to exit application.");
|
||||
System.in.read();
|
||||
|
||||
controller.close();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue