Skip to content

Commit

Permalink
add command line config
Browse files Browse the repository at this point in the history
  • Loading branch information
Blooof committed Oct 31, 2021
1 parent fa5996d commit dca6697
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 17 deletions.
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
mvn clean compile assembly:single
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.5.0</version>
</dependency>

</dependencies>

<build>
Expand Down
2 changes: 2 additions & 0 deletions run.sh
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
22 changes: 12 additions & 10 deletions src/main/java/ru/bloof/AppMain.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package ru.bloof;

import org.eclipse.paho.client.mqttv3.MqttException;
import org.pmw.tinylog.Logger;
import ru.bloof.conf.AppConfig;
import ru.bloof.conf.ArgsParser;
import ru.bloof.mks.PrinterClient;
import ru.bloof.mks.cmd.*;
import ru.bloof.mqtt.MqttProxy;
Expand All @@ -11,19 +12,18 @@
import java.util.concurrent.TimeUnit;

/**
* Hello world!
* Main class
*/
public class AppMain {
private static final String PRINTER_HOST = "";
private static final int PRINTER_PORT = 8080;
private static final String MQTT_SERVER = "";
private static final String MQTT_PUBLISHER = "Mks2MqttReporter/1.0.0";
public static void main(String[] args) throws Exception {
AppConfig config = new ArgsParser().parse(args);
if (config == null) {
return;
}

public static void main(String[] args) throws MqttException {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

try (PrinterClient pc = new PrinterClient(PRINTER_HOST, PRINTER_PORT);
MqttProxy mqttProxy = new MqttProxy(MQTT_SERVER, MQTT_PUBLISHER)) {
try (PrinterClient pc = new PrinterClient(config);
MqttProxy mqttProxy = new MqttProxy(config)) {
executor.scheduleWithFixedDelay(() -> sendTempStatus(pc, mqttProxy), 0, 5, TimeUnit.SECONDS);
executor.scheduleWithFixedDelay(() -> sendPrintStatus(pc, mqttProxy), 0, 30, TimeUnit.SECONDS);
while (!Thread.currentThread().isInterrupted()) {
Expand All @@ -33,6 +33,8 @@ public static void main(String[] args) throws MqttException {
Thread.currentThread().interrupt();
}
}
} finally {
executor.shutdownNow();
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/ru/bloof/conf/AppConfig.java
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;
}
34 changes: 34 additions & 0 deletions src/main/java/ru/bloof/conf/ArgsParser.java
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;
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/ru/bloof/mks/PrinterClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.bloof.mks;

import ru.bloof.conf.AppConfig;

import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
Expand All @@ -13,8 +15,8 @@ public class PrinterClient implements Closeable {

private PrinterConnection cnxn;

public PrinterClient(String host, int port) {
this.addr = new InetSocketAddress(host, port);
public PrinterClient(AppConfig config) {
this.addr = new InetSocketAddress(config.printerHost, config.printerPort);
}

public synchronized <T> T executeCommand(PrinterCommand<T> command) {
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/ru/bloof/mqtt/MqttProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down

0 comments on commit dca6697

Please sign in to comment.