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