This repository has been archived by the owner on Oct 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
187 lines (157 loc) · 3.92 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
/*
* +===============================================
* | Author: Parham Alvani <[email protected]>
* |
* | Creation Date: 17-01-2018
* |
* | File Name: main.go
* +===============================================
*/
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/LightsPlatform/vActuator/actuator"
"github.com/LightsPlatform/vActuator/stateManager"
"encoding/json"
"github.com/LightsPlatform/vSensor/sensor"
)
var actuators map[string]*actuator.Actuator
// init initiates global variables
func init() {
actuators = make(map[string]*actuator.Actuator)
}
// handle registers apis and create http handler
func handle() http.Handler {
r := gin.Default()
api := r.Group("/api")
{
api.GET("/about", aboutHandler)
api.POST("/actuator/:id", actuatorCreateHandler)
api.POST("/actuator/:id/trigger", actuatorTriggerHandler)
api.GET("/actuator/:id/state", actuatorDataHandler)
api.GET("/actuator/", actuatorListHandler)
api.DELETE("/actuator/:id", acuatorDeleteHandler)
}
r.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{"error": "404 Not Found"})
})
return r
}
func acuatorDeleteHandler(c *gin.Context) {
log.Println("delete")
id := c.Param("id")
actuator, ok := actuators[id]
if !ok {
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("Sensor %s was not found on vSensor", id)})
return
}
actuator.Stop()
state := actuator.State
delete(actuators, id)
c.JSON(http.StatusOK, state)
}
func actuatorListHandler(c *gin.Context) {
output := make([]string, 0)
for _, actuator := range actuators {
output = append(output, actuator.Name)
}
c.JSON(http.StatusOK, output)
}
func actuatorTriggerHandler(c *gin.Context){
id := c.Param("id")
if actuators[id] == nil{
c.JSON(http.StatusNotFound, gin.H{
"error": "Actuator not found!",
})
return
}
action,_ := c.GetPostForm("action")
if(action == ""){
action = "null"
}
result := actuators[id].Trigger(action);
c.JSON(http.StatusOK,result)
}
func actuatorDataHandler(c *gin.Context){
id := c.Param("id")
if actuators[id] == nil{
c.JSON(http.StatusNotFound, gin.H{
"error": "Actuator not found!",
})
return
}
c.JSON(http.StatusOK, actuators[id].State)
}
func actuatorCreateHandler(c *gin.Context) {
id := c.Param("id")
code, ok := c.GetPostForm("code")
if ok == false {
c.JSON(http.StatusBadRequest, gin.H{
"error": "code must send!",
})
return
}
configData, ok := c.GetPostForm("config")
if ok == false {
c.JSON(http.StatusBadRequest, gin.H{
"error": "config must send!",
})
return
}
config := &stateManager.Config{
StateType: map[string][]string{},
}
error := json.Unmarshal([]byte(configData), config)
if error != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "config is not correct! ",
"description" : error.Error(),
})
return
}
actuator, error := actuator.New(id, []byte(code), *config)
if error != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": error.Error()})
return
}
if _, ok := actuators[id]; !ok {
go actuator.Run()
}
actuators[id] = actuator
c.String(http.StatusOK, id)
}
func main() {
fmt.Println("vActuator Light @ 2018")
srv := &http.Server{
Addr: ":8181",
Handler: handle(),
}
go func() {
fmt.Printf("vActuator Listen: %s\n", srv.Addr)
// service connections
if err := srv.ListenAndServe(); err != nil {
log.Fatal("Listen Error:", err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
fmt.Println("vActuator Shutdown")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Shutdown Error:", err)
}
}
func aboutHandler(c *gin.Context) {
c.String(http.StatusOK, "I'll keep Light in my heart ❤")
}