Compare commits

..

6 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
herel de0ff18112 no json in main activity 2022-04-26 15:34:41 +02:00
herel ff27f52269 finish renaming app 2022-04-26 15:33:51 +02:00
herel d60d497935 No action bar 2022-04-26 15:33:36 +02:00
6 changed files with 87 additions and 68 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ android {
compileSdk 31
defaultConfig {
applicationId "com.example.mysecondapp"
applicationId "ch.luria.mq1"
minSdk 29
targetSdk 31
versionCode 1
@@ -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"));
}
};
@@ -61,11 +66,7 @@ public class MainActivity extends AppCompatActivity {
}
public void toggleButtonState(View view) {
if (stateButton.isChecked()) {
mService.doToggle("{\"state\":\"ON\"}");
} else {
mService.doToggle("{\"state\":\"OFF\"}");
}
mService.doToggle(stateButton.isChecked());
}
private final ServiceConnection connection = new ServiceConnection() {
+63 -48
View File
@@ -6,6 +6,7 @@ import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import com.hivemq.client.mqtt.MqttClientState;
import com.hivemq.client.mqtt.mqtt5.Mqtt5AsyncClient;
import com.hivemq.client.mqtt.mqtt5.Mqtt5Client;
@@ -18,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() {
@@ -30,52 +33,43 @@ 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();
client.connectWith()
.send()
.whenComplete((connAck, throwable) -> {
if (throwable != null) {
// handle failure
Log.i(TAG, "could not connect");
} else {
Log.i(TAG, "connect ok");
subscribe();
}
});
}
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind");
connect();
return binder;
}
public void doToggle(String what) {
publishMessage("zigbee2mqtt/Ampoule Chambre Isaac/set", what);
private void connect() {
if (client.getState() == MqttClientState.DISCONNECTED) {
Log.i(TAG, "connect()");
client.connectWith()
.send()
.whenComplete((connAck, throwable) -> {
if (throwable != null) {
// handle failure
Log.i(TAG, "could not connect - " + connAck.getReasonString());
} else {
Log.i(TAG, "connect ok");
subscribe();
}
private void sendMessage(String msg) {
Intent intent = new Intent();
intent.setAction("ch.luria.mq1");
try {
final JSONObject obj = new JSONObject(msg);
String state = obj.getString("state");
intent.putExtra("STATE", state);
sendBroadcast(intent);
} catch (JSONException e) {
e.printStackTrace();
});
} else {
Log.i(TAG, "connect() in state " + client.getState());
}
}
private void subscribe() {
client.subscribeWith()
.topicFilter("zigbee2mqtt/Ampoule Chambre Isaac")
.topicFilter(getString(R.string.mqtt_base_topic) + "/" + getString(R.string.mqtt_room_address))
.callback(publish -> {
// Process the received message
String msg = new String(publish.getPayloadAsBytes()) + " from " + publish.getTopic();
@@ -90,45 +84,66 @@ public class MqttService extends Service {
} else {
// Handle successful subscription, e.g. logging or incrementing a metric
Log.i(TAG, "subscribe ok");
getStatus();
}
});
}
public void getStatus() {
if (client != null) {
client.publishWith()
.topic("zigbee2mqtt/Ampoule Chambre Isaac/get")
.payload("{\"state\":\"\"}".getBytes())
.send()
.whenComplete((mqtt3Publish, throwable) -> {
if (throwable != null) {
// handle failure to publish
Log.i(TAG, "could not publish");
} else {
// handle successful publish, e.g. logging or incrementing a metric
Log.i(TAG, "published");
}
});
private void sendMessage(String msg) {
Intent intent = new Intent();
intent.setAction("ch.luria.mq1");
try {
final JSONObject obj = new JSONObject(msg);
String state = obj.getString(getString(R.string.mqtt_state_field));
intent.putExtra("STATE", state);
sendBroadcast(intent);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void publishMessage(String topic, String message) {
if (client != null) {
if (client.getState() == MqttClientState.CONNECTED) {
client.publishWith()
.topic(topic)
.payload(message.getBytes())
.send()
.whenComplete((mqtt3Publish, throwable) -> {
.whenComplete((mqttPublish, throwable) -> {
if (throwable != null) {
// handle failure to publish
Log.i(TAG, "could not publish");
Log.i(TAG, "publishMessage(): could not publish: " + throwable);
} else {
// handle successful publish, e.g. logging or incrementing a metric
Log.i(TAG, "published " + message + " on " + topic);
getStatus();
}
});
} else {
Log.i(TAG, "publishMessage(): not connected");
}
} else {
Log.i(TAG, "publishMessage(): null client");
}
}
public void doToggle(boolean state) {
JSONObject obj = new JSONObject();
try {
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) {
e.printStackTrace();
}
}
public void getStatus() {
JSONObject obj = new JSONObject();
try {
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) {
e.printStackTrace();
}
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Mq1" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<style name="Theme.Mq1" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
+3
View File
@@ -3,4 +3,7 @@
<string name="button_on">ALLUMEE</string>
<string name="button_off">ETEINTE</string>
<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>
+1 -1
View File
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Mq1" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<style name="Theme.Mq1" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>