Example #1
0
 public function store()
 {
     if ($this->getUserId() != 0 and $this->getHostname() != "" and $this->getCrawlMethod() != "" and $this->getChipsetId() != 0) {
         $router_test = new Router(false, false, $this->getHostname());
         $router_test->fetch();
         if ($this->getRouterId() != 0 and !($router_test->getRouterId() != $this->getRouterId() and $router_test->getHostname() == $this->getHostname())) {
             try {
                 $stmt = DB::getInstance()->prepare("UPDATE routers SET\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tuser_id = ?,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\thostname = ?,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tdescription = ?,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tlocation = ?,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tlatitude = ?,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tlongitude = ?,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tchipset_id = ?,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tcrawl_method = ?,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tupdate_date = NOW()\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE id=?");
                 $stmt->execute(array($this->getUserId(), $this->getHostname(), $this->getDescription(), $this->getLocation(), $this->getLatitude(), $this->getLongitude(), $this->getChipsetId(), $this->getCrawlMethod(), $this->getRouterId()));
                 return $stmt->rowCount();
             } catch (PDOException $e) {
                 echo $e->getMessage();
                 echo $e->getTraceAsString();
             }
         } elseif ($router_test->getRouterId() == 0) {
             try {
                 $stmt = DB::getInstance()->prepare("INSERT INTO routers (user_id, hostname, description, location, \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tlatitude, longitude, chipset_id, crawl_method,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tcreate_date, update_date)\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tVALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())");
                 $stmt->execute(array($this->getUserId(), $this->getHostname(), $this->getDescription(), $this->getLocation(), $this->getLatitude(), $this->getLongitude(), $this->getChipsetId(), $this->getCrawlMethod()));
                 $this->setRouterId((int) DB::getInstance()->lastInsertId());
                 //create event for new router
                 $event = new Event(false, false, 'router', $this->getRouterId(), 'new', array('hostname' => $router->getHostname()));
                 $event->store();
                 return $this->getRouterId();
             } catch (PDOException $e) {
                 echo $e->getMessage();
                 echo $e->getTraceAsString();
             }
         }
     }
     return false;
 }
 public function notify()
 {
     //check if notification has not already been send
     if ($this->getNotify() == true) {
         //check which event to test
         if ($this->getAction() == 'router_offline') {
             $crawl_cycles = ConfigLine::configByName('event_notification_router_offline_crawl_cycles');
             $router = new Router((int) $this->getObject());
             $router->fetch();
             $statusdata_history = new RouterStatusList($router->getRouterId(), 0, (int) $crawl_cycles, "status_id", "desc");
             if ($statusdata_history->getTotalCount() >= $crawl_cycles) {
                 $statusdata_history = $statusdata_history->getRouterStatuslist();
                 $online = false;
                 foreach ($statusdata_history as $key => $statusdata) {
                     if ($statusdata->getStatus() == 'online') {
                         $online = true;
                         break;
                     }
                 }
                 if (!$online and $this->getNotified() == 0) {
                     //if router is marked as offline in each of the $crawl_cycles last crawl cycles, then
                     //send a notification
                     $user = new User($this->getUserId());
                     if ($user->fetch()) {
                         $this->notifyRouterOffline($user, $router, $statusdata_history[$crawl_cycles - 1]->getCreateDate());
                         //store into database that the router has been notified
                         $this->setNotified(1);
                         $this->setNotificationDate(time());
                         $this->store();
                     }
                 } elseif ($online and $this->getNotified() == 1) {
                     //if the router has been notified but is not offline anymore, then reset notification
                     $this->setNotified(0);
                     $this->store();
                 }
             }
         } elseif ($this->getAction() == 'network_down') {
         }
     }
 }