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);
}
}
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()));
}
}
Aggregations