Compare commits
3 Commits
50f897c55c
...
de0ff18112
| Author | SHA1 | Date | |
|---|---|---|---|
|
de0ff18112
|
|||
|
ff27f52269
|
|||
|
d60d497935
|
+1
-1
@@ -6,7 +6,7 @@ android {
|
|||||||
compileSdk 31
|
compileSdk 31
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId "com.example.mysecondapp"
|
applicationId "ch.luria.mq1"
|
||||||
minSdk 29
|
minSdk 29
|
||||||
targetSdk 31
|
targetSdk 31
|
||||||
versionCode 1
|
versionCode 1
|
||||||
|
|||||||
@@ -61,11 +61,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void toggleButtonState(View view) {
|
public void toggleButtonState(View view) {
|
||||||
if (stateButton.isChecked()) {
|
mService.doToggle(stateButton.isChecked());
|
||||||
mService.doToggle("{\"state\":\"ON\"}");
|
|
||||||
} else {
|
|
||||||
mService.doToggle("{\"state\":\"OFF\"}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final ServiceConnection connection = new ServiceConnection() {
|
private final ServiceConnection connection = new ServiceConnection() {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import android.os.Binder;
|
|||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.hivemq.client.mqtt.MqttClientState;
|
||||||
import com.hivemq.client.mqtt.mqtt5.Mqtt5AsyncClient;
|
import com.hivemq.client.mqtt.mqtt5.Mqtt5AsyncClient;
|
||||||
import com.hivemq.client.mqtt.mqtt5.Mqtt5Client;
|
import com.hivemq.client.mqtt.mqtt5.Mqtt5Client;
|
||||||
|
|
||||||
@@ -36,46 +37,38 @@ public class MqttService extends Service {
|
|||||||
.serverHost("192.168.178.64")
|
.serverHost("192.168.178.64")
|
||||||
.serverPort(1883)
|
.serverPort(1883)
|
||||||
.buildAsync();
|
.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
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
Log.i(TAG, "onBind");
|
Log.i(TAG, "onBind");
|
||||||
|
connect();
|
||||||
return binder;
|
return binder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void doToggle(String what) {
|
private void connect() {
|
||||||
publishMessage("zigbee2mqtt/Ampoule Chambre Isaac/set", what);
|
if (client.getState() == MqttClientState.DISCONNECTED) {
|
||||||
}
|
Log.i(TAG, "connect()");
|
||||||
|
client.connectWith()
|
||||||
private void sendMessage(String msg) {
|
.send()
|
||||||
Intent intent = new Intent();
|
.whenComplete((connAck, throwable) -> {
|
||||||
intent.setAction("ch.luria.mq1");
|
if (throwable != null) {
|
||||||
try {
|
// handle failure
|
||||||
final JSONObject obj = new JSONObject(msg);
|
Log.i(TAG, "could not connect - " + connAck.getReasonString());
|
||||||
String state = obj.getString("state");
|
} else {
|
||||||
intent.putExtra("STATE", state);
|
Log.i(TAG, "connect ok");
|
||||||
sendBroadcast(intent);
|
subscribe();
|
||||||
} catch (JSONException e) {
|
}
|
||||||
e.printStackTrace();
|
});
|
||||||
|
} else {
|
||||||
|
Log.i(TAG, "connect() in state " + client.getState());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void subscribe() {
|
private void subscribe() {
|
||||||
client.subscribeWith()
|
client.subscribeWith()
|
||||||
.topicFilter("zigbee2mqtt/Ampoule Chambre Isaac")
|
.topicFilter(getString(R.string.mqtt_base_topic) + "/" + getString(R.string.mqtt_room_address))
|
||||||
.callback(publish -> {
|
.callback(publish -> {
|
||||||
// Process the received message
|
// Process the received message
|
||||||
String msg = new String(publish.getPayloadAsBytes()) + " from " + publish.getTopic();
|
String msg = new String(publish.getPayloadAsBytes()) + " from " + publish.getTopic();
|
||||||
@@ -93,42 +86,65 @@ public class MqttService extends Service {
|
|||||||
getStatus();
|
getStatus();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getStatus() {
|
private void sendMessage(String msg) {
|
||||||
if (client != null) {
|
Intent intent = new Intent();
|
||||||
client.publishWith()
|
intent.setAction("ch.luria.mq1");
|
||||||
.topic("zigbee2mqtt/Ampoule Chambre Isaac/get")
|
try {
|
||||||
.payload("{\"state\":\"\"}".getBytes())
|
final JSONObject obj = new JSONObject(msg);
|
||||||
.send()
|
String state = obj.getString("state");
|
||||||
.whenComplete((mqtt3Publish, throwable) -> {
|
intent.putExtra("STATE", state);
|
||||||
if (throwable != null) {
|
sendBroadcast(intent);
|
||||||
// handle failure to publish
|
} catch (JSONException e) {
|
||||||
Log.i(TAG, "could not publish");
|
e.printStackTrace();
|
||||||
} else {
|
|
||||||
// handle successful publish, e.g. logging or incrementing a metric
|
|
||||||
Log.i(TAG, "published");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void publishMessage(String topic, String message) {
|
private void publishMessage(String topic, String message) {
|
||||||
client.publishWith()
|
if (client != null) {
|
||||||
.topic(topic)
|
if (client.getState() == MqttClientState.CONNECTED) {
|
||||||
.payload(message.getBytes())
|
client.publishWith()
|
||||||
.send()
|
.topic(topic)
|
||||||
.whenComplete((mqtt3Publish, throwable) -> {
|
.payload(message.getBytes())
|
||||||
if (throwable != null) {
|
.send()
|
||||||
// handle failure to publish
|
.whenComplete((mqttPublish, throwable) -> {
|
||||||
Log.i(TAG, "could not publish");
|
if (throwable != null) {
|
||||||
} else {
|
// handle failure to publish
|
||||||
// handle successful publish, e.g. logging or incrementing a metric
|
Log.i(TAG, "publishMessage(): could not publish: " + throwable);
|
||||||
Log.i(TAG, "published " + message + " on " + topic);
|
} else {
|
||||||
getStatus();
|
// 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">
|
<resources xmlns:tools="http://schemas.android.com/tools">
|
||||||
<!-- Base application theme. -->
|
<!-- Base application theme. -->
|
||||||
<style name="Theme.Mq1" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
|
<style name="Theme.Mq1" parent="Theme.MaterialComponents.DayNight.NoActionBar">
|
||||||
<!-- Primary brand color. -->
|
<!-- Primary brand color. -->
|
||||||
<item name="colorPrimary">@color/purple_200</item>
|
<item name="colorPrimary">@color/purple_200</item>
|
||||||
<item name="colorPrimaryVariant">@color/purple_700</item>
|
<item name="colorPrimaryVariant">@color/purple_700</item>
|
||||||
|
|||||||
@@ -3,4 +3,6 @@
|
|||||||
<string name="button_on">ALLUMEE</string>
|
<string name="button_on">ALLUMEE</string>
|
||||||
<string name="button_off">ETEINTE</string>
|
<string name="button_off">ETEINTE</string>
|
||||||
<string name="room_name">Chambre d\'Isaac</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>
|
</resources>
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<resources xmlns:tools="http://schemas.android.com/tools">
|
<resources xmlns:tools="http://schemas.android.com/tools">
|
||||||
<!-- Base application theme. -->
|
<!-- Base application theme. -->
|
||||||
<style name="Theme.Mq1" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
|
<style name="Theme.Mq1" parent="Theme.MaterialComponents.DayNight.NoActionBar">
|
||||||
<!-- Primary brand color. -->
|
<!-- Primary brand color. -->
|
||||||
<item name="colorPrimary">@color/purple_500</item>
|
<item name="colorPrimary">@color/purple_500</item>
|
||||||
<item name="colorPrimaryVariant">@color/purple_700</item>
|
<item name="colorPrimaryVariant">@color/purple_700</item>
|
||||||
|
|||||||
Reference in New Issue
Block a user