Skip to content

Commit 40f55d2

Browse files
committed
fixing power messages, remove semaphore, allow qos and retain configuration
1 parent d31acb0 commit 40f55d2

File tree

7 files changed

+44
-45
lines changed

7 files changed

+44
-45
lines changed

Diff for: README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ rootTopic=home # the mqtt root topic
1414
refresh=60 # number of seconds for MQTT status updates. Do not go below 60!
1515
mqttServer=tcp://192.168.0.1 # IP or hostname of your mqtt broker
1616
logLevel=INFO # logLevel
17+
retained=true # MQTT messages will be sent retained. Defaults to "false"
18+
qos=1 # MQTT messages will be sent with qos=1. Defaults to "0"
1719
1820
daikin1.host=192.168.0.2 # IP adress of your first Daikin Wifi adapter
1921
daikin1.name=ac-room1 # a name for the Daikin device, used in the MQTT topic
@@ -116,8 +118,7 @@ home/ac-room1/aircon/mode/set
116118

117119
Notes
118120
* `otemp` will go to `0` in case the device is not cooling or heating.
119-
* You actually don't need to use `power` to turn the AC on or off. Sending `mode=Cool` or `mode=None` does the job as well.
120-
* Possible values for `mode`, `fandirection` etc. can be found [here](https://bitbucket.org/m-do/jdaikin/src/default/src/main/java/net/jonathangiles/daikin/enums/)
121+
* Possible values for `mode`, `fandirection` etc. can be found [here](https://github.com/magcode/daikin-mqtt/tree/master/src/main/java/org/magcode/daikin/connector/enums)
121122

122123
# Openhab integration example
123124

@@ -129,7 +130,7 @@ Thing mqtt:topic:ac-room1 "Aircondition room 1" (mqtt:broker:mosquitto) {
129130
Type number : intemp "Inside temp" [ stateTopic="home/ac-room1/aircon/intemp"]
130131
Type number : outtemp "Outside temp" [ stateTopic="home/ac-room1/aircon/otemp"]
131132
Type number : targettemp "Target temp" [ stateTopic="home/ac-room1/aircon/targettemp"]
132-
Type switch : power "Power" [ stateTopic="home/ac-room1/aircon/power", commandTopic="home/ac-room1/aircon/power/set", on="1", off="0"]
133+
Type switch : power "Power" [ stateTopic="home/ac-room1/aircon/power", commandTopic="home/ac-room1/aircon/power/set", on="On", off="Off"]
133134
Type switch : online "Online" [ stateTopic="home/ac-room1/$state", on="ready", off="lost"]
134135
}
135136
```

Diff for: src/main/java/org/magcode/daikin/DaikinMqttClient.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
import org.magcode.daikin.mqtt.DevicePublisher;
3232
import org.magcode.daikin.mqtt.NodePublisher;
3333

34-
import java.util.concurrent.Semaphore;
35-
3634
/**
3735
* @author magcode MQTT Gateway for Daikin Wifi Adapter
3836
*
@@ -42,16 +40,16 @@ public class DaikinMqttClient {
4240
private static String mqttServer;
4341
private static int refresh = 60;
4442
private static String rootTopic;
43+
private static boolean retained;
44+
private static int qos;
4545
private static MqttClient mqttClient;
4646
public static final String nodeName = "aircon";
4747
private static final int deviceRefresh = 600;
48-
private static final int MAX_INFLIGHT = 100;
49-
private static Semaphore semaphore;
48+
private static final int MAX_INFLIGHT = 200;
5049
private static Logger logger = LogManager.getLogger(DaikinMqttClient.class);
5150
private static String logLevel = "INFO";
5251

5352
public static void main(String[] args) throws Exception {
54-
semaphore = new Semaphore(MAX_INFLIGHT);
5553
daikins = new HashMap<String, DaikinConfig>();
5654
readProps();
5755
reConfigureLogger();
@@ -66,12 +64,12 @@ public static void main(String[] args) throws Exception {
6664

6765
// start mqtt node publisher
6866
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
69-
Runnable nodePublisher = new NodePublisher(daikins, rootTopic, mqttClient, semaphore);
67+
Runnable nodePublisher = new NodePublisher(daikins, rootTopic, mqttClient, retained, qos);
7068
ScheduledFuture<?> nodePublisherFuture = executor.scheduleAtFixedRate(nodePublisher, 10, refresh,
7169
TimeUnit.SECONDS);
7270

7371
// start mqtt device publisher
74-
Runnable devicePublisher = new DevicePublisher(daikins, rootTopic, mqttClient, deviceRefresh, semaphore);
72+
Runnable devicePublisher = new DevicePublisher(daikins, rootTopic, mqttClient, deviceRefresh, retained, qos);
7573
ScheduledFuture<?> devicePublisherFuture = executor.scheduleAtFixedRate(devicePublisher, 15, deviceRefresh,
7674
TimeUnit.SECONDS);
7775

@@ -120,6 +118,8 @@ private static void readProps() {
120118
refresh = Integer.parseInt(props.getProperty("refresh", "60"));
121119
mqttServer = props.getProperty("mqttServer", "tcp://localhost");
122120
logLevel = props.getProperty("logLevel", "INFO");
121+
retained = Boolean.valueOf(props.getProperty("retained", "false"));
122+
qos = Integer.valueOf(props.getProperty("qos", "0"));
123123
Enumeration<?> e = props.propertyNames();
124124

125125
while (e.hasMoreElements()) {
@@ -160,12 +160,17 @@ private static void startMQTTClient() throws MqttException {
160160
mqttClient = new MqttClient(mqttServer, "client-for-daikin-on-" + hostName, new MemoryPersistence());
161161
MqttConnectOptions connOpt = new MqttConnectOptions();
162162
connOpt.setCleanSession(true);
163-
connOpt.setKeepAliveInterval(30);
164163
connOpt.setMaxInflight(MAX_INFLIGHT);
165164
connOpt.setAutomaticReconnect(true);
166-
mqttClient.setCallback(new Subscriber(daikins, rootTopic, semaphore));
165+
mqttClient.setCallback(new Subscriber(daikins, rootTopic));
167166
mqttClient.connect();
168167
logger.info("Connected to MQTT broker.");
168+
try {
169+
// give some time before subscribing
170+
Thread.sleep(200);
171+
} catch (InterruptedException e) {
172+
//
173+
}
169174
for (Entry<String, DaikinConfig> entry : daikins.entrySet()) {
170175
DaikinConfig value = entry.getValue();
171176
String subTopic = rootTopic + "/" + value.getName() + "/aircon/+/set";

Diff for: src/main/java/org/magcode/daikin/connector/enums/Power.java

+9
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,13 @@ public String getValue() {
2828
public static Power get(String value) {
2929
return lookup.get(value);
3030
}
31+
32+
public static Power getFromString(String power) {
33+
try {
34+
return Power.valueOf(power);
35+
} catch (Exception e) {
36+
// ignore
37+
}
38+
return Power.Off;
39+
}
3140
}

Diff for: src/main/java/org/magcode/daikin/mqtt/DevicePublisher.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.net.UnknownHostException;
55
import java.util.Map;
66
import java.util.Map.Entry;
7-
import java.util.concurrent.Semaphore;
87

98
import org.apache.commons.lang3.StringUtils;
109
import org.apache.logging.log4j.LogManager;
@@ -20,12 +19,13 @@ public class DevicePublisher extends Publisher implements Runnable {
2019
private static Logger logger = LogManager.getLogger(DevicePublisher.class);
2120

2221
public DevicePublisher(Map<String, DaikinConfig> daikinHosts, String topic, MqttClient mqttClient,
23-
int devicerefresh, Semaphore semaphore) {
22+
int devicerefresh,boolean retained, int qos) {
2423
this.daikinHosts = daikinHosts;
2524
this.topic = topic;
2625
this.mqttClient = mqttClient;
2726
this.deviceRefresh = devicerefresh;
28-
this.semaphore = semaphore;
27+
this.qos = qos;
28+
this.retained = retained;
2929
}
3030

3131
@Override

Diff for: src/main/java/org/magcode/daikin/mqtt/NodePublisher.java

+7-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.Date;
44
import java.util.Map;
55
import java.util.Map.Entry;
6-
import java.util.concurrent.Semaphore;
76

87
import org.apache.logging.log4j.LogManager;
98
import org.apache.logging.log4j.Logger;
@@ -23,12 +22,13 @@ public class NodePublisher extends Publisher implements Runnable {
2322
private Map<String, DaikinConfig> daikinHosts;
2423
private static Logger logger = LogManager.getLogger(NodePublisher.class);
2524

26-
public NodePublisher(Map<String, DaikinConfig> daikinHosts, String topic, MqttClient mqttClient,
27-
Semaphore semaphore) {
25+
public NodePublisher(Map<String, DaikinConfig> daikinHosts, String topic, MqttClient mqttClient, boolean retained,
26+
int qos) {
2827
this.daikinHosts = daikinHosts;
2928
this.topic = topic;
3029
this.mqttClient = mqttClient;
31-
this.semaphore = semaphore;
30+
this.qos = qos;
31+
this.retained = retained;
3232
}
3333

3434
@Override
@@ -52,15 +52,8 @@ public void run() {
5252
long uptime = (now.getTime() - daikinConfig.getOnlineSince().getTime()) / 1000;
5353
Publish(deviceTopic + "/$stats/uptime", "" + uptime);
5454

55-
Publish(nodeTopic + TopicConstants.PR_POWER, status.getPower().getValue());
56-
57-
// we force "none" if the device is OFF
58-
if (status.getPower() == Power.On) {
59-
Publish(nodeTopic + TopicConstants.PR_MODE, status.getMode().toString());
60-
} else {
61-
Publish(nodeTopic + TopicConstants.PR_MODE, Mode.None.toString());
62-
}
63-
55+
Publish(nodeTopic + TopicConstants.PR_POWER, status.getPower().toString());
56+
Publish(nodeTopic + TopicConstants.PR_MODE, status.getMode().toString());
6457
Publish(nodeTopic + TopicConstants.PR_TARGETTEMP, status.getTargetTemp());
6558
Publish(nodeTopic + TopicConstants.PR_INTEMP, status.getInsideTemp());
6659
Publish(nodeTopic + TopicConstants.PR_OUTTEMP, status.getOutsideTemp());
@@ -74,7 +67,7 @@ public void run() {
7467
Publish(deviceTopic + "/$stats/uptime", 0);
7568
Publish(deviceTopic + "/$state", "lost");
7669
Publish(nodeTopic + TopicConstants.PR_MODE, Mode.None.toString());
77-
Publish(nodeTopic + TopicConstants.PR_POWER, false);
70+
Publish(nodeTopic + TopicConstants.PR_POWER, Power.Off.toString());
7871
Publish(nodeTopic + TopicConstants.PR_OUTTEMP, 0.0f);
7972
Publish(nodeTopic + TopicConstants.PR_INTEMP, 0.0f);
8073
Publish(nodeTopic + TopicConstants.PR_TARGETTEMP, 0.0f);

Diff for: src/main/java/org/magcode/daikin/mqtt/Publisher.java

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.magcode.daikin.mqtt;
22

3-
import java.util.concurrent.Semaphore;
4-
53
import org.apache.logging.log4j.LogManager;
64
import org.apache.logging.log4j.Logger;
75
import org.eclipse.paho.client.mqttv3.MqttClient;
@@ -11,7 +9,8 @@
119

1210
public abstract class Publisher {
1311
protected MqttClient mqttClient;
14-
protected Semaphore semaphore;
12+
protected int qos = 0;
13+
protected boolean retained = false;
1514
private static Logger logger = LogManager.getLogger(Publisher.class);
1615

1716
protected void Publish(String topic, String payload) {
@@ -33,14 +32,9 @@ protected void Publish(String topic, int payload) {
3332

3433
private void Publish(String topic, MqttMessage message) {
3534
try {
36-
message.setRetained(true);
37-
if (semaphore.availablePermits() < 10) {
38-
logger.warn("Semaphore available permits: {}", semaphore.availablePermits());
39-
}
40-
semaphore.acquire();
35+
message.setRetained(this.retained);
36+
message.setQos(this.qos);
4137
this.mqttClient.publish(topic, message);
42-
} catch (InterruptedException e) {
43-
logger.error("InterruptedException", e);
4438
} catch (MqttPersistenceException e) {
4539
logger.error("MqttPersistenceException", e);
4640
} catch (MqttException e) {

Diff for: src/main/java/org/magcode/daikin/mqtt/Subscriber.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.magcode.daikin.mqtt;
22

33
import java.util.Map;
4-
import java.util.concurrent.Semaphore;
54

65
import org.apache.commons.lang3.StringUtils;
76
import org.apache.logging.log4j.LogManager;
@@ -22,12 +21,10 @@ public class Subscriber implements MqttCallback {
2221
private Map<String, DaikinConfig> daikins;
2322
private String rootTopic;
2423
private static Logger logger = LogManager.getLogger(Subscriber.class);
25-
private Semaphore semaphore;
2624

27-
public Subscriber(Map<String, DaikinConfig> daikins, String topic, Semaphore semaphore) {
25+
public Subscriber(Map<String, DaikinConfig> daikins, String topic) {
2826
this.daikins = daikins;
2927
this.rootTopic = topic;
30-
this.semaphore = semaphore;
3128
}
3229

3330
@Override
@@ -89,7 +86,7 @@ public void messageArrived(String topic, MqttMessage message) throws Exception {
8986
break;
9087
case TopicConstants.PR_POWER:
9188
String powerString = message.toString();
92-
Power power = Power.valueOf(powerString);
89+
Power power = Power.getFromString(powerString);
9390
logger.info("Sending power={} to {}", power, targetDaikin.getName());
9491
targetDaikin.getDaikin().updateStatus();
9592
targetDaikin.getDaikin().setPower(power);
@@ -117,6 +114,6 @@ public void messageArrived(String topic, MqttMessage message) throws Exception {
117114

118115
@Override
119116
public void deliveryComplete(IMqttDeliveryToken token) {
120-
semaphore.release();
117+
121118
}
122119
}

0 commit comments

Comments
 (0)