Search in sources :

Example 1 with MqttData

use of org.flyve.mdm.agent.data.MqttData in project android-mdm-agent by flyve-mdm.

the class PoliciesController method useTLS.

/**
 * TLS
 * Example "useTLS": "true|false", "taskId": "25"
 */
public void useTLS(String taskId, Boolean enable) {
    try {
        MqttData cache = new MqttData(context);
        if (enable) {
            cache.setTls("1");
            // stop service
            ((Service) context).stopSelf();
            // restart MQTT connection with this new parameters
            MQTTService.start(MDMAgent.getInstance());
            // return the status of the task
            sendTaskStatus(taskId, FEEDBACK_DONE);
        } else {
            cache.setTls("0");
            // stop service
            ((Service) context).stopSelf();
            // restart MQTT connection with this new parameters
            MQTTService.start(MDMAgent.getInstance());
            // return the status of the task
            sendTaskStatus(taskId, FEEDBACK_DONE);
        }
    } catch (Exception ex) {
        FlyveLog.e(ex.getMessage());
        // return the status of the task
        sendTaskStatus(taskId, FEEDBACK_FAILED);
    }
}
Also used : MqttData(org.flyve.mdm.agent.data.MqttData) Service(android.app.Service)

Example 2 with MqttData

use of org.flyve.mdm.agent.data.MqttData in project android-mdm-agent by flyve-mdm.

the class MQTTService method connect.

/**
 * This function connect the agent with MQTT server
 */
public void connect() {
    Context mContext = this.getApplicationContext();
    MqttData cache = new MqttData(mContext);
    final String mBroker = cache.getBroker();
    final String mPort = cache.getPort();
    final String mUser = cache.getMqttuser();
    final String mPassword = cache.getMqttpasswd();
    final String mTopic = cache.getTopic();
    final String mTLS = cache.getTls();
    if (mPassword == null) {
        FlyveLog.d(TAG, "Password can't be null");
        return;
    }
    storeLog(Helpers.broadCastMessage(MQTT_LOGIN, "Broker", mBroker));
    storeLog(Helpers.broadCastMessage(MQTT_LOGIN, "Port", mPort));
    storeLog(Helpers.broadCastMessage(MQTT_LOGIN, "User", mUser));
    storeLog(Helpers.broadCastMessage(MQTT_LOGIN, "Topic", mTopic));
    FlyveLog.i("is TLS (0=false;1=true): %s", mTLS);
    String protocol = "tcp";
    // TLS is active change protocol
    if (mTLS.equals("1")) {
        protocol = "ssl";
    }
    String clientId = MqttClient.generateClientId();
    client = new MqttAndroidClient(mContext, protocol + "://" + mBroker + ":" + mPort, clientId);
    client.setCallback(this);
    try {
        MqttConnectOptions options = new MqttConnectOptions();
        options.setPassword(mPassword.toCharArray());
        options.setUserName(mUser);
        options.setCleanSession(true);
        options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
        options.setConnectionTimeout(0);
        options.setAutomaticReconnect(true);
        // Create a testament to send when MQTT connection is down
        String will = "{ online: false }";
        options.setWill("/Status/Online", will.getBytes(), 0, false);
        // If TLS is active needs ssl connection option
        if (mTLS.equals("1")) {
            // SSL
            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
            sslContext.init(null, null, null);
            options.setSocketFactory(sslContext.getSocketFactory());
        }
        IMqttToken token = client.connect(options);
        token.setActionCallback(new IMqttActionListener() {

            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                // We are connected
                // Everything ready waiting for message
                FlyveLog.d(TAG, "Success we are online!");
                broadcastServiceStatus(true);
                policiesController = new PoliciesController(getApplicationContext(), client);
                // send status online true to MQTT
                policiesController.sendOnlineStatus(true);
                // main channel
                String channel = mTopic + "/#";
                FlyveLog.d(TAG, "MQTT Channel: " + channel);
                policiesController.subscribe("#");
                // subscribe to manifest
                policiesController.subscribe("/FlyvemdmManifest/Status/Version");
                // send inventory on connect
                policiesController.createInventory();
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable ex) {
                // Something went wrong e.g. connection timeout or firewall problems
                String errorMessage = "";
                if (ex.getMessage().equalsIgnoreCase("MqttException")) {
                    errorMessage = ((MqttException) ex).toString();
                } else {
                    errorMessage = ex.getMessage();
                }
                FlyveLog.e(TAG, "Connection fail: " + errorMessage);
                String errorCode;
                try {
                    errorCode = String.valueOf(((MqttException) ex).getReasonCode());
                } catch (Exception exception) {
                    errorCode = "0";
                }
                storeLog(Helpers.broadCastMessage(ERROR, "Error on connect - client.connect", errorMessage));
                broadcastMessage(Helpers.broadCastMessage(ERROR, errorCode, errorMessage));
                broadcastServiceStatus(false);
            }
        });
    } catch (MqttException ex) {
        FlyveLog.e(TAG, ex.getMessage());
        broadcastMessage(Helpers.broadCastMessage(ERROR, String.valueOf(ex.getReasonCode()), ex.getMessage()));
        storeLog(Helpers.broadCastMessage(ERROR, "Error on connect", ex.getMessage()));
    } catch (Exception ex) {
        FlyveLog.e(TAG, ex.getMessage());
        broadcastMessage(Helpers.broadCastMessage(ERROR, "0", mContext.getResources().getString(R.string.MQTT_ERROR_CONNECTION)));
        storeLog(Helpers.broadCastMessage(ERROR, "Error on connect", ex.getMessage()));
    }
}
Also used : Context(android.content.Context) SSLContext(javax.net.ssl.SSLContext) MqttData(org.flyve.mdm.agent.data.MqttData) IMqttActionListener(org.eclipse.paho.client.mqttv3.IMqttActionListener) IMqttToken(org.eclipse.paho.client.mqttv3.IMqttToken) SSLContext(javax.net.ssl.SSLContext) MqttAndroidClient(org.eclipse.paho.android.service.MqttAndroidClient) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) MqttException(org.eclipse.paho.client.mqttv3.MqttException)

Aggregations

MqttData (org.flyve.mdm.agent.data.MqttData)2 Service (android.app.Service)1 Context (android.content.Context)1 SSLContext (javax.net.ssl.SSLContext)1 MqttAndroidClient (org.eclipse.paho.android.service.MqttAndroidClient)1 IMqttActionListener (org.eclipse.paho.client.mqttv3.IMqttActionListener)1 IMqttToken (org.eclipse.paho.client.mqttv3.IMqttToken)1 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)1 MqttException (org.eclipse.paho.client.mqttv3.MqttException)1