Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
O2OA_plugin_demo
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
周田
O2OA_plugin_demo
Commits
ded53cf6
Commit
ded53cf6
authored
Dec 12, 2023
by
周田
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 向上寻找配置文件
parent
75d5329b
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
71 additions
and
10 deletions
+71
-10
setting.properties
HslDemo/setting.properties
+7
-0
OnMessageCallback.java
.../src/main/java/org/linkor/callback/OnMessageCallback.java
+7
-7
Setting.java
HslDemo/src/main/java/org/linkor/setting/Setting.java
+57
-3
No files found.
HslDemo/setting.properties
0 → 100644
View file @
ded53cf6
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
HslDemo/src/main/java/org/linkor/callback/OnMessageCallback.java
View file @
ded53cf6
...
@@ -25,13 +25,13 @@ public class OnMessageCallback implements MqttCallback {
...
@@ -25,13 +25,13 @@ public class OnMessageCallback implements MqttCallback {
// subscribe后得到的消息会执行到这里面
// subscribe后得到的消息会执行到这里面
System
.
out
.
println
(
"topic: "
+
topic
+
", content: "
+
message
.
toString
());
System
.
out
.
println
(
"topic: "
+
topic
+
", content: "
+
message
.
toString
());
//
JsonObject data = JsonParser.parseString(message.toString()).getAsJsonObject();
JsonObject
data
=
JsonParser
.
parseString
(
message
.
toString
()).
getAsJsonObject
();
//
String location = data.get("__name").getAsString();
String
location
=
data
.
get
(
"__name"
).
getAsString
();
//
boolean state = Objects.equals(data.get("开关状态").getAsString(), "true");
boolean
state
=
Objects
.
equals
(
data
.
get
(
"开关状态"
).
getAsString
(),
"true"
);
//
int temperature = data.get("温度").getAsInt();
int
temperature
=
data
.
get
(
"温度"
).
getAsInt
();
//
int press = data.get("压力").getAsInt();
int
press
=
data
.
get
(
"压力"
).
getAsInt
();
//
// 存数据
// 存数据
//
TDengineUtils.insertData(location, state, temperature, press);
TDengineUtils
.
insertData
(
location
,
state
,
temperature
,
press
);
}
}
@Override
@Override
...
...
HslDemo/src/main/java/org/linkor/setting/Setting.java
View file @
ded53cf6
package
org
.
linkor
.
setting
;
package
org
.
linkor
.
setting
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.time.LocalDateTime
;
import
java.time.LocalDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.time.format.DateTimeFormatter
;
...
@@ -22,16 +24,68 @@ public class Setting {
...
@@ -22,16 +24,68 @@ public class Setting {
public
static
void
init
()
{
public
static
void
init
()
{
// read from setting.properties
// read from setting.properties
try
{
// Properties properties = new Properties();
Properties
properties
=
new
Properties
();
// properties.load(Setting.class.getClassLoader().getResourceAsStream("setting.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"
));
QOS
=
Integer
.
parseInt
(
properties
.
getProperty
(
"qos"
));
BROKER
=
properties
.
getProperty
(
"broker"
);
BROKER
=
properties
.
getProperty
(
"broker"
);
TOPIC
=
properties
.
getProperty
(
"topic"
);
TOPIC
=
properties
.
getProperty
(
"topic"
);
USERNAME
=
properties
.
getProperty
(
"username"
);
USERNAME
=
properties
.
getProperty
(
"username"
);
PASSWORD
=
properties
.
getProperty
(
"password"
);
PASSWORD
=
properties
.
getProperty
(
"password"
);
}
public
static
String
findFile
(
String
fileName
)
{
File
currentDir
=
null
;
try
{
currentDir
=
new
File
(
"."
).
getCanonicalFile
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
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
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment