Search in sources :

Example 1 with BlueGigaCommand

use of org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.BlueGigaCommand 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)

Example 2 with BlueGigaCommand

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

the class BlueGigaBridgeHandler method bgStopProcedure.

/*
     * The following methods are private methods for handling the BlueGiga protocol
     */
private boolean bgStopProcedure() {
    BlueGigaCommand command = new BlueGigaEndProcedureCommand();
    BlueGigaEndProcedureResponse response = (BlueGigaEndProcedureResponse) getBgHandler().sendTransaction(command);
    return response.getResult() == BgApiResponse.SUCCESS;
}
Also used : BlueGigaCommand(org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.BlueGigaCommand) BlueGigaEndProcedureResponse(org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.command.gap.BlueGigaEndProcedureResponse) BlueGigaEndProcedureCommand(org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.command.gap.BlueGigaEndProcedureCommand)

Aggregations

BlueGigaCommand (org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.BlueGigaCommand)2 BluetoothAddress (org.eclipse.smarthome.binding.bluetooth.BluetoothAddress)1 BlueGigaSerialHandler (org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.BlueGigaSerialHandler)1 BlueGigaEndProcedureCommand (org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.command.gap.BlueGigaEndProcedureCommand)1 BlueGigaEndProcedureResponse (org.eclipse.smarthome.binding.bluetooth.bluegiga.internal.command.gap.BlueGigaEndProcedureResponse)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