-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
263 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ main | |
.vscode | ||
key.rsa.pub | ||
key.rsa | ||
mongodb/ | ||
mongodb/ | ||
vendor/ |
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Gopkg.toml example | ||
# | ||
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html | ||
# for detailed Gopkg.toml documentation. | ||
# | ||
# required = ["github.com/user/thing/cmd/thing"] | ||
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project" | ||
# version = "1.0.0" | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project2" | ||
# branch = "dev" | ||
# source = "github.com/myfork/project2" | ||
# | ||
# [[override]] | ||
# name = "github.com/x/y" | ||
# version = "2.4.0" | ||
# | ||
# [prune] | ||
# non-go = false | ||
# go-tests = true | ||
# unused-packages = true | ||
|
||
|
||
[[constraint]] | ||
name = "github.com/appleboy/gin-jwt" | ||
version = "2.5.0" | ||
|
||
[prune] | ||
go-tests = true | ||
unused-packages = true |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package user | ||
|
||
import ( | ||
"encoding/json" | ||
"math/rand" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gin-contrib/sessions" | ||
"github.com/gin-gonic/gin" | ||
"github.com/gorilla/websocket" | ||
) | ||
|
||
var wsupgrader = websocket.Upgrader{ | ||
ReadBufferSize: 1024, | ||
WriteBufferSize: 1024, | ||
CheckOrigin: wsCheckOrigin, | ||
} | ||
|
||
// WebSocket : WebSocket request handling | ||
func WebSocket(c *gin.Context) { | ||
wsRouter(c.Writer, c.Request, sessions.Default(c)) | ||
} | ||
|
||
func wsRouter(w http.ResponseWriter, r *http.Request, userSession sessions.Session) { | ||
conn, err := wsupgrader.Upgrade(w, r, nil) | ||
if common.CheckErr(err) { | ||
return | ||
} | ||
common.NewWebsocketConnectionInfo(userSession, conn) | ||
previousMillisecond := time.Now().UnixNano() / int64(time.Millisecond) | ||
go func() { | ||
for { | ||
// ****************** Device Log websocket broadcast simulator ****************** | ||
if (time.Now().UnixNano()/int64(time.Millisecond))-previousMillisecond > 2000 { | ||
mockDeviceLog := gin.H{ | ||
"aX": rand.Float64() * 10, | ||
"aY": rand.Float64() * 10, | ||
"aZ": rand.Float64() * 10, | ||
"angleX": rand.Float64() * 90, | ||
"angleY": rand.Float64() * 90, | ||
"angleZ": rand.Float64() * 90, | ||
"gyroX": rand.Float64() * 10, | ||
"gyroY": rand.Float64() * 10, | ||
"gyroZ": rand.Float64() * 10, | ||
} | ||
jsonMsg, _ := json.Marshal(mockDeviceLog) | ||
common.BroadCastAll("deviceLog", string(jsonMsg)) | ||
println("simulated deviceLog") | ||
previousMillisecond = (time.Now().UnixNano() / int64(time.Millisecond)) | ||
} | ||
} | ||
}() | ||
for { | ||
t, msg, err := conn.ReadMessage() | ||
if common.CheckErr(err) { | ||
break | ||
} | ||
println("Message from client webSocket:", string(msg)) | ||
|
||
incomeCommand := common.WsCommand{} | ||
err = json.Unmarshal(msg, &incomeCommand) | ||
if !common.CheckErr(err) { | ||
switch incomeCommand.Event { | ||
case "throttle": | ||
err = Device.SetThrottle(incomeCommand.Payload) | ||
case "DeviceMotorSpeed": | ||
err = Device.SetMotor(incomeCommand.Payload) | ||
case "TurnDeviceCamLeft": | ||
err = Device.SetCamera("Left", incomeCommand.Payload) | ||
case "TurnDeviceCamRight": | ||
err = Device.SetCamera("Right", incomeCommand.Payload) | ||
case "GetDeviceStatus": | ||
common.BroadCastAll("DeviceStatus", Client.EarliestDevicePhysicalLog("HardCode101")) | ||
} | ||
|
||
} | ||
wsReponseStatus(incomeCommand.Event, err, conn, t) | ||
} | ||
} | ||
|
||
func wsCheckOrigin(r *http.Request) bool { | ||
return true | ||
} | ||
|
||
func wsReponseStatus(event string, err error, conn *websocket.Conn, msgType int) { | ||
callbackMessage := common.WsCommand{ | ||
Event: event, | ||
Payload: "success", | ||
} | ||
if err != nil { | ||
callbackMessage.Payload = err.Error() | ||
} | ||
jsonMsg, _ := json.Marshal(callbackMessage) | ||
conn.WriteMessage(msgType, jsonMsg) | ||
} |
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.