devicemanagement

package
v0.0.0-...-905d515 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2024 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DeviceStateUnknown = -1
	DeviceStateOK      = 1
	DeviceStateWarning = 2
	DeviceStateError   = 3
)

Variables

View Source
var ErrDeviceNotFound = fmt.Errorf("device not found")
View Source
var ErrRepositoryError = fmt.Errorf("could not fetch data from repository")

Functions

This section is empty.

Types

type Alarm

type Alarm struct {
	gorm.Model `json:"-"`
	DeviceID   uint `json:"-"`

	AlarmID    int       `json:"-"`
	Severity   int       `json:"-"`
	ObservedAt time.Time `json:"-"`
}

func (Alarm) TableName

func (Alarm) TableName() string

type Device

type Device struct {
	gorm.Model `json:"-"`

	SensorID string `gorm:"uniqueIndex" json:"sensorID"`
	DeviceID string `gorm:"uniqueIndex" json:"deviceID"`

	Active       bool         `json:"active"`
	Name         string       `json:"name"`
	Description  string       `json:"description"`
	Location     Location     `json:"location"`
	Environment  string       `json:"environment"`
	Source       string       `json:"source"`
	DeviceStatus DeviceStatus `json:"deviceStatus"`
	DeviceState  DeviceState  `json:"deviceState"`

	TenantID uint   `gorm:"foreignKey:TenantID" json:"-"`
	Tenant   Tenant `json:"tenant"`

	DeviceProfileID uint          `gorm:"foreignKey:DeviceProfileID" json:"-"`
	DeviceProfile   DeviceProfile `json:"deviceProfile"`

	Lwm2mTypes []Lwm2mType `gorm:"many2many:device_lwm2mType;" json:"types"`
	Tags       []Tag       `gorm:"many2many:device_tags;" json:"tags"`

	Alarms []Alarm `json:"-"`
}

func (*Device) BeforeSave

func (d *Device) BeforeSave(tx *gorm.DB) (err error)

func (*Device) HasActiveAlarms

func (d *Device) HasActiveAlarms() (bool, int, []Alarm)

func (*Device) HasAlarm

func (d *Device) HasAlarm(id int) bool

type DeviceProfile

type DeviceProfile struct {
	gorm.Model `json:"-"`

	Name     string `gorm:"uniqueIndex" json:"name"`
	Decoder  string `json:"decoder"`
	Interval int    `json:"interval"`
}

type DeviceRepository

type DeviceRepository interface {
	GetDevices(ctx context.Context, tenants ...string) ([]Device, error)
	GetOnlineDevices(ctx context.Context, tenants ...string) ([]Device, error)
	GetDeviceBySensorID(ctx context.Context, sensorID string, tenants ...string) (Device, error)
	GetDeviceByDeviceID(ctx context.Context, deviceID string, tenants ...string) (Device, error)

	Save(ctx context.Context, device *Device) error

	UpdateDeviceStatus(ctx context.Context, deviceID string, deviceStatus DeviceStatus) error
	UpdateDeviceState(ctx context.Context, deviceID string, deviceState DeviceState) error

	AddAlarm(ctx context.Context, deviceID string, alarmID int, severity int, observedAt time.Time) error
	RemoveAlarmByID(ctx context.Context, alarmID int) (string, error)

	Seed(context.Context, io.Reader, ...string) error
}

func NewDeviceRepository

func NewDeviceRepository(connect ConnectorFunc) (DeviceRepository, error)

type DeviceRepositoryMock

type DeviceRepositoryMock struct {
	// AddAlarmFunc mocks the AddAlarm method.
	AddAlarmFunc func(ctx context.Context, deviceID string, alarmID int, severity int, observedAt time.Time) error

	// GetDeviceByDeviceIDFunc mocks the GetDeviceByDeviceID method.
	GetDeviceByDeviceIDFunc func(ctx context.Context, deviceID string, tenants ...string) (Device, error)

	// GetDeviceBySensorIDFunc mocks the GetDeviceBySensorID method.
	GetDeviceBySensorIDFunc func(ctx context.Context, sensorID string, tenants ...string) (Device, error)

	// GetDevicesFunc mocks the GetDevices method.
	GetDevicesFunc func(ctx context.Context, tenants ...string) ([]Device, error)

	// GetOnlineDevicesFunc mocks the GetOnlineDevices method.
	GetOnlineDevicesFunc func(ctx context.Context, tenants ...string) ([]Device, error)

	// RemoveAlarmByIDFunc mocks the RemoveAlarmByID method.
	RemoveAlarmByIDFunc func(ctx context.Context, alarmID int) (string, error)

	// SaveFunc mocks the Save method.
	SaveFunc func(ctx context.Context, device *Device) error

	// SeedFunc mocks the Seed method.
	SeedFunc func(contextMoqParam context.Context, reader io.Reader, strings ...string) error

	// UpdateDeviceStateFunc mocks the UpdateDeviceState method.
	UpdateDeviceStateFunc func(ctx context.Context, deviceID string, deviceState DeviceState) error

	// UpdateDeviceStatusFunc mocks the UpdateDeviceStatus method.
	UpdateDeviceStatusFunc func(ctx context.Context, deviceID string, deviceStatus DeviceStatus) error
	// contains filtered or unexported fields
}

DeviceRepositoryMock is a mock implementation of DeviceRepository.

func TestSomethingThatUsesDeviceRepository(t *testing.T) {

	// make and configure a mocked DeviceRepository
	mockedDeviceRepository := &DeviceRepositoryMock{
		AddAlarmFunc: func(ctx context.Context, deviceID string, alarmID int, severity int, observedAt time.Time) error {
			panic("mock out the AddAlarm method")
		},
		GetDeviceByDeviceIDFunc: func(ctx context.Context, deviceID string, tenants ...string) (Device, error) {
			panic("mock out the GetDeviceByDeviceID method")
		},
		GetDeviceBySensorIDFunc: func(ctx context.Context, sensorID string, tenants ...string) (Device, error) {
			panic("mock out the GetDeviceBySensorID method")
		},
		GetDevicesFunc: func(ctx context.Context, tenants ...string) ([]Device, error) {
			panic("mock out the GetDevices method")
		},
		GetOnlineDevicesFunc: func(ctx context.Context, tenants ...string) ([]Device, error) {
			panic("mock out the GetOnlineDevices method")
		},
		RemoveAlarmByIDFunc: func(ctx context.Context, alarmID int) (string, error) {
			panic("mock out the RemoveAlarmByID method")
		},
		SaveFunc: func(ctx context.Context, device *Device) error {
			panic("mock out the Save method")
		},
		SeedFunc: func(contextMoqParam context.Context, reader io.Reader, strings ...string) error {
			panic("mock out the Seed method")
		},
		UpdateDeviceStateFunc: func(ctx context.Context, deviceID string, deviceState DeviceState) error {
			panic("mock out the UpdateDeviceState method")
		},
		UpdateDeviceStatusFunc: func(ctx context.Context, deviceID string, deviceStatus DeviceStatus) error {
			panic("mock out the UpdateDeviceStatus method")
		},
	}

	// use mockedDeviceRepository in code that requires DeviceRepository
	// and then make assertions.

}

func (*DeviceRepositoryMock) AddAlarm

func (mock *DeviceRepositoryMock) AddAlarm(ctx context.Context, deviceID string, alarmID int, severity int, observedAt time.Time) error

AddAlarm calls AddAlarmFunc.

func (*DeviceRepositoryMock) AddAlarmCalls

func (mock *DeviceRepositoryMock) AddAlarmCalls() []struct {
	Ctx        context.Context
	DeviceID   string
	AlarmID    int
	Severity   int
	ObservedAt time.Time
}

AddAlarmCalls gets all the calls that were made to AddAlarm. Check the length with:

len(mockedDeviceRepository.AddAlarmCalls())

func (*DeviceRepositoryMock) GetDeviceByDeviceID

func (mock *DeviceRepositoryMock) GetDeviceByDeviceID(ctx context.Context, deviceID string, tenants ...string) (Device, error)

GetDeviceByDeviceID calls GetDeviceByDeviceIDFunc.

func (*DeviceRepositoryMock) GetDeviceByDeviceIDCalls

func (mock *DeviceRepositoryMock) GetDeviceByDeviceIDCalls() []struct {
	Ctx      context.Context
	DeviceID string
	Tenants  []string
}

GetDeviceByDeviceIDCalls gets all the calls that were made to GetDeviceByDeviceID. Check the length with:

len(mockedDeviceRepository.GetDeviceByDeviceIDCalls())

func (*DeviceRepositoryMock) GetDeviceBySensorID

func (mock *DeviceRepositoryMock) GetDeviceBySensorID(ctx context.Context, sensorID string, tenants ...string) (Device, error)

GetDeviceBySensorID calls GetDeviceBySensorIDFunc.

func (*DeviceRepositoryMock) GetDeviceBySensorIDCalls

func (mock *DeviceRepositoryMock) GetDeviceBySensorIDCalls() []struct {
	Ctx      context.Context
	SensorID string
	Tenants  []string
}

GetDeviceBySensorIDCalls gets all the calls that were made to GetDeviceBySensorID. Check the length with:

len(mockedDeviceRepository.GetDeviceBySensorIDCalls())

func (*DeviceRepositoryMock) GetDevices

func (mock *DeviceRepositoryMock) GetDevices(ctx context.Context, tenants ...string) ([]Device, error)

GetDevices calls GetDevicesFunc.

func (*DeviceRepositoryMock) GetDevicesCalls

func (mock *DeviceRepositoryMock) GetDevicesCalls() []struct {
	Ctx     context.Context
	Tenants []string
}

GetDevicesCalls gets all the calls that were made to GetDevices. Check the length with:

len(mockedDeviceRepository.GetDevicesCalls())

func (*DeviceRepositoryMock) GetOnlineDevices

func (mock *DeviceRepositoryMock) GetOnlineDevices(ctx context.Context, tenants ...string) ([]Device, error)

GetOnlineDevices calls GetOnlineDevicesFunc.

func (*DeviceRepositoryMock) GetOnlineDevicesCalls

func (mock *DeviceRepositoryMock) GetOnlineDevicesCalls() []struct {
	Ctx     context.Context
	Tenants []string
}

GetOnlineDevicesCalls gets all the calls that were made to GetOnlineDevices. Check the length with:

len(mockedDeviceRepository.GetOnlineDevicesCalls())

func (*DeviceRepositoryMock) RemoveAlarmByID

func (mock *DeviceRepositoryMock) RemoveAlarmByID(ctx context.Context, alarmID int) (string, error)

RemoveAlarmByID calls RemoveAlarmByIDFunc.

func (*DeviceRepositoryMock) RemoveAlarmByIDCalls

func (mock *DeviceRepositoryMock) RemoveAlarmByIDCalls() []struct {
	Ctx     context.Context
	AlarmID int
}

RemoveAlarmByIDCalls gets all the calls that were made to RemoveAlarmByID. Check the length with:

len(mockedDeviceRepository.RemoveAlarmByIDCalls())

func (*DeviceRepositoryMock) Save

func (mock *DeviceRepositoryMock) Save(ctx context.Context, device *Device) error

Save calls SaveFunc.

func (*DeviceRepositoryMock) SaveCalls

func (mock *DeviceRepositoryMock) SaveCalls() []struct {
	Ctx    context.Context
	Device *Device
}

SaveCalls gets all the calls that were made to Save. Check the length with:

len(mockedDeviceRepository.SaveCalls())

func (*DeviceRepositoryMock) Seed

func (mock *DeviceRepositoryMock) Seed(contextMoqParam context.Context, reader io.Reader, strings ...string) error

Seed calls SeedFunc.

func (*DeviceRepositoryMock) SeedCalls

func (mock *DeviceRepositoryMock) SeedCalls() []struct {
	ContextMoqParam context.Context
	Reader          io.Reader
	Strings         []string
}

SeedCalls gets all the calls that were made to Seed. Check the length with:

len(mockedDeviceRepository.SeedCalls())

func (*DeviceRepositoryMock) UpdateDeviceState

func (mock *DeviceRepositoryMock) UpdateDeviceState(ctx context.Context, deviceID string, deviceState DeviceState) error

UpdateDeviceState calls UpdateDeviceStateFunc.

func (*DeviceRepositoryMock) UpdateDeviceStateCalls

func (mock *DeviceRepositoryMock) UpdateDeviceStateCalls() []struct {
	Ctx         context.Context
	DeviceID    string
	DeviceState DeviceState
}

UpdateDeviceStateCalls gets all the calls that were made to UpdateDeviceState. Check the length with:

len(mockedDeviceRepository.UpdateDeviceStateCalls())

func (*DeviceRepositoryMock) UpdateDeviceStatus

func (mock *DeviceRepositoryMock) UpdateDeviceStatus(ctx context.Context, deviceID string, deviceStatus DeviceStatus) error

UpdateDeviceStatus calls UpdateDeviceStatusFunc.

func (*DeviceRepositoryMock) UpdateDeviceStatusCalls

func (mock *DeviceRepositoryMock) UpdateDeviceStatusCalls() []struct {
	Ctx          context.Context
	DeviceID     string
	DeviceStatus DeviceStatus
}

UpdateDeviceStatusCalls gets all the calls that were made to UpdateDeviceStatus. Check the length with:

len(mockedDeviceRepository.UpdateDeviceStatusCalls())

type DeviceState

type DeviceState struct {
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`

	DeviceID uint `gorm:"primarykey" json:"-"`

	Online     bool      `json:"online"`
	State      int       `json:"state"`
	ObservedAt time.Time `json:"observedAt"`
}

type DeviceStatus

type DeviceStatus struct {
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`

	DeviceID uint `gorm:"primarykey" json:"-"`

	BatteryLevel int       `json:"batteryLevel"`
	LastObserved time.Time `json:"lastObservedAt"`
}

type Location

type Location struct {
	gorm.Model `json:"-"`
	DeviceID   uint `json:"-"`

	Latitude  float64 `json:"latitude"`
	Longitude float64 `json:"longitude"`
	Altitude  float64 `json:"altitude"`
}

type Lwm2mType

type Lwm2mType struct {
	gorm.Model `json:"-"`

	Urn string `gorm:"uniqueIndex" json:"urn"`
}

type Tag

type Tag struct {
	gorm.Model `json:"-"`

	Name string `json:"name"`
}

type Tenant

type Tenant struct {
	gorm.Model `json:"-"`

	Name string `gorm:"uniqueIndex" json:"name"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL