2018-05-15 23:13:09 +08: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-08-17 22:20:35 +08:00
|
|
|
ID uint64 `json:"id"`
|
|
|
|
Owner string `json:"-"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
Metadata string `json:"metadata,omitempty"`
|
2018-05-15 23:13:09 +08: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-05-21 18:51:46 +08:00
|
|
|
Save(Thing) (uint64, error)
|
2018-05-15 23:13:09 +08:00
|
|
|
|
|
|
|
// Update performs an update to the existing thing. A non-nil error is
|
|
|
|
// returned to indicate operation failure.
|
|
|
|
Update(Thing) error
|
|
|
|
|
2018-05-18 02:17:02 +08:00
|
|
|
// RetrieveByID retrieves the thing having the provided identifier, that is owned
|
2018-05-15 23:13:09 +08:00
|
|
|
// by the specified user.
|
2018-05-21 18:51:46 +08:00
|
|
|
RetrieveByID(string, uint64) (Thing, error)
|
2018-05-15 23:13:09 +08:00
|
|
|
|
2018-05-18 02:17:02 +08:00
|
|
|
// RetrieveByKey returns thing ID for given thing key.
|
2018-05-21 18:51:46 +08:00
|
|
|
RetrieveByKey(string) (uint64, error)
|
2018-05-18 02:17:02 +08:00
|
|
|
|
|
|
|
// RetrieveAll retrieves the subset of things owned by the specified user.
|
|
|
|
RetrieveAll(string, int, int) []Thing
|
2018-05-15 23:13:09 +08:00
|
|
|
|
|
|
|
// Remove removes the thing having the provided identifier, that is owned
|
|
|
|
// by the specified user.
|
2018-05-21 18:51:46 +08:00
|
|
|
Remove(string, uint64) error
|
2018-05-15 23:13:09 +08:00
|
|
|
}
|