Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
de0ff18112
|
|||
|
ff27f52269
|
|||
|
d60d497935
|
+1
-1
@@ -6,7 +6,7 @@ android {
|
||||
compileSdk 31
|
||||
|
||||
defaultConfig {
|
||||
applicationId "com.example.mysecondapp"
|
||||
applicationId "ch.luria.mq1"
|
||||
minSdk 29
|
||||
targetSdk 31
|
||||
versionCode 1
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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 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 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();
|
||||
}
|
||||
});
|
||||
} 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) {
|
||||
client.publishWith()
|
||||
.topic(topic)
|
||||
.payload(message.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 " + message + " on " + topic);
|
||||
getStatus();
|
||||
}
|
||||
});
|
||||
if (client != null) {
|
||||
if (client.getState() == MqttClientState.CONNECTED) {
|
||||
client.publishWith()
|
||||
.topic(topic)
|
||||
.payload(message.getBytes())
|
||||
.send()
|
||||
.whenComplete((mqttPublish, throwable) -> {
|
||||
if (throwable != null) {
|
||||
// handle failure to 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,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>
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user