Skip to content

Commit

Permalink
fix: rename package + force mqtt connection before publish
Browse files Browse the repository at this point in the history
  • Loading branch information
ben8p committed Sep 14, 2019
1 parent 621cc2c commit 7d851f2
Show file tree
Hide file tree
Showing 27 changed files with 45 additions and 39 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
Binary file modified .idea/caches/gradle_models.ser
Binary file not shown.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
// + versionMinor * 100
// + versionPatch
// versionName “${versionMajor}.${versionMinor}.${versionPatch}”
versionCode 101000
versionName "1.1.0"
versionCode 101001
versionName "1.1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Expand Down
2 changes: 1 addition & 1 deletion app/release/output.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":101000,"versionName":"1.1.0","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":101001,"versionName":"1.1.1","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bsantalucia.beaconmqtt;
package com.bsantalucia.beaconmqtt.beaconmqtt;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
import static com.bsantalucia.beaconmqtt.settings.SettingsActivity.MQTT_PASS_KEY;


interface ConnectCallback {
void run(MqttAndroidClient mqttAndroidClient);
}

public class MqttBroadcaster {

private static final String TAG = MqttBroadcaster.class.getName();
Expand All @@ -44,7 +48,6 @@ public class MqttBroadcaster {
private static final String DEFAULT_EXIT_DISTANCE_TOPIC = "beacon/exit/distance";
private final Context context;

private MqttAndroidClient mqttAndroidClient = null;
private SharedPreferences.OnSharedPreferenceChangeListener listener;
private final SharedPreferences defaultSharedPreferences;
private final LogPersistence logPersistence;
Expand All @@ -55,13 +58,6 @@ public MqttBroadcaster(final Context context) {
defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

registerSettingsChangeListener();

String mqttServer = defaultSharedPreferences.getString(MQTT_SERVER_KEY, null);
String mqttPort = defaultSharedPreferences.getString(MQTT_PORT_KEY, null);
String mqttUser = defaultSharedPreferences.getString(MQTT_USER_KEY, null);
String mqttPassword = defaultSharedPreferences.getString(MQTT_PASS_KEY, null);

connectToMqttServer(mqttServer, mqttPort, mqttUser, mqttPassword);
}

public void publishEnterMessage(String uuid, String mac, String major, String minor) {
Expand Down Expand Up @@ -89,18 +85,30 @@ private void registerSettingsChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (MQTT_SERVER_KEY.equals(key) || MQTT_PORT_KEY.equals(key)) {
String mqttServer = defaultSharedPreferences.getString(MQTT_SERVER_KEY, null);
String mqttPort = defaultSharedPreferences.getString(MQTT_PORT_KEY, null);
String mqttUser = defaultSharedPreferences.getString(MQTT_USER_KEY, null);
String mqttPassword = defaultSharedPreferences.getString(MQTT_PASS_KEY, null);
connectToMqttServer(mqttServer, mqttPort, mqttUser, mqttPassword);
connectToMqttServer();
}
}
};
defaultSharedPreferences.registerOnSharedPreferenceChangeListener(listener);
}

private void connectToMqttServer(String mqttServer, String mqttPort, String mqttUser, String mqttPassword) {
private void connectToMqttServer() {
connectToMqttServer(new ConnectCallback() {
@Override
public void run(MqttAndroidClient mqttAndroidClient) {

}
});
}

private void connectToMqttServer(final ConnectCallback callback) {
String mqttServer = defaultSharedPreferences.getString(MQTT_SERVER_KEY, null);
String mqttPort = defaultSharedPreferences.getString(MQTT_PORT_KEY, null);
String mqttUser = defaultSharedPreferences.getString(MQTT_USER_KEY, null);
String mqttPassword = defaultSharedPreferences.getString(MQTT_PASS_KEY, null);

final MqttAndroidClient mqttAndroidClient;

if (mqttServer != null && mqttPort != null) {
final String serverUri = "tcp://" + mqttServer + ":" + mqttPort;

Expand Down Expand Up @@ -134,11 +142,11 @@ public void onSuccess(IMqttToken asyncActionToken) {
disconnectedBufferOptions.setPersistBuffer(false);
disconnectedBufferOptions.setDeleteOldestMessages(false);
mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
callback.run(mqttAndroidClient);
}

@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
mqttAndroidClient = null;
logPersistence.saveNewLog(context.getString(R.string.failed_to_connect_mqtt_server, serverUri), "");
Toast.makeText(context, context.getString(R.string.failed_to_connect_mqtt_server, serverUri), Toast.LENGTH_LONG).show();
Log.e(TAG, context.getString(R.string.failed_to_connect_mqtt_server, serverUri), exception);
Expand All @@ -149,12 +157,10 @@ public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
}

} else {
mqttAndroidClient = null;
logPersistence.saveNewLog(context.getString(R.string.mqtt_missing_server_or_port), "");
Toast.makeText(context, R.string.mqtt_missing_server_or_port, Toast.LENGTH_LONG).show();
Log.i(TAG, context.getString(R.string.mqtt_missing_server_or_port));
}

}

private JSONObject getMessagePayload(String uuid, String mac, String major, String minor, double distance) {
Expand Down Expand Up @@ -187,22 +193,23 @@ private JSONObject getMessagePayload(String uuid, String mac, String major, Stri
return jsonObject;
}

private void publishMessage(JSONObject payload, String topic) {
if (mqttAndroidClient != null) {
MqttMessage mqttMessage = new MqttMessage();
mqttMessage.setPayload(payload.toString().getBytes());
try {
mqttAndroidClient.publish(topic, mqttMessage);
} catch (MqttException ex){
ex.printStackTrace();
}
boolean logEvent = defaultSharedPreferences.getBoolean(GENEARL_LOG_KEY, false);
if (logEvent) {
String logMessage = context.getString(R.string.published_mqtt_message_to_topic, mqttMessage, topic);
logPersistence.saveNewLog(logMessage, "");
private void publishMessage(final JSONObject payload, final String topic) {
connectToMqttServer(new ConnectCallback() {
@Override
public void run(MqttAndroidClient mqttAndroidClient) {
MqttMessage mqttMessage = new MqttMessage();
mqttMessage.setPayload(payload.toString().getBytes());
try {
mqttAndroidClient.publish(topic, mqttMessage);
} catch (MqttException ex){
ex.printStackTrace();
}
boolean logEvent = defaultSharedPreferences.getBoolean(GENEARL_LOG_KEY, false);
if (logEvent) {
String logMessage = context.getString(R.string.published_mqtt_message_to_topic, mqttMessage, topic);
logPersistence.saveNewLog(logMessage, "");
}
}
} else {
Log.i(TAG, context.getString(R.string.publish_failed_not_set_up));
}
});
}
}
1 change: 0 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
<string name="published_mqtt_message_to_topic">Published MQTT message: %1$s to topic: %2$s</string>
<string name="error_creating_payload">Error Creating payload for uuid: %1$s, mac %2$s, major: %3$s, minor: %4$s</string>
<string name="error_creating_distance_payload">Error Creating payload for uuid: %1$s, mac %2$s, major: %3$s, minor: %4$s, distance: %5$f</string>
<string name="publish_failed_not_set_up">Publish not done because mqttAndroidClient is not set up</string>
<string name="connecting_to_mqtt_server">Connecting to MQTT server...</string>
<string name="connection_successful">Connection successful!</string>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bsantalucia.beaconmqtt;
package com.bsantalucia.beaconmqtt.beaconmqtt;

import org.junit.Test;

Expand Down

0 comments on commit 7d851f2

Please sign in to comment.