models

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrGeofenceNotFound = errors.New("geofence not found")

ErrGeofenceNotFound is returned when a geofence is not found

Functions

This section is empty.

Types

type Action added in v0.0.2

type Action struct {
	ID        string    `json:"id" db:"id"`
	Name      string    `json:"name" db:"name"`
	Key       string    `json:"key" db:"key"`
	Data      *string   `json:"data,omitempty" db:"data"`
	CreatedAt time.Time `json:"created_at" db:"created_at"`
}

Action represents an action that can be triggered by automations

type Automation added in v0.0.2

type Automation struct {
	ID          string
	Name        string
	Title       *string
	DeviceID    string
	EventRuleID *string
	EventRule   *EventRule
	SpaceID     *uuid.UUID
	UpdatedAt   time.Time
	CreatedAt   time.Time
}

Automation represents an automation database row

type AutomationSummary added in v0.0.3

type AutomationSummary struct {
	Total    int `json:"total"`
	Active   int `json:"active"`
	Disabled int `json:"disabled"`
}

AutomationSummary holds counts of automations grouped by status.

type AutomationWithActions added in v0.0.2

type AutomationWithActions struct {
	Automation
	Actions []Action
}

AutomationWithActions represents an automation with its associated actions

type CeleryMessage added in v0.0.2

type CeleryMessage struct {
	Args     json.RawMessage `json:"-"` // Positional arguments array (index 0)
	Kwargs   json.RawMessage `json:"-"` // Keyword arguments (index 1)
	Metadata json.RawMessage `json:"-"` // Celery metadata (index 2)
}

CeleryMessage represents the raw Celery message format Format: [args, kwargs, metadata]

func (*CeleryMessage) UnmarshalJSON added in v0.0.2

func (m *CeleryMessage) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the Celery array format

type DeleteDeviceTask added in v0.0.2

type DeleteDeviceTask struct {
	OrganizationSlugName string `json:"organization_slug_name"`
	DeviceID             string `json:"device_id"`
}

DeleteDeviceTask represents the Celery task kwargs for delete_device

type DeleteSpaceTask added in v0.0.2

type DeleteSpaceTask struct {
	OrganizationSlugName string     `json:"organization_slug_name"`
	PK                   DjangoUUID `json:"pk"`
}

DeleteSpaceTask represents the Celery task kwargs for delete_space

type DeviceGeofenceCheck added in v0.0.2

type DeviceGeofenceCheck struct {
	DeviceID   string
	Latitude   float64
	Longitude  float64
	IsInside   bool
	ReportedAt sql.NullTime
	DistanceKm float64
}

DeviceGeofenceCheck holds the result of a spatial check between a device's last location and a geofence.

type DeviceLocationMessage

type DeviceLocationMessage struct {
	DeviceID     string              `json:"device_id"`
	Location     LocationCoordinates `json:"location"`
	Timestamp    string              `json:"timestamp"`
	Space        string              `json:"space_slug"`
	Organization string              `json:"organization"`
	Source       string              `json:"source"`
	Metadata     map[string]any      `json:"metadata"`
}

DeviceLocationMessage represents the transformed device location message from RabbitMQ

func (*DeviceLocationMessage) ToTelemetryPayload

func (m *DeviceLocationMessage) ToTelemetryPayload() *TelemetryPayload

ToTelemetryPayload converts DeviceLocationMessage to TelemetryPayload for entity_states storage

type DjangoUUID added in v0.0.2

type DjangoUUID string

DjangoUUID represents a UUID that can be unmarshaled from either a plain string or Django's UUID object format

func (DjangoUUID) String added in v0.0.2

func (d DjangoUUID) String() string

func (*DjangoUUID) UnmarshalJSON added in v0.0.2

func (d *DjangoUUID) UnmarshalJSON(data []byte) error

type Event

type Event struct {
	EventID      int64     `json:"event_id" db:"event_id"`
	EventTypeID  int       `json:"event_type_id" db:"event_type_id"`
	EventLevel   *string   `json:"event_level,omitempty" db:"event_level"`     // manufacturer, system, automation
	EventRuleID  *string   `json:"event_rule_id,omitempty" db:"event_rule_id"` // Rule that triggered this event
	AutomationID *string   `json:"automation_id,omitempty" db:"automation_id"` // Automation that triggered this event
	GeofenceID   *string   `json:"geofence_id,omitempty" db:"geofence_id"`     // Geofence that triggered this event
	SpaceSlug    string    `json:"space_slug,omitempty" db:"space_slug"`
	DeviceID     string    `json:"device_id,omitempty" db:"device_id"`
	EntityID     *string   `json:"entity_id,omitempty" db:"entity_id"`
	StateID      uuid.UUID `json:"state_id,omitempty" db:"state_id"`
	Title        string    `json:"title,omitempty" db:"title"`
	TimeFiredTs  int64     `json:"time_fired_ts" db:"time_fired_ts"`

	// Joined fields for internal use
	EventType          string  `json:"event_type,omitempty" db:"event_type"`
	AutomationName     *string `json:"automation_name,omitempty" db:"automation_name"`
	AutomationDeviceID string  `json:"automation_device_id,omitempty" db:"automation_device_id"`
	GeofenceName       *string `json:"geofence_name,omitempty" db:"geofence_name"`
	GeofenceTypeZone   *string `json:"geofence_type_zone,omitempty" db:"geofence_type_zone"`

	Location *Location `json:"location,omitempty"`
}

Event represents an event occurrence

func (*Event) TimeFired

func (e *Event) TimeFired() time.Time

TimeFired converts the time_fired_ts to time.Time

type EventDetail

type EventDetail struct {
	EventID      int64                  `json:"event_id"`
	EventType    string                 `json:"event_type"`
	LevelEventID *string                `json:"level_event_id,omitempty"`
	EventRuleID  *string                `json:"event_rule_id,omitempty"` // Rule that triggered this event
	AutomationID *string                `json:"automation_id,omitempty"` // Automation that triggered this event
	GeofenceID   *string                `json:"geofence_id,omitempty"`   // Geofence that triggered this event
	SpaceSlug    string                 `json:"space_slug,omitempty"`
	EntityID     *string                `json:"entity_id,omitempty"`
	StateID      uuid.UUID              `json:"state_id"`
	TriggerID    *string                `json:"trigger_id,omitempty"`
	TimeFired    time.Time              `json:"time_fired"`
	EventData    map[string]interface{} `json:"event_data,omitempty"`
	ContextID    []byte                 `json:"context_id,omitempty"`
}

EventDetail represents a full event with its type and data

type EventRule

type EventRule struct {
	EventRuleID string `json:"event_rule_id" db:"event_rule_id"`
	RuleKey     string `json:"rule_key" db:"rule_key"`
	// Definition  *string   `json:"definition,omitempty" db:"definition"`
	Definition  json.RawMessage `json:"definition" db:"definition"`
	IsActive    *bool           `json:"is_active,omitempty" db:"is_active"`
	RepeatAble  *bool           `json:"repeat_able,omitempty" db:"repeat_able"`
	CooldownSec *int            `json:"cooldown_sec,omitempty" db:"cooldown_sec"`
	Description *string         `json:"description,omitempty" db:"description"`
	CreatedAt   time.Time       `json:"created_at" db:"created_at"`
}

EventRule represents a rule that can trigger events

type EventRuleRequest

type EventRuleRequest struct {
	RuleKey     *string `json:"rule_key" validate:"required"`
	Definition  *string `json:"definition"`
	IsActive    *bool   `json:"is_active"`
	RepeatAble  *bool   `json:"repeat_able"`
	CooldownSec *int    `json:"cooldown_sec"`
	Description *string `json:"description"`
}

EventRuleRequest represents a request to create or update an event rule

type EventRuleResponse

type EventRuleResponse struct {
	EventRuleID string    `json:"event_rule_id"`
	RuleKey     string    `json:"rule_key"`
	Definition  *string   `json:"definition,omitempty"`
	IsActive    bool      `json:"is_active"`
	RepeatAble  bool      `json:"repeat_able"`
	CooldownSec int       `json:"cooldown_sec"`
	Description *string   `json:"description,omitempty"`
	CreatedAt   time.Time `json:"created_at"`
}

EventRuleResponse represents an event rule response

type EventType

type EventType struct {
	EventTypeID int       `json:"event_type_id" db:"event_type_id"`
	EventType   string    `json:"event_type" db:"event_type"`
	CreatedAt   time.Time `json:"created_at" db:"created_at"`
}

EventType represents a type of event (e.g., "state_changed", "service_call")

type EventsListResponse

type EventsListResponse struct {
	Events     []Event `json:"events"`
	TotalCount int     `json:"total_count"`
	Page       int     `json:"page"`
	PageSize   int     `json:"page_size"`
}

EventsListResponse represents paginated events

type Geofence added in v0.0.2

type Geofence struct {
	GeofenceID  uuid.UUID         `json:"geofence_id" db:"geofence_id"`
	Name        string            `json:"name" db:"name"`
	TypeZone    string            `json:"type_zone" db:"type_zone"`
	Features    []json.RawMessage `json:"features" db:"features"`
	Color       string            `json:"color" db:"color"`
	IsActive    bool              `json:"is_active" db:"is_active"`
	SpaceID     *uuid.UUID        `json:"space_id,omitempty" db:"space_id"`
	EventRuleID *uuid.UUID        `json:"event_rule_id,omitempty" db:"event_rule_id"`
	CreatedAt   time.Time         `json:"created_at" db:"created_at"`
	UpdatedAt   time.Time         `json:"updated_at" db:"updated_at"`
}

Geofence represents a geofence boundary

type GeofenceWithSpace added in v0.0.2

type GeofenceWithSpace struct {
	GeofenceID  uuid.UUID         `json:"geofence_id" db:"geofence_id"`
	Name        string            `json:"name" db:"name"`
	TypeZone    string            `json:"type_zone" db:"type_zone"`
	Features    []json.RawMessage `json:"features" db:"features"`
	Color       string            `json:"color" db:"color"`
	IsActive    bool              `json:"is_active" db:"is_active"`
	SpaceID     *uuid.UUID        `json:"space_id,omitempty" db:"space_id"`
	EventRuleID *uuid.UUID        `json:"event_rule_id,omitempty" db:"event_rule_id"`
	CreatedAt   time.Time         `json:"created_at" db:"created_at"`
	UpdatedAt   time.Time         `json:"updated_at" db:"updated_at"`
}

GeofenceWithSpace represents a geofence with its associated space details

type Geometry added in v0.0.2

type Geometry json.RawMessage

Geometry represents PostGIS geometry as GeoJSON

func (Geometry) MarshalJSON added in v0.0.2

func (g Geometry) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface

func (*Geometry) Scan added in v0.0.2

func (g *Geometry) Scan(value interface{}) error

Scan implements sql.Scanner interface

func (*Geometry) UnmarshalJSON added in v0.0.2

func (g *Geometry) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler interface

func (Geometry) Value added in v0.0.2

func (g Geometry) Value() (driver.Value, error)

Value implements sql.Valuer interface

type Location added in v0.0.2

type Location struct {
	Latitude  float64 `json:"latitude"`
	Longitude float64 `json:"longitude"`
}

Location represents a geographic location

type LocationCoordinates

type LocationCoordinates struct {
	Latitude  float64 `json:"latitude"`
	Longitude float64 `json:"longitude"`
	Accuracy  float64 `json:"accuracy"`
	Direction *string `json:"direction,omitempty"`
}

LocationCoordinates represents geographic coordinates with accuracy

type MatchedEvent

type MatchedEvent struct {
	DeviceID       string    `json:"device_id"`
	EntityType     string    `json:"entity_type"`
	RuleKey        string    `json:"rule_key"`
	EventType      string    `json:"event_type"`
	EventLevel     string    `json:"event_level"`
	Title          string    `json:"title"`
	Description    string    `json:"description"`
	Value          float64   `json:"value"`
	Threshold      float64   `json:"threshold"`
	Operator       string    `json:"operator"`
	Timestamp      int64     `json:"timestamp"`               // Unix timestamp in milliseconds
	EventRuleID    *string   `json:"event_rule_id,omitempty"` // Rule that triggered this event
	AutomationID   *string   `json:"automation_id,omitempty"` // Automation that triggered this event
	AutomationName *string   `json:"automation_name,omitempty"`
	GeofenceID     *string   `json:"geofence_id,omitempty"` // Geofence that triggered this event
	GeofenceName   *string   `json:"geofence_name,omitempty"`
	StateID        uuid.UUID `json:"state_id"`
	Location       *Location `json:"location,omitempty"`
}

MatchedEvent represents an event rule that matched evaluation

type OrgDiscoveryRequest

type OrgDiscoveryRequest struct {
	EventType   OrgEventType `json:"event_type"`
	EventID     string       `json:"event_id"`
	Timestamp   time.Time    `json:"timestamp"`
	ServiceName string       `json:"service_name"`
	ReplyTo     string       `json:"reply_to"`
}

OrgDiscoveryRequest is sent to discover active organizations

type OrgDiscoveryResponse

type OrgDiscoveryResponse struct {
	EventType  OrgEventType `json:"event_type"`
	EventID    string       `json:"event_id"`
	Timestamp  time.Time    `json:"timestamp"`
	Spaces     []OrgInfo    `json:"spaces"`
	TotalCount int          `json:"total_count"`
}

OrgDiscoveryResponse contains all active organizations

type OrgEvent

type OrgEvent struct {
	EventType OrgEventType    `json:"event_type"`
	EventID   string          `json:"event_id"`
	Timestamp time.Time       `json:"timestamp"`
	Payload   OrgEventPayload `json:"payload"`
}

OrgEvent represents an organization lifecycle event

type OrgEventPayload

type OrgEventPayload struct {
	ID               string    `json:"id"`
	Slug             string    `json:"slug"`
	Name             string    `json:"name"`
	Vhost            string    `json:"vhost,omitempty"`
	Exchange         string    `json:"exchange,omitempty"`
	TransformerQueue string    `json:"transformer_queue,omitempty"`
	TelemetryQueue   string    `json:"telemetry_queue,omitempty"`
	IsActive         bool      `json:"is_active"`
	CreatedAt        time.Time `json:"created_at,omitempty"`
	UpdatedAt        time.Time `json:"updated_at,omitempty"`
}

OrgEventPayload contains organization details

type OrgEventType

type OrgEventType string

OrgEventType represents the type of organization event

const (
	// OrgCreated is emitted when a new organization is created
	OrgCreated OrgEventType = "org.created"
	// OrgUpdated is emitted when an organization is updated
	OrgUpdated OrgEventType = "org.updated"
	// OrgDeleted is emitted when an organization is deleted
	OrgDeleted OrgEventType = "org.deleted"
	// OrgDeactivated is emitted when an organization is deactivated
	OrgDeactivated OrgEventType = "org.deactivated"
	// OrgActivated is emitted when an organization is activated
	OrgActivated OrgEventType = "org.activated"
	// OrgDiscoveryReq is emitted when requesting active orgs
	OrgDiscoveryReq OrgEventType = "org.discovery.request"
	// OrgDiscoveryResp is the response with active orgs
	OrgDiscoveryResp OrgEventType = "org.discovery.response"
)

type OrgInfo

type OrgInfo struct {
	Slug     string `json:"slug"`
	Name     string `json:"name"`
	Vhost    string `json:"vhost"`
	IsActive bool   `json:"is_active"`
}

OrgInfo contains basic organization information

type SpaceData added in v0.0.2

type SpaceData struct {
	ID           uuid.UUID `json:"-"`
	Name         string    `json:"name"`
	SlugName     string    `json:"slug_name"`
	IsActive     bool      `json:"is_active"`
	IsDefault    bool      `json:"is_default"`
	TotalDevices int       `json:"total_devices"`
	Description  *string   `json:"description,omitempty"`
	CreatedBy    uuid.UUID `json:"-"`
}

SpaceData represents the space data sent in Celery update_space task

func (*SpaceData) UnmarshalJSON added in v0.0.2

func (s *SpaceData) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling for SpaceData to handle UUID fields that come in Django's special format

type State

type State struct {
	StateID       uuid.UUID `json:"state_id" db:"state_id"`
	MetadataID    int       `json:"metadata_id" db:"metadata_id"`
	State         string    `json:"state" db:"state"`
	AttributesID  *int      `json:"attributes_id,omitempty" db:"attributes_id"`
	EventID       *int64    `json:"event_id,omitempty" db:"event_id"`
	LastChangedTs int64     `json:"last_changed_ts" db:"last_changed_ts"`
	LastUpdatedTs int64     `json:"last_updated_ts" db:"last_updated_ts"`
	CreatedAt     time.Time `json:"created_at" db:"created_at"`

	// Joined fields
	EntityID    string          `json:"entity_id,omitempty" db:"entity_id"`
	SharedAttrs json.RawMessage `json:"shared_attrs,omitempty" db:"shared_attrs"`
}

State represents an entity state with event linkage

func (*State) LastChangedTime

func (s *State) LastChangedTime() time.Time

TimestampsToTime converts timestamp fields to time.Time

func (*State) LastUpdatedTime

func (s *State) LastUpdatedTime() time.Time

LastUpdatedTime converts the last_updated_ts to time.Time

func (*State) ParseAttributes

func (s *State) ParseAttributes() (map[string]interface{}, error)

ParseAttributes parses the shared_attrs JSON into a map

type StateAttributes

type StateAttributes struct {
	AttributesID int             `json:"attributes_id" db:"attributes_id"`
	Hash         int64           `json:"hash" db:"hash"`
	SharedAttrs  json.RawMessage `json:"shared_attrs" db:"shared_attrs"`
	CreatedAt    time.Time       `json:"created_at" db:"created_at"`
}

StateAttributes represents shared state attributes (deduplicated by hash)

func (*StateAttributes) SetSharedAttrs

func (s *StateAttributes) SetSharedAttrs(attrs map[string]interface{}) error

SetSharedAttrs sets the shared_attrs from a map

type StateChangeRequest

type StateChangeRequest struct {
	EntityID    string                 `json:"entity_id"`
	SpaceSlug   string                 `json:"space_slug,omitempty"` // optional, will be resolved from headers if not provided
	NewState    string                 `json:"new_state"`
	OldState    string                 `json:"old_state,omitempty"`
	Attributes  map[string]interface{} `json:"attributes,omitempty"`
	EventType   string                 `json:"event_type,omitempty"` // defaults to "state_changed"
	TimeFiredTs *int64                 `json:"time_fired_ts,omitempty"`
	ContextID   []byte                 `json:"context_id,omitempty"`
	TriggerID   *string                `json:"trigger_id,omitempty"` // for future automations reference
}

StateChangeRequest represents a request to record a state change

type StateDetailResponse

type StateDetailResponse struct {
	StateID         uuid.UUID              `json:"state_id"`
	EntityID        string                 `json:"entity_id"`
	State           string                 `json:"state"`
	Attributes      map[string]interface{} `json:"attributes,omitempty"`
	LastChanged     time.Time              `json:"last_changed"`
	LastUpdated     time.Time              `json:"last_updated"`
	TriggeringEvent *EventDetail           `json:"triggering_event,omitempty"`
}

StateDetailResponse combines state with metadata and attributes

type StateHistoryResponse

type StateHistoryResponse struct {
	EntityID    string                 `json:"entity_id"`
	State       string                 `json:"state"`
	Attributes  map[string]interface{} `json:"attributes,omitempty"`
	LastChanged time.Time              `json:"last_changed"`
	LastUpdated time.Time              `json:"last_updated"`
	EventID     *int64                 `json:"event_id,omitempty"`
}

StateHistoryResponse represents historical state data for an entity

type StatesMeta

type StatesMeta struct {
	MetadataID int       `json:"metadata_id" db:"metadata_id"`
	EntityID   string    `json:"entity_id" db:"entity_id"`
	CreatedAt  time.Time `json:"created_at" db:"created_at"`
}

StatesMeta represents metadata about entity states

type TelemetryDevice

type TelemetryDevice struct {
	Identifiers  []string `json:"identifiers"`
	Name         string   `json:"name"`
	Manufacturer string   `json:"manufacturer"`
	Model        string   `json:"model"`
	ModelID      string   `json:"model_id"`
}

TelemetryDevice holds basic device metadata.

type TelemetryEntity

type TelemetryEntity struct {
	UniqueID    string         `json:"unique_id"`
	EntityID    string         `json:"entity_id"`
	EntityType  string         `json:"entity_type"`
	DeviceClass string         `json:"device_class,omitempty"`
	Name        string         `json:"name"`
	State       any            `json:"state"`
	DisplayType []string       `json:"display_type,omitempty"`
	Attributes  map[string]any `json:"attributes,omitempty"`
	UnitOfMeas  string         `json:"unit_of_measurement,omitempty"`
	Icon        string         `json:"icon,omitempty"`
	Timestamp   string         `json:"timestamp"`
	StateID     uuid.UUID      `json:"state_id,omitempty"`
}

TelemetryEntity describes a single entity in telemetry payloads.

type TelemetryPayload

type TelemetryPayload struct {
	Organization string            `json:"organization"`
	DeviceEUI    string            `json:"device_eui"`
	DeviceID     string            `json:"device_id,omitempty"`
	SpaceSlug    string            `json:"space_slug,omitempty"`
	DeviceInfo   TelemetryDevice   `json:"device_info"`
	Entities     []TelemetryEntity `json:"entities"`
	Timestamp    string            `json:"timestamp"`
	Source       string            `json:"source"`
	Metadata     map[string]any    `json:"metadata,omitempty"`
}

TelemetryPayload represents the entity-centric telemetry emitted by transformer-service.

type UpdateSpaceTask added in v0.0.2

type UpdateSpaceTask struct {
	OrganizationSlugName string    `json:"organization_slug_name"`
	Data                 SpaceData `json:"data"`
}

UpdateSpaceTask represents the Celery task kwargs for update_space

Jump to

Keyboard shortcuts

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