logic

package
v2.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2022 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrExpressionNotBool = fmt.Errorf("invalid result of expression. Only bool expressions are valid")

Functions

This section is empty.

Types

type Logic

type Logic struct {
	StateStore *SavedStateStore
	Rules      map[string]*Rule

	// ActionProgressChan chan ActionProgress
	sync.RWMutex
	sync.WaitGroup

	WebsocketSender websocket.Sender
	// contains filtered or unexported fields
}

Logic is the main struct.

func New

func New(sss *SavedStateStore, websocketSender websocket.Sender) *Logic

New returns a new logic that is ready to use.

func (*Logic) AddRule

func (l *Logic) AddRule(name string) *Rule

AddRule adds a new logic rule. Mainly used in tests.

func (*Logic) EvaluateRules

func (l *Logic) EvaluateRules(ctx context.Context)

EvaluateRules loops over each rule and run evaluation on them.

func (*Logic) GetRules

func (l *Logic) GetRules() Rules

GetRules returns a list of Rules.

func (*Logic) Load

func (l *Logic) Load() error

Load loads the rules from rules.json.

func (*Logic) OnReportState

func (l *Logic) OnReportState(callback func(string, devices.State))

func (*Logic) OnTriggerDestination

func (l *Logic) OnTriggerDestination(callback func(string, string) error)

func (*Logic) Save

func (l *Logic) Save() error

Save saves the rules to rules.json.

func (*Logic) SetRules

func (l *Logic) SetRules(rules Rules)

SetRules overwrites all rules in logic.

func (*Logic) Start

func (l *Logic) Start(ctx context.Context)

Start starts the logic worker.

func (*Logic) UpdateDevice

func (l *Logic) UpdateDevice(dev *devices.Device)

UpdateDevice update the state in the logic store with the new state from the device.

type Rule

type Rule struct {
	Name_         string          `json:"name"`
	Uuid_         string          `json:"uuid"`
	Active_       bool            `json:"active"`
	Pending_      bool            `json:"pending"`
	Enabled       bool            `json:"enabled"`
	Expression_   string          `json:"expression"`
	Conditions_   map[string]bool `json:"conditions"`
	Actions_      []string        `json:"actions"`
	Labels_       models.Labels   `json:"labels"`
	For_          stypes.Duration `json:"for"`
	Type_         string          `json:"type"`
	Destinations_ []string        `json:"destinations"`

	sync.RWMutex
	// contains filtered or unexported fields
}

func (*Rule) Active

func (r *Rule) Active() bool

func (*Rule) Cancel

func (r *Rule) Cancel()

func (*Rule) Conditions

func (r *Rule) Conditions() map[string]bool

func (*Rule) Eval

func (r *Rule) Eval(devices *devices.List, rules map[string]bool) (bool, error)

Eval evaluates the cel expression.

func (*Rule) Expression

func (r *Rule) Expression() string

func (*Rule) For

func (r *Rule) For() stypes.Duration

func (*Rule) Name

func (r *Rule) Name() string

func (*Rule) Pending

func (r *Rule) Pending() bool

func (*Rule) Run

func (r *Rule) Run(store *SavedStateStore, sender websocket.Sender, triggerDestination func(string, string) error)

func (*Rule) SetActive

func (r *Rule) SetActive(a bool)

func (*Rule) SetPending

func (r *Rule) SetPending(a bool)

func (*Rule) SetUuid

func (r *Rule) SetUuid(uuid string)

func (*Rule) Stop

func (r *Rule) Stop()

func (*Rule) Type

func (r *Rule) Type() string

func (*Rule) Uuid

func (r *Rule) Uuid() string

type Rules

type Rules map[string]*Rule

Rules is a list of rules.

type SavedState

type SavedState struct {
	Name  string                       `json:"name"`
	UUID  string                       `json:"uuid"`
	State map[devices.ID]devices.State `json:"state"`
}

type SavedStateStore

type SavedStateStore struct {
	State SavedStates
	sync.RWMutex
}

func NewSavedStateStore

func NewSavedStateStore() *SavedStateStore

func (*SavedStateStore) All

func (sss *SavedStateStore) All() SavedStates

func (*SavedStateStore) Get

func (sss *SavedStateStore) Get(id string) *SavedState

func (*SavedStateStore) Load

func (sss *SavedStateStore) Load() error

func (*SavedStateStore) Save

func (sss *SavedStateStore) Save() error

func (*SavedStateStore) SetState

func (sss *SavedStateStore) SetState(s SavedStates)

type SavedStates

type SavedStates map[string]*SavedState

type Scheduler

type Scheduler struct {
	Cron *cron.Cron
	sync.RWMutex

	SavedStateStore *SavedStateStore
	// contains filtered or unexported fields
}

Scheduler that schedule running saved state actions.

func NewScheduler

func NewScheduler(savedStateStore *SavedStateStore, sender websocket.Sender, logic *Logic) *Scheduler

func (*Scheduler) AddTask

func (s *Scheduler) AddTask(name string) *Task

func (*Scheduler) Load

func (s *Scheduler) Load() error

func (*Scheduler) RemoveTask

func (s *Scheduler) RemoveTask(uuid string)

func (*Scheduler) Save

func (s *Scheduler) Save() error

func (*Scheduler) ScheduleTask

func (s *Scheduler) ScheduleTask(t *Task)

func (*Scheduler) SetTasks

func (s *Scheduler) SetTasks(t Tasks)

func (*Scheduler) Start

func (s *Scheduler) Start(parentCtx context.Context)

func (*Scheduler) Stop

func (s *Scheduler) Stop()

func (*Scheduler) Task

func (s *Scheduler) Task(uuid string) *Task

func (*Scheduler) Tasks

func (s *Scheduler) Tasks() Tasks

type Task

type Task struct {
	XName   string   `json:"name"`
	XUuid   string   `json:"uuid"`
	Actions []string `json:"actions"`

	When        string `json:"when"`
	Enabled     bool   `json:"enabled"`
	Expression_ string `json:"expression"`

	sync.RWMutex
	// contains filtered or unexported fields
}

Task is a task that can be scheduled using scheduler.

func (*Task) AddAction

func (t *Task) AddAction(uuid string)

func (*Task) CronId

func (t *Task) CronId() int64

func (*Task) Expression added in v2.2.0

func (t *Task) Expression() string

func (*Task) Name

func (t *Task) Name() string

func (*Task) Run

func (t *Task) Run()

func (*Task) SetUuid

func (t *Task) SetUuid(uuid string)

SetUuid sets ths uuid on the task.

func (*Task) SetWhen

func (t *Task) SetWhen(when string)

SetWhen sets when the task should be run in cron syntax.

func (*Task) Uuid

func (t *Task) Uuid() string

Uuid returns tasks uuid.

type Tasks

type Tasks map[string]*Task

Jump to

Keyboard shortcuts

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