Stian Fredrik Aune
3 years ago
5 changed files with 191 additions and 14 deletions
-
22app/services/handlerfactory/config.go
-
92app/services/handlerfactory/factory.go
-
72app/services/handlerfactory/utils.go
-
4cmd/lucy/factorycmd.go
-
15cmd/lucy/help.go
@ -0,0 +1,72 @@ |
|||
package handlerfactory |
|||
|
|||
import ( |
|||
"git.aiterp.net/lucifer/new-server/models" |
|||
) |
|||
|
|||
func optimizeLists( |
|||
add []models.EventHandler, |
|||
del []models.EventHandler, |
|||
) ([]models.EventHandler, []models.EventHandler) { |
|||
newAdd := make([]models.EventHandler, 0, len(add)) |
|||
newDel := make([]models.EventHandler, 0, len(del)) |
|||
|
|||
OptimizeLoop1: |
|||
for _, addItem := range add { |
|||
for _, delItem := range del { |
|||
if checkDuplicate(&addItem, &delItem) { |
|||
continue OptimizeLoop1 |
|||
} |
|||
} |
|||
|
|||
newAdd = append(newAdd, addItem) |
|||
} |
|||
|
|||
OptimizeLoop2: |
|||
for _, delItem := range del { |
|||
for _, addItem := range add { |
|||
if checkDuplicate(&addItem, &delItem) { |
|||
continue OptimizeLoop2 |
|||
} |
|||
} |
|||
|
|||
newDel = append(newDel, delItem) |
|||
} |
|||
|
|||
return newAdd, newDel |
|||
} |
|||
|
|||
func checkDuplicate(a *models.EventHandler, b *models.EventHandler) bool { |
|||
check := true |
|||
check = check && a.EventName == b.EventName |
|||
check = check && len(a.Conditions) == len(b.Conditions) |
|||
for k := range a.Conditions { |
|||
check = check && a.Conditions[k] == b.Conditions[k] |
|||
} |
|||
check = check && a.OneShot == b.OneShot |
|||
check = check && a.Priority == b.Priority |
|||
check = check && a.TargetKind == b.TargetKind |
|||
check = check && a.TargetValue == b.TargetValue |
|||
check = check && a.From == b.From |
|||
check = check && a.To == b.To |
|||
|
|||
aPower := a.Actions.SetPower |
|||
bPower := b.Actions.SetPower |
|||
powerEq := (aPower == nil && bPower == nil) || (aPower != nil && bPower != nil && *aPower == *bPower) |
|||
|
|||
aColor := a.Actions.SetColor |
|||
bColor := b.Actions.SetColor |
|||
colorEq := (aColor == nil && bColor == nil) || (aColor != nil && bColor != nil && *aColor == *bColor) |
|||
|
|||
aInt := a.Actions.SetIntensity |
|||
bInt := b.Actions.SetIntensity |
|||
intEq := (aInt == nil && bInt == nil) || (aInt != nil && bInt != nil && *aInt == *bInt) |
|||
|
|||
aTemp := a.Actions.SetTemperature |
|||
bTemp := b.Actions.SetTemperature |
|||
tempEq := (aTemp == nil && bTemp == nil) || (aTemp != nil && bTemp != nil && *aTemp == *bTemp) |
|||
|
|||
check = check && powerEq && colorEq && intEq && tempEq |
|||
|
|||
return check |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue