Skip to content

Commit

Permalink
設定の外部ファイル化
Browse files Browse the repository at this point in the history
  • Loading branch information
heterodain committed Sep 28, 2020
1 parent 7c6db6d commit a3b4db9
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 50 deletions.
36 changes: 18 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<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">
<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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.heterodain</groupId>
Expand All @@ -14,35 +13,35 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fazecast/jSerialComm -->
<dependency>
<groupId>com.fazecast</groupId>
<artifactId>jSerialComm</artifactId>
<version>2.6.2</version>
<groupId>com.fazecast</groupId>
<artifactId>jSerialComm</artifactId>
<version>2.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.13.3</version>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand All @@ -53,7 +52,8 @@
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<pluginManagement>
<!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
Expand Down
12 changes: 12 additions & 0 deletions settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"smartMeter": {
"comPort": "ttyUSB0",
"brouteId": "************",
"broutePassword": "*********************************"
},
"ambient": {
"channelId": 99999,
"readKey": "****************",
"writeKey": "****************"
}
}
38 changes: 20 additions & 18 deletions src/main/java/com/heterodain/smartmeter/App.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.heterodain.smartmeter;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.heterodain.smartmeter.device.SmartMeter;
import com.heterodain.smartmeter.device.SmartMeter.Power;
import com.heterodain.smartmeter.model.Power;
import com.heterodain.smartmeter.model.Settings;
import com.heterodain.smartmeter.service.Ambient;

import lombok.var;
Expand All @@ -18,20 +20,20 @@
*/
@Slf4j
public class App {
private static final String COM_PORT = "ttyUSB0";

private static final int AMBIENT_CHANNEL = 999999;
private static final String AMBIENT_WRITE_KEY = "************";

private static final String BROUTE_ID = "*********************************";
private static final String BROUTE_PASSWORD = "************";

// バックグラウンドタスクを動かすためのスレッドプール
private static ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(2);

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

try (var smartMeter = new SmartMeter(COM_PORT, BROUTE_ID, BROUTE_PASSWORD)) {
var ambientSettings = settings.getAmbient();
var ambient = new Ambient(ambientSettings.getChannelId(), ambientSettings.getReadKey(),
ambientSettings.getWriteKey());

var smSettings = settings.getSmartMeter();
try (var smartMeter = new SmartMeter(smSettings.getComPort(), smSettings.getBrouteId(),
smSettings.getBroutePassword())) {
smartMeter.init();
smartMeter.connect();

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

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

// SIGINT(Ctrl + C)で止められるまで待つ
// プログラムが止められるまで待つ : SIGINT(Ctrl + C)
var wait = new Object();
synchronized (wait) {
try {
wait.wait();
} catch (InterruptedException e) {
} catch (InterruptedException ignore) {
// NOP
}
}

threadPool.shutdown();
threadPool.awaitTermination(15, TimeUnit.SECONDS);
}

threadPool.shutdown();
threadPool.awaitTermination(15, TimeUnit.SECONDS);
}
}
15 changes: 1 addition & 14 deletions src/main/java/com/heterodain/smartmeter/device/SmartMeter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
import java.util.Map;
import java.util.stream.Collectors;

import lombok.Data;
import lombok.var;
import lombok.extern.slf4j.Slf4j;

import com.fazecast.jSerialComm.SerialPort;
import com.heterodain.smartmeter.model.Power;

@Slf4j
public class SmartMeter implements Closeable {
Expand Down Expand Up @@ -268,17 +268,4 @@ private void writeEchonetLite(byte[] frame) throws IOException {
bytes = ByteBuffer.allocate(bytes.length + frame.length).put(bytes).put(frame).array();
serial.writeBytes(bytes, bytes.length);
}

/**
* 電力情報
*/
@Data
public static class Power {
// 瞬時電力(W)
private Long instantPower;
// R相電流(0.1A)
private Long instantRAmp;
// T相電流(0.1A)
private Long instantTAmp;
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/heterodain/smartmeter/model/Power.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.heterodain.smartmeter.model;

import lombok.Data;

/**
* 電力情報
*/
@Data
public class Power {
// 瞬時電力(W)
private Long instantPower;
// R相電流(0.1A)
private Long instantRAmp;
// T相電流(0.1A)
private Long instantTAmp;
}
44 changes: 44 additions & 0 deletions src/main/java/com/heterodain/smartmeter/model/Settings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.heterodain.smartmeter.model;

import lombok.Getter;
import lombok.ToString;

/**
* 設定情報
*/
@Getter
@ToString
public class Settings {
// スマートメーターの設定
private SmartMeter smartMeter;
// Ambientの設定
private Ambient ambient;

/**
* スマートメーターの設定情報
*/
@Getter
@ToString
public static class SmartMeter {
// シリアル通信ポート名
private String comPort;
// BルートID
private String brouteId;
// Bルートパスワード
private String broutePassword;
}

/**
* Ambientの設定情報
*/
@Getter
@ToString
public static class Ambient {
// チャネルID
private Integer channelId;
// リードキー
private String readKey;
// ライトキー
private String writeKey;
}
}

0 comments on commit a3b4db9

Please sign in to comment.