Subversion Repositories HomeAutomation

Rev

Rev 1952 | Rev 1962 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. function TrackerManager(host, username, password, database)
  3. {
  4.   var self = this;
  5.  
  6.   this.tracked_node_ids_ = {};
  7.   this.db_resource_ = null;
  8.  
  9.   /* Initialize connection */
  10.   self.db_resource_ = MySql_Connect(host, username, password);
  11.  
  12.   MySql_SelectDb(self.db_resource_, database);
  13.  
  14.   MySql_Query(self.db_resource_, "SET time_zone='+0:00';");
  15.  
  16.  
  17.   /* Subscribe to MBB tracker events */
  18.   Mbb_SubscribeToDeviceStatus(function(event_string, data)
  19.   {
  20.     if (event_string == "connected")
  21.     {
  22.       self._HandleConnectedTracker(data);
  23.     }
  24.     else if (event_string == "disconnected")
  25.     {
  26.       delete self.tracked_node_ids_[data.Info.Imei];
  27.     }
  28.     else if (event_string == "position")
  29.     {
  30.       if (data.Position !== false && self.tracked_node_ids_[data.Info.Imei])
  31.       {
  32.         if (data.Position.Type != "NO")
  33.         {
  34.           var query  =  "INSERT INTO `Positions` (`node_id`, `source`, `type`, `latitude_longitude`, `datetime`, `pdop`, `hdop`, `vdop`, `satellites`, `altitude`, `height_of_geoid`, `speed`, `angle`) VALUES (";
  35.           query     +=  self.tracked_node_ids_[data.Info.Imei] + ", ";
  36.           query     +=  "'gps', ";
  37.           query     +=  "'" + data.Position.Type + "', ";
  38.           query     +=  "GeomFromText('POINT(" + data.Position.Latitude + " " + data.Position.Longitude + ")'), ";
  39.           query     +=  "'" + data.Position.Datetime + "', ";
  40.           query     +=  data.Position.Pdop + ", ";
  41.           query     +=  data.Position.Hdop + ", ";
  42.           query     +=  data.Position.Vdop + ", ";
  43.           query     +=  data.Position.Satellites + ", ";
  44.           query     +=  data.Position.Altitude + ", ";
  45.           query     +=  data.Position.HeightOfGeoid + ", ";
  46.           query     +=  data.Position.Speed + ", ";
  47.           query     +=  data.Position.Angle;
  48.           query     +=  ");";
  49.          
  50.           MySql_Query(self.db_resource_, query);
  51.          
  52.           /*query = "SELECT `node_id`, `source`, AsText(latitude_longitude), `created`, `datetime`, `hdop`, `satellites`, `altitude`, `height_of_geoid`, `speed`, `angle` FROM `Positions` WHERE `id` = " + MySql_InsertId(self.db_resource_) + " LIMIT 1";
  53.          
  54.           Log("From DB:" + JSON.stringify(MySql_Query(self.db_resource_, query)));*/
  55.         }
  56.        
  57.         Log(JSON.stringify(data.Position));
  58.       }
  59.     }
  60.   });
  61.  
  62.  
  63.   /* Function for finding tracked object */
  64.   this._HandleConnectedTracker = function(data)
  65.   {
  66.     var result = MySql_Query(self.db_resource_, "SELECT `Nodes`.* FROM `Nodes`, `Attributes` WHERE `Nodes`.`type` = 'tracker' AND `Nodes`.`id` = `Attributes`.`node_id` AND `Attributes`.`name` = 'Imei' AND `Attributes`.`value` = '" + data.Info.Imei + "'");
  67.      
  68.     if (result === false || result.length == 0)
  69.     {
  70.       Log("Unable to find a tracker with IMEI " + data.Info.Imei);
  71.     }
  72.     else
  73.     {
  74.       var tracker_node_id = result[0]["id"];
  75.       var tracker_node_name = result[0]["name"];
  76.      
  77.       Log("Found tracker \"" + tracker_node_name + "\" (NodeId " + tracker_node_id + ") that corresponds to IMEI " + data.Info.Imei);
  78.        
  79.       result = MySql_Query(self.db_resource_, "SELECT * FROM `Links` WHERE `node_id_down` = " + tracker_node_id + " AND `Role` = 'tracker'");
  80.      
  81.       if (result === false || result.length == 0)
  82.       {
  83.         Log("Unable to find an object that is being tracked with this tracker, will store positions on the tracker itself!");
  84.        
  85.         self.tracked_node_ids_[data.Info.Imei] = tracker_node_id;
  86.       }
  87.       else
  88.       {
  89.         result = MySql_Query(self.db_resource_, "SELECT * FROM `Nodes` WHERE `id` = " + result[0]["node_id_up"]);
  90.      
  91.         if (result === false || result.length == 0)
  92.         {
  93.           Log("Failed to fetch the name of the tracked object (node_id " + result[0]["node_id_up"] + ")!");
  94.         }
  95.         else
  96.         {
  97.           Log("Found \"" + result[0]["name"] + "\" (node_id " + result[0]["id"] + ") which is being tracked by \"" + tracker_node_name + "\".");
  98.  
  99.           self.tracked_node_ids_[data.Info.Imei] = result[0]["id"];
  100.         }
  101.       }
  102.     }
  103.   }
  104. }
  105.