From 7673cf34250d7b32bde01df0190c1efd201f47c1 Mon Sep 17 00:00:00 2001 From: Supan Adit Pratama Date: Sun, 20 Oct 2019 10:34:17 +0700 Subject: [PATCH] Some Change --- main.go | 44 +++++++++++++++++++-------- model/Location.go | 8 +++++ model/{ => tile38}/tile38data.go | 2 +- model/{ => tile38}/tile38object.go | 2 +- model/tile38/tile38subobject.go | 48 ++++++++++++++++++++++++++++++ model/tile38subobject.go | 14 --------- 6 files changed, 89 insertions(+), 29 deletions(-) create mode 100644 model/Location.go rename model/{ => tile38}/tile38data.go (98%) rename model/{ => tile38}/tile38object.go (87%) create mode 100644 model/tile38/tile38subobject.go delete mode 100644 model/tile38subobject.go diff --git a/main.go b/main.go index 3fccc89..4d5f630 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,14 @@ package main import ( + "encoding/json" "fmt" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/go-redis/redis" socketio "github.com/googollee/go-socket.io" "github.com/supanadit/geosmartsystem/model" + "github.com/supanadit/geosmartsystem/model/tile38" "log" ) @@ -17,17 +19,18 @@ func main() { }) router := gin.Default() - router.Use(cors.New(cors.Config{ - AllowOrigins: []string{"http://localhost:4200"}, - AllowMethods: []string{"PUT", "PATCH", "POST", "GET"}, - AllowHeaders: []string{"Origin"}, - ExposeHeaders: []string{"Content-Length"}, - AllowWebSockets: true, - AllowCredentials: true, - AllowWildcard: true, - })) - router.GET("/point", func(c *gin.Context) { - data, _ := model.FromScan(client, "sales") + //router.Use(cors.New(cors.Config{ + // AllowOrigins: []string{"http://localhost:4200"}, + // AllowMethods: []string{"PUT", "PATCH", "POST", "GET"}, + // AllowHeaders: []string{"Origin"}, + // ExposeHeaders: []string{"Content-Length"}, + // AllowWebSockets: true, + // AllowCredentials: true, + // AllowWildcard: true, + //})) + router.Use(cors.Default()) + router.GET("/points", func(c *gin.Context) { + data, _ := tile38.FromScan(client, "sales") c.JSON(200, data) }) // Socket.IO Start @@ -38,10 +41,25 @@ func main() { server.OnConnect("/", func(s socketio.Conn) error { s.SetContext("") fmt.Println("Connected:", s.ID()) + data, _ := tile38.FromScan(client, "sales") + s.Emit("points", data) return nil }) - server.OnEvent("/", "message", func(s socketio.Conn, msg string) { - s.Emit("message", "have "+msg) + server.OnEvent("/", "set-points", func(s socketio.Conn, msg string) { + var location model.Location + err = json.Unmarshal([]byte(msg), &location) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(location) + data, err := tile38.GetDataLocation(client, "sales", location.Id) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(data) + } + + } }) server.OnEvent("/", "bye", func(s socketio.Conn) string { last := s.Context().(string) diff --git a/model/Location.go b/model/Location.go new file mode 100644 index 0000000..3220d03 --- /dev/null +++ b/model/Location.go @@ -0,0 +1,8 @@ +package model + +type Location struct { + Id string `json:"id"` + Type string `json:"type"` + Lat string `json:"lat"` + Lng string `json:"lng"` +} diff --git a/model/tile38data.go b/model/tile38/tile38data.go similarity index 98% rename from model/tile38data.go rename to model/tile38/tile38data.go index 97a9e5a..a68d9ac 100644 --- a/model/tile38data.go +++ b/model/tile38/tile38data.go @@ -1,4 +1,4 @@ -package model +package tile38 import ( "encoding/json" diff --git a/model/tile38object.go b/model/tile38/tile38object.go similarity index 87% rename from model/tile38object.go rename to model/tile38/tile38object.go index 6257248..5185adc 100644 --- a/model/tile38object.go +++ b/model/tile38/tile38object.go @@ -1,4 +1,4 @@ -package model +package tile38 type Tile38Object struct { Id string `json:"id"` diff --git a/model/tile38/tile38subobject.go b/model/tile38/tile38subobject.go new file mode 100644 index 0000000..fd5e66a --- /dev/null +++ b/model/tile38/tile38subobject.go @@ -0,0 +1,48 @@ +package tile38 + +import ( + "encoding/json" + "fmt" + "github.com/go-redis/redis" + "github.com/supanadit/geosmartsystem/model" + "strconv" +) + +type Tile38SubObject struct { + Type string `json:"type"` + Coordinates []float64 `json:"coordinates"` +} + +func (tile38SubObject Tile38SubObject) Lng() float64 { + return tile38SubObject.Coordinates[0] +} + +func (tile38SubObject Tile38SubObject) Lat() float64 { + return tile38SubObject.Coordinates[1] +} + +func FromLocation(location model.Location) Tile38SubObject { + var tile38SubObject Tile38SubObject + tile38SubObject.Type = "Point" + lng, _ := strconv.ParseFloat(location.Lng, 64) + lat, _ := strconv.ParseFloat(location.Lat, 64) + tile38SubObject.Coordinates = []float64{ + lng, + lat, + } + return tile38SubObject +} + +func GetDataLocation(client *redis.Client, typeLocation string, id string) (Tile38Object, error) { + var tile38Object Tile38Object + var err error + data, err := client.Do("GET", typeLocation, id).Result() + if err == nil { + dataMarshal, err := json.Marshal(data) + if err == nil { + fmt.Println(string(dataMarshal)) + err = json.Unmarshal(dataMarshal, &tile38Object) + } + } + return tile38Object, err +} diff --git a/model/tile38subobject.go b/model/tile38subobject.go deleted file mode 100644 index c3ad259..0000000 --- a/model/tile38subobject.go +++ /dev/null @@ -1,14 +0,0 @@ -package model - -type Tile38SubObject struct { - Type string `json:"type"` - Coordinates []float64 `json:"coordinates"` -} - -func (tile38SubObject Tile38SubObject) Lng() float64 { - return tile38SubObject.Coordinates[0] -} - -func (tile38SubObject Tile38SubObject) Lat() float64 { - return tile38SubObject.Coordinates[1] -}