use of org.flyve.mdm.agent.data.database.entity.Topics in project android-mdm-agent by flyve-mdm.
the class TopicsData method setValue.
public List<Topics> setValue(String topic, int qos) {
if (dataBase.TopicsDao().getByTopic(topic).isEmpty()) {
// remove previous fleet to keep just one
if (topic.contains("fleet")) {
dataBase.TopicsDao().deleteFleets();
}
Topics topics = new Topics();
topics.topic = topic;
topics.qos = qos;
topics.status = 0;
dataBase.TopicsDao().insert(topics);
} else {
Topics topics = dataBase.TopicsDao().getByTopic(topic).get(0);
topics.qos = qos;
dataBase.TopicsDao().update(topics);
}
return dataBase.TopicsDao().getByTopic(topic);
}
use of org.flyve.mdm.agent.data.database.entity.Topics in project android-mdm-agent by flyve-mdm.
the class MqttController method subscribe.
/**
* Subscribe to the topic
* When come from MQTT has a format like this {"subscribe":[{"topic":"/2/fleet/22"}]}
*/
public void subscribe(final String channel) {
if (channel == null || channel.contains("null")) {
// case of unsubscribe
AppDataBase dataBase = AppDataBase.getAppDatabase(context);
dataBase.TopicsDao().deleteFleets();
return;
}
final List<Topics> topics = new TopicsData(context).setValue(channel, 0);
// if topic null
if (topics == null || topics.isEmpty()) {
return;
}
// use List instead array because we need to keep only topic with status 0
List<String> lstTopics = new ArrayList<>();
List<Integer> lstQos = new ArrayList<>();
for (int i = 0; i < topics.size(); i++) {
// if not subscribed
if (topics.get(i).status != 1) {
lstTopics.add(topics.get(i).topic);
lstQos.add(topics.get(i).qos);
}
}
try {
String[] arrayTopics;
arrayTopics = lstTopics.toArray(new String[lstTopics.size()]);
// transform list of Integer to array of int 'primitive'
int size = lstQos.size();
int[] arrayQos = new int[size];
Integer[] temp = lstQos.toArray(new Integer[size]);
for (int n = 0; n < size; ++n) {
arrayQos[n] = temp[n];
}
IMqttToken subToken = client.subscribe(arrayTopics, arrayQos);
subToken.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
// The message was published
for (String topic : asyncActionToken.getTopics()) {
new TopicsData(context).setStatusTopic(topic, 1);
FlyveLog.d("Subscribe from fleet " + topic);
broadcastReceivedLog(" -> " + topic, "Subscribed", String.valueOf(asyncActionToken.getTopics().length));
}
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
// The subscription could not be performed, maybe the user was not
// authorized to subscribe on the specified topic e.g. using wildcards
String errorMessage = " unknown";
if (exception != null) {
errorMessage = exception.getMessage();
Log.d("Subscribe", "Error", exception);
}
FlyveLog.e(this.getClass().getName() + ", subscribe", "ERROR on subscribe: " + errorMessage);
broadcastReceivedLog(ERROR, "Error on subscribe", errorMessage);
}
});
} catch (Exception ex) {
FlyveLog.e(this.getClass().getName() + ", subscribe", ex.getMessage());
}
}
use of org.flyve.mdm.agent.data.database.entity.Topics in project android-mdm-agent by flyve-mdm.
the class FragmentTopics method loadData.
private void loadData(ListView lst) {
List<Topics> arrTopics = new TopicsData(FragmentTopics.this.getContext()).getAllTopics();
ArrayList arr = new ArrayList<HashMap<String, Boolean>>();
if (arrTopics.isEmpty()) {
HashMap<String, String> map = new HashMap<>();
map.put("topic", "");
map.put("status", "");
arr.add(map);
} else {
for (int i = 0; i < arrTopics.size(); i++) {
HashMap<String, String> map = new HashMap<>();
map.put("topic", String.valueOf(arrTopics.get(i).topic));
map.put("status", String.valueOf(arrTopics.get(i).status));
arr.add(map);
}
}
lst.setAdapter(new TopicsAdapter(FragmentTopics.this.getActivity(), arr));
}
use of org.flyve.mdm.agent.data.database.entity.Topics in project android-mdm-agent by flyve-mdm.
the class TopicsData method setStatusTopic.
public void setStatusTopic(String topic, int status) {
Topics topics = dataBase.TopicsDao().getByTopic(topic).get(0);
topics.status = status;
dataBase.TopicsDao().update(topics);
}
use of org.flyve.mdm.agent.data.database.entity.Topics in project android-mdm-agent by flyve-mdm.
the class MqttController method unsubscribe.
/**
* Unsubscribe to the topic
* When come from MQTT has a format like this {"subscribe":[{"topic":null}]}
*/
public void unsubscribe() {
final AppDataBase dataBase = AppDataBase.getAppDatabase(context);
List<Topics> topics = dataBase.TopicsDao().getFleets();
if (!topics.isEmpty()) {
for (int i = 0; i < topics.size(); i++) {
final Topics topicToUnsubscribe = topics.get(i);
try {
IMqttToken subToken = client.unsubscribe(topicToUnsubscribe.topic);
subToken.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
FlyveLog.d("Unsubscribe from fleet " + topicToUnsubscribe.topic);
dataBase.TopicsDao().delete(topicToUnsubscribe);
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
// The unsubscription could not be performed, maybe the user was not
// authorized to subscribe on the specified topic e.g. using wildcards
String errorMessage = " unknown";
if (exception != null) {
errorMessage = exception.getMessage();
Log.d("Unsubscribe", "Error", exception);
}
FlyveLog.e(this.getClass().getName() + ", unsubscribe", "ERROR on unsubscribe: " + errorMessage);
broadcastReceivedLog(ERROR, "Error on unsubscribe", errorMessage);
}
});
} catch (Exception ex) {
FlyveLog.e(this.getClass().getName() + ", unsubscribe", ex.getMessage());
}
}
}
}
Aggregations