Compare commits

...

3 Commits

Author SHA1 Message Date
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 78 additions and 64 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
@@ -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() {
+72 -56
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 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 -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>
+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>
+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>