Subversion Repositories HomeAutomation

Rev

Rev 1952 | Rev 1959 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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