-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
222 lines (199 loc) · 6.21 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
_ "time/tzdata"
)
type Configuration struct {
Tz string
Token string
StartDate time.Time
UpdateInterval time.Duration
}
type Response struct {
Data []DeviceData `json:"data"`
}
type DeviceData struct {
DeviceInfo DeviceInfo `json:"deviceInfo"`
}
type DeviceInfo struct {
TemperatureF int `json:"temperatureF"`
Humidity int `json:"humidity"`
Ports []Port `json:"ports"`
Sensors []Sensor `json:"sensors"`
}
type Port struct {
PortName string `json:"portName"`
Speak int `json:"speak"`
Port int `json:"port"`
CurMode int `json:"curMode"`
Online int `json:"online"`
}
type Sensor struct {
SensorType int `json:"sensorType"`
AccessPort int `json:"accessPort"`
SensorData int `json:"sensorData"`
}
func main() {
log.Printf("Starting")
file, err := os.Open("config.properties")
if err != nil {
log.Fatalf("Failed to open config file: %s", err)
}
defer file.Close()
config := Configuration{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
split := strings.SplitN(line, "=", 2) // Split at the first "="
if len(split) != 2 {
log.Fatalf("Invalid line in config: %s", line)
}
key, value := split[0], split[1]
switch key {
case "tz":
config.Tz = value
case "token":
config.Token = value
case "start_date":
loc, _ := time.LoadLocation(config.Tz)
config.StartDate, err = time.ParseInLocation("2006-01-02 15:04:05", value, loc)
if err != nil {
log.Fatalf("Failed to parse start_date: %s", err)
}
default:
log.Printf("Unknown key: %s", key)
}
}
if err := scanner.Err(); err != nil {
log.Fatalf("Error reading config file: %s", err)
}
for {
location, err := time.LoadLocation(config.Tz)
if err != nil {
fmt.Println("Error loading location:", err)
return
}
startDate := config.StartDate
currentDate := time.Now().In(location)
log.Printf("Current date: %v Start date: %v", currentDate, startDate)
duration := currentDate.Sub(startDate)
log.Printf("Duration: %v", duration)
days := int(duration.Hours()/24) + 1
log.Printf("Days: %d", days)
formattedOutput := fmt.Sprintf("Day %d", days)
err = ioutil.WriteFile("days.txt", []byte(formattedOutput), 0644)
if err != nil {
log.Printf("Error writing to file: %v", err)
}
url := "http://www.acinfinityserver.com/api/user/devInfoListAll?userId=" + config.Token
reqBody := bytes.NewBuffer([]byte(""))
req, err := http.NewRequest("POST", url, reqBody)
if err != nil {
log.Printf("Error creating request: %v", err)
time.Sleep(15 * time.Second)
continue
}
req.Header.Add("token", config.Token)
req.Header.Add("Host", "www.acinfinityserver.com")
req.Header.Add("User-Agent", "okhttp/3.10.0")
req.Header.Add("Content-Encoding", "gzip")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("Error sending request: %v", err)
time.Sleep(15 * time.Second)
continue
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Error reading response body: %v", err)
time.Sleep(15 * time.Second)
continue
}
var jsonResponse Response
err = json.Unmarshal(respBody, &jsonResponse)
if err != nil {
log.Printf("Error unmarshalling JSON: %v", err)
time.Sleep(15 * time.Second)
continue
}
if len(jsonResponse.Data) > 0 {
for _, deviceData := range jsonResponse.Data {
for _, sensor := range deviceData.DeviceInfo.Sensors {
if sensor.SensorType == 0 { //Inside Temp
value := float64(sensor.SensorData) / 100.0
formattedOutput := fmt.Sprintf("Temp: %.1f°", value)
err = ioutil.WriteFile("inside_temp.txt", []byte(formattedOutput), 0644)
if err != nil {
log.Printf("Error writing to file: %v", err)
}
} else if sensor.SensorType == 2 { //Inside Humidity
value := float64(sensor.SensorData) / 100.0
formattedOutput = fmt.Sprintf("Humidity: %.1f%%", value)
err = ioutil.WriteFile("inside_humidity.txt", []byte(formattedOutput), 0644)
if err != nil {
log.Printf("Error writing to file: %v", err)
}
} else if sensor.SensorType == 3 { //Inside VPD
value := float64(sensor.SensorData) / 100.0
formattedOutput = fmt.Sprintf("VPD: %.1f", value)
err = ioutil.WriteFile("vpd.txt", []byte(formattedOutput), 0644)
if err != nil {
log.Printf("Error writing to file: %v", err)
}
} else if sensor.SensorType == 4 { //Outside Temp
value := float64(sensor.SensorData) / 100.0
formattedOutput := fmt.Sprintf("Temp: %.1f°", value)
err = ioutil.WriteFile("outside_temp.txt", []byte(formattedOutput), 0644)
if err != nil {
log.Printf("Error writing to file: %v", err)
}
} else if sensor.SensorType == 6 { //Outside Humidity
value := float64(sensor.SensorData) / 100.0
formattedOutput = fmt.Sprintf("Humidity: %.1f%%", value)
err = ioutil.WriteFile("outside_humidity.txt", []byte(formattedOutput), 0644)
if err != nil {
log.Printf("Error writing to file: %v", err)
}
/*} else if sensor.SensorType == 7 { //Outside VPD
value := float64(sensor.SensorData) / 100.0
formattedOutput = fmt.Sprintf("VPD: %.1f", value)
err = ioutil.WriteFile("outside_vpd.txt", []byte(formattedOutput), 0644)
if err != nil {
log.Printf("Error writing to file: %v", err)
}*/
}
}
for _, port := range deviceData.DeviceInfo.Ports {
if port.Online == 1 {
formattedOutput = fmt.Sprintf("%s: %d%%", port.PortName, port.Speak*10)
err = ioutil.WriteFile("port_"+strconv.Itoa(port.Port)+".txt", []byte(formattedOutput), 0644)
if err != nil {
log.Printf("Error writing to file: %v", err)
}
} else {
formattedOutput = "Offline"
err = ioutil.WriteFile("port_"+strconv.Itoa(port.Port)+".txt", []byte(formattedOutput), 0644)
if err != nil {
log.Printf("Error writing to file: %v", err)
}
}
}
}
log.Printf("Sleeping for 15 seconds", config.UpdateInterval)
time.Sleep(15 * time.Second)
log.Printf("Waking up")
}
}
}