Skip to content

Commit

Permalink
integrate with go dep
Browse files Browse the repository at this point in the history
  • Loading branch information
intaniger committed Dec 21, 2018
1 parent b4cccff commit d30ecd0
Show file tree
Hide file tree
Showing 24 changed files with 263 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ main
.vscode
key.rsa.pub
key.rsa
mongodb/
mongodb/
vendor/
Empty file modified Dockerfile
100644 → 100755
Empty file.
121 changes: 121 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Gopkg.toml
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 modified README.md
100644 → 100755
Empty file.
Empty file modified api/device/control.go
100644 → 100755
Empty file.
Empty file modified api/device/greeting.go
100644 → 100755
Empty file.
Empty file modified api/device/index.go
100644 → 100755
Empty file.
Empty file modified api/middleware/login.go
100644 → 100755
Empty file.
Empty file modified api/middleware/ownerCheck.go
100644 → 100755
Empty file.
20 changes: 0 additions & 20 deletions api/user/index.go

This file was deleted.

2 changes: 1 addition & 1 deletion api/user/register.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/gin-gonic/gin"
)

func register(c *gin.Context) {
func Register(c *gin.Context) {
mdb, err := common.Mongo()
if common.PrintError(err) {
c.JSON(500, "error")
Expand Down
Empty file modified api/user/userDevice.go
100644 → 100755
Empty file.
96 changes: 96 additions & 0 deletions api/user/websocket.go
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 modified common/common.go
100644 → 100755
Empty file.
Empty file modified config.txt
100644 → 100755
Empty file.
Empty file modified data-scheme.jsonc
100644 → 100755
Empty file.
Empty file modified docker-compose.yml
100644 → 100755
Empty file.
1 change: 0 additions & 1 deletion main.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ func main() {

common.InitializeKeyPair()

// go router.MQTT()
r := gin.Default()
middleware.Initialize()

Expand Down
Empty file modified model/user.go
100644 → 100755
Empty file.
Empty file modified reminder.md
100644 → 100755
Empty file.
Empty file modified router/MQTT.go
100644 → 100755
Empty file.
11 changes: 9 additions & 2 deletions router/httpAPI.go
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package router

import (
"../api/middleware"
"../api/user"
"../common"
"github.com/gin-gonic/gin"
Expand All @@ -10,6 +11,12 @@ func SetUpHttpAPI(r *gin.Engine) {

common.ShouldPrintDebug = true
httpAPI := r.Group("api/v1")
// device.DeviceControlAPI(httpAPI)
user.SetUpUserAPI(httpAPI)
// define short name
userAuth := middleware.UserAuth.MiddlewareFunc()
// ownerCheck := middleware.OwnerCheck
{
httpAPI.POST("/login", middleware.UserAuth.LoginHandler)
httpAPI.POST("/register", user.Register)
httpAPI.POST("/ws", userAuth /* WS*/)
}
}
Empty file modified test/example.go
100644 → 100755
Empty file.

0 comments on commit d30ecd0

Please sign in to comment.