Esempio n. 1
0
 public function getRecentSensorEventQuery($limit = 10)
 {
     return SensorEvent::find()->where(['sensor_id' => $this->id])->orderBy(['created' => SORT_DESC])->limit($limit);
 }
Esempio n. 2
0
 protected function triggerStateChange(CheckEvent $event)
 {
     $payload = [];
     $duration = false;
     $previousSensorEvent = SensorEvent::find()->where(['sensor_id' => $this->model->primaryKey])->orderBy(['created' => SORT_DESC])->one();
     if ($previousSensorEvent) {
         $durationSeconds = time() - strtotime($previousSensorEvent->created . ' UTC');
         $duration = DateHelper::niceDuration($durationSeconds, 2);
     }
     $sensorEvent = new SensorEvent();
     $sensorEvent->sensor_id = $this->model->primaryKey;
     $sensorEvent->old_state = $this->model->state;
     if (empty($sensorEvent->old_state)) {
         $sensorEvent->old_state = BaseSensor::STATE_UNCHECKED;
     }
     $payload['oldState'] = $sensorEvent->old_state;
     $sensorEvent->new_state = $payload['newState'] = $event->state;
     $sensorEvent->data = serialize($event);
     $sensorEvent->save();
     $this->model->state = $event->state;
     $payload['duration'] = $duration;
     $payload['sensor'] = [];
     $payload['sensor']['id'] = $this->model->primaryKey;
     $payload['sensor']['system_id'] = $this->model->system_id;
     $payload['sensor']['name'] = $this->object->name;
     $payload['object']['id'] = null;
     $payload['object']['name'] = null;
     $objectId = $this->model->primaryKey;
     if (!empty($this->objectModel)) {
         $payload['object']['id'] = $this->objectModel->primaryKey;
         $payload['object']['name'] = $this->objectModel->descriptor;
     }
     $priority = EventType::PRIORITY_MEDIUM;
     if ($this->object->isCritical && $payload['oldState'] !== BaseSensor::STATE_UNCHECKED) {
         $priority = EventType::PRIORITY_CRITICAL;
     }
     $this->triggerBroadcastEvent('sensor_state_changed', $payload, $objectId, $priority);
     return true;
 }