Compare commits
3 Commits
de0ff18112
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 830d594806 | |||
| a027f8068f | |||
|
fd65f7fd3e
|
@@ -12,6 +12,8 @@ import android.util.Log;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ToggleButton;
|
import android.widget.ToggleButton;
|
||||||
|
|
||||||
|
import com.google.android.material.snackbar.Snackbar;
|
||||||
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
@@ -22,6 +24,9 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
String msg = intent.getStringExtra("STATE");
|
String msg = intent.getStringExtra("STATE");
|
||||||
Log.i(TAG, "Received " + msg);
|
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"));
|
stateButton.setChecked(!msg.equals("OFF"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ public class MqttService extends Service {
|
|||||||
private Mqtt5AsyncClient client;
|
private Mqtt5AsyncClient client;
|
||||||
private static final String TAG = "MQTT Service";
|
private static final String TAG = "MQTT Service";
|
||||||
private final IBinder binder = new LocalBinder();
|
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 class LocalBinder extends Binder {
|
||||||
public MqttService getService() {
|
public MqttService getService() {
|
||||||
@@ -31,11 +33,10 @@ public class MqttService extends Service {
|
|||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
Log.i(TAG, "onCreate");
|
Log.i(TAG, "onCreate");
|
||||||
if (client == null) {
|
if (client == null) {
|
||||||
Log.i(TAG, "connecting");
|
|
||||||
client = Mqtt5Client.builder()
|
client = Mqtt5Client.builder()
|
||||||
.identifier(UUID.randomUUID().toString())
|
.identifier(UUID.randomUUID().toString())
|
||||||
.serverHost("192.168.178.64")
|
.serverHost(mqttHost)
|
||||||
.serverPort(1883)
|
.serverPort(mqttPort)
|
||||||
.buildAsync();
|
.buildAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -83,7 +84,6 @@ public class MqttService extends Service {
|
|||||||
} else {
|
} else {
|
||||||
// Handle successful subscription, e.g. logging or incrementing a metric
|
// Handle successful subscription, e.g. logging or incrementing a metric
|
||||||
Log.i(TAG, "subscribe ok");
|
Log.i(TAG, "subscribe ok");
|
||||||
getStatus();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -93,7 +93,7 @@ public class MqttService extends Service {
|
|||||||
intent.setAction("ch.luria.mq1");
|
intent.setAction("ch.luria.mq1");
|
||||||
try {
|
try {
|
||||||
final JSONObject obj = new JSONObject(msg);
|
final JSONObject obj = new JSONObject(msg);
|
||||||
String state = obj.getString("state");
|
String state = obj.getString(getString(R.string.mqtt_state_field));
|
||||||
intent.putExtra("STATE", state);
|
intent.putExtra("STATE", state);
|
||||||
sendBroadcast(intent);
|
sendBroadcast(intent);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
@@ -115,7 +115,6 @@ public class MqttService extends Service {
|
|||||||
} else {
|
} else {
|
||||||
// handle successful publish, e.g. logging or incrementing a metric
|
// handle successful publish, e.g. logging or incrementing a metric
|
||||||
Log.i(TAG, "published " + message + " on " + topic);
|
Log.i(TAG, "published " + message + " on " + topic);
|
||||||
getStatus();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -129,7 +128,7 @@ public class MqttService extends Service {
|
|||||||
public void doToggle(boolean state) {
|
public void doToggle(boolean state) {
|
||||||
JSONObject obj = new JSONObject();
|
JSONObject obj = new JSONObject();
|
||||||
try {
|
try {
|
||||||
obj.put("state", state ? "ON" : "OFF");
|
obj.put(getString(R.string.mqtt_state_field), state ? "ON" : "OFF");
|
||||||
Log.i(TAG, "doToggle: " + obj);
|
Log.i(TAG, "doToggle: " + obj);
|
||||||
publishMessage(getString(R.string.mqtt_base_topic) + "/" + getString(R.string.mqtt_room_address) + "/set", obj.toString());
|
publishMessage(getString(R.string.mqtt_base_topic) + "/" + getString(R.string.mqtt_room_address) + "/set", obj.toString());
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
@@ -140,7 +139,7 @@ public class MqttService extends Service {
|
|||||||
public void getStatus() {
|
public void getStatus() {
|
||||||
JSONObject obj = new JSONObject();
|
JSONObject obj = new JSONObject();
|
||||||
try {
|
try {
|
||||||
obj.put("state", "");
|
obj.put(getString(R.string.mqtt_state_field), "");
|
||||||
Log.i(TAG, "getStatus()");
|
Log.i(TAG, "getStatus()");
|
||||||
publishMessage(getString(R.string.mqtt_base_topic) + "/" + getString(R.string.mqtt_room_address) + "/get", obj.toString());
|
publishMessage(getString(R.string.mqtt_base_topic) + "/" + getString(R.string.mqtt_room_address) + "/get", obj.toString());
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
|
|||||||
@@ -5,4 +5,5 @@
|
|||||||
<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_base_topic">zigbee2mqtt</string>
|
||||||
<string name="mqtt_room_address">Ampoule Chambre Isaac</string>
|
<string name="mqtt_room_address">Ampoule Chambre Isaac</string>
|
||||||
|
<string name="mqtt_state_field">state</string>
|
||||||
</resources>
|
</resources>
|
||||||
Reference in New Issue
Block a user