@@ -34,6 +34,7 @@ dependencies { | |||||
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0' | implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0' | ||||
implementation group: 'org.apache.commons', name: 'commons-text', version: '1.9' | implementation group: 'org.apache.commons', name: 'commons-text', version: '1.9' | ||||
implementation group: 'org.apache.tika', name: 'tika-core', version: '2.4.1' | implementation group: 'org.apache.tika', name: 'tika-core', version: '2.4.1' | ||||
implementation group: 'org.springframework.integration', name: 'spring-integration-mqtt', version: '6.0.2' | |||||
implementation 'net.sf.jasperreports:jasperreports:6.19.1' | implementation 'net.sf.jasperreports:jasperreports:6.19.1' | ||||
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.9' | implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.9' | ||||
@@ -0,0 +1,25 @@ | |||||
package com.ffii.towngas.config; | |||||
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; | |||||
import org.eclipse.paho.client.mqttv3.MqttCallback; | |||||
import org.eclipse.paho.client.mqttv3.MqttMessage; | |||||
public class MqttConsumerCallBack implements MqttCallback { | |||||
@Override | |||||
public void connectionLost(Throwable throwable) { | |||||
System.out.println("Disconnected with server"); | |||||
} | |||||
@Override | |||||
public void messageArrived(String topic, MqttMessage message) throws Exception { | |||||
System.out.println(String.format("Topic: %s", topic)); | |||||
System.out.println(String.format("Qos : %d", message.getQos())); | |||||
System.out.println(String.format("Content: %s", new String(message.getPayload()))); | |||||
System.out.println(String.format("Retained : %b", message.isRetained())); | |||||
} | |||||
@Override | |||||
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { | |||||
} | |||||
} |
@@ -0,0 +1,77 @@ | |||||
package com.ffii.towngas.config; | |||||
import javax.annotation.PostConstruct; | |||||
import org.eclipse.paho.client.mqttv3.MqttClient; | |||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; | |||||
import org.eclipse.paho.client.mqttv3.MqttException; | |||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; | |||||
import org.springframework.beans.factory.annotation.Value; | |||||
import org.springframework.context.annotation.Configuration; | |||||
@Configuration | |||||
public class MqttConsumerConfig { | |||||
@Value("${spring.mqtt.username}") | |||||
private String username; | |||||
@Value("${spring.mqtt.password}") | |||||
private String password; | |||||
@Value("${spring.mqtt.url}") | |||||
private String hostUrl; | |||||
@Value("${spring.mqtt.client.id}") | |||||
private String clientId; | |||||
@Value("${spring.mqtt.default.topic}") | |||||
private String defaultTopic; | |||||
private MqttClient client; | |||||
@PostConstruct | |||||
public void init() { | |||||
connect(); | |||||
} | |||||
public void connect() { | |||||
try { | |||||
// Create a client instance | |||||
client = new MqttClient(hostUrl, clientId, new MemoryPersistence()); | |||||
// Create a connection option object | |||||
MqttConnectOptions options = new MqttConnectOptions(); | |||||
// set connection option | |||||
options.setCleanSession(true); | |||||
options.setUserName(username); | |||||
options.setPassword(password.toCharArray()); | |||||
options.setConnectionTimeout(100); | |||||
options.setKeepAliveInterval(20); | |||||
options.setWill("willTopic", (clientId + "Disconnected").getBytes(), 0, false); | |||||
client.setCallback(new MqttConsumerCallBack()); | |||||
System.out.println("Start Connection"); | |||||
client.connect(options); | |||||
System.out.println("End Connection"); | |||||
int[] qos = { 1, 1 }; | |||||
// Topics | |||||
String[] topics = { "topic1", "topic2" }; | |||||
client.subscribe(topics, qos); | |||||
} catch (MqttException e) { | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
public void disConnect() { | |||||
try { | |||||
client.disconnect(); | |||||
} catch (MqttException e) { | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
public void subscribe(String topic, int qos) { | |||||
try { | |||||
client.subscribe(topic, qos); | |||||
} catch (MqttException e) { | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,38 @@ | |||||
package com.ffii.towngas.sensor.web; | |||||
import org.springframework.beans.factory.annotation.Value; | |||||
import org.springframework.web.bind.annotation.RequestMapping; | |||||
import org.springframework.web.bind.annotation.ResponseBody; | |||||
import org.springframework.web.bind.annotation.RestController; | |||||
import com.ffii.core.support.AbstractController; | |||||
import com.ffii.towngas.config.MqttConsumerConfig; | |||||
@RestController | |||||
@RequestMapping("/protected/sensorData") | |||||
public class TestController extends AbstractController { | |||||
@Value("${spring.mqtt.client.id}") | |||||
private String clientId; | |||||
private MqttConsumerConfig client; | |||||
public TestController(MqttConsumerConfig client) { | |||||
this.client = client; | |||||
} | |||||
@RequestMapping("connect") | |||||
@ResponseBody | |||||
public String connect() { | |||||
client.connect(); | |||||
return clientId + "Connected to the server"; | |||||
} | |||||
@RequestMapping("dis-connect") | |||||
@ResponseBody | |||||
public String disConnect() { | |||||
client.disConnect(); | |||||
return clientId + "Disconnected from the server"; | |||||
} | |||||
} |
@@ -0,0 +1,5 @@ | |||||
spring: | |||||
datasource: | |||||
url: jdbc:mysql://192.168.1.238:3306/towngasdb?useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8 | |||||
username: root | |||||
password: secret |
@@ -0,0 +1,9 @@ | |||||
spring: | |||||
mqtt: | |||||
url: tcp://202.130.68.18:1883 | |||||
username: test | |||||
password: q1w2e3r4 | |||||
client: | |||||
id: cfghiftghigyui | |||||
default: | |||||
topic: topic |