yats.git

ref: e6ce5ae3b106649644dc07173edf7534baa9e533

server/rest/rest-position.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
package rest

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"strings"
	"time"
	"yats-server/config"
	"yats-server/db"
	"yats-server/model"
)

// WritePosition godoc
// @Param X-SSL-Client-CN header string true "clientCN"
// @Summary write position to db
// @Schemes
// @Description store position
// @Tags Positions
// @Accept json
// @Param position body model.PositionModel true "Position body data"
// @Produce json
// @Success 202 {string} WriteEvent
// @Router /position [post]
func WritePosition(cfg config.Configuration) gin.HandlerFunc {
	return func(c *gin.Context) {
		var position model.PositionModel

		if err := c.BindJSON(&position); err != nil {
			c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"})
			return
		}

		clientCN := GetClientCN(c, cfg)
		fmt.Printf("%s / %s / %f /%f", clientCN, position.Name, position.Lat, position.Lon)
		if position.Ptime == 0 {
			db.SavePosition(clientCN, position.Lat, position.Lon, position.Name)

		} else {
			unixTimeUTC := time.Unix(position.Ptime, 0)
			db.SavePositionAt(clientCN, position.Lat, position.Lon, unixTimeUTC, position.Name)
		}
		c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK"})
	}
}

// SearchPositions  godoc
// @Param X-SSL-Client-CN header string true "clientCN"
// @Summary Get the positions From ... and To when present
// @Schemes
// @Description retrieve positions in the specified time window
// @Tags Positions
// @Accept json
// @Param position body model.PositionSearchRequest true "Position query filters"
// @Produce json
// @Success 200 {string} SearchPositions
// @Router /position/search [post]
func SearchPositions(cfg config.Configuration) gin.HandlerFunc {
	return func(c *gin.Context) {
		var position model.PositionSearchRequest

		if err := c.BindJSON(&position); err != nil {
			c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"})
			return
		}

		clientCN := GetClientCN(c, cfg)
		fmt.Printf("Client ID: %s\n ", clientCN)

		if position.SourceApplication != "" {
			if !db.CanReadSourcePosition(clientCN, position.SourceApplication) {
				c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"})
				return
			}
			clientCN = position.SourceApplication
		}

		var eventsPack []model.EventModel
		if position.To == 0 {
			unixTimeUTC := time.Unix(position.From, 0)
			timeString := unixTimeUTC.UTC().String()
			eventsPack = db.EventsFrom(db.Session, clientCN, timeString, 100)
		} else if position.From != 0 {
			fromUnixTimeUTC := time.Unix(position.From, 0)
			toUnixTimeUTC := time.Unix(position.To, 0)
			fromString := fromUnixTimeUTC.UTC().String()
			toString := toUnixTimeUTC.UTC().String()
			eventsPack = db.EventsBetween(db.Session, clientCN, fromString, toString, 100)
		}
		c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack})
	}
}

// GetPositionsFrom godoc
// @Param X-SSL-Client-CN header string true "clientCN"
// @Summary Get the positions from timestamp {from}
// @Schemes
// @Description retrieve positions  from timestamp {from}
// @Tags Positions
// @Accept json
// @Param position body model.PositionSearchRequest true "Event query filters"
// @Produce json
// @Success 200 {string} SearchPositions
// @Router /position/{from} [get]
// @Param from path string true "Starting from timestamp :from"
func GetPositionsFrom(cfg config.Configuration) gin.HandlerFunc {
	return func(c *gin.Context) {

		fromParam := c.Param("from")
		fromParamInt64, err := strconv.ParseInt(fromParam, 10, 64)
		if err != nil {
			c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"})
		}

		clientCN := GetClientCN(c, cfg)
		fmt.Printf("Client ID: %s\n ", clientCN)

		var eventsPack []model.EventModel

		unixTimeUTC := time.Unix(fromParamInt64, 0)
		timeString := unixTimeUTC.UTC().String()
		eventsPack = db.EventsFrom(db.Session, clientCN, strings.TrimSpace(timeString[0:19]), 100)

		c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack})
	}
}