|
@ -13,11 +13,25 @@ type Scene struct { |
|
|
roleMap map[int]int |
|
|
roleMap map[int]int |
|
|
roleList map[int][]models.Device |
|
|
roleList map[int][]models.Device |
|
|
lastStates map[int]models.DeviceState |
|
|
lastStates map[int]models.DeviceState |
|
|
|
|
|
sensors []models.SceneSensor |
|
|
|
|
|
|
|
|
due bool |
|
|
due bool |
|
|
lastInterval int64 |
|
|
lastInterval int64 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *Scene) UpdateSensor(data models.SceneSensor) { |
|
|
|
|
|
s.due = true |
|
|
|
|
|
|
|
|
|
|
|
for i, sensor := range s.sensors { |
|
|
|
|
|
if sensor.ID == data.ID { |
|
|
|
|
|
s.sensors[i] = data |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
s.sensors = append(s.sensors, data) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// UpdateScene updates the scene data and re-seats all devices.
|
|
|
// UpdateScene updates the scene data and re-seats all devices.
|
|
|
func (s *Scene) UpdateScene(data models.Scene) { |
|
|
func (s *Scene) UpdateScene(data models.Scene) { |
|
|
devices := make([]models.Device, 0, 16) |
|
|
devices := make([]models.Device, 0, 16) |
|
@ -86,6 +100,13 @@ func (s *Scene) RemoveDevice(device models.Device) { |
|
|
|
|
|
|
|
|
delete(s.roleMap, device.ID) |
|
|
delete(s.roleMap, device.ID) |
|
|
delete(s.lastStates, device.ID) |
|
|
delete(s.lastStates, device.ID) |
|
|
|
|
|
|
|
|
|
|
|
for i, sensor := range s.sensors { |
|
|
|
|
|
if sensor.ID == device.ID { |
|
|
|
|
|
s.sensors = append(s.sensors[:i], s.sensors[i+1:]...) |
|
|
|
|
|
break |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (s *Scene) Empty() bool { |
|
|
func (s *Scene) Empty() bool { |
|
@ -130,6 +151,7 @@ func (s *Scene) Run() []models.Device { |
|
|
return []models.Device{} |
|
|
return []models.Device{} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
currentTime := time.Now() |
|
|
intervalNumber := int64(0) |
|
|
intervalNumber := int64(0) |
|
|
intervalMax := int64(1) |
|
|
intervalMax := int64(1) |
|
|
if s.data.IntervalMS > 0 { |
|
|
if s.data.IntervalMS > 0 { |
|
@ -149,14 +171,31 @@ func (s *Scene) Run() []models.Device { |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sensorCount := 0 |
|
|
|
|
|
for _, device := range list { |
|
|
|
|
|
if device.IsOnlySensor() { |
|
|
|
|
|
sensorCount += 1 |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
role := s.data.Roles[i] |
|
|
role := s.data.Roles[i] |
|
|
|
|
|
|
|
|
|
|
|
jSkip := 0 |
|
|
|
|
|
|
|
|
for j, device := range list { |
|
|
for j, device := range list { |
|
|
|
|
|
if device.IsOnlySensor() { |
|
|
|
|
|
updatedDevices = append(updatedDevices, device) |
|
|
|
|
|
jSkip += 1 |
|
|
|
|
|
continue |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
newState := role.ApplyEffect(&device, models.SceneRunContext{ |
|
|
newState := role.ApplyEffect(&device, models.SceneRunContext{ |
|
|
Index: j, |
|
|
|
|
|
Length: len(list), |
|
|
|
|
|
|
|
|
CurrentTime: currentTime, |
|
|
|
|
|
Index: j - jSkip, |
|
|
|
|
|
Length: len(list) - sensorCount, |
|
|
IntervalNumber: intervalNumber, |
|
|
IntervalNumber: intervalNumber, |
|
|
IntervalMax: intervalMax, |
|
|
IntervalMax: intervalMax, |
|
|
|
|
|
Sensors: s.sensors, |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
err := device.SetState(newState) |
|
|
err := device.SetState(newState) |
|
|