Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. function TrackerManager(host, username, password, database, server_port)
  3. {
  4.   this.host_              = host;
  5.   this.username_          = username;
  6.   this.password_          = password;
  7.   this.database_          = database;
  8.   this.tracked_node_ids_  = {};
  9.   this.db_resource_       = null;
  10.   this.server_port_       = server_port;
  11.   this.server_id_         = null;
  12.  
  13.  
  14.   this._ConnectToDatabase = function()
  15.   {
  16.     if (this.db_resource_)
  17.     {
  18.       MySql_Close(this.db_resource_);
  19.       this.db_resource_ = null;
  20.     }
  21.    
  22.     this.db_resource_ = MySql_Connect(this.host_, this.username_, this.password_);
  23.  
  24.     if (this.db_resource_ === false)
  25.     {
  26.       return false;
  27.     }
  28.    
  29.     MySql_SelectDb(this.db_resource_, this.database_);
  30.  
  31.     MySql_Query(this.db_resource_, "SET time_zone='+0:00';");
  32.    
  33.     return true;
  34.   }
  35.  
  36.  
  37.   /* Function for finding tracked object */
  38.   this._IdentifyTracker = function(data)
  39.   {
  40.     var result = MySql_Query(this.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.Imei + "'");
  41.      
  42.     if (result === false || result.length == 0)
  43.     {
  44.       Log("Unable to find a tracker with IMEI " + data.Imei);
  45.     }
  46.     else
  47.     {
  48.       var tracker_node_id = result[0]["id"];
  49.       var tracker_node_name = result[0]["name"];
  50.      
  51.       Log("Found tracker \"" + tracker_node_name + "\" (NodeId " + tracker_node_id + ") that corresponds to IMEI " + data.Imei);
  52.        
  53.       result = MySql_Query(this.db_resource_, "SELECT * FROM `Links` WHERE `node_id_down` = " + tracker_node_id + " AND `Role` = 'tracker'");
  54.      
  55.       if (result === false || result.length == 0)
  56.       {
  57.         Log("Unable to find an object that is being tracked with this tracker, will store positions on the tracker itself!");
  58.        
  59.         return tracker_node_id;
  60.       }
  61.       else
  62.       {
  63.         result = MySql_Query(this.db_resource_, "SELECT * FROM `Nodes` WHERE `id` = " + result[0]["node_id_up"]);
  64.      
  65.         if (result === false || result.length == 0)
  66.         {
  67.           Log("Failed to fetch the name of the tracked object (node_id " + result[0]["node_id_up"] + ")!");
  68.         }
  69.         else
  70.         {
  71.           Log("Found \"" + result[0]["name"] + "\" (node_id " + result[0]["id"] + ") which is being tracked by \"" + tracker_node_name + "\".");
  72.  
  73.           return result[0]["id"];
  74.         }
  75.       }
  76.     }
  77.    
  78.     return false;
  79.   }
  80.  
  81.   /* Function for inserting new position in database */
  82.   this._InsertPosition = function(tracker_node_id, data)
  83.   {
  84.     if (data.Type != "NO")
  85.     {
  86.       var query  =  "INSERT INTO `Positions` (`node_id`, `source`, `type`, `latitude_longitude`, `datetime`, `pdop`, `hdop`, `vdop`, `satellites`, `altitude`, `height_of_geoid`, `speed`, `angle`) VALUES (";
  87.       query     +=  tracker_node_id + ",";
  88.       query     +=  "'gps',";
  89.       query     +=  "'" + data.Type + "',";
  90.       query     +=  "GeomFromText('POINT(" + data.Latitude + " " + data.Longitude + ")'),";
  91.       query     +=  "'" + data.Datetime + "',";
  92.       query     +=  data.Pdop + ",";
  93.       query     +=  data.Hdop + ",";
  94.       query     +=  data.Vdop + ",";
  95.       query     +=  data.Satellites + ",";
  96.       query     +=  data.Altitude + ",";
  97.       query     +=  data.HeightOfGeoid + ",";
  98.       query     +=  data.Speed + ",";
  99.       query     +=  data.Angle;
  100.       query     +=  ");";
  101.      
  102.       if (!MySql_Query(this.db_resource_, query))
  103.       {
  104.         if (this._ConnectToDatabase())
  105.         {
  106.           Log("Reconnected to MySql database!");
  107.          
  108.           MySql_Query(this.db_resource_, query);
  109.         }
  110.       }
  111.      
  112.       /*query = "SELECT `node_id`, `source`, AsText(latitude_longitude), `created`, `datetime`, `hdop`, `satellites`, `altitude`, `height_of_geoid`, `speed`, `angle` FROM `Positions` WHERE `id` = " + MySql_InsertId(this.db_resource_) + " LIMIT 1";
  113.      
  114.       Log("From DB:" + JSON.stringify(MySql_Query(this.db_resource_, query)));*/
  115.     }
  116.      
  117.     Log(JSON.stringify(data));
  118.   }
  119.  
  120.   /* Function for handling tracker connected disconnected */
  121.   this._HandleClientStatusUpdate = function(client_id, state)
  122.   {
  123.     switch (state)
  124.     {
  125.       case SOCKET_STATE_CONNECTED:
  126.       {
  127.         Log("Client " + client_id + " connected!");
  128.        
  129.         this.tracked_node_ids_[client_id] = true;
  130.        
  131.         break;
  132.       }
  133.       case SOCKET_STATE_DISCONNECTED:
  134.       {
  135.         Log("Client " + client_id + " disconnected!");
  136.        
  137.         if (this.tracked_node_ids_[client_id])
  138.         {
  139.           delete this.tracked_node_ids_[client_id];
  140.         }
  141.        
  142.         break;
  143.       }
  144.       default:
  145.       {
  146.         Log("Unknown state (" + state + ") for client " + client_id + "!");
  147.         break;
  148.       }
  149.     }
  150.   }
  151.  
  152.   /* Functio for handling tracker data */
  153.   this._HandleClientDataReceived = function(client_id, data)
  154.   {
  155.     if (this.tracked_node_ids_[client_id])
  156.     {
  157.       Log(data);
  158.    
  159.       var json_data = eval("(" + data + ")");
  160.      
  161.       if (json_data.method == "MBB.OnPosition")
  162.       {
  163.         if (this.tracked_node_ids_[client_id])
  164.         {
  165.           this._InsertPosition(this.tracked_node_ids_[client_id], json_data.params);
  166.         }
  167.       }
  168.       else if (json_data.method == "MBB.OnConnected")
  169.       {
  170.         this.tracked_node_ids_[client_id] = this._IdentifyTracker(json_data.params);
  171.       }
  172.       else
  173.       {
  174.         Log("Got unknown data \"" + data + "\" from client " + client_id);
  175.       }
  176.     }
  177.   }
  178.  
  179.   this._StartServer = function()
  180.   {
  181.     var self = this;
  182.    
  183.     this.server_id_ = Socket_StartServer(this.server_port_, function(client_id, data) { self._HandleClientDataReceived(client_id, data); }, function(client_id, state) { self._HandleClientStatusUpdate(client_id, state); });
  184.    
  185.     if (this.server_id_ === false)
  186.     {
  187.       return false;
  188.     }
  189.    
  190.     return true;
  191.   }
  192.  
  193.   /* Initialize connection */
  194.   this._ConnectToDatabase();
  195.   this._StartServer();
  196. }
  197.