mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00

* Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
219 lines
4.5 KiB
Go
219 lines
4.5 KiB
Go
//
|
|
// Copyright (c) 2018
|
|
// Mainflux
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package redis
|
|
|
|
import (
|
|
"github.com/go-redis/redis"
|
|
"github.com/mainflux/mainflux/things"
|
|
)
|
|
|
|
const (
|
|
streamID = "mainflux.things"
|
|
streamLen = 1000
|
|
)
|
|
|
|
var _ things.Service = (*eventStore)(nil)
|
|
|
|
type eventStore struct {
|
|
svc things.Service
|
|
client *redis.Client
|
|
}
|
|
|
|
// NewEventStoreMiddleware returns wrapper around things service that sends
|
|
// events to event store.
|
|
func NewEventStoreMiddleware(svc things.Service, client *redis.Client) things.Service {
|
|
return eventStore{
|
|
svc: svc,
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
func (es eventStore) AddThing(key string, thing things.Thing) (things.Thing, error) {
|
|
sth, err := es.svc.AddThing(key, thing)
|
|
if err != nil {
|
|
return sth, err
|
|
}
|
|
|
|
event := createThingEvent{
|
|
id: sth.ID,
|
|
owner: sth.Owner,
|
|
kind: sth.Type,
|
|
name: sth.Name,
|
|
metadata: sth.Metadata,
|
|
}
|
|
record := &redis.XAddArgs{
|
|
Stream: streamID,
|
|
MaxLenApprox: streamLen,
|
|
Values: event.Encode(),
|
|
}
|
|
es.client.XAdd(record).Err()
|
|
|
|
return sth, err
|
|
}
|
|
|
|
func (es eventStore) UpdateThing(key string, thing things.Thing) error {
|
|
if err := es.svc.UpdateThing(key, thing); err != nil {
|
|
return err
|
|
}
|
|
|
|
event := updateThingEvent{
|
|
id: thing.ID,
|
|
kind: thing.Type,
|
|
name: thing.Name,
|
|
metadata: thing.Metadata,
|
|
}
|
|
record := &redis.XAddArgs{
|
|
Stream: streamID,
|
|
MaxLenApprox: streamLen,
|
|
Values: event.Encode(),
|
|
}
|
|
es.client.XAdd(record).Err()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (es eventStore) ViewThing(key, id string) (things.Thing, error) {
|
|
return es.svc.ViewThing(key, id)
|
|
}
|
|
|
|
func (es eventStore) ListThings(key string, offset, limit uint64) ([]things.Thing, error) {
|
|
return es.svc.ListThings(key, offset, limit)
|
|
}
|
|
|
|
func (es eventStore) RemoveThing(key, id string) error {
|
|
if err := es.svc.RemoveThing(key, id); err != nil {
|
|
return err
|
|
}
|
|
|
|
event := removeThingEvent{
|
|
id: id,
|
|
}
|
|
record := &redis.XAddArgs{
|
|
Stream: streamID,
|
|
MaxLenApprox: streamLen,
|
|
Values: event.Encode(),
|
|
}
|
|
es.client.XAdd(record).Err()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (es eventStore) CreateChannel(key string, channel things.Channel) (things.Channel, error) {
|
|
sch, err := es.svc.CreateChannel(key, channel)
|
|
if err != nil {
|
|
return sch, err
|
|
}
|
|
|
|
event := createChannelEvent{
|
|
id: sch.ID,
|
|
owner: sch.Owner,
|
|
name: sch.Name,
|
|
metadata: sch.Metadata,
|
|
}
|
|
record := &redis.XAddArgs{
|
|
Stream: streamID,
|
|
MaxLenApprox: streamLen,
|
|
Values: event.Encode(),
|
|
}
|
|
es.client.XAdd(record).Err()
|
|
|
|
return sch, err
|
|
}
|
|
|
|
func (es eventStore) UpdateChannel(key string, channel things.Channel) error {
|
|
if err := es.svc.UpdateChannel(key, channel); err != nil {
|
|
return err
|
|
}
|
|
|
|
event := updateChannelEvent{
|
|
id: channel.ID,
|
|
name: channel.Name,
|
|
metadata: channel.Metadata,
|
|
}
|
|
record := &redis.XAddArgs{
|
|
Stream: streamID,
|
|
MaxLenApprox: streamLen,
|
|
Values: event.Encode(),
|
|
}
|
|
es.client.XAdd(record).Err()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (es eventStore) ViewChannel(key, id string) (things.Channel, error) {
|
|
return es.svc.ViewChannel(key, id)
|
|
}
|
|
|
|
func (es eventStore) ListChannels(key string, offset, limit uint64) ([]things.Channel, error) {
|
|
return es.svc.ListChannels(key, offset, limit)
|
|
}
|
|
|
|
func (es eventStore) RemoveChannel(key, id string) error {
|
|
if err := es.svc.RemoveChannel(key, id); err != nil {
|
|
return err
|
|
}
|
|
|
|
event := removeChannelEvent{
|
|
id: id,
|
|
}
|
|
record := &redis.XAddArgs{
|
|
Stream: streamID,
|
|
MaxLenApprox: streamLen,
|
|
Values: event.Encode(),
|
|
}
|
|
es.client.XAdd(record).Err()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (es eventStore) Connect(key, chanID, thingID string) error {
|
|
if err := es.svc.Connect(key, chanID, thingID); err != nil {
|
|
return err
|
|
}
|
|
|
|
event := connectThingEvent{
|
|
chanID: chanID,
|
|
thingID: thingID,
|
|
}
|
|
record := &redis.XAddArgs{
|
|
Stream: streamID,
|
|
MaxLenApprox: streamLen,
|
|
Values: event.Encode(),
|
|
}
|
|
es.client.XAdd(record).Err()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (es eventStore) Disconnect(key, chanID, thingID string) error {
|
|
if err := es.svc.Disconnect(key, chanID, thingID); err != nil {
|
|
return err
|
|
}
|
|
|
|
event := disconnectThingEvent{
|
|
chanID: chanID,
|
|
thingID: thingID,
|
|
}
|
|
record := &redis.XAddArgs{
|
|
Stream: streamID,
|
|
MaxLenApprox: streamLen,
|
|
Values: event.Encode(),
|
|
}
|
|
es.client.XAdd(record).Err()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (es eventStore) CanAccess(chanID string, key string) (string, error) {
|
|
return es.svc.CanAccess(chanID, key)
|
|
}
|
|
|
|
func (es eventStore) Identify(key string) (string, error) {
|
|
return es.svc.Identify(key)
|
|
}
|