11package com .heterodain .smartmeter ;
22
3+ import java .io .File ;
34import java .util .ArrayList ;
4- import java .util .Collection ;
55import java .util .concurrent .Executors ;
66import java .util .concurrent .ScheduledExecutorService ;
77import java .util .concurrent .TimeUnit ;
88
9+ import com .fasterxml .jackson .databind .ObjectMapper ;
910import 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 ;
1113import com .heterodain .smartmeter .service .Ambient ;
1214
1315import lombok .var ;
1820 */
1921@ Slf4j
2022public 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}
0 commit comments