-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
76 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
mvn clean compile assembly:single |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
java -Xms80M -Xmx80m -Xlog:gc -XX:+UseG1GC -jar Mks2MqttReporter.jar -ms tcp://192.168.1.11:1883 -ph 192.168.1.173 -pp 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ru.bloof.conf; | ||
|
||
public class AppConfig { | ||
public String printerHost; | ||
public int printerPort; | ||
public String mqttServer; | ||
public String mqttPublisher; | ||
public String topicPrefix; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package ru.bloof.conf; | ||
|
||
import org.apache.commons.cli.*; | ||
import org.pmw.tinylog.Logger; | ||
|
||
public class ArgsParser { | ||
private static final String MQTT_PUBLISHER_DEF = "Mks2MqttReporter/1.0.0"; | ||
private static final String TOPIC_PREFIX_DEF = "home/ghost4s/"; | ||
|
||
public AppConfig parse(String[] args) { | ||
Options opts = new Options(); | ||
opts.addRequiredOption("ph", "printer-host", true, "printer network host (ip address). ex: 192.168.1.80"); | ||
opts.addRequiredOption("pp", "printer-port", true, "printer network port. ex: 8080"); | ||
opts.addRequiredOption("ms", "mqtt-server", true, "mqtt server. format scheme://host:port. ex: tcp://192.168.1.200:1883 or ssl://192.168.1.200:8883"); | ||
opts.addOption("p", "publisher-name", true, "mqtt publisher name. default: " + MQTT_PUBLISHER_DEF); | ||
opts.addOption("t", "topic-prefix", true, "mqtt topic prefix. default: " + TOPIC_PREFIX_DEF); | ||
|
||
CommandLineParser parser = new DefaultParser(); | ||
try { | ||
CommandLine parsed = parser.parse(opts, args); | ||
AppConfig appConfig = new AppConfig(); | ||
appConfig.printerHost = parsed.getOptionValue("ph"); | ||
appConfig.printerPort = Integer.parseInt(parsed.getOptionValue("pp")); | ||
appConfig.mqttServer = parsed.getOptionValue("ms"); | ||
appConfig.mqttPublisher = parsed.getOptionValue("p", MQTT_PUBLISHER_DEF); | ||
appConfig.topicPrefix = parsed.getOptionValue("t", TOPIC_PREFIX_DEF); | ||
return appConfig; | ||
} catch (ParseException e) { | ||
Logger.error("Bad parameters provided: " + e.getMessage()); | ||
new HelpFormatter().printHelp("java [some java options, like GC, memory, etc] -jar Mks2MqttReporter.jar", opts, true); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,19 +4,21 @@ | |
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; | ||
import org.eclipse.paho.client.mqttv3.MqttException; | ||
import org.pmw.tinylog.Logger; | ||
import ru.bloof.conf.AppConfig; | ||
|
||
import java.io.Closeable; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Oleg Larionov</a> | ||
*/ | ||
public class MqttProxy implements Closeable { | ||
private static final String MQTT_TOPIC_PREFIX = "home/ghost4s/"; | ||
|
||
private final AppConfig appConfig; | ||
private final MqttClient mqttClient; | ||
|
||
public MqttProxy(String server, String publisher) throws MqttException { | ||
mqttClient = new MqttClient(server, publisher); | ||
public MqttProxy(AppConfig config) throws MqttException { | ||
this.appConfig = config; | ||
|
||
mqttClient = new MqttClient(config.mqttServer, config.mqttPublisher); | ||
MqttConnectOptions options = new MqttConnectOptions(); | ||
options.setAutomaticReconnect(true); | ||
options.setCleanSession(true); | ||
|
@@ -26,7 +28,7 @@ public MqttProxy(String server, String publisher) throws MqttException { | |
} | ||
|
||
public void send(String statId, Object value) throws MqttException { | ||
String topic = MQTT_TOPIC_PREFIX + statId; | ||
String topic = appConfig.topicPrefix + statId; | ||
Logger.info("Sending {} to topic {}", value, topic); | ||
mqttClient.publish(topic, value.toString().getBytes(), 0, false); | ||
} | ||
|