-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
190 lines (154 loc) · 3.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"
"time"
"github.com/rs/zerolog/log"
"github.com/yakumo-saki/b-route-reader-go/src/bp35a1"
"github.com/yakumo-saki/b-route-reader-go/src/config"
"github.com/yakumo-saki/b-route-reader-go/src/global"
"github.com/yakumo-saki/b-route-reader-go/src/logger"
)
var exitcode = 0
func main() {
ret := run()
os.Exit(ret)
}
func run() int {
config.Initialize()
logger.Initiallize()
log.Info().Msgf("Version %s. Build %s", global.Version, global.GitBuild)
log.Info().Msgf("%s", global.Url)
err := bp35a1.Connect()
if err != nil {
log.Err(err).Msg("Serial port open error. Exiting.")
exitcode = 1
goto EXIT
}
err = runWithSerialPort()
if err != nil {
exitcode = 1
log.Err(err).Msg("ERR")
}
EXIT:
err = bp35a1.Close()
if err != nil {
log.Err(err).Msg("Error occured in close connection. do nothing.")
}
if exitcode == 0 {
log.Info().Msg("Normal end.")
}
return exitcode
}
func runWithSerialPort() error {
var err error
var ipv6 string
err = bp35a1.StartConnection()
if err != nil {
return fmt.Errorf("test connection failed: %w", err)
}
ipv6, err = bp35a1.InitializeBrouteConnection()
if err != nil {
log.Err(err).Msg(". Exiting.")
return fmt.Errorf("cannot initialize B-route connection: %w", err)
}
// echonet start
err = bp35a1.GetSmartMeterInitialData(ipv6)
if err != nil {
return fmt.Errorf("error occured while initializing echonet lite: %w", err)
}
log.Info().Msg("Starting main loop")
// TODO シグナルハンドリング
nowTimer := time.NewTimer(config.NOW_CONSUMPTION_WAIT)
totalTimer := time.NewTimer(config.TOTAL_CONSUMPTION_WAIT)
for {
select {
case <-nowTimer.C:
ret, err := bp35a1.GetNowConsumptionData(ipv6)
if err != nil {
return fmt.Errorf("error occured while getting consumption: %w", err)
}
nowTimer = time.NewTimer(config.NOW_CONSUMPTION_WAIT)
log.Info().Msgf("Smartmeter Response: %v", ret)
err = handleResult(ret)
if err != nil {
return fmt.Errorf("error occured while executing %s: %w", config.EXEC_CMD, err)
}
case <-totalTimer.C:
ret, err := bp35a1.GetDeltaConsumptionData(ipv6)
if err != nil {
return fmt.Errorf("error occured while getting delta consumption: %w", err)
}
totalTimer = time.NewTimer(config.TOTAL_CONSUMPTION_WAIT)
log.Info().Msgf("Smartmeter Response: %v", ret)
err = handleResult(ret)
if err != nil {
return fmt.Errorf("error occured while executing %s: %w", config.EXEC_CMD, err)
}
}
}
}
func handleResult(data bp35a1.ElectricData) error {
jsonMap := map[string]interface{}{}
for k, v := range data {
jsonMap[k] = v
}
jsonMap["datetime"] = time.Now().Format("2006-01-02T15:04:05.999Z")
json, err := json.Marshal(jsonMap)
if err != nil {
return err
}
f, err := ioutil.TempFile(os.TempDir(), "b-route-")
if err != nil {
return err
}
written, err := f.Write(json)
if err != nil {
return err
}
if written != len(json) {
return fmt.Errorf("bytes written != actual")
}
filepath := f.Name()
f.Close()
// exec
output, err := exec.Command(config.EXEC_CMD, f.Name()).CombinedOutput()
if err != nil {
log.Err(err).Msgf("error executing EXEC_CMD %s", config.EXEC_CMD)
outputByteStringsToLog(output, true)
} else {
outputByteStringsToLog(output, false)
}
os.Remove(filepath)
return nil
}
func outputByteStringsToLog(byteStrings []byte, isError bool) {
newline := "\n"
switch runtime.GOOS {
case "windows":
newline = "\r\n"
case "darwin":
newline = "\n"
case "linux":
newline = "\n"
}
allStrings := string(byteStrings)
lines := strings.Split(allStrings, newline)
for _, v := range lines {
line := v
line = strings.ReplaceAll(line, "\r", "")
line = strings.ReplaceAll(line, "\n", "")
if len(line) > 0 {
if isError {
log.Error().Msgf("EXEC OUTPUT: %s", line)
} else {
log.Debug().Msgf("EXEC OUTPUT: %s", line)
}
}
}
}