/
devices.go
257 lines (222 loc) · 5.92 KB
/
devices.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package api
import (
"encoding/json"
"fmt"
device2 "github.com/fsuhrau/automationhub/device"
"github.com/fsuhrau/automationhub/hub/action"
"github.com/fsuhrau/automationhub/storage/models"
"github.com/fsuhrau/automationhub/tester/unity"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
"net"
"net/http"
"strconv"
"strings"
"time"
)
func osFromPlatformType(t models.PlatformType) string {
switch t {
case models.PlatformTypeiOS:
return "iPhone OS"
case models.PlatformTypeAndroid:
return "android"
case models.PlatformTypeMac:
return "Mac OS X"
case models.PlatformTypeWindows:
return "windows"
case models.PlatformTypeLinux:
return "linux"
case models.PlatformTypeWeb:
return "web"
case models.PlatformTypeEditor:
return "editor"
}
return ""
}
func (s *Service) getDevices(c *gin.Context, project *models.Project) {
p := c.Query("platform")
var devices []models.Device
query := s.db
if p != "" {
platform, _ := strconv.ParseInt(p, 10, 64)
query = query.Where("os = ?", osFromPlatformType(models.PlatformType(platform)))
}
if err := query.Find(&devices).Error; err != nil {
s.error(c, http.StatusNotFound, err)
return
}
for i := range devices {
dev := s.devicesManager.GetDevice(devices[i].DeviceIdentifier)
devices[i].Dev = dev
devices[i].Status = device2.StateUnknown
if dev != nil {
devices[i].Status = dev.DeviceState()
if dev.Connection() != nil {
devices[i].Connection = dev.Connection().ConnectionParameter
}
}
}
c.JSON(http.StatusOK, devices)
}
func (s *Service) registerDevices(msg []byte, conn *websocket.Conn, c *gin.Context) {
type Request struct {
Type string
DeviceID string
IP string
Version string
OS string
Name string
}
clientIp := net.ParseIP(c.ClientIP())
var req Request
json.Unmarshal(msg, &req)
register := device2.RegisterData{
DeviceOSVersion: req.Version,
Name: req.Name,
DeviceOS: req.OS,
DeviceID: req.DeviceID,
ManagerType: req.Type,
DeviceIP: clientIp,
Conn: conn,
}
s.devicesManager.RegisterDevice(register)
}
func (s *Service) getDevice(c *gin.Context, project *models.Project) {
deviceID := c.Param("device_id")
_ = deviceID
var device models.Device
if err := s.db.Preload("ConnectionParameter").Preload("Parameter").Find(&device, "id = ?", deviceID).Error; err != nil {
s.error(c, http.StatusNotFound, err)
return
}
dev := s.devicesManager.GetDevice(device.DeviceIdentifier)
device.Dev = dev
device.Status = device2.StateUnknown
if dev != nil {
device.Status = dev.DeviceState()
if dev.Connection() != nil {
device.Connection = dev.Connection().ConnectionParameter
}
}
c.JSON(http.StatusOK, device)
}
func (s *Service) deleteDevice(c *gin.Context, project *models.Project) {
deviceID := c.Param("device_id")
var device models.Device
if err := s.db.First(&device, deviceID).Error; err != nil {
s.error(c, http.StatusInternalServerError, err)
return
}
if err := s.db.Delete(&device).Error; err != nil {
s.error(c, http.StatusInternalServerError, err)
return
}
c.Status(http.StatusOK)
}
func (s *Service) updateDevice(c *gin.Context, project *models.Project) {
deviceID := c.Param("device_id")
var dev models.Device
c.Bind(&dev)
var device models.Device
if err := s.db.Preload("ConnectionParameter").Preload("Parameter").First(&device, deviceID).Error; err != nil {
s.error(c, http.StatusInternalServerError, err)
return
}
var ids []uint
for i := range device.Parameter {
exists := false
for d := range dev.Parameter {
if device.Parameter[i].Key == dev.Parameter[d].Key {
exists = true
break
}
}
if !exists {
ids = append(ids, device.Parameter[i].ID)
}
}
if len(ids) > 0 {
if err := s.db.Delete(&models.DeviceParameter{}, "id in (?)", ids).Error; err != nil {
s.error(c, http.StatusInternalServerError, err)
return
}
}
if err := s.db.Updates(&dev).Error; err != nil {
s.error(c, http.StatusInternalServerError, err)
return
}
c.Status(http.StatusOK)
}
func (s *Service) deviceRunTests(c *gin.Context, project *models.Project) {
type Request struct {
TestName string
Env string
}
type Response struct {
Success bool
Message string
}
var req Request
c.Bind(&req)
arr := strings.Split(req.TestName, " ")
if len(arr) != 2 {
s.error(c, http.StatusNotFound, fmt.Errorf("invalid method signature"))
return
}
deviceID := c.Param("device_id")
_ = deviceID
var device models.Device
if err := s.db.Find(&device, "id = ?", deviceID).Error; err != nil {
s.error(c, http.StatusNotFound, err)
return
}
dev := s.devicesManager.GetDevice(device.DeviceIdentifier)
if dev == nil {
s.error(c, http.StatusNotFound, fmt.Errorf("real device not found"))
return
}
if err := dev.Lock(); err != nil {
s.error(c, http.StatusNotFound, err)
return
}
defer func() {
_ = dev.Unlock()
}()
if !dev.IsAppConnected() {
s.error(c, http.StatusNotFound, fmt.Errorf("no app connected"))
return
}
if false {
var testsAction action.TestsGet
s.devicesManager.SendAction(dev, &testsAction)
logrus.Info("tests %v", testsAction)
time.Sleep(2 * time.Minute)
for _, test := range testsAction.Tests {
runTestAction := action.TestStart{
Assembly: test.Assembly,
Class: test.Class,
Method: test.Method,
}
s.devicesManager.SendAction(dev, &runTestAction)
time.Sleep(3 * time.Minute)
}
return
}
reset := action.UnityReset{}
s.devicesManager.SendAction(dev, &reset)
envParams := extractParams(req.Env)
runTestAction := action.TestStart{
Class: arr[0],
Method: arr[1],
Env: envParams,
}
executor := unity.NewExecutor(s.devicesManager)
if err := executor.Execute(dev, runTestAction, 5*time.Minute); err != nil {
logrus.Errorf("Execute failed: %v", err)
s.error(c, http.StatusInternalServerError, err)
return
}
logrus.Debug("Execution Finished")
c.JSON(http.StatusOK, &Response{true, "Execution Finished"})
}