2018-11-21 02:46:33 +08:00
|
|
|
package redis
|
|
|
|
|
|
|
|
const (
|
|
|
|
thingPrefix = "thing."
|
|
|
|
thingCreate = thingPrefix + "create"
|
|
|
|
thingUpdate = thingPrefix + "update"
|
|
|
|
thingRemove = thingPrefix + "remove"
|
|
|
|
thingConnect = thingPrefix + "connect"
|
|
|
|
thingDisconnect = thingPrefix + "disconnect"
|
|
|
|
|
|
|
|
channelPrefix = "channel."
|
|
|
|
channelCreate = channelPrefix + "create"
|
|
|
|
channelUpdate = channelPrefix + "update"
|
|
|
|
channelRemove = channelPrefix + "remove"
|
|
|
|
)
|
|
|
|
|
|
|
|
type event interface {
|
|
|
|
Encode() map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ event = (*createThingEvent)(nil)
|
|
|
|
_ event = (*updateThingEvent)(nil)
|
|
|
|
_ event = (*removeThingEvent)(nil)
|
|
|
|
_ event = (*createChannelEvent)(nil)
|
|
|
|
_ event = (*updateChannelEvent)(nil)
|
|
|
|
_ event = (*removeChannelEvent)(nil)
|
|
|
|
_ event = (*connectThingEvent)(nil)
|
|
|
|
_ event = (*disconnectThingEvent)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type createThingEvent struct {
|
|
|
|
id string
|
|
|
|
owner string
|
|
|
|
kind string
|
|
|
|
name string
|
|
|
|
metadata string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cte createThingEvent) Encode() map[string]interface{} {
|
|
|
|
val := map[string]interface{}{
|
|
|
|
"id": cte.id,
|
|
|
|
"owner": cte.owner,
|
|
|
|
"type": cte.kind,
|
|
|
|
"operation": thingCreate,
|
|
|
|
}
|
|
|
|
|
|
|
|
if cte.name != "" {
|
|
|
|
val["name"] = cte.name
|
|
|
|
}
|
|
|
|
|
|
|
|
if cte.metadata != "" {
|
|
|
|
val["metadata"] = cte.metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
type updateThingEvent struct {
|
|
|
|
id string
|
|
|
|
kind string
|
|
|
|
name string
|
|
|
|
metadata string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ute updateThingEvent) Encode() map[string]interface{} {
|
|
|
|
val := map[string]interface{}{
|
|
|
|
"id": ute.id,
|
|
|
|
"type": ute.kind,
|
|
|
|
"operation": thingUpdate,
|
|
|
|
}
|
|
|
|
|
|
|
|
if ute.name != "" {
|
|
|
|
val["name"] = ute.name
|
|
|
|
}
|
|
|
|
|
|
|
|
if ute.metadata != "" {
|
|
|
|
val["metadata"] = ute.metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
type removeThingEvent struct {
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rte removeThingEvent) Encode() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"id": rte.id,
|
|
|
|
"operation": thingRemove,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type createChannelEvent struct {
|
2018-11-28 22:58:48 +08:00
|
|
|
id string
|
|
|
|
owner string
|
|
|
|
name string
|
|
|
|
metadata string
|
2018-11-21 02:46:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cce createChannelEvent) Encode() map[string]interface{} {
|
|
|
|
val := map[string]interface{}{
|
|
|
|
"id": cce.id,
|
|
|
|
"owner": cce.owner,
|
|
|
|
"operation": channelCreate,
|
|
|
|
}
|
|
|
|
|
|
|
|
if cce.name != "" {
|
|
|
|
val["name"] = cce.name
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:58:48 +08:00
|
|
|
if cce.metadata != "" {
|
|
|
|
val["metadata"] = cce.metadata
|
|
|
|
}
|
|
|
|
|
2018-11-21 02:46:33 +08:00
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
type updateChannelEvent struct {
|
2018-11-28 22:58:48 +08:00
|
|
|
id string
|
|
|
|
name string
|
|
|
|
metadata string
|
2018-11-21 02:46:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (uce updateChannelEvent) Encode() map[string]interface{} {
|
2018-11-28 22:58:48 +08:00
|
|
|
val := map[string]interface{}{
|
2018-11-21 02:46:33 +08:00
|
|
|
"id": uce.id,
|
|
|
|
"operation": channelUpdate,
|
|
|
|
}
|
2018-11-28 22:58:48 +08:00
|
|
|
|
|
|
|
if uce.name != "" {
|
|
|
|
val["name"] = uce.name
|
|
|
|
}
|
|
|
|
|
|
|
|
if uce.metadata != "" {
|
|
|
|
val["metadata"] = uce.metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
return val
|
2018-11-21 02:46:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type removeChannelEvent struct {
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rce removeChannelEvent) Encode() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"id": rce.id,
|
|
|
|
"operation": channelRemove,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type connectThingEvent struct {
|
|
|
|
chanID string
|
|
|
|
thingID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cte connectThingEvent) Encode() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"chan_id": cte.chanID,
|
|
|
|
"thing_id": cte.thingID,
|
|
|
|
"operation": thingConnect,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type disconnectThingEvent struct {
|
|
|
|
chanID string
|
|
|
|
thingID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dte disconnectThingEvent) Encode() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"chan_id": dte.chanID,
|
|
|
|
"thing_id": dte.thingID,
|
|
|
|
"operation": thingDisconnect,
|
|
|
|
}
|
|
|
|
}
|