2018-08-26 13:15:48 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
package things
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
|
|
|
// Thing represents a Mainflux thing. Each thing is owned by one user, and
|
|
|
|
// it is assigned with the unique identifier and (temporary) access key.
|
|
|
|
type Thing struct {
|
2018-12-05 13:09:25 +01:00
|
|
|
ID string
|
2018-10-24 11:21:03 +02:00
|
|
|
Owner string
|
|
|
|
Type string
|
|
|
|
Name string
|
|
|
|
Key string
|
|
|
|
Metadata string
|
2018-05-15 17:13:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var thingTypes = map[string]bool{
|
|
|
|
"app": true,
|
|
|
|
"device": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate returns an error if thing representation is invalid.
|
|
|
|
func (c *Thing) Validate() error {
|
|
|
|
if c.Type = strings.ToLower(c.Type); !thingTypes[c.Type] {
|
|
|
|
return ErrMalformedEntity
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ThingRepository specifies a thing persistence API.
|
|
|
|
type ThingRepository interface {
|
|
|
|
// Save persists the thing. Successful operation is indicated by non-nil
|
|
|
|
// error response.
|
2018-12-05 13:09:25 +01:00
|
|
|
Save(Thing) (string, error)
|
2018-05-15 17:13:09 +02:00
|
|
|
|
|
|
|
// Update performs an update to the existing thing. A non-nil error is
|
|
|
|
// returned to indicate operation failure.
|
|
|
|
Update(Thing) error
|
|
|
|
|
2018-05-17 20:17:02 +02:00
|
|
|
// RetrieveByID retrieves the thing having the provided identifier, that is owned
|
2018-05-15 17:13:09 +02:00
|
|
|
// by the specified user.
|
2018-12-05 13:09:25 +01:00
|
|
|
RetrieveByID(string, string) (Thing, error)
|
2018-05-15 17:13:09 +02:00
|
|
|
|
2018-05-17 20:17:02 +02:00
|
|
|
// RetrieveByKey returns thing ID for given thing key.
|
2018-12-05 13:09:25 +01:00
|
|
|
RetrieveByKey(string) (string, error)
|
2018-05-17 20:17:02 +02:00
|
|
|
|
|
|
|
// RetrieveAll retrieves the subset of things owned by the specified user.
|
2018-10-24 11:21:03 +02:00
|
|
|
RetrieveAll(string, uint64, uint64) []Thing
|
2018-05-15 17:13:09 +02:00
|
|
|
|
|
|
|
// Remove removes the thing having the provided identifier, that is owned
|
|
|
|
// by the specified user.
|
2018-12-05 13:09:25 +01:00
|
|
|
Remove(string, string) error
|
2018-05-15 17:13:09 +02:00
|
|
|
}
|
2018-09-04 22:19:43 +02:00
|
|
|
|
|
|
|
// ThingCache contains thing caching interface.
|
|
|
|
type ThingCache interface {
|
|
|
|
// Save stores pair thing key, thing id.
|
2018-12-05 13:09:25 +01:00
|
|
|
Save(string, string) error
|
2018-09-04 22:19:43 +02:00
|
|
|
|
|
|
|
// ID returns thing ID for given key.
|
2018-12-05 13:09:25 +01:00
|
|
|
ID(string) (string, error)
|
2018-09-04 22:19:43 +02:00
|
|
|
|
|
|
|
// Removes thing from cache.
|
2018-12-05 13:09:25 +01:00
|
|
|
Remove(string) error
|
2018-09-04 22:19:43 +02:00
|
|
|
}
|