Example #1
0
 /**
  * update sensor status with input status
  * - update temperature
  * - update humidity
  * - update security mode
  * - update all sensor equipment
  */
 public function updateSensor()
 {
     $inputBin = Convert::powOf2($this->request['input_status']);
     // get all sensor status of this station
     $query = new Query();
     $query->select('s.binary_pos, s.type, st.id, st.sensor_id, st.value');
     $query->from('sensor_status st');
     $query->leftJoin('sensor s', 'st.sensor_id = s.id');
     $query->where('station_id = ' . $this->request['id']);
     $sensors = $query->all();
     if (!empty($sensors)) {
         foreach ($sensors as $sensor) {
             $vs = in_array($sensor['binary_pos'], $inputBin) ? 1 : 0;
             if ($sensor['type'] == Sensor::TYPE_CONFIGURE) {
                 Yii::$app->db->createCommand()->update('sensor_status', ['value' => $vs], ['id' => $sensor['id']])->execute();
             } else {
                 if ($sensor['type'] == Sensor::TYPE_VALUE) {
                     $value = '';
                     // if this is security mode
                     if ($sensor['sensor_id'] == Sensor::ID_SECURITY) {
                         // security mode handler
                         if ($this->request['message'] == self::MSG_ARMING) {
                             $value = 1;
                         }
                         if ($this->request['message'] == self::MSG_DISARM) {
                             $value = 0;
                         }
                         // compare with handler status
                         if (isset($this->handler['security']['status']) && $value != $this->handler['security']['status']) {
                             $value = $this->handler['security']['status'];
                             $this->sendBack = true;
                         }
                         // if security has been turn off, create an alarm
                         if ($sensor['value'] != $value) {
                             if ($value == 0) {
                                 // create turn off security mode alarm
                                 $message = 'Tat bao dong';
                             } elseif ($value == 1) {
                                 // create turn on security mode alarm
                                 $message = 'Bat bao dong';
                             }
                             $this->alarm($message);
                         }
                     }
                     // if this is temperature
                     if ($sensor['sensor_id'] == Sensor::ID_TEMPERATURE) {
                         $value = $this->request['temp'];
                     }
                     // if this is humidity
                     if ($sensor['sensor_id'] == Sensor::ID_HUMIDITY) {
                         $value = $this->request['humi'];
                     }
                     Yii::$app->db->createCommand()->update('sensor_status', ['value' => $value], ['id' => $sensor['id']])->execute();
                 }
             }
         }
     }
 }