2019-04-08 23:46:07 +08:00
|
|
|
//
|
|
|
|
// Copyright (c) 2019
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
|
|
|
package producer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux/bootstrap"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
configPrefix = "config."
|
|
|
|
configCreate = configPrefix + "create"
|
|
|
|
configUpdate = configPrefix + "update"
|
|
|
|
configRemove = configPrefix + "remove"
|
|
|
|
|
|
|
|
thingPrefix = "thing."
|
|
|
|
thingBootstrap = thingPrefix + "bootstrap"
|
|
|
|
thingStateChange = thingPrefix + "state_change"
|
|
|
|
thingUpdateConnections = thingPrefix + "update_connections"
|
|
|
|
)
|
|
|
|
|
|
|
|
type event interface {
|
|
|
|
encode() map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ event = (*createConfigEvent)(nil)
|
|
|
|
_ event = (*updateConfigEvent)(nil)
|
|
|
|
_ event = (*removeConfigEvent)(nil)
|
|
|
|
_ event = (*bootstrapEvent)(nil)
|
|
|
|
_ event = (*changeStateEvent)(nil)
|
|
|
|
_ event = (*updateConnectionsEvent)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type createConfigEvent struct {
|
|
|
|
mfThing string
|
|
|
|
owner string
|
|
|
|
name string
|
|
|
|
mfChannels []string
|
|
|
|
externalID string
|
|
|
|
content string
|
|
|
|
timestamp time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cce createConfigEvent) encode() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2019-04-17 00:32:05 +08:00
|
|
|
"thing_id": cce.mfThing,
|
2019-04-11 19:55:21 +08:00
|
|
|
"owner": cce.owner,
|
|
|
|
"name": cce.name,
|
|
|
|
"channels": strings.Join(cce.mfChannels, ", "),
|
|
|
|
"external_id": cce.externalID,
|
|
|
|
"content": cce.content,
|
|
|
|
"timestamp": cce.timestamp.Unix(),
|
|
|
|
"operation": configCreate,
|
2019-04-08 23:46:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type updateConfigEvent struct {
|
|
|
|
mfThing string
|
|
|
|
name string
|
|
|
|
content string
|
|
|
|
timestamp time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (uce updateConfigEvent) encode() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2019-04-17 00:32:05 +08:00
|
|
|
"thing_id": uce.mfThing,
|
2019-04-08 23:46:07 +08:00
|
|
|
"name": uce.name,
|
|
|
|
"content": uce.content,
|
|
|
|
"timestamp": uce.timestamp.Unix(),
|
|
|
|
"operation": configUpdate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type removeConfigEvent struct {
|
|
|
|
mfThing string
|
|
|
|
timestamp time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rce removeConfigEvent) encode() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2019-04-17 00:32:05 +08:00
|
|
|
"thing_id": rce.mfThing,
|
2019-04-08 23:46:07 +08:00
|
|
|
"timestamp": rce.timestamp.Unix(),
|
|
|
|
"operation": configRemove,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type bootstrapEvent struct {
|
2019-04-17 00:32:05 +08:00
|
|
|
externalID string
|
|
|
|
success bool
|
|
|
|
timestamp time.Time
|
2019-04-08 23:46:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (be bootstrapEvent) encode() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2019-04-11 19:55:21 +08:00
|
|
|
"external_id": be.externalID,
|
2019-04-17 00:32:05 +08:00
|
|
|
"success": be.success,
|
2019-04-08 23:46:07 +08:00
|
|
|
"timestamp": be.timestamp.Unix(),
|
|
|
|
"operation": thingBootstrap,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type changeStateEvent struct {
|
|
|
|
mfThing string
|
|
|
|
state bootstrap.State
|
|
|
|
timestamp time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cse changeStateEvent) encode() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2019-04-17 00:32:05 +08:00
|
|
|
"thing_id": cse.mfThing,
|
2019-04-08 23:46:07 +08:00
|
|
|
"state": cse.state.String(),
|
|
|
|
"timestamp": cse.timestamp.Unix(),
|
|
|
|
"operation": thingStateChange,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type updateConnectionsEvent struct {
|
|
|
|
mfThing string
|
|
|
|
mfChannels []string
|
|
|
|
timestamp time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (uce updateConnectionsEvent) encode() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2019-04-17 00:32:05 +08:00
|
|
|
"thing_id": uce.mfThing,
|
2019-04-08 23:46:07 +08:00
|
|
|
"channels": strings.Join(uce.mfChannels, ", "),
|
|
|
|
"timestamp": uce.timestamp.Unix(),
|
|
|
|
"operation": thingUpdateConnections,
|
|
|
|
}
|
|
|
|
}
|