Some Change

This commit is contained in:
Supan Adit Pratama 2019-10-20 10:34:17 +07:00
parent a5a5e459bd
commit 7673cf3425
6 changed files with 89 additions and 29 deletions

44
main.go
View File

@ -1,12 +1,14 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-redis/redis" "github.com/go-redis/redis"
socketio "github.com/googollee/go-socket.io" socketio "github.com/googollee/go-socket.io"
"github.com/supanadit/geosmartsystem/model" "github.com/supanadit/geosmartsystem/model"
"github.com/supanadit/geosmartsystem/model/tile38"
"log" "log"
) )
@ -17,17 +19,18 @@ func main() {
}) })
router := gin.Default() router := gin.Default()
router.Use(cors.New(cors.Config{ //router.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:4200"}, // AllowOrigins: []string{"http://localhost:4200"},
AllowMethods: []string{"PUT", "PATCH", "POST", "GET"}, // AllowMethods: []string{"PUT", "PATCH", "POST", "GET"},
AllowHeaders: []string{"Origin"}, // AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"}, // ExposeHeaders: []string{"Content-Length"},
AllowWebSockets: true, // AllowWebSockets: true,
AllowCredentials: true, // AllowCredentials: true,
AllowWildcard: true, // AllowWildcard: true,
})) //}))
router.GET("/point", func(c *gin.Context) { router.Use(cors.Default())
data, _ := model.FromScan(client, "sales") router.GET("/points", func(c *gin.Context) {
data, _ := tile38.FromScan(client, "sales")
c.JSON(200, data) c.JSON(200, data)
}) })
// Socket.IO Start // Socket.IO Start
@ -38,10 +41,25 @@ func main() {
server.OnConnect("/", func(s socketio.Conn) error { server.OnConnect("/", func(s socketio.Conn) error {
s.SetContext("") s.SetContext("")
fmt.Println("Connected:", s.ID()) fmt.Println("Connected:", s.ID())
data, _ := tile38.FromScan(client, "sales")
s.Emit("points", data)
return nil return nil
}) })
server.OnEvent("/", "message", func(s socketio.Conn, msg string) { server.OnEvent("/", "set-points", func(s socketio.Conn, msg string) {
s.Emit("message", "have "+msg) 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 { server.OnEvent("/", "bye", func(s socketio.Conn) string {
last := s.Context().(string) last := s.Context().(string)

8
model/Location.go Normal file
View File

@ -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"`
}

View File

@ -1,4 +1,4 @@
package model package tile38
import ( import (
"encoding/json" "encoding/json"

View File

@ -1,4 +1,4 @@
package model package tile38
type Tile38Object struct { type Tile38Object struct {
Id string `json:"id"` Id string `json:"id"`

View File

@ -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
}

View File

@ -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]
}