-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
202 lines (185 loc) · 5.66 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
package main
import (
"fmt"
"time"
"net/http"
"log"
"strings"
"encoding/json"
"os"
"bytes"
"strconv"
"github.com/tarm/serial"
"github.com/gorilla/mux"
)
type serManager struct {
SerialPort *serial.Port
}
type SlotPower struct {
I2CAddress int `json:"i2caddress"`
I2CSlot int `json:"i2cslot"`
PowerStatus int `json:"powercon"`
}
type SlotAddress struct {
I2CAddress int `json:"i2ca"`
I2CSlot int `json:"i2cs"`
}
type SlotInfo struct {
PowerStatus int `json:"ps"`
AlwaysOn int `json:"ao"`
CaddyType int `json:"ct"`
}
type ExceptionE struct {
Error string `json:"error"`
}
type Acceptance struct {
Success string `json:"success"`
}
var bushwodServer = ""
var bushwoodToken = ""
func main() {
/* grab the details for the bushwod server so we can talk to it */
bushwodServer = os.Getenv("BUSHWOOD_SERVER")
bushwoodToken = os.Getenv("BUSHWOOD_TOKEN")
serialDevice := os.Getenv("SERIAL_DEVICE")
serialDeviceBaud := os.Getenv("SERIAL_DEVICE_BAUD")
if bushwodServer == "" {
log.Fatalln("BUSHWOOD_SERVER env var not set")
}
if bushwoodToken == "" {
log.Fatalln("BUSHWOOD_TOKEN env var not set")
}
if serialDevice == "" {
log.Fatalln("SERIAL_DEVICE env var not set")
}
if serialDeviceBaud == "" {
log.Fatalln("SERIAL_DEVICE_BAUD env var not set")
}
/* create the serial manager to start the serial port communication */
baudRate, _ := strconv.Atoi(serialDeviceBaud)
serman, err := NewSerialManager(serialDevice, baudRate)
if err != nil {
log.Println(err)
}
/* start reading from port event handler */
go readSer(serman.SerialPort, err)
/* start the api */
r := mux.NewRouter()
r.HandleFunc("/api/v1/power/{i2cAddress}/{i2cSlot}/{powerStatus}", serman.sentToSer).Methods("GET")
log.Println("Ready to check the backplanes!")
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", 8080), r))
serman.SerialPort.Close()
}
/* function to send data to the serial port */
func (sm *serManager) sentToSer(w http.ResponseWriter, r *http.Request) {
urlvars := mux.Vars(r)
i2cAddress, _ := strconv.Atoi(urlvars["i2cAddress"])
i2cSlot, _ := strconv.Atoi(urlvars["i2cSlot"])
powerStatus, _ := strconv.Atoi(urlvars["powerStatus"])
//i2cAddress := urlvars["i2cAddress"]
//i2cSlot := urlvars["i2cSlot"]
//powerStatus := urlvars["powerStatus"]
jsonData := SlotPower{
I2CAddress: i2cAddress,
I2CSlot: i2cSlot,
PowerStatus: powerStatus,
}
result, err2 := json.Marshal(jsonData)
if err2 != nil {
log.Println(err2)
}
_, err := sm.SerialPort.Write([]byte(string(result)+"\n"))
if err != nil {
log.Println(err)
}
log.Println("result write: "+string(result))
//w.Write([]byte(string(result)+"\n"))
json.NewEncoder(w).Encode(Acceptance{Success: fmt.Sprintf("sent command")})
}
/* create serial manager */
func NewSerialManager(serialDevice string, baudRate int) (*serManager, error) {
c := &serial.Config{Name: serialDevice, Baud: baudRate, ReadTimeout: time.Millisecond * 50}
s, err := serial.OpenPort(c)
if err != nil {
log.Println(err)
}
newInv := &serManager{
SerialPort: s,
}
return newInv, nil
}
/* function to read data from serial port */
func readSer(s *serial.Port, err error) {
for {
buf := make([]byte, 40)
var content []byte
for {
n, err := s.Read(buf)
if err != nil {
//need to fix this so it stops spewing "EOF" to screen
if err.Error() != "EOF" {
log.Println(err)
}
}
if n == 0 {
break
}
content = append(content, buf[:n]...)
}
/* check the content we get from the serial port */
if len(content) != 0 {
/* FIXME do better checking of the content to perform the right functions */
contents := strings.Split(string(content), "\n")
for b := range contents {
if contents[b] != "" {
if strings.Contains(string(contents[b]), "{") {
log.Println("result read: "+strings.TrimSpace(string(contents[b])))
slotAddress := SlotAddress{}
if err := json.Unmarshal([]byte(strings.TrimSpace(string(contents[b]))), &slotAddress); err != nil {
//fmt.Println("err1")
log.Println(err)
}
slotInfo := SlotInfo{}
if err := json.Unmarshal([]byte(strings.TrimSpace(string(contents[b]))), &slotInfo); err != nil {
//fmt.Println("err2")
log.Println(err)
}
var netClient = &http.Client{
Timeout: time.Second * 10,
}
token := bushwoodToken
result, err2 := json.Marshal(slotInfo)
if err2 != nil {
log.Println(err2)
}
i2ca := strconv.Itoa(slotAddress.I2CAddress)
i2cs := strconv.Itoa(slotAddress.I2CSlot)
req, _ := http.NewRequest("POST", bushwodServer+"/api/v1/caddydata/i2c/" + i2ca + "/slot/" + i2cs, bytes.NewBuffer(result))
req.Header.Add("apikey", token)
req.Header.Set("Content-Type", "application/json")
resp, resperr := netClient.Do(req)
/* FIXME Check for connection! */
if resperr != nil {
log.Println(resperr)
} else {
if check200(resp.StatusCode) {
//fmt.Println("Successful update")
log.Println("Successful update")
}
resp.Body.Close()
}
} else {
log.Println("not json output: "+strings.TrimSpace(string(contents[b])))
}
}
}
}
}
}
func check200(httpcode int) bool {
if httpcode == 200 {
return true
} else {
return false
}
}