-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
193 lines (144 loc) · 3.87 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
"syscall"
"time"
"github.com/mitchellh/mapstructure"
)
func main() {
var uname syscall.Utsname
if err := syscall.Uname(&uname); err != nil {
fmt.Printf("Uname: %v", err)
}
println(cmdTitle)
print("Listen in port : ")
println("8080")
print("OS : ")
println(arrayToString(uname.Sysname))
print("Kernel : ")
println(arrayToString(uname.Release))
print("Arch : ")
println(arrayToString(uname.Machine))
println("\n███████\n")
InitController()
InitWebsocketServer()
go notifyRelayStatus()
http.HandleFunc("/", handleRequest)
http.HandleFunc("/ws", websocketRequest)
http.ListenAndServe(":8080", nil)
}
func handleRequest(w http.ResponseWriter, r *http.Request) {
bodyContent := []byte(indexHTML)
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
w.Write(bodyContent)
}
func websocketRequest(w http.ResponseWriter, r *http.Request) {
wsServer := GetWebsocketServerInstance()
idConnection, _ := wsServer.AddConnection(w, r, wsHandleRequest)
controller := GetControllerInstance()
relay1, relay2, relay3, relay4 := controller.GetAllRelaysStatus()
status := RelaysStatus{
Relay1Status: relay1,
Relay2Status: relay2,
Relay3Status: relay3,
Relay4Status: relay4,
}
bodyBytes := prepareMessageToSocket("INITIAL_RELAY_STATUS", status)
print("Server message ==> ")
println(string(bodyBytes))
wsServer.SendMessage(bodyBytes, idConnection)
}
func wsHandleRequest(body []byte) {
print("Client message ==> ")
println(string(body))
var clientMessage []interface{}
dec := json.NewDecoder(bytes.NewReader(body))
err := dec.Decode(&clientMessage)
if err != nil {
return
}
messageTopic, ok := clientMessage[0].(string)
if !ok {
return
}
switch messageTopic {
case "SET_VALUE_RELAY_COMMAND":
var setValueRelayCommand SetValueRelayCommand
mapstructure.Decode(clientMessage[1], &setValueRelayCommand)
relayInstance := GetControllerInstance()
switch setValueRelayCommand.RelayNumber {
case 1:
relayInstance.SetRelay1Status(setValueRelayCommand.RelayValue)
break
case 2:
relayInstance.SetRelay2Status(setValueRelayCommand.RelayValue)
break
case 3:
relayInstance.SetRelay3Status(setValueRelayCommand.RelayValue)
break
case 4:
relayInstance.SetRelay4Status(setValueRelayCommand.RelayValue)
break
default:
return
}
break
default:
return
}
}
func notifyRelayStatus() {
controller := GetControllerInstance()
wsServer := GetWebsocketServerInstance()
for status := range controller.changeStatusChannel {
messageStatus := RelayStatusChange{
RelayNumber: status.relayNumber,
RelayNewValue: status.relayNewValue,
ChangedAt: status.changedAt,
}
bodyBytes := prepareMessageToSocket("NOTIFY_RELAY_STATUS", messageStatus)
print("Server message ==> ")
println(string(bodyBytes))
wsServer.BroadcastMessage(bodyBytes)
}
}
func prepareMessageToSocket(topic string, data interface{}) []byte {
message := make([]interface{}, 0, 2)
message = append(message, topic, data)
jsonBytes, _ := json.Marshal(message)
return jsonBytes
}
// RelaysStatus struct
type RelaysStatus struct {
Relay1Status bool `json:"relay1Status"`
Relay2Status bool `json:"relay2Status"`
Relay3Status bool `json:"relay3Status"`
Relay4Status bool `json:"relay4Status"`
}
// RelayStatusChange struct
type RelayStatusChange struct {
RelayNumber int `json:"relayNumber"`
RelayNewValue bool `json:"relayNewValue"`
ChangedAt time.Time `json:"changedAt"`
}
// SetValueRelayCommand struct
type SetValueRelayCommand struct {
RelayNumber int `json:"relayNumber"`
RelayValue bool `json:"relayValue"`
}
func arrayToString(x [65]int8) string {
var buf [65]byte
for i, b := range x {
buf[i] = byte(b)
}
str := string(buf[:])
if i := strings.Index(str, "\x00"); i != -1 {
str = str[:i]
}
return str
}