All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bidib.jbidibc.netbidib.client.pairingstates.MyRequestPairingState Maven / Gradle / Ivy

package org.bidib.jbidibc.netbidib.client.pairingstates;

import org.bidib.jbidibc.messages.BidibLibrary;
import org.bidib.jbidibc.messages.enums.PairingResult;
import org.bidib.jbidibc.messages.exception.ProtocolException;
import org.bidib.jbidibc.messages.message.netbidib.BidibLinkData;
import org.bidib.jbidibc.messages.message.netbidib.LocalLinkMessage;
import org.bidib.jbidibc.messages.message.netbidib.NetBidibLinkData.PairingStatus;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyRequestPairingState extends AbstractPairingState {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyRequestPairingState.class);

    public MyRequestPairingState(final DefaultPairingStateHandler pairingStateHandler,
        BidibLinkData remotePartnerLinkData, ProxyBidibLinkData clientLinkData) {
        super(pairingStateHandler, remotePartnerLinkData, clientLinkData);
    }

    @Override
    public void onLocalLink(LocalLinkMessage localLinkMessage) {

        switch (localLinkMessage.getLinkDescriptor()) {
            case BidibLibrary.BIDIB_LINK_PAIRING_REQUEST:
                LOGGER
                    .info("Received a pairing request, senderUID: {}, receiverUID: {}, pairingTimeout: {}",
                        ByteUtils.formatHexUniqueId(localLinkMessage.getSenderUniqueId()),
                        ByteUtils.formatHexUniqueId(localLinkMessage.getReceiverUniqueId()),
                        localLinkMessage.getPairingTimeout());

                // remotePartnerLinkData.setPairingStatus(PairingStatus.PAIRING_REQUESTED);
                signalPairingResult(PairingStatus.PAIRING_REQUESTED);
                remotePartnerLinkData.setRequestedPairingTimeout(localLinkMessage.getPairingTimeout());

                // signal the pairing request to the ui
                signalPairingRequest();

                break;
            case BidibLibrary.BIDIB_LINK_STATUS_UNPAIRED:
                LOGGER
                    .info("Received the status unpaired from remote partner: {}",
                        ByteUtils.formatHexUniqueId(localLinkMessage.getSenderUniqueId()));

                signalPairingResult(PairingStatus.UNPAIRED);

                setNextPairingState(PairingStateEnum.Unpaired);
                break;
            case BidibLibrary.BIDIB_LINK_STATUS_PAIRED:
                LOGGER
                    .info("Received the status paired from remote partner: {}",
                        ByteUtils.formatHexUniqueId(localLinkMessage.getSenderUniqueId()));

                if (clientLinkData.getPairingStatus() != PairingStatus.PAIRED) {
                    LOGGER.info("Send the paired state to the remote partner.");
                    try {
                        // send the paired state
                        LocalLinkMessage pairedStateMessage =
                            getRequestFactory()
                                .createLocalLinkStatusPaired(clientLinkData.getUniqueId(),
                                    remotePartnerLinkData.getUniqueId());
                        publishMessage(pairedStateMessage);
                    }
                    catch (ProtocolException ex) {
                        LOGGER.warn("Send LocalLinkStatusPaired to remote partner failed.", ex);
                        // TODO: handle exception
                    }
                }

                signalPairingResult(PairingStatus.PAIRED);

                setNextPairingState(PairingStateEnum.Paired);

                break;
            default:
                super.onLocalLink(localLinkMessage);
                break;
        }
    }

    @Override
    public void pairingResult(PairingResult pairingResult) {
        LOGGER.info("pairingResult, pairingResult: {}", pairingResult);

        if (PairingResult.PAIRED == pairingResult) {
            LOGGER.info("Pairing request was accepted by user.");

            clientLinkData.setPairingStatus(PairingStatus.PAIRED);
            try {
                // send the status paired
                LocalLinkMessage pairedMessage =
                    getRequestFactory()
                        .createLocalLinkStatusPaired(clientLinkData.getUniqueId(), remotePartnerLinkData.getUniqueId());
                publishMessage(pairedMessage);

                // keep the state
                // setNextPairingState(PairingStateEnum.Paired);
            }
            catch (ProtocolException ex) {
                LOGGER.warn("Send LocalLinkStatusPaired to remote partner failed.", ex);
                // TODO: handle exception
            }

        }
        else {
            LOGGER.warn("Pairing request was not accepted by user.");

            clientLinkData.setPairingStatus(PairingStatus.UNPAIRED);
            try {
                // send the status unpaired
                LocalLinkMessage unpairedMessage =
                    getRequestFactory()
                        .createLocalLinkStatusUnpaired(clientLinkData.getUniqueId(),
                            remotePartnerLinkData.getUniqueId());
                publishMessage(unpairedMessage);

                setNextPairingState(PairingStateEnum.Unpaired);
            }
            catch (ProtocolException ex) {
                LOGGER.warn("Send LocalLinkStatusUnpaired to remote partner failed.", ex);
                // TODO: handle exception
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy