├── bin ├── SmartESS-proxy.jar └── conf.ini ├── README.md ├── FakeClient.java ├── ModbusClient.java ├── Engine.java ├── ModbusServer.java ├── tester └── testDataExtract.java ├── ProcessInverterData.java └── MQTTClient.java /bin/SmartESS-proxy.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Paxy/SmartESS-proxy/HEAD/bin/SmartESS-proxy.jar -------------------------------------------------------------------------------- /bin/conf.ini: -------------------------------------------------------------------------------- 1 | fakeClient=true 2 | mqttServer=172.16.2.1 3 | mqttPort=1883 4 | enableMqttAuth=false 5 | mqttUser= 6 | mqttPass= 7 | mqttTopic=paxyhome/Inverter/ 8 | updateFrequency=10 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SmartESS-proxy 2 | SmartESS (PowMr) to MQTT proxy 3 | 4 | Aim of the project is to adopt PowMr WiFi Plug Pro to send data over MQTT to HASS beside SmartESS cloud. 5 | It is achived by running SmartESS-proxy and poisoning DNS ess.eybond.com to point to the proxy. 6 | 7 | For use without compiling, place content of bin/ to the same folder. 8 | 9 | Change parameters in conf.ini 10 | ``` 11 | fakeClient=true 12 | mqttServer=172.16.2.1 13 | mqttPort=1883 14 | enableMqttAuth=false 15 | mqttUser= 16 | mqttPass= 17 | mqttTopic=paxyhome/Inverter/ 18 | updateFrequency=10 19 | ``` 20 | 21 | Run project with: 22 | ``` 23 | java -jar SmartESS-proxy.jar 24 | ``` 25 | 26 | FakeClient is initiated by default to prevent data to be sent to SmartESS cloud. If you still want to use SmartESS cloud, set fakeClient to false. 27 | 28 | Project requires org.eclipse.paho.client.mqtt library. 29 | -------------------------------------------------------------------------------- /FakeClient.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedInputStream; 2 | import java.io.BufferedWriter; 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.OutputStream; 6 | import java.net.ServerSocket; 7 | import java.net.Socket; 8 | import java.sql.Timestamp; 9 | 10 | public class FakeClient extends ModbusClient implements Runnable { 11 | 12 | public FakeClient(Engine engine) throws Exception { 13 | super(engine); 14 | this.engine = engine; 15 | } 16 | 17 | private Engine engine; 18 | private Socket srv; 19 | 20 | private static String cfg = "3D0A0001000EFF020102030405080C0E191A2041"; 21 | private static String ping = "3D0B0001000AFF011609190F00350023"; 22 | private static String getData = "3D0C00010003001100"; 23 | 24 | public void run() { 25 | while (true) { 26 | try { 27 | sendMsgToClient(cfg); 28 | int cnt = 0; 29 | while (true) { 30 | int res = sendMsgToClient(getData); 31 | if (res == -1) 32 | break; 33 | Thread.currentThread() 34 | .sleep(engine.fakeClientUpdateFrequency * 1000); 35 | 36 | } 37 | 38 | } catch (Exception e) { 39 | // TODO Auto-generated catch block 40 | e.printStackTrace(); 41 | } 42 | } 43 | } 44 | public int sendData(byte[] data) throws InterruptedException { 45 | return 0; 46 | } 47 | private int sendMsgToClient(String msg) throws InterruptedException { 48 | while (engine.nsrv == null || engine.nsrv.node == null) 49 | Thread.currentThread().sleep(100); 50 | byte[] data = engine.hexStringToByteArray(msg); 51 | int res = engine.nsrv.sendData(data); 52 | String time = new Timestamp(System.currentTimeMillis()).toString(); 53 | System.out.println(time + " - Server: " + msg); 54 | return res; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /ModbusClient.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedInputStream; 2 | import java.io.BufferedWriter; 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.OutputStream; 6 | import java.net.ServerSocket; 7 | import java.net.Socket; 8 | import java.sql.Timestamp; 9 | 10 | public class ModbusClient implements Runnable { 11 | 12 | private Engine engine; 13 | private Socket srv; 14 | 15 | public ModbusClient(Engine engine) throws IOException { 16 | this.engine = engine; 17 | } 18 | 19 | public void run() { 20 | while (true) { 21 | try { 22 | srv=new Socket(engine.realModbusServer,502); 23 | String time = new Timestamp(System.currentTimeMillis()).toString(); 24 | System.out.println(time + " - Connected to server " 25 | + srv.getInetAddress().getHostAddress()); 26 | InputStream in=srv.getInputStream(); 27 | while (true) { 28 | while (in.available() == 0) { 29 | Thread.currentThread().sleep(100); 30 | } 31 | byte[] data=in.readNBytes(in.available()); 32 | String hex=Engine.bytesToHex(data); 33 | time = new Timestamp(System.currentTimeMillis()).toString(); 34 | System.out.println(time+" - Server: "+hex); 35 | int ret=engine.nsrv.sendData(data); 36 | if(ret==-1) break; 37 | } 38 | 39 | 40 | } catch (Exception e) { 41 | // TODO Auto-generated catch block 42 | e.printStackTrace(); 43 | } 44 | } 45 | } 46 | 47 | public int sendData(byte[] data) throws InterruptedException { 48 | if(this.srv==null) { 49 | String time = new Timestamp(System.currentTimeMillis()).toString(); 50 | System.out.println(time + " - Waiting for server ..."); 51 | while(srv==null) Thread.currentThread().sleep(100); 52 | } 53 | try { 54 | if(this.srv.getOutputStream()==null) return -1; 55 | 56 | OutputStream out=this.srv.getOutputStream(); 57 | out.write(data); 58 | out.flush(); 59 | 60 | } catch (IOException e) { 61 | e.printStackTrace(); 62 | return -1; 63 | } 64 | return 0; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /Engine.java: -------------------------------------------------------------------------------- 1 | import java.io.FileInputStream; 2 | import java.nio.charset.StandardCharsets; 3 | import java.util.Properties; 4 | import java.util.concurrent.Executor; 5 | import java.util.concurrent.Executors; 6 | 7 | public class Engine { 8 | 9 | static boolean fakeClient = true; 10 | static String mqttServer = "172.16.2.1"; 11 | static boolean enableMqttAuth=false; 12 | static String mqttUser=""; 13 | static String mqttPass=""; 14 | static int mqttPort = 1883; 15 | static String mqttTopic = "paxyhome/Inverter/"; 16 | static int fakeClientUpdateFrequency = 10; // 10 seconds 17 | static String realModbusServer="47.242.188.205"; 18 | 19 | 20 | static Executor pool = Executors.newFixedThreadPool(4); 21 | private static final byte[] HEX_ARRAY = "0123456789ABCDEF" 22 | .getBytes(StandardCharsets.US_ASCII); 23 | ModbusServer nsrv; 24 | ModbusClient ncli; 25 | MQTTClient mqtt; 26 | 27 | byte[] lastData; 28 | 29 | public Engine() throws Exception { 30 | 31 | Properties p = new Properties(); 32 | p.load(new FileInputStream("conf.ini")); 33 | fakeClient = Boolean.parseBoolean(p.getProperty("fakeClient")); 34 | mqttServer = p.getProperty("mqttServer"); 35 | mqttPort = Integer.parseInt(p.getProperty("mqttPort")); 36 | enableMqttAuth = Boolean.parseBoolean(p.getProperty("enableMqttAuth")); 37 | mqttUser = p.getProperty("mqttUser"); 38 | mqttPass = p.getProperty("mqttPass"); 39 | mqttTopic = p.getProperty("mqttTopic"); 40 | fakeClientUpdateFrequency = Integer.parseInt(p.getProperty("updateFrequency")); 41 | 42 | 43 | nsrv = new ModbusServer(this); 44 | pool.execute(nsrv); 45 | if (!fakeClient) 46 | ncli = new ModbusClient(this); 47 | else 48 | ncli = new FakeClient(this); 49 | pool.execute(ncli); 50 | mqtt = new MQTTClient(this); 51 | pool.execute(mqtt); 52 | ProcessInverterData procesor = new ProcessInverterData(this); 53 | pool.execute(procesor); 54 | 55 | Thread.currentThread().sleep(5000); 56 | // test(); 57 | } 58 | 59 | public static void main(String[] args) throws Exception { 60 | new Engine(); 61 | 62 | } 63 | 64 | public static String bytesToHex(byte[] bytes) { 65 | char[] hexChars = new char[bytes.length * 2]; 66 | for (int j = 0; j < bytes.length; j++) { 67 | int v = bytes[j] & 0xFF; 68 | hexChars[j * 2] = (char) HEX_ARRAY[v >>> 4]; 69 | hexChars[j * 2 + 1] = (char) HEX_ARRAY[v & 0x0F]; 70 | } 71 | return new String(hexChars); 72 | } 73 | 74 | public static byte[] hexStringToByteArray(String s) { 75 | int len = s.length(); 76 | byte[] data = new byte[len / 2]; 77 | for (int i = 0; i < len; i += 2) { 78 | data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) 79 | + Character.digit(s.charAt(i + 1), 16)); 80 | } 81 | return data; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /ModbusServer.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedInputStream; 2 | import java.io.IOException; 3 | import java.io.InputStream; 4 | import java.io.OutputStream; 5 | import java.net.ServerSocket; 6 | import java.net.Socket; 7 | import java.sql.Timestamp; 8 | 9 | public class ModbusServer implements Runnable { 10 | 11 | private Engine engine; 12 | public Socket node; 13 | private boolean close = false; 14 | 15 | public ModbusServer(Engine engine) throws IOException { 16 | this.engine = engine; 17 | } 18 | 19 | public void run() { 20 | ServerSocket ss; 21 | try { 22 | ss = new ServerSocket(502); 23 | while (true) { 24 | node = ss.accept(); 25 | String time = new Timestamp(System.currentTimeMillis()) 26 | .toString(); 27 | System.out.println(time + " - Connected node " 28 | + node.getInetAddress().getHostAddress()); 29 | InputStream in = node.getInputStream(); 30 | this.close=false; 31 | while (true) { 32 | while (in.available() == 0) { 33 | Thread.currentThread().sleep(100); 34 | if (close) 35 | break; 36 | } 37 | Thread.currentThread().sleep(100); 38 | if (close) 39 | break; 40 | byte[] data = in.readNBytes(in.available()); 41 | String hex = Engine.bytesToHex(data); 42 | time = new Timestamp(System.currentTimeMillis()).toString(); 43 | System.out.println(time + " - Node: " + hex); 44 | int res=engine.ncli.sendData(data); 45 | // engine.mqtt.sendMsg("data", hex); 46 | engine.lastData = data; 47 | if(res==-1) break; 48 | } 49 | 50 | } 51 | 52 | } catch (Exception e) { 53 | // TODO Auto-generated catch block 54 | e.printStackTrace(); 55 | } 56 | } 57 | 58 | public int sendData(byte[] data) throws InterruptedException { 59 | if (this.node == null) { 60 | String time = new Timestamp(System.currentTimeMillis()).toString(); 61 | System.out.println(time + " - Waiting for node ..."); 62 | while (node == null) 63 | Thread.currentThread().sleep(100); 64 | } 65 | try { 66 | if (this.node.getOutputStream() == null) { 67 | this.node=null; 68 | this.close=true; 69 | return -1; 70 | } 71 | if (!node.isConnected()) { 72 | this.node=null; 73 | this.close=true; 74 | return -1; 75 | } 76 | 77 | OutputStream out = this.node.getOutputStream(); 78 | out.write(data); 79 | out.flush(); 80 | 81 | } catch (IOException e) { 82 | e.printStackTrace(); 83 | try { 84 | this.node.close(); 85 | } catch (IOException e1) { 86 | // TODO Auto-generated catch block 87 | e1.printStackTrace(); 88 | } 89 | this.node=null; 90 | this.close=true; 91 | return -1; 92 | } 93 | return 0; 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /tester/testDataExtract.java: -------------------------------------------------------------------------------- 1 | package tester; 2 | import java.nio.ByteBuffer; 3 | 4 | public class testDataExtract { 5 | private short modeIdx = 14; 6 | private short acVoltageIdx = 16; 7 | private short acFrequencyIdx = 18; 8 | private short pvVoltageIdx = 20; 9 | private short pvPowerIdx = 22; 10 | private short batteryVoltageIdx = 24; 11 | private short batteryChargedIdx = 26; 12 | private short batteryChargingCurrIdx = 28; 13 | private short batteryDisChargingCurrIdx = 30; 14 | private short outputVoltageIdx = 32; 15 | private short outputFrequencyIdx = 34; 16 | private short outputPowerIdx = 38; 17 | private short outputLoadIdx = 40; 18 | private short chargeStateIdx=84; 19 | private short loadStateIdx=86; 20 | 21 | 22 | public testDataExtract() { 23 | String hex = "2B270925008205110000119511D10400CE08F301B90001007C00420000000000CE08F301100000000100010072B20000C1A200000100DC05DC05E60006007800E600F401060000000000F9231601D70F72006501020001000000020000003C00E6001E00740087007E007D0064008D003C0078001E0062ECE90E010000004A000000000000000000"; 24 | byte[] data = hexStringToByteArray(hex); 25 | if (data[2] == 0x09 && data[3] == 0x25) { 26 | double batteryVoltage = getData(data, batteryVoltageIdx, 10); 27 | System.out.println("batteryVoltage: " + batteryVoltage); 28 | int batteryCharged = getData(data, batteryChargedIdx, 1, true); 29 | System.out.println("batteryCharged: " + batteryCharged); 30 | double batteryChargingCurr = getData(data, 31 | batteryChargingCurrIdx, 10); 32 | System.out.println("batteryChargingCurr: " + batteryChargingCurr); 33 | double batteryDisChargingCurr = getData(data, 34 | batteryDisChargingCurrIdx, 10); 35 | System.out.println( 36 | "batteryDisChargingCurr: " + batteryDisChargingCurr); 37 | double outputVoltage = getData(data, outputVoltageIdx, 10); 38 | System.out.println("outputVoltage: " + outputVoltage); 39 | double outputFrequency = getData(data, outputFrequencyIdx, 10); 40 | System.out.println("outputFrequency: " + outputFrequency); 41 | int outputPower = getData(data, outputPowerIdx, 1, true); 42 | System.out.println("outputPower: " + outputPower); 43 | int outputLoad = getData(data, outputLoadIdx, 1, true); 44 | System.out.println("outputLoad: " + outputLoad); 45 | double acVoltage = getData(data, acVoltageIdx, 10); 46 | System.out.println("acVoltage: " + acVoltage); 47 | double acFrequency = getData(data, acFrequencyIdx, 10); 48 | System.out.println("acFrequency: " + acFrequency); 49 | double pvVoltage = getData(data, pvVoltageIdx, 10); 50 | System.out.println("pvVoltage: " + pvVoltage); 51 | int pvPower = getData(data, pvPowerIdx, 1, true); 52 | System.out.println("pvPower: " + pvPower); 53 | int mode = getData(data, modeIdx, 1, true); 54 | System.out.println("mode: " + mode); 55 | int chargeState = getData(data, chargeStateIdx, 1, true); 56 | System.out.println("chargeState: " + chargeState); 57 | int loadState = getData(data, loadStateIdx, 1, true); 58 | System.out.println("loadState"+loadState); 59 | } 60 | } 61 | 62 | private double getData(byte[] data, short idx, 63 | int denominator) { 64 | int b = 0; 65 | for (int i = 1; i >= 0; i--) { 66 | b = (b << 8) + (data[idx + i] & 0xFF); 67 | } 68 | return b * 1.0 / denominator; 69 | } 70 | 71 | private int getData(byte[] data, short idx, int denominator, 72 | boolean toInt) { 73 | int b = 0; 74 | for (int i = 1; i >= 0; i--) { 75 | b = (b << 8) + (data[idx + i] & 0xFF); 76 | } 77 | return b / denominator; 78 | } 79 | 80 | public static void main(String[] args) { 81 | new testDataExtract(); 82 | } 83 | 84 | public static byte[] hexStringToByteArray(String s) { 85 | int len = s.length(); 86 | byte[] data = new byte[len / 2]; 87 | for (int i = 0; i < len; i += 2) { 88 | data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) 89 | + Character.digit(s.charAt(i + 1), 16)); 90 | } 91 | return data; 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /ProcessInverterData.java: -------------------------------------------------------------------------------- 1 | import java.nio.ByteBuffer; 2 | 3 | public class ProcessInverterData implements Runnable { 4 | 5 | private Engine engine; 6 | private short modeIdx = 14; 7 | private short acVoltageIdx = 16; 8 | private short acFrequencyIdx = 18; 9 | private short pvVoltageIdx = 20; 10 | private short pvPowerIdx = 22; 11 | private short batteryVoltageIdx = 24; 12 | private short batteryChargedIdx = 26; 13 | private short batteryChargingCurrIdx = 28; 14 | private short batteryDisChargingCurrIdx = 30; 15 | private short outputVoltageIdx = 32; 16 | private short outputFrequencyIdx = 34; 17 | private short outputPowerIdx = 38; 18 | private short outputLoadIdx = 40; 19 | private short chargeStateIdx=84; 20 | private short loadStateIdx=86; 21 | 22 | 23 | public ProcessInverterData(Engine engine) { 24 | this.engine = engine; 25 | } 26 | 27 | public void run() { 28 | while (true) 29 | try { 30 | while (engine.lastData == null || engine.lastData.length == 0) 31 | Thread.currentThread().sleep(100); 32 | byte[] data = engine.lastData; 33 | String hex = Engine.bytesToHex(data); 34 | if (data[2] == 0x09 && data[3] == 0x25) { 35 | double batteryVoltage = getData(data, batteryVoltageIdx, 36 | 10); 37 | engine.mqtt.sendMsg("batteryVoltage", batteryVoltage); 38 | int batteryCharged = getData(data, batteryChargedIdx, 1, 39 | true); 40 | engine.mqtt.sendMsg("batteryCharged", batteryCharged); 41 | double batteryChargingCurr = getData(data, 42 | batteryChargingCurrIdx, 10); 43 | engine.mqtt.sendMsg("batteryChargingCurr", 44 | batteryChargingCurr); 45 | double batteryDisChargingCurr = getData(data, 46 | batteryDisChargingCurrIdx, 10); 47 | engine.mqtt.sendMsg("batteryDisChargingCurr", 48 | batteryDisChargingCurr); 49 | double outputVoltage = getData(data, outputVoltageIdx, 10); 50 | engine.mqtt.sendMsg("outputVoltage", outputVoltage); 51 | double outputFrequency = getData(data, outputFrequencyIdx, 52 | 10); 53 | engine.mqtt.sendMsg("outputFrequency", outputFrequency); 54 | int outputPower = getData(data, outputPowerIdx, 1, true); 55 | engine.mqtt.sendMsg("outputPower", outputPower); 56 | int outputLoad = getData(data, outputLoadIdx, 1, true); 57 | engine.mqtt.sendMsg("outputLoad", outputLoad); 58 | double acVoltage = getData(data, acVoltageIdx, 10); 59 | engine.mqtt.sendMsg("acVoltage", acVoltage); 60 | double acFrequency = getData(data, acFrequencyIdx, 10); 61 | engine.mqtt.sendMsg("acFrequency", acFrequency); 62 | double pvVoltage = getData(data, pvVoltageIdx, 10); 63 | engine.mqtt.sendMsg("pvVoltage", pvVoltage); 64 | int pvPower = getData(data, pvPowerIdx, 1, true); 65 | engine.mqtt.sendMsg("pvPower", pvPower); 66 | int mode = getData(data, modeIdx, 1, true); 67 | engine.mqtt.sendMsg("mode", mode); 68 | int chargeState = getData(data, chargeStateIdx, 1, true); 69 | engine.mqtt.sendMsg("chargeState", chargeState); 70 | int loadState = getData(data, loadStateIdx, 1, true); 71 | engine.mqtt.sendMsg("loadState", loadState); 72 | }if (data[2] == 0x00 && data[3] == 0x01) { 73 | int chargeState=-1; 74 | int loadState=-1; 75 | if(hex.equals(MQTTClient.chargeSolarOnly)) chargeState=3; 76 | else if(hex.equals(MQTTClient.chargeSolarUtility)) chargeState=2; 77 | else if(hex.equals(MQTTClient.loadSBU)) loadState=2; 78 | else if(hex.equals(MQTTClient.loadUtility)) loadState=0; 79 | if(chargeState!=-1) engine.mqtt.sendMsg("chargeState", chargeState); 80 | if(loadState!=-1) engine.mqtt.sendMsg("loadState", loadState); 81 | 82 | } 83 | engine.lastData = null; 84 | } catch (Exception e) { 85 | e.printStackTrace(); 86 | } 87 | } 88 | 89 | private double getData(byte[] data, short idx, int denominator) { 90 | int b = 0; 91 | for (int i = 1; i >= 0; i--) { 92 | b = (b << 8) + (data[idx + i] & 0xFF); 93 | } 94 | return b * 1.0 / denominator; 95 | } 96 | 97 | private int getData(byte[] data, short idx, int denominator, 98 | boolean toInt) { 99 | int b = 0; 100 | for (int i = 1; i >= 0; i--) { 101 | b = (b << 8) + (data[idx + i] & 0xFF); 102 | } 103 | return b / denominator; 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /MQTTClient.java: -------------------------------------------------------------------------------- 1 | import java.sql.Timestamp; 2 | import java.util.UUID; 3 | import java.util.concurrent.Callable; 4 | 5 | import org.eclipse.paho.client.mqttv3.IMqttClient; 6 | import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; 7 | import org.eclipse.paho.client.mqttv3.MqttCallback; 8 | import org.eclipse.paho.client.mqttv3.MqttClient; 9 | import org.eclipse.paho.client.mqttv3.MqttConnectOptions; 10 | import org.eclipse.paho.client.mqttv3.MqttException; 11 | import org.eclipse.paho.client.mqttv3.MqttMessage; 12 | import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; 13 | 14 | public class MQTTClient implements Runnable, MqttCallback { 15 | 16 | private Engine engine; 17 | private IMqttClient client; 18 | private String prefix; 19 | 20 | public static final String chargeSolarOnly = "40630001000A05040506139900031CE4"; 21 | public static final String chargeSolarUtility = "48E30001000A0504050613990002DD24"; 22 | public static final String loadUtility = "490A0001000A05040506139A0000ACE5"; 23 | public static final String loadSBU = "490D0001000A05040506139A00022D24"; 24 | 25 | public MQTTClient(Engine engine) throws Exception { 26 | this.engine = engine; 27 | this.prefix = engine.mqttTopic; 28 | connect(); 29 | 30 | } 31 | 32 | public void run() { 33 | 34 | } 35 | 36 | public void connect() { 37 | try { 38 | String publisherId = UUID.randomUUID().toString(); 39 | MemoryPersistence persistence = new MemoryPersistence(); 40 | this.client = new MqttClient( 41 | "tcp://" + engine.mqttServer + ":" + engine.mqttPort, 42 | publisherId, persistence); 43 | MqttConnectOptions options = new MqttConnectOptions(); 44 | if (engine.enableMqttAuth) { 45 | options.setUserName(engine.mqttUser); 46 | options.setPassword(engine.mqttPass.toCharArray()); 47 | } 48 | options.setAutomaticReconnect(true); 49 | options.setCleanSession(true); 50 | options.setConnectionTimeout(10); 51 | client.connect(options); 52 | while (!client.isConnected()) 53 | Thread.currentThread().sleep(100); 54 | String time = new Timestamp(System.currentTimeMillis()).toString(); 55 | System.out.println(time + " - MQTT connected"); 56 | client.setCallback(this); 57 | client.subscribe(engine.mqttTopic + "Set/#"); 58 | 59 | } catch (Exception e) { 60 | e.printStackTrace(); 61 | } 62 | } 63 | 64 | public void sendMsg(String topic, String msg) { 65 | if (client == null || !client.isConnected()) { 66 | connect(); 67 | return; 68 | } 69 | try { 70 | MqttMessage mmsg = new MqttMessage(msg.getBytes()); 71 | client.publish(this.prefix + topic, mmsg); 72 | } catch (MqttException e) { 73 | e.printStackTrace(); 74 | } 75 | } 76 | 77 | public void sendMsg(String topic, int val) { 78 | sendMsg(topic, String.valueOf(val)); 79 | } 80 | 81 | public void sendMsg(String topic, double val) { 82 | sendMsg(topic, String.valueOf(val)); 83 | } 84 | 85 | @Override 86 | public void connectionLost(Throwable arg0) { 87 | // TODO Auto-generated method stub 88 | 89 | } 90 | 91 | @Override 92 | public void deliveryComplete(IMqttDeliveryToken arg0) { 93 | // TODO Auto-generated method stub 94 | 95 | } 96 | 97 | public void messageArrived(String topic, MqttMessage message) 98 | throws Exception { 99 | String msg = new String(message.getPayload()); 100 | 101 | if (topic.contains("chargeState")) { 102 | if (msg.equals("3")) { 103 | engine.nsrv.sendData( 104 | engine.hexStringToByteArray(this.chargeSolarOnly)); 105 | String time = new Timestamp(System.currentTimeMillis()) 106 | .toString(); 107 | System.out.println(time + " - Server: " + chargeSolarOnly); 108 | } else { 109 | engine.nsrv.sendData( 110 | engine.hexStringToByteArray(this.chargeSolarUtility)); 111 | String time = new Timestamp(System.currentTimeMillis()) 112 | .toString(); 113 | System.out.println(time + " - Server: " + chargeSolarUtility); 114 | } 115 | } else if (topic.contains("loadState")) { 116 | if (msg.equals("2")) { 117 | engine.nsrv.sendData(engine.hexStringToByteArray(this.loadSBU)); 118 | String time = new Timestamp(System.currentTimeMillis()) 119 | .toString(); 120 | System.out.println(time + " - Server: " + loadSBU); 121 | } else { 122 | engine.nsrv.sendData( 123 | engine.hexStringToByteArray(this.loadUtility)); 124 | String time = new Timestamp(System.currentTimeMillis()) 125 | .toString(); 126 | System.out.println(time + " - Server: " + loadUtility); 127 | } 128 | } else { 129 | String time = new Timestamp(System.currentTimeMillis()).toString(); 130 | System.out.println("\nReceived a Message!" + "\n\tTime: " + time 131 | + "\n\tTopic: " + topic + "\n\tMessage: " 132 | + new String(message.getPayload()) + "\n\tQoS: " 133 | + message.getQos() + "\n"); 134 | } 135 | 136 | } 137 | 138 | } 139 | --------------------------------------------------------------------------------