|
|
@ -1,15 +1,17 @@ |
|
|
|
package main |
|
|
|
|
|
|
|
import ( |
|
|
|
"git.aiterp.net/lucifer/new-server/models" |
|
|
|
"log" |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
) |
|
|
|
|
|
|
|
type Param struct { |
|
|
|
Index int |
|
|
|
Key string |
|
|
|
Value string |
|
|
|
Index int |
|
|
|
Key string |
|
|
|
Value string |
|
|
|
Operator string |
|
|
|
} |
|
|
|
|
|
|
|
func (p *Param) String() *string { |
|
|
@ -28,7 +30,6 @@ func (p *Param) StringOr(fallback string) string { |
|
|
|
return p.Value |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (p *Param) Int() *int { |
|
|
|
if p == nil { |
|
|
|
return nil |
|
|
@ -42,6 +43,15 @@ func (p *Param) Int() *int { |
|
|
|
return &n |
|
|
|
} |
|
|
|
|
|
|
|
func (p *Param) IntOr(other int) int { |
|
|
|
val := p.Int() |
|
|
|
if val == nil { |
|
|
|
return other |
|
|
|
} |
|
|
|
|
|
|
|
return *val |
|
|
|
} |
|
|
|
|
|
|
|
func (p *Param) Float() *float64 { |
|
|
|
if p == nil { |
|
|
|
return nil |
|
|
@ -108,7 +118,7 @@ func (p Params) Subset(prefix string) Params { |
|
|
|
|
|
|
|
for _, param := range p { |
|
|
|
if param.Index == -1 && strings.HasPrefix(param.Key, prefix) { |
|
|
|
res = append(res, Param{Index: -1, Key: param.Key[len(prefix):], Value: param.Value}) |
|
|
|
res = append(res, Param{Index: -1, Key: param.Key[len(prefix):], Value: param.Value, Operator: param.Operator}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -152,6 +162,34 @@ func (p Params) Strings(minIndex int) []string { |
|
|
|
return res |
|
|
|
} |
|
|
|
|
|
|
|
func (p Params) EventConditions() map[string]models.EventCondition { |
|
|
|
ecMap := make(map[string]models.EventCondition, len(p)) |
|
|
|
|
|
|
|
for _, param := range p { |
|
|
|
element, ok := ecMap[param.Key] |
|
|
|
if !ok { |
|
|
|
element = models.EventCondition{} |
|
|
|
} |
|
|
|
|
|
|
|
switch param.Operator { |
|
|
|
case "<": |
|
|
|
element.LT = param.Value |
|
|
|
case "<=": |
|
|
|
element.LTE = param.Value |
|
|
|
case ">=": |
|
|
|
element.GTE = param.Value |
|
|
|
case ">": |
|
|
|
element.GT = param.Value |
|
|
|
default: |
|
|
|
element.EQ = param.Value |
|
|
|
} |
|
|
|
|
|
|
|
ecMap[param.Key] = element |
|
|
|
} |
|
|
|
|
|
|
|
return ecMap |
|
|
|
} |
|
|
|
|
|
|
|
type Command struct { |
|
|
|
Name string |
|
|
|
Params Params |
|
|
@ -169,10 +207,22 @@ func parseCommand(args []string) Command { |
|
|
|
|
|
|
|
nextIndex := 0 |
|
|
|
for _, arg := range args[1:] { |
|
|
|
kv := strings.SplitN(arg, "=", 2) |
|
|
|
|
|
|
|
if len(kv) == 2 { |
|
|
|
cmd.Params = append(cmd.Params, Param{Index: -1, Key: kv[0], Value: kv[1]}) |
|
|
|
kvle := strings.SplitN(arg, "<=", 2) |
|
|
|
kvge := strings.SplitN(arg, ">=", 2) |
|
|
|
kvl := strings.SplitN(arg, "<", 2) |
|
|
|
kvg := strings.SplitN(arg, ">", 2) |
|
|
|
kve := strings.SplitN(arg, "=", 2) |
|
|
|
|
|
|
|
if len(kvle) == 2 { |
|
|
|
cmd.Params = append(cmd.Params, Param{Index: -1, Key: kvle[0], Value: kvle[1], Operator: "<="}) |
|
|
|
} else if len(kvge) == 2 { |
|
|
|
cmd.Params = append(cmd.Params, Param{Index: -1, Key: kvge[0], Value: kvge[1], Operator: ">="}) |
|
|
|
} else if len(kvl) == 2 { |
|
|
|
cmd.Params = append(cmd.Params, Param{Index: -1, Key: kvl[0], Value: kvl[1], Operator: "<"}) |
|
|
|
} else if len(kvg) == 2 { |
|
|
|
cmd.Params = append(cmd.Params, Param{Index: -1, Key: kvg[0], Value: kvg[1], Operator: ">"}) |
|
|
|
} else if len(kve) == 2 { |
|
|
|
cmd.Params = append(cmd.Params, Param{Index: -1, Key: kve[0], Value: kve[1], Operator: "="}) |
|
|
|
} else { |
|
|
|
cmd.Params = append(cmd.Params, Param{Index: nextIndex, Value: arg}) |
|
|
|
nextIndex += 1 |
|
|
@ -181,3 +231,12 @@ func parseCommand(args []string) Command { |
|
|
|
|
|
|
|
return cmd |
|
|
|
} |
|
|
|
|
|
|
|
func (cmd Command) NewDeviceState(prefix string) models.NewDeviceState { |
|
|
|
return models.NewDeviceState{ |
|
|
|
Power: cmd.Params.Get(prefix + "power").Bool(), |
|
|
|
Color: cmd.Params.Get(prefix + "color").String(), |
|
|
|
Intensity: cmd.Params.Get(prefix + "intensity").Float(), |
|
|
|
Temperature: cmd.Params.Get(prefix + "temperature").Int(), |
|
|
|
} |
|
|
|
} |