From a3b4db9566f0c18a1aae386722699564fffd0c57 Mon Sep 17 00:00:00 2001 From: heterodain Date: Mon, 28 Sep 2020 11:24:35 +0900 Subject: [PATCH] =?UTF-8?q?=E8=A8=AD=E5=AE=9A=E3=81=AE=E5=A4=96=E9=83=A8?= =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 36 +++++++-------- settings.json | 12 +++++ .../java/com/heterodain/smartmeter/App.java | 38 ++++++++-------- .../smartmeter/device/SmartMeter.java | 15 +------ .../heterodain/smartmeter/model/Power.java | 16 +++++++ .../heterodain/smartmeter/model/Settings.java | 44 +++++++++++++++++++ 6 files changed, 111 insertions(+), 50 deletions(-) create mode 100644 settings.json create mode 100644 src/main/java/com/heterodain/smartmeter/model/Power.java create mode 100644 src/main/java/com/heterodain/smartmeter/model/Settings.java diff --git a/pom.xml b/pom.xml index 29eab5b..caeabc8 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,6 @@ - + 4.0.0 com.heterodain @@ -14,35 +13,35 @@ UTF-8 - 1.8 - 1.8 + 8 + 8 - org.projectlombok - lombok - 1.18.12 - provided + org.projectlombok + lombok + 1.18.12 + provided - com.fasterxml.jackson.core - jackson-databind - 2.11.2 + com.fasterxml.jackson.core + jackson-databind + 2.11.2 - com.fazecast - jSerialComm - 2.6.2 + com.fazecast + jSerialComm + 2.6.2 - org.apache.logging.log4j - log4j-slf4j-impl - 2.13.3 + org.apache.logging.log4j + log4j-slf4j-impl + 2.13.3 junit @@ -53,7 +52,8 @@ - + + diff --git a/settings.json b/settings.json new file mode 100644 index 0000000..057ccf3 --- /dev/null +++ b/settings.json @@ -0,0 +1,12 @@ +{ + "smartMeter": { + "comPort": "ttyUSB0", + "brouteId": "************", + "broutePassword": "*********************************" + }, + "ambient": { + "channelId": 99999, + "readKey": "****************", + "writeKey": "****************" + } +} diff --git a/src/main/java/com/heterodain/smartmeter/App.java b/src/main/java/com/heterodain/smartmeter/App.java index c354cec..b9a06fd 100644 --- a/src/main/java/com/heterodain/smartmeter/App.java +++ b/src/main/java/com/heterodain/smartmeter/App.java @@ -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; @@ -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(); @@ -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); @@ -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); } } diff --git a/src/main/java/com/heterodain/smartmeter/device/SmartMeter.java b/src/main/java/com/heterodain/smartmeter/device/SmartMeter.java index 5117ee9..b17aba0 100644 --- a/src/main/java/com/heterodain/smartmeter/device/SmartMeter.java +++ b/src/main/java/com/heterodain/smartmeter/device/SmartMeter.java @@ -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 { @@ -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; - } } diff --git a/src/main/java/com/heterodain/smartmeter/model/Power.java b/src/main/java/com/heterodain/smartmeter/model/Power.java new file mode 100644 index 0000000..c1dffd6 --- /dev/null +++ b/src/main/java/com/heterodain/smartmeter/model/Power.java @@ -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; +} diff --git a/src/main/java/com/heterodain/smartmeter/model/Settings.java b/src/main/java/com/heterodain/smartmeter/model/Settings.java new file mode 100644 index 0000000..d92fde4 --- /dev/null +++ b/src/main/java/com/heterodain/smartmeter/model/Settings.java @@ -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; + } +}