|
@ -92,7 +92,7 @@ type hueSensorState struct { |
|
|
uniqueID string |
|
|
uniqueID string |
|
|
prevData *SensorData |
|
|
prevData *SensorData |
|
|
prevTime time.Time |
|
|
prevTime time.Time |
|
|
presenceCooldown bool |
|
|
|
|
|
|
|
|
presenceCooldown int |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (state *hueSensorState) Update(newData SensorData) *models.Event { |
|
|
func (state *hueSensorState) Update(newData SensorData) *models.Event { |
|
@ -153,26 +153,36 @@ func (state *hueSensorState) Update(newData SensorData) *models.Event { |
|
|
case "ZLLPresence": |
|
|
case "ZLLPresence": |
|
|
{ |
|
|
{ |
|
|
if state.prevData != nil && state.prevData.State.Presence != newData.State.Presence { |
|
|
if state.prevData != nil && state.prevData.State.Presence != newData.State.Presence { |
|
|
name := models.ENSensorPresenceStarted |
|
|
|
|
|
if !newData.State.Presence { |
|
|
|
|
|
name = models.ENSensorPresenceEnding |
|
|
|
|
|
state.presenceCooldown = true |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if newData.State.Presence { |
|
|
|
|
|
state.presenceCooldown = -1 |
|
|
|
|
|
|
|
|
return &models.Event{ |
|
|
return &models.Event{ |
|
|
Name: name, |
|
|
|
|
|
|
|
|
Name: models.ENSensorPresenceStarted, |
|
|
Payload: map[string]string{ |
|
|
Payload: map[string]string{ |
|
|
"deviceId": strconv.Itoa(state.externalID), |
|
|
"deviceId": strconv.Itoa(state.externalID), |
|
|
"deviceInternalId": newData.UniqueID, |
|
|
"deviceInternalId": newData.UniqueID, |
|
|
}, |
|
|
}, |
|
|
} |
|
|
} |
|
|
} else if state.presenceCooldown && !newData.State.Presence && time.Since(stateTime) > time.Minute { |
|
|
|
|
|
state.presenceCooldown = false |
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
state.presenceCooldown = 0 |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if state.presenceCooldown == -2 { |
|
|
|
|
|
state.presenceCooldown = int(time.Since(stateTime) / time.Minute) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
nextEventWait := time.Minute * time.Duration(state.presenceCooldown) |
|
|
|
|
|
if state.presenceCooldown != -1 && !newData.State.Presence && time.Since(stateTime) > nextEventWait { |
|
|
|
|
|
state.presenceCooldown += 1 |
|
|
|
|
|
|
|
|
return &models.Event{ |
|
|
return &models.Event{ |
|
|
Name: models.ENSensorPresenceEnded, |
|
|
Name: models.ENSensorPresenceEnded, |
|
|
Payload: map[string]string{ |
|
|
Payload: map[string]string{ |
|
|
"deviceId": strconv.Itoa(state.externalID), |
|
|
"deviceId": strconv.Itoa(state.externalID), |
|
|
"deviceInternalId": newData.UniqueID, |
|
|
"deviceInternalId": newData.UniqueID, |
|
|
|
|
|
"minutesElapsed": strconv.Itoa(state.presenceCooldown - 1), |
|
|
|
|
|
"secondsElapsed": strconv.Itoa((state.presenceCooldown - 1) * 60), |
|
|
}, |
|
|
}, |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|