github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/common/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 common
    20  
    21  import (
    22  	"strings"
    23  
    24  	"github.com/gin-gonic/gin"
    25  )
    26  
    27  // ScriptLang ...
    28  type ScriptLang string
    29  
    30  const (
    31  	// ScriptLangTs ...
    32  	ScriptLangTs = ScriptLang("ts")
    33  	// ScriptLangCoffee ...
    34  	ScriptLangCoffee = ScriptLang("coffeescript")
    35  	// ScriptLangJavascript ...
    36  	ScriptLangJavascript = ScriptLang("javascript")
    37  )
    38  
    39  // StatusType ...
    40  type StatusType string
    41  
    42  const (
    43  	// Enabled ...
    44  	Enabled = StatusType("enabled")
    45  	// Disabled ...
    46  	Disabled = StatusType("disabled")
    47  	// Frozen ...
    48  	Frozen = StatusType("frozen")
    49  )
    50  
    51  // LogLevel ...
    52  type LogLevel string
    53  
    54  const (
    55  	// LogLevelEmergency ...
    56  	LogLevelEmergency = LogLevel("Emergency")
    57  	// LogLevelAlert ...
    58  	LogLevelAlert = LogLevel("Alert")
    59  	// LogLevelCritical ...
    60  	LogLevelCritical = LogLevel("Critical")
    61  	// LogLevelError ...
    62  	LogLevelError = LogLevel("Error")
    63  	// LogLevelWarning ...
    64  	LogLevelWarning = LogLevel("Warning")
    65  	// LogLevelNotice ...
    66  	LogLevelNotice = LogLevel("Notice")
    67  	// LogLevelInfo ...
    68  	LogLevelInfo = LogLevel("Info")
    69  	// LogLevelDebug ...
    70  	LogLevelDebug = LogLevel("Debug")
    71  )
    72  
    73  // GinEngine ...
    74  type GinEngine interface {
    75  	GetEngine() *gin.Engine
    76  }
    77  
    78  // EntityHistoryType ...
    79  type EntityHistoryType string
    80  
    81  const (
    82  	// EntityHistoryState ...
    83  	EntityHistoryState = EntityHistoryType("state")
    84  	// EntityHistoryOption ...
    85  	EntityHistoryOption = EntityHistoryType("option")
    86  )
    87  
    88  // MetricType ...
    89  type MetricType string
    90  
    91  const (
    92  	// MetricTypeLine ...
    93  	MetricTypeLine = MetricType("line")
    94  	// MetricTypeBar ...
    95  	MetricTypeBar = MetricType("bar")
    96  	// MetricTypeDoughnut ...
    97  	MetricTypeDoughnut = MetricType("doughnut")
    98  	// MetricTypeRadar ...
    99  	MetricTypeRadar = MetricType("radar")
   100  	// MetricTypePie ...
   101  	MetricTypePie = MetricType("pie")
   102  	// MetricTypeHorizontalBar ...
   103  	MetricTypeHorizontalBar = MetricType("horizontal bar")
   104  )
   105  
   106  type MetricRange string
   107  
   108  const (
   109  	MetricRange6H  = MetricRange("6h")
   110  	MetricRange12H = MetricRange("12h")
   111  	MetricRange24H = MetricRange("24h")
   112  	MetricRange7d  = MetricRange("7d")
   113  	MetricRange30d = MetricRange("30d")
   114  	MetricRange1m  = MetricRange("1m")
   115  )
   116  
   117  func (m MetricRange) String() string {
   118  	return string(m)
   119  }
   120  
   121  func (m MetricRange) Ptr() *MetricRange {
   122  	return &m
   123  }
   124  
   125  // EntityId ...
   126  type EntityId string
   127  
   128  // NewEntityId ...
   129  func NewEntityId(s string) *EntityId {
   130  	e := EntityId(s)
   131  	return &e
   132  }
   133  
   134  // NewEntityIdFromPtr ...
   135  func NewEntityIdFromPtr(s *string) *EntityId {
   136  	if s == nil {
   137  		return nil
   138  	}
   139  	e := EntityId(*s)
   140  	return &e
   141  }
   142  
   143  // Name ...
   144  func (e EntityId) Name() string {
   145  	arr := strings.Split(string(e), ".")
   146  	if len(arr) > 1 {
   147  		return arr[1]
   148  	}
   149  	return string(e)
   150  }
   151  
   152  // PluginName ...
   153  func (e EntityId) PluginName() string {
   154  	arr := strings.Split(string(e), ".")
   155  	if len(arr) > 1 {
   156  		return arr[0]
   157  	}
   158  	return string(e)
   159  }
   160  
   161  // String ...
   162  func (e *EntityId) String() string {
   163  	if e == nil {
   164  		return ""
   165  	} else {
   166  		return string(*e)
   167  	}
   168  }
   169  
   170  // StringPtr ...
   171  func (e *EntityId) StringPtr() *string {
   172  	if e == nil {
   173  		return nil
   174  	}
   175  	r := e.String()
   176  	return &r
   177  }
   178  
   179  // Ptr ...
   180  func (e EntityId) Ptr() *EntityId {
   181  	return &e
   182  }
   183  
   184  // AttributeType ...
   185  type AttributeType string
   186  
   187  const (
   188  	// AttributeString ...
   189  	AttributeString = AttributeType("string")
   190  	// AttributeInt ...
   191  	AttributeInt = AttributeType("int")
   192  	// AttributeTime ...
   193  	AttributeTime = AttributeType("time")
   194  	// AttributeBool ...
   195  	AttributeBool = AttributeType("bool")
   196  	// AttributeFloat ...
   197  	AttributeFloat = AttributeType("float")
   198  	// AttributeImage ...
   199  	AttributeImage = AttributeType("image")
   200  	// AttributeIcon ...
   201  	AttributeIcon = AttributeType("icon")
   202  	// AttributePoint ...
   203  	AttributePoint = AttributeType("point")
   204  	// AttributeEncrypted ...
   205  	AttributeEncrypted = AttributeType("encrypted")
   206  	//DEPRECATED
   207  	AttributeArray = AttributeType("array")
   208  	//DEPRECATED
   209  	AttributeMap = AttributeType("map")
   210  )
   211  
   212  // ConditionType ...
   213  type ConditionType string
   214  
   215  const (
   216  	// ConditionOr ...
   217  	ConditionOr = ConditionType("or")
   218  	// ConditionAnd ...
   219  	ConditionAnd = ConditionType("and")
   220  )
   221  
   222  // RunMode ...
   223  type RunMode string
   224  
   225  const (
   226  	// DebugMode ...
   227  	DebugMode = RunMode("debug")
   228  	// ReleaseMode ...
   229  	ReleaseMode = RunMode("release")
   230  	// DemoMode
   231  	DemoMode = RunMode("demo")
   232  )
   233  
   234  // PageParams ...
   235  type PageParams struct {
   236  	Limit   int64  `json:"limit" validate:"required,gte=1,lte=1000"`
   237  	Offset  int64  `json:"offset" validate:"required,gte=0,lte=1000"`
   238  	Order   string `json:"order" validate:"required,oneof=created_at"`
   239  	SortBy  string `json:"sort_by" validate:"required,oneof=desc asc"`
   240  	PageReq int64
   241  	SortReq string
   242  }
   243  
   244  // SearchParams ...
   245  type SearchParams struct {
   246  	Query  string `json:"query" validate:"required,min=1,max;255"`
   247  	Limit  int64  `json:"limit" validate:"required,gte=1,lte=1000"`
   248  	Offset int64  `json:"offset" validate:"required,gte=0,lte=1000"`
   249  }