no json in main activity

This commit is contained in:
2022-04-26 15:34:41 +02:00
parent ff27f52269
commit de0ff18112
3 changed files with 75 additions and 61 deletions
@@ -61,11 +61,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() {
+59 -43
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;
@@ -36,46 +37,38 @@ public class MqttService extends Service {
.serverHost("192.168.178.64")
.serverPort(1883)
.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();
@@ -93,42 +86,65 @@ public class MqttService extends Service {
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("state");
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("state", 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("state", "");
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();
}
}
}
+2
View File
@@ -3,4 +3,6 @@
<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>
</resources>