/
context.go
135 lines (120 loc) · 4.09 KB
/
context.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
128
129
130
131
132
133
134
135
package api
import (
"encoding/json"
"github.com/futurehomeno/fimpgo"
log "github.com/sirupsen/logrus"
"github.com/thingsplex/tpflow/model"
"time"
)
type ContextApi struct {
ctx *model.Context
msgTransport *fimpgo.MqttTransport
}
func NewContextApi(ctx *model.Context) *ContextApi {
ctxApi := ContextApi{ctx: ctx}
//ctxApi.RegisterRestApi()
return &ctxApi
}
//func (ctx *ContextApi) RegisterRestApi() {
// ctx.echo.GET("/fimp/api/flow/context/:flowid", func(c echo.Context) error {
// id := c.Param("flowid")
// if id != "-" {
// result := ctx.ctx.GetRecords(id)
// return c.JSON(http.StatusOK, result)
// }
// var result []model.ContextRecord
// return c.JSON(http.StatusOK, result)
// })
//
// ctx.echo.POST("/fimp/api/flow/context/record/:flowid", func(c echo.Context) error {
// flowId := c.Param("flowid")
// body, err := ioutil.ReadAll(c.Request().Body)
// if err != nil {
// return err
// }
// rec := model.ContextRecord{}
// rec.UpdatedAt = time.Now()
// err = json.Unmarshal(body, &rec)
// if err != nil {
// log.Error("<ContextApi> Can't unmarshal context record.")
// return err
// }
// ctx.ctx.PutRecord(&rec, flowId, false)
//
// return c.JSON(http.StatusOK, rec)
// })
//
// ctx.echo.DELETE("/fimp/api/flow/context/record/:flowid", func(c echo.Context) error {
// // flowId is variable name here
// name := c.Param("flowid")
// log.Info("<ctx> Request to delete record with name ", name)
// if name != "" {
// err := ctx.ctx.DeleteRecord(name, "global", false)
// return c.JSON(http.StatusOK, err)
// }
// return c.JSON(http.StatusOK, nil)
// })
//}
type ContextExtRecord struct {
FlowId string `json:"flow_id"`
Rec model.ContextRecord `json:"rec"`
}
func (ctx *ContextApi) RegisterMqttApi(msgTransport *fimpgo.MqttTransport) {
ctx.msgTransport = msgTransport
ctx.msgTransport.Subscribe("pt:j1/mt:cmd/rt:app/rn:tpflow/ad:1")
apiCh := make(fimpgo.MessageCh, 10)
ctx.msgTransport.RegisterChannel("flow-ctx-api",apiCh)
var fimp *fimpgo.FimpMessage
go func() {
for {
newMsg := <-apiCh
fimp = nil
log.Debug("New context message of type ", newMsg.Payload.Type)
switch newMsg.Payload.Type {
case "cmd.flow.ctx_get_records":
val,_ := newMsg.Payload.GetStrMapValue()
flowId , _ := val["flow_id"]
if flowId == "-"|| flowId=="" {
flowId = "global"
}
result := ctx.ctx.GetRecords(flowId)
fimp = fimpgo.NewMessage("evt.flow.ctx_records_report", "tpflow", fimpgo.VTypeObject, result, nil, nil, newMsg.Payload)
case "cmd.flow.ctx_update_record":
var reqValue ContextExtRecord
reqRawObject := newMsg.Payload.GetRawObjectValue()
err := json.Unmarshal(reqRawObject, &reqValue)
if err != nil {
log.Error("<ctx> cmd.flow.ctx_update_record Can't unmarshal request")
fimp = fimpgo.NewMessage("evt.flow.ctx_update_report", "tpflow", "string", err.Error(), nil, nil, newMsg.Payload)
break
}
reqValue.Rec.UpdatedAt = time.Now()
ctx.ctx.PutRecord(&reqValue.Rec, reqValue.FlowId, reqValue.Rec.InMemory)
fimp = fimpgo.NewMessage("evt.flow.ctx_update_report", "tpflow", "string", "ok", nil, nil, newMsg.Payload)
case "cmd.flow.ctx_delete":
// flowId is variable name here
req ,err := newMsg.Payload.GetStrMapValue()
if err != nil {
log.Error("<ctx> Can't unmarshal request.")
fimp = fimpgo.NewMessage("evt.flow.ctx_delete_report", "tpflow", "string", err, nil, nil, newMsg.Payload)
break
}
log.Info("<ctx> Request to delete record with name ", req["name"])
flowId := "global"
if req["flow_id"] != "" {
flowId = req["flow_id"]
}
err = ctx.ctx.DeleteRecord(req["name"],flowId , false)
if err != nil {
fimp = fimpgo.NewMessage("evt.flow.ctx_delete_report", "tpflow", "string", err, nil, nil, newMsg.Payload)
}else {
fimp = fimpgo.NewMessage("evt.flow.ctx_delete_report", "tpflow", "string", "ok", nil, nil, newMsg.Payload)
}
}
if fimp != nil {
addr := fimpgo.Address{MsgType: fimpgo.MsgTypeEvt, ResourceType: fimpgo.ResourceTypeApp, ResourceName: "tpflow", ResourceAddress: "1",}
ctx.msgTransport.Publish(&addr, fimp)
}
}
}()
}