Subversion Repositories HomeAutomation

Rev

Rev 1956 | 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.         var query  =  "INSERT INTO `Positions` (`node_id`, `source`, `latitude_longitude`, `datetime`, `hdop`, `satellites`, `altitude`, `height_of_goid`, `speed`, `angle`, `magnetic_declination`) VALUES (";
  33.         query     +=  self.tracked_node_ids_[data.Info.Imei] + ", ";
  34.         query     +=  "'gps', ";
  35.         query     +=  "GeomFromText('POINT(" + data.Position.Latitude + " " + data.Position.Longitude + ")'), ";
  36.         query     +=  "'" + data.Position.Datetime + "', ";
  37.         query     +=  data.Position.Hdop + ", ";
  38.         query     +=  data.Position.Satellites + ", ";
  39.         query     +=  data.Position.Altitude + ", ";
  40.         query     +=  data.Position.HeightOfGeoid + ", ";
  41.         query     +=  data.Position.Speed + ", ";
  42.         query     +=  data.Position.Angle + ", ";
  43.         query     += (data.Position.MagneticDeclination ? data.Position.MagneticDeclination : "NULL");
  44.         query     +=  ");";
  45.        
  46.         MySql_Query(self.db_resource_, query);
  47.        
  48.         Log(JSON.stringify(data.Position));
  49.       }
  50.     }
  51.   });
  52.  
  53.  
  54.   /* Function for finding tracked object */
  55.   this._HandleConnectedTracker = function(data)
  56.   {
  57.     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 + "'");
  58.      
  59.     if (result === false || result.length == 0)
  60.     {
  61.       Log("Unable to find a tracker with IMEI " + data.Info.Imei);
  62.     }
  63.     else
  64.     {
  65.       var tracker_node_id = result[0]["id"];
  66.      
  67.       Log("Found tracker " + result[0]["name"] + " (NodeId " + result[0]["id"] + ") that corresponds to IMEI " + data.Info.Imei);
  68.        
  69.       result = MySql_Query(self.db_resource_, "SELECT * FROM `Links` WHERE `node_id_down` = " + tracker_node_id + " AND `Role` = 'tracker'");
  70.      
  71.       if (result === false || result.length == 0)
  72.       {
  73.         Log("Unable to find an object that is being tracked with this tracker, will store positions on the tracker itself!");
  74.        
  75.         self.tracked_node_ids_[data.Info.Imei] = tracker_node_id;
  76.       }
  77.       else
  78.       {
  79.         result = MySql_Query(self.db_resource_, "SELECT * FROM `Nodes` WHERE `id` = " + result[0]["node_id_up"]);
  80.      
  81.         if (result === false || result.length == 0)
  82.         {
  83.           Log("Failed to fetch the name of the tracked object (node_id " + result[0]["node_id_up"] + ")!");
  84.         }
  85.         else
  86.         {
  87.           Log("Found " + result[0]["name"] + " (node_id " + result[0]["id"] + ") which is being tracked by tracker with IMEI " + data.Info.Imei);
  88.  
  89.           self.tracked_node_ids_[data.Info.Imei] = result[0]["id"];
  90.         }
  91.       }
  92.     }
  93.   }
  94. }
  95.