Subversion Repositories HomeAutomation

Rev

Rev 1962 | 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._Identify = 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.     var result = false;
  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     +=  "'20" + 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.     result = MySql_Query(this.db_resource_, query);
  103.    
  104.     if (!result)
  105.     {
  106.       Log("Insert failed will try to reconnect to MySql database!");
  107.      
  108.       if (this._ConnectToDatabase())
  109.       {
  110.         Log("Reconnected to MySql database!");
  111.        
  112.         result = MySql_Query(this.db_resource_, query);
  113.       }
  114.     }
  115.    
  116.     /*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";
  117.    
  118.     Log("From DB:" + JSON.stringify(MySql_Query(this.db_resource_, query)));*/
  119.      
  120.     //Log(JSON.stringify(data));
  121.    
  122.     return !(result === false);
  123.   }
  124.  
  125.   /* Function for handling tracker connected disconnected */
  126.   this._HandleClientStatusUpdate = function(client_id, state)
  127.   {
  128.     switch (state)
  129.     {
  130.       case SOCKET_STATE_CONNECTED:
  131.       {
  132.         Log("Client " + client_id + " connected!");
  133.        
  134.         this.tracked_node_ids_[client_id] = true;
  135.        
  136.         break;
  137.       }
  138.       case SOCKET_STATE_DISCONNECTED:
  139.       {
  140.         Log("Client " + client_id + " disconnected!");
  141.        
  142.         if (this.tracked_node_ids_[client_id])
  143.         {
  144.           delete this.tracked_node_ids_[client_id];
  145.         }
  146.        
  147.         break;
  148.       }
  149.       default:
  150.       {
  151.         Log("Unknown state (" + state + ") for client " + client_id + "!");
  152.         break;
  153.       }
  154.     }
  155.   }
  156.  
  157.   this._SendResponse = function(client_id, id, success)
  158.   {
  159.     var data = "";
  160.     var json_data = {};
  161.    
  162.     json_data["jsonrpc"]  = "2.0";
  163.     json_data["result"]   = success;
  164.     json_data["id"]       = id;
  165.  
  166.     var data = JSON.stringify(json_data);
  167.    
  168.     Log("Answer:" + data);
  169.  
  170.     Socket_Send(client_id, data);
  171.   }
  172.  
  173.   /* Functio for handling tracker data */
  174.   this._HandleClientDataReceived = function(client_id, data)
  175.   {
  176.     if (this.tracked_node_ids_[client_id])
  177.     {
  178.       var result = false;
  179.      
  180.       Log("Receive:" + data);
  181.    
  182.       var json_data = eval("(" + data + ")");
  183.      
  184.       if (json_data.method == "Tracker.AddPosition")
  185.       {
  186.         if (this.tracked_node_ids_[client_id])
  187.         {
  188.           result = this._InsertPosition(this.tracked_node_ids_[client_id], json_data.params);
  189.         }
  190.       }
  191.       else if (json_data.method == "Tracker.Identify")
  192.       {
  193.         this.tracked_node_ids_[client_id] = this._Identify(json_data.params);
  194.        
  195.         result = !(this.tracked_node_ids_[client_id] === false);
  196.       }
  197.       else if (json_data.method == "Tracker.StatusReport")
  198.       {
  199.         result = true;
  200.       }
  201.       else
  202.       {
  203.         Log("Got unknown data \"" + data + "\" from client " + client_id);
  204.       }
  205.      
  206.       this._SendResponse(client_id, json_data.id, result);
  207.     }
  208.   }
  209.  
  210.   this._HandleNewClient = function(client_id, server_id)
  211.   {
  212.     Log("Client " + client_id + " connected to tracker server " + server_id);
  213.   }
  214.  
  215.   this._StartServer = function()
  216.   {
  217.     var self = this;
  218.    
  219.     this.server_id_ = Socket_StartServer(this.server_port_, function(client_id, server_id) { self._HandleNewClient(client_id, server_id); }, function(client_id, data) { self._HandleClientDataReceived(client_id, data); }, function(client_id, state) { self._HandleClientStatusUpdate(client_id, state); });
  220.    
  221.     if (this.server_id_ === false)
  222.     {
  223.       return false;
  224.     }
  225.    
  226.     return true;
  227.   }
  228.  
  229.   /* Initialize connection */
  230.   this._ConnectToDatabase();
  231.   this._StartServer();
  232. }
  233.