Search in sources :

Example 1 with BlueGigaSerialHandler

use of org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.BlueGigaSerialHandler in project smarthome by eclipse.

the class BlueGigaBridgeHandler method dispose.

@Override
public void dispose() {
    try {
        BlueGigaSerialHandler bgh = getBgHandler();
        bgh.removeEventListener(this);
        bgh.removeHandlerListener(this);
        bgh.close();
    } catch (IllegalStateException e) {
    // ignore if handler wasn't set at all
    }
    closeSerialPort();
}
Also used : BlueGigaSerialHandler(org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.BlueGigaSerialHandler)

Example 2 with BlueGigaSerialHandler

use of org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.BlueGigaSerialHandler in project smarthome by eclipse.

the class BlueGigaBridgeHandler method initialize.

@Override
public void initialize() {
    Object discovery = getConfig().get(BlueGigaAdapterConstants.PROPERTY_DISCOVERY);
    if (discovery != null && discovery.toString().equalsIgnoreCase(Boolean.FALSE.toString())) {
        discoveryActive = false;
        logger.debug("Deactivated discovery participation.");
    }
    final String portId = (String) getConfig().get(BlueGigaAdapterConstants.CONFIGURATION_PORT);
    if (portId == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Serial port must be configured!");
        return;
    }
    if (openSerialPort(portId, 115200)) {
        BlueGigaSerialHandler bgh = new BlueGigaSerialHandler(inputStream, outputStream);
        // Create and send the reset command to the dongle
        bgh.addEventListener(this);
        bgh.addHandlerListener(this);
        this.setBgHandler(bgh);
        updateStatus(ThingStatus.UNKNOWN);
        scheduler.submit(() -> {
            // Stop any procedures that are running
            bgStopProcedure();
            // Close all transactions
            BlueGigaCommand command = new BlueGigaGetConnectionsCommand();
            BlueGigaGetConnectionsResponse connectionsResponse = (BlueGigaGetConnectionsResponse) bgh.sendTransaction(command);
            if (connectionsResponse != null) {
                maxConnections = connectionsResponse.getMaxconn();
            }
            // Close all connections so we start from a known position
            for (int connection = 0; connection < maxConnections; connection++) {
                bgDisconnect(connection);
            }
            // Get our Bluetooth address
            command = new BlueGigaAddressGetCommand();
            BlueGigaAddressGetResponse addressResponse = (BlueGigaAddressGetResponse) bgh.sendTransaction(command);
            if (addressResponse != null) {
                address = new BluetoothAddress(addressResponse.getAddress());
                updateStatus(ThingStatus.ONLINE);
            } else {
                updateStatus(ThingStatus.OFFLINE);
            }
            command = new BlueGigaGetInfoCommand();
            BlueGigaGetInfoResponse infoResponse = (BlueGigaGetInfoResponse) bgh.sendTransaction(command);
            // Set mode to non-discoverable etc.
            // Not doing this will cause connection failures later
            bgSetMode();
            // Start passive scan
            bgStartScanning(false, passiveScanInterval, passiveScanWindow);
            Map<String, String> properties = editProperties();
            properties.put(BluetoothBindingConstants.PROPERTY_MAXCONNECTIONS, Integer.toString(maxConnections));
            properties.put(Thing.PROPERTY_FIRMWARE_VERSION, String.format("%d.%d", infoResponse.getMajor(), infoResponse.getMinor()));
            properties.put(Thing.PROPERTY_HARDWARE_VERSION, Integer.toString(infoResponse.getHardware()));
            properties.put(BlueGigaAdapterConstants.PROPERTY_PROTOCOL, Integer.toString(infoResponse.getProtocolVersion()));
            properties.put(BlueGigaAdapterConstants.PROPERTY_LINKLAYER, Integer.toString(infoResponse.getLlVersion()));
            updateProperties(properties);
        });
    } else {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Failed opening serial port.");
    }
}
Also used : BluetoothAddress(org.eclipse.smarthome.binding.bluetooth.BluetoothAddress) BlueGigaGetConnectionsResponse(org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.command.system.BlueGigaGetConnectionsResponse) BlueGigaGetConnectionsCommand(org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.command.system.BlueGigaGetConnectionsCommand) BlueGigaAddressGetResponse(org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.command.system.BlueGigaAddressGetResponse) BlueGigaAddressGetCommand(org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.command.system.BlueGigaAddressGetCommand) BlueGigaCommand(org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.BlueGigaCommand) BlueGigaGetInfoCommand(org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.command.system.BlueGigaGetInfoCommand) BlueGigaSerialHandler(org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.BlueGigaSerialHandler) BlueGigaGetInfoResponse(org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.command.system.BlueGigaGetInfoResponse)

Aggregations

BlueGigaSerialHandler (org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.BlueGigaSerialHandler)2 BluetoothAddress (org.eclipse.smarthome.binding.bluetooth.BluetoothAddress)1 BlueGigaCommand (org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.BlueGigaCommand)1 BlueGigaAddressGetCommand (org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.command.system.BlueGigaAddressGetCommand)1 BlueGigaAddressGetResponse (org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.command.system.BlueGigaAddressGetResponse)1 BlueGigaGetConnectionsCommand (org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.command.system.BlueGigaGetConnectionsCommand)1 BlueGigaGetConnectionsResponse (org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.command.system.BlueGigaGetConnectionsResponse)1 BlueGigaGetInfoCommand (org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.command.system.BlueGigaGetInfoCommand)1 BlueGigaGetInfoResponse (org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.command.system.BlueGigaGetInfoResponse)1