Commit ded53cf6 by 周田

feat: 向上寻找配置文件

parent 75d5329b
qos=1
broker=tcp://192.168.0.176:1883
#broker=tcp://127.0.0.1:521
topic=ModbusTcp
username=admin
password=123456
\ No newline at end of file
......@@ -25,13 +25,13 @@ public class OnMessageCallback implements MqttCallback {
// subscribe后得到的消息会执行到这里面
System.out.println("topic: " + topic + ", content: " + message.toString());
// JsonObject data = JsonParser.parseString(message.toString()).getAsJsonObject();
// String location = data.get("__name").getAsString();
// boolean state = Objects.equals(data.get("开关状态").getAsString(), "true");
// int temperature = data.get("温度").getAsInt();
// int press = data.get("压力").getAsInt();
// // 存数据
// TDengineUtils.insertData(location, state, temperature, press);
JsonObject data = JsonParser.parseString(message.toString()).getAsJsonObject();
String location = data.get("__name").getAsString();
boolean state = Objects.equals(data.get("开关状态").getAsString(), "true");
int temperature = data.get("温度").getAsInt();
int press = data.get("压力").getAsInt();
// 存数据
TDengineUtils.insertData(location, state, temperature, press);
}
@Override
......
package org.linkor.setting;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
......@@ -22,16 +24,68 @@ public class Setting {
public static void init() {
// read from setting.properties
// Properties properties = new Properties();
// properties.load(Setting.class.getClassLoader().getResourceAsStream("setting.properties"));
String configFile = findFile("setting.properties");
if (configFile == null) {
System.out.println("setting.properties not found");
}
Properties properties = readConfig(configFile);
QOS = Integer.parseInt(properties.getProperty("qos"));
BROKER = properties.getProperty("broker");
TOPIC = properties.getProperty("topic");
USERNAME = properties.getProperty("username");
PASSWORD = properties.getProperty("password");
}
public static String findFile(String fileName) {
File currentDir = null;
try {
Properties properties = new Properties();
properties.load(Setting.class.getClassLoader().getResourceAsStream("setting.properties"));
QOS = Integer.parseInt(properties.getProperty("qos"));
BROKER = properties.getProperty("broker");
TOPIC = properties.getProperty("topic");
USERNAME = properties.getProperty("username");
PASSWORD = properties.getProperty("password");
currentDir = new File(".").getCanonicalFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
return findFile(currentDir, fileName);
}
private static String findFile(File currentDir, String fileName) {
if(currentDir == null) {
return null;
}
System.out.println(currentDir.getAbsolutePath() + " " + fileName);
File[] files = currentDir.listFiles();
if(files != null) {
for(File file : files) {
if(file.getName().equals(fileName)) {
return file.getAbsolutePath();
}
}
}
File parent = currentDir.getParentFile();
if (parent != null) {
return findFile(parent, fileName);
}
return null;
}
public static Properties readConfig(String configFilePath) {
Properties props = new Properties();
try {
FileInputStream fis = new FileInputStream(configFilePath);
props.load(fis);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return props;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment