github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/supervisor/types.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package supervisor
    20  
    21  import (
    22  	"context"
    23  	"github.com/e154/smart-home/common/events"
    24  	"time"
    25  
    26  	"github.com/e154/smart-home/adaptors"
    27  	"github.com/e154/smart-home/common"
    28  	"github.com/e154/smart-home/common/web"
    29  	m "github.com/e154/smart-home/models"
    30  	"github.com/e154/smart-home/system/bus"
    31  	"github.com/e154/smart-home/system/mqtt"
    32  	"github.com/e154/smart-home/system/scheduler"
    33  	"github.com/e154/smart-home/system/scripts"
    34  )
    35  
    36  // PluginInfo ...
    37  type PluginInfo struct {
    38  	Name    string `json:"name"`
    39  	Version string `json:"version"`
    40  	Enabled bool   `json:"enabled"`
    41  	System  bool   `json:"system"`
    42  }
    43  
    44  // Supervisor ...
    45  type Supervisor interface {
    46  	Start(context.Context) error
    47  	Shutdown(context.Context) error
    48  	Restart(context.Context) error
    49  	GetPlugin(name string) (interface{}, error)
    50  	EnablePlugin(context.Context, string) error
    51  	DisablePlugin(context.Context, string) error
    52  	PluginList() (list []PluginInfo, total int64, err error)
    53  	SetMetric(common.EntityId, string, map[string]interface{})
    54  	SetState(common.EntityId, EntityStateParams) error
    55  	GetActorById(common.EntityId) (PluginActor, error)
    56  	CallAction(common.EntityId, string, map[string]interface{})
    57  	CallActionV2(CallActionV2, map[string]interface{})
    58  	CallScene(common.EntityId, map[string]interface{})
    59  	AddEntity(*m.Entity) error
    60  	GetEntityById(common.EntityId) (m.EntityShort, error)
    61  	UpdateEntity(*m.Entity) error
    62  	UnloadEntity(common.EntityId)
    63  	EntityIsLoaded(id common.EntityId) bool
    64  	PluginIsLoaded(string) bool
    65  	GetService() Service
    66  	GetPluginReadme(context.Context, string, *string) ([]byte, error)
    67  	PushSystemEvent(strCommand string, params map[string]interface{})
    68  }
    69  
    70  // PluginActor ...
    71  type PluginActor interface {
    72  	Spawn()
    73  	Destroy()
    74  	StopWatchers()
    75  	Attributes() m.Attributes
    76  	Settings() m.Attributes
    77  	Metrics() []*m.Metric
    78  	SetState(EntityStateParams) error
    79  	Info() ActorInfo
    80  	GetCurrentState() *events.EventEntityState
    81  	GetOldState() *events.EventEntityState
    82  	SetCurrentState(events.EventEntityState)
    83  	GetEventState() events.EventEntityState
    84  	AddMetric(name string, value map[string]interface{})
    85  	MatchTags(tags []string) bool
    86  	Area() *m.Area
    87  }
    88  
    89  // ActorConstructor ...
    90  type ActorConstructor func(*m.Entity) (PluginActor, error)
    91  
    92  // ActorAction ...
    93  type ActorAction struct {
    94  	Name         string                 `json:"name"`
    95  	Description  string                 `json:"description"`
    96  	ImageUrl     *string                `json:"image_url"`
    97  	Icon         *string                `json:"icon"`
    98  	ScriptEngine *scripts.EngineWatcher `json:"-"`
    99  }
   100  
   101  // ToEntityActionShort ...
   102  func ToEntityActionShort(from map[string]ActorAction) (to map[string]m.EntityActionShort) {
   103  	to = make(map[string]m.EntityActionShort)
   104  	for k, v := range from {
   105  		to[k] = m.EntityActionShort{
   106  			Name:        v.Name,
   107  			Description: v.Description,
   108  			ImageUrl:    v.ImageUrl,
   109  			Icon:        v.Icon,
   110  		}
   111  	}
   112  	return
   113  }
   114  
   115  // ActorState ...
   116  type ActorState struct {
   117  	Name        string  `json:"name"`
   118  	Description string  `json:"description"`
   119  	ImageUrl    *string `json:"image_url"`
   120  	Icon        *string `json:"icon"`
   121  }
   122  
   123  // ToEntityStateShort ...
   124  func ToEntityStateShort(from map[string]ActorState) (to map[string]m.EntityStateShort) {
   125  	to = make(map[string]m.EntityStateShort)
   126  	for k, v := range from {
   127  		to[k] = m.EntityStateShort{
   128  			Name:        v.Name,
   129  			Description: v.Description,
   130  			ImageUrl:    v.ImageUrl,
   131  			Icon:        v.Icon,
   132  		}
   133  	}
   134  	return
   135  }
   136  
   137  // Copy ...
   138  func (a *ActorState) Copy() (state *ActorState) {
   139  
   140  	if a == nil {
   141  		return nil
   142  	}
   143  
   144  	state = &ActorState{
   145  		Name:        a.Name,
   146  		Description: a.Description,
   147  	}
   148  	if a.ImageUrl != nil {
   149  		state.ImageUrl = common.String(*a.ImageUrl)
   150  	}
   151  	if a.Icon != nil {
   152  		state.Icon = common.String(*a.Icon)
   153  	}
   154  	return
   155  }
   156  
   157  const (
   158  	// StateAwait ...
   159  	StateAwait = "await"
   160  	// StateOk ...
   161  	StateOk = "ok"
   162  	// StateError ...
   163  	StateError = "error"
   164  	// StateInProcess ...
   165  	StateInProcess = "in process"
   166  )
   167  
   168  // ActorInfo ...
   169  type ActorInfo struct {
   170  	Id                common.EntityId        `json:"id"`
   171  	ParentId          *common.EntityId       `json:"parent_id"`
   172  	PluginName        string                 `json:"plugin_name"`
   173  	Name              string                 `json:"name"`
   174  	Description       string                 `json:"description"`
   175  	Hidde             bool                   `json:"hidde"`
   176  	UnitOfMeasurement string                 `json:"unit_of_measurement"`
   177  	LastChanged       *time.Time             `json:"last_changed"`
   178  	LastUpdated       *time.Time             `json:"last_updated"`
   179  	DependsOn         []string               `json:"depends_on"`
   180  	State             *ActorState            `json:"state"`
   181  	ImageUrl          *string                `json:"image_url"`
   182  	Icon              *string                `json:"icon"`
   183  	Area              *m.Area                `json:"area"`
   184  	AutoLoad          bool                   `json:"auto_load"`
   185  	RestoreState      bool                   `json:"restoreState"`
   186  	Value             interface{}            `json:"value"`
   187  	States            map[string]ActorState  `json:"states"`
   188  	Actions           map[string]ActorAction `json:"actions"`
   189  }
   190  
   191  // PluginType ...
   192  type PluginType string
   193  
   194  const (
   195  	// PluginBuiltIn ...
   196  	PluginBuiltIn = PluginType("System")
   197  	// PluginInstallable ...
   198  	PluginInstallable = PluginType("Installable")
   199  )
   200  
   201  // Service ...
   202  type Service interface {
   203  	Plugins() map[string]Pluggable
   204  	EventBus() bus.Bus
   205  	Adaptors() *adaptors.Adaptors
   206  	Supervisor() Supervisor
   207  	ScriptService() scripts.ScriptService
   208  	MqttServ() mqtt.MqttServ
   209  	AppConfig() *m.AppConfig
   210  	Scheduler() *scheduler.Scheduler
   211  	Crawler() web.Crawler
   212  }
   213  
   214  // Pluggable ...
   215  type Pluggable interface {
   216  	Load(context.Context, Service) error
   217  	Unload(context.Context) error
   218  	Name() string
   219  	Type() PluginType
   220  	Depends() []string
   221  	Version() string
   222  	Options() m.PluginOptions
   223  	EntityIsLoaded(id common.EntityId) bool
   224  	GetActor(id common.EntityId) (pla PluginActor, err error)
   225  	AddOrUpdateActor(*m.Entity) error
   226  	RemoveActor(common.EntityId) error
   227  	Readme(lang *string) ([]byte, error)
   228  }
   229  
   230  // Installable ...
   231  type Installable interface {
   232  	Install() error
   233  	Uninstall() error
   234  }
   235  
   236  type CallActionV2 struct {
   237  	EntityId   *common.EntityId `json:"entity_id"`
   238  	ActionName string           `json:"action_name"`
   239  	Tags       []string         `json:"tags"`
   240  	AreaId     *int64           `json:"area_id"`
   241  }