Skip to content

Commit a3b4db9

Browse files
author
heterodain
committed
設定の外部ファイル化
1 parent 7c6db6d commit a3b4db9

6 files changed

Lines changed: 111 additions & 50 deletions

File tree

pom.xml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
54
<modelVersion>4.0.0</modelVersion>
65

76
<groupId>com.heterodain</groupId>
@@ -14,35 +13,35 @@
1413

1514
<properties>
1615
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17-
<maven.compiler.source>1.8</maven.compiler.source>
18-
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>8</maven.compiler.source>
17+
<maven.compiler.target>8</maven.compiler.target>
1918
</properties>
2019

2120
<dependencies>
2221
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
2322
<dependency>
24-
<groupId>org.projectlombok</groupId>
25-
<artifactId>lombok</artifactId>
26-
<version>1.18.12</version>
27-
<scope>provided</scope>
23+
<groupId>org.projectlombok</groupId>
24+
<artifactId>lombok</artifactId>
25+
<version>1.18.12</version>
26+
<scope>provided</scope>
2827
</dependency>
2928
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
3029
<dependency>
31-
<groupId>com.fasterxml.jackson.core</groupId>
32-
<artifactId>jackson-databind</artifactId>
33-
<version>2.11.2</version>
30+
<groupId>com.fasterxml.jackson.core</groupId>
31+
<artifactId>jackson-databind</artifactId>
32+
<version>2.11.2</version>
3433
</dependency>
3534
<!-- https://mvnrepository.com/artifact/com.fazecast/jSerialComm -->
3635
<dependency>
37-
<groupId>com.fazecast</groupId>
38-
<artifactId>jSerialComm</artifactId>
39-
<version>2.6.2</version>
36+
<groupId>com.fazecast</groupId>
37+
<artifactId>jSerialComm</artifactId>
38+
<version>2.6.2</version>
4039
</dependency>
4140
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
4241
<dependency>
43-
<groupId>org.apache.logging.log4j</groupId>
44-
<artifactId>log4j-slf4j-impl</artifactId>
45-
<version>2.13.3</version>
42+
<groupId>org.apache.logging.log4j</groupId>
43+
<artifactId>log4j-slf4j-impl</artifactId>
44+
<version>2.13.3</version>
4645
</dependency>
4746
<dependency>
4847
<groupId>junit</groupId>
@@ -53,7 +52,8 @@
5352
</dependencies>
5453

5554
<build>
56-
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
55+
<pluginManagement>
56+
<!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
5757
<plugins>
5858
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
5959
<plugin>

settings.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"smartMeter": {
3+
"comPort": "ttyUSB0",
4+
"brouteId": "************",
5+
"broutePassword": "*********************************"
6+
},
7+
"ambient": {
8+
"channelId": 99999,
9+
"readKey": "****************",
10+
"writeKey": "****************"
11+
}
12+
}

src/main/java/com/heterodain/smartmeter/App.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.heterodain.smartmeter;
22

3+
import java.io.File;
34
import java.util.ArrayList;
4-
import java.util.Collection;
55
import java.util.concurrent.Executors;
66
import java.util.concurrent.ScheduledExecutorService;
77
import java.util.concurrent.TimeUnit;
88

9+
import com.fasterxml.jackson.databind.ObjectMapper;
910
import com.heterodain.smartmeter.device.SmartMeter;
10-
import com.heterodain.smartmeter.device.SmartMeter.Power;
11+
import com.heterodain.smartmeter.model.Power;
12+
import com.heterodain.smartmeter.model.Settings;
1113
import com.heterodain.smartmeter.service.Ambient;
1214

1315
import lombok.var;
@@ -18,20 +20,20 @@
1820
*/
1921
@Slf4j
2022
public class App {
21-
private static final String COM_PORT = "ttyUSB0";
22-
23-
private static final int AMBIENT_CHANNEL = 999999;
24-
private static final String AMBIENT_WRITE_KEY = "************";
25-
26-
private static final String BROUTE_ID = "*********************************";
27-
private static final String BROUTE_PASSWORD = "************";
28-
23+
// バックグラウンドタスクを動かすためのスレッドプール
2924
private static ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(2);
3025

3126
public static void main(final String[] args) throws Exception {
32-
var ambient = new Ambient(AMBIENT_CHANNEL, "TODO", AMBIENT_WRITE_KEY);
27+
var om = new ObjectMapper();
28+
var settings = om.readValue(new File("settings.json"), Settings.class);
3329

34-
try (var smartMeter = new SmartMeter(COM_PORT, BROUTE_ID, BROUTE_PASSWORD)) {
30+
var ambientSettings = settings.getAmbient();
31+
var ambient = new Ambient(ambientSettings.getChannelId(), ambientSettings.getReadKey(),
32+
ambientSettings.getWriteKey());
33+
34+
var smSettings = settings.getSmartMeter();
35+
try (var smartMeter = new SmartMeter(smSettings.getComPort(), smSettings.getBrouteId(),
36+
smSettings.getBroutePassword())) {
3537
smartMeter.init();
3638
smartMeter.connect();
3739

@@ -45,7 +47,7 @@ public static void main(final String[] args) throws Exception {
4547
powers.add(power);
4648
}
4749

48-
} catch (InterruptedException e) {
50+
} catch (InterruptedException ignore) {
4951
// NOP
5052
} catch (Exception e) {
5153
log.warn("スマートメーターへのアクセスに失敗しました。", e);
@@ -86,18 +88,18 @@ public static void main(final String[] args) throws Exception {
8688
};
8789
threadPool.scheduleWithFixedDelay(sendAmbientTask, 1, 1, TimeUnit.MINUTES);
8890

89-
// SIGINT(Ctrl + C)で止められるまで待つ
91+
// プログラムが止められるまで待つ : SIGINT(Ctrl + C)
9092
var wait = new Object();
9193
synchronized (wait) {
9294
try {
9395
wait.wait();
94-
} catch (InterruptedException e) {
96+
} catch (InterruptedException ignore) {
9597
// NOP
9698
}
9799
}
98-
99-
threadPool.shutdown();
100-
threadPool.awaitTermination(15, TimeUnit.SECONDS);
101100
}
101+
102+
threadPool.shutdown();
103+
threadPool.awaitTermination(15, TimeUnit.SECONDS);
102104
}
103105
}

src/main/java/com/heterodain/smartmeter/device/SmartMeter.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
import java.util.Map;
1414
import java.util.stream.Collectors;
1515

16-
import lombok.Data;
1716
import lombok.var;
1817
import lombok.extern.slf4j.Slf4j;
1918

2019
import com.fazecast.jSerialComm.SerialPort;
20+
import com.heterodain.smartmeter.model.Power;
2121

2222
@Slf4j
2323
public class SmartMeter implements Closeable {
@@ -268,17 +268,4 @@ private void writeEchonetLite(byte[] frame) throws IOException {
268268
bytes = ByteBuffer.allocate(bytes.length + frame.length).put(bytes).put(frame).array();
269269
serial.writeBytes(bytes, bytes.length);
270270
}
271-
272-
/**
273-
* 電力情報
274-
*/
275-
@Data
276-
public static class Power {
277-
// 瞬時電力(W)
278-
private Long instantPower;
279-
// R相電流(0.1A)
280-
private Long instantRAmp;
281-
// T相電流(0.1A)
282-
private Long instantTAmp;
283-
}
284271
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.heterodain.smartmeter.model;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* 電力情報
7+
*/
8+
@Data
9+
public class Power {
10+
// 瞬時電力(W)
11+
private Long instantPower;
12+
// R相電流(0.1A)
13+
private Long instantRAmp;
14+
// T相電流(0.1A)
15+
private Long instantTAmp;
16+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.heterodain.smartmeter.model;
2+
3+
import lombok.Getter;
4+
import lombok.ToString;
5+
6+
/**
7+
* 設定情報
8+
*/
9+
@Getter
10+
@ToString
11+
public class Settings {
12+
// スマートメーターの設定
13+
private SmartMeter smartMeter;
14+
// Ambientの設定
15+
private Ambient ambient;
16+
17+
/**
18+
* スマートメーターの設定情報
19+
*/
20+
@Getter
21+
@ToString
22+
public static class SmartMeter {
23+
// シリアル通信ポート名
24+
private String comPort;
25+
// BルートID
26+
private String brouteId;
27+
// Bルートパスワード
28+
private String broutePassword;
29+
}
30+
31+
/**
32+
* Ambientの設定情報
33+
*/
34+
@Getter
35+
@ToString
36+
public static class Ambient {
37+
// チャネルID
38+
private Integer channelId;
39+
// リードキー
40+
private String readKey;
41+
// ライトキー
42+
private String writeKey;
43+
}
44+
}

0 commit comments

Comments
 (0)