Compare commits

...

3 Commits

Author SHA1 Message Date
herel 830d594806 Display Snackbar when message is received 2022-04-26 22:56:22 +02:00
herel a027f8068f Remove unneeded getStatus() 2022-04-26 22:31:16 +02:00
herel fd65f7fd3e Use constants of strings 2022-04-26 15:43:07 +02:00
3 changed files with 13 additions and 8 deletions
@@ -12,6 +12,8 @@ import android.util.Log;
import android.view.View;
import android.widget.ToggleButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@@ -22,6 +24,9 @@ public class MainActivity extends AppCompatActivity {
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("STATE");
Log.i(TAG, "Received " + msg);
View contextView = findViewById(R.id.toggleButton);
Snackbar.make(contextView, "Received " + msg, Snackbar.LENGTH_LONG)
.show();
stateButton.setChecked(!msg.equals("OFF"));
}
};
@@ -19,6 +19,8 @@ public class MqttService extends Service {
private Mqtt5AsyncClient client;
private static final String TAG = "MQTT Service";
private final IBinder binder = new LocalBinder();
private static final String mqttHost = "192.168.178.64";
private static final int mqttPort = 1883;
public class LocalBinder extends Binder {
public MqttService getService() {
@@ -31,11 +33,10 @@ public class MqttService extends Service {
public void onCreate() {
Log.i(TAG, "onCreate");
if (client == null) {
Log.i(TAG, "connecting");
client = Mqtt5Client.builder()
.identifier(UUID.randomUUID().toString())
.serverHost("192.168.178.64")
.serverPort(1883)
.serverHost(mqttHost)
.serverPort(mqttPort)
.buildAsync();
}
}
@@ -83,7 +84,6 @@ public class MqttService extends Service {
} else {
// Handle successful subscription, e.g. logging or incrementing a metric
Log.i(TAG, "subscribe ok");
getStatus();
}
});
}
@@ -93,7 +93,7 @@ public class MqttService extends Service {
intent.setAction("ch.luria.mq1");
try {
final JSONObject obj = new JSONObject(msg);
String state = obj.getString("state");
String state = obj.getString(getString(R.string.mqtt_state_field));
intent.putExtra("STATE", state);
sendBroadcast(intent);
} catch (JSONException e) {
@@ -115,7 +115,6 @@ public class MqttService extends Service {
} else {
// handle successful publish, e.g. logging or incrementing a metric
Log.i(TAG, "published " + message + " on " + topic);
getStatus();
}
});
} else {
@@ -129,7 +128,7 @@ public class MqttService extends Service {
public void doToggle(boolean state) {
JSONObject obj = new JSONObject();
try {
obj.put("state", state ? "ON" : "OFF");
obj.put(getString(R.string.mqtt_state_field), state ? "ON" : "OFF");
Log.i(TAG, "doToggle: " + obj);
publishMessage(getString(R.string.mqtt_base_topic) + "/" + getString(R.string.mqtt_room_address) + "/set", obj.toString());
} catch (JSONException e) {
@@ -140,7 +139,7 @@ public class MqttService extends Service {
public void getStatus() {
JSONObject obj = new JSONObject();
try {
obj.put("state", "");
obj.put(getString(R.string.mqtt_state_field), "");
Log.i(TAG, "getStatus()");
publishMessage(getString(R.string.mqtt_base_topic) + "/" + getString(R.string.mqtt_room_address) + "/get", obj.toString());
} catch (JSONException e) {
+1
View File
@@ -5,4 +5,5 @@
<string name="room_name">Chambre d\'Isaac</string>
<string name="mqtt_base_topic">zigbee2mqtt</string>
<string name="mqtt_room_address">Ampoule Chambre Isaac</string>
<string name="mqtt_state_field">state</string>
</resources>