dataRT/route/handler.go

136 lines
2.4 KiB
Go

package route
import (
"datart/data/influx"
"strconv"
"github.com/gin-gonic/gin"
)
func getLast(ctx *gin.Context) {
// station := ctx.DefaultQuery("station", "0")
// component := ctx.DefaultQuery("component", "0")
point := ctx.DefaultQuery("point", "")
request := &influx.Request{
RespType: "csv",
Database: "influxBucket",
Measure: "samples",
Station: "TJSH_FZ001",
Device: "DDJ001",
Field: "",
}
switch point {
case "i":
request.Field = "channel_1"
case "v":
request.Field = "channel_2"
default:
ctx.JSON(200, gin.H{
"code": 1,
"msg": "invalid point",
})
return
}
data, err := influx.GetLast(ctx, request)
if err != nil {
ctx.JSON(200, gin.H{
"code": 2,
"msg": err.Error(),
})
return
}
ctx.JSON(200, gin.H{
"code": 0,
"msg": "",
"data": data,
})
}
func getPointData(ctx *gin.Context) {
// station := ctx.DefaultQuery("station", "0")
// component := ctx.DefaultQuery("component", "0")
request := &influx.Request{
RespType: "csv",
Database: "influxBucket",
Measure: "samples",
Station: "TJSH_FZ001",
Device: "DDJ001",
Field: "",
Begin: 0,
End: 0,
Operate: ctx.DefaultQuery("operate", ""),
Step: ctx.DefaultQuery("step", ""),
Default: ctx.DefaultQuery("default", ""),
}
point := ctx.DefaultQuery("point", "")
switch point {
case "i":
request.Field = "channel_1"
case "v":
request.Field = "channel_2"
default:
ctx.JSON(200, gin.H{
"code": 1,
"msg": "invalid point",
})
return
}
var err error
begin := ctx.DefaultQuery("begin", "")
end := ctx.DefaultQuery("end", "")
if begin != "" {
request.Begin, err = strconv.ParseInt(begin, 10, 64)
if err != nil {
ctx.JSON(200, gin.H{
"code": 1,
"msg": "invalid begin",
})
return
}
}
if end != "" {
request.End, err = strconv.ParseInt(end, 10, 64)
if err != nil {
ctx.JSON(200, gin.H{
"code": 1,
"msg": "invalid end",
})
return
}
}
var data []*influx.TV
switch {
case begin != "" && end != "":
data, err = influx.GetPointData(ctx, request)
case begin != "":
data, err = influx.GetAfterOne(ctx, request)
case end != "":
data, err = influx.GetBeforeOne(ctx, request)
default:
data, err = influx.GetLast(ctx, request)
}
if err != nil {
ctx.JSON(200, gin.H{
"code": 2,
"msg": err.Error(),
})
return
}
ctx.JSON(200, gin.H{
"code": 0,
"msg": "",
"data": data,
})
}