Rev 1962 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1962 | Rev 1986 | ||
|---|---|---|---|
| 1 | 1 | ||
| 2 | function TrackerManager(host, username, password, database, server_port) |
2 | function TrackerManager(host, username, password, database, server_port) |
| 3 | { |
3 | { |
| 4 | this.host_ = host; |
4 | this.host_ = host; |
| 5 | this.username_ = username; |
5 | this.username_ = username; |
| 6 | this.password_ = password; |
6 | this.password_ = password; |
| 7 | this.database_ = database; |
7 | this.database_ = database; |
| 8 | this.tracked_node_ids_ = {}; |
8 | this.tracked_node_ids_ = {}; |
| 9 | this.db_resource_ = null; |
9 | this.db_resource_ = null; |
| 10 | this.server_port_ = server_port; |
10 | this.server_port_ = server_port; |
| 11 | this.server_id_ = null; |
11 | this.server_id_ = null; |
| 12 | 12 | ||
| 13 | 13 | ||
| 14 | this._ConnectToDatabase = function() |
14 | this._ConnectToDatabase = function() |
| 15 | { |
15 | { |
| 16 | if (this.db_resource_) |
16 | if (this.db_resource_) |
| 17 | { |
17 | { |
| 18 | MySql_Close(this.db_resource_); |
18 | MySql_Close(this.db_resource_); |
| 19 | this.db_resource_ = null; |
19 | this.db_resource_ = null; |
| 20 | } |
20 | } |
| 21 | 21 | ||
| 22 | this.db_resource_ = MySql_Connect(this.host_, this.username_, this.password_); |
22 | this.db_resource_ = MySql_Connect(this.host_, this.username_, this.password_); |
| 23 | 23 | ||
| 24 | if (this.db_resource_ === false) |
24 | if (this.db_resource_ === false) |
| 25 | { |
25 | { |
| 26 | return false; |
26 | return false; |
| 27 | } |
27 | } |
| 28 | 28 | ||
| 29 | MySql_SelectDb(this.db_resource_, this.database_); |
29 | MySql_SelectDb(this.db_resource_, this.database_); |
| 30 | 30 | ||
| 31 | MySql_Query(this.db_resource_, "SET time_zone='+0:00';"); |
31 | MySql_Query(this.db_resource_, "SET time_zone='+0:00';"); |
| 32 | 32 | ||
| 33 | return true; |
33 | return true; |
| 34 | } |
34 | } |
| 35 | 35 | ||
| 36 | 36 | ||
| 37 | /* Function for finding tracked object */ |
37 | /* Function for finding tracked object */ |
| 38 | this. |
38 | this._Identify = function(data) |
| 39 | { |
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 + "'"); |
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 | 41 | ||
| 42 | if (result === false || result.length == 0) |
42 | if (result === false || result.length == 0) |
| 43 | { |
43 | { |
| 44 | Log("Unable to find a tracker with IMEI " + data.Imei); |
44 | Log("Unable to find a tracker with IMEI " + data.Imei); |
| 45 | } |
45 | } |
| 46 | else |
46 | else |
| 47 | { |
47 | { |
| 48 | var tracker_node_id = result[0]["id"]; |
48 | var tracker_node_id = result[0]["id"]; |
| 49 | var tracker_node_name = result[0]["name"]; |
49 | var tracker_node_name = result[0]["name"]; |
| 50 | 50 | ||
| 51 | 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); |
| 52 | 52 | ||
| 53 | result = MySql_Query(this.db_resource_, "SELECT * FROM `Links` WHERE `node_id_down` = " + tracker_node_id + " AND `Role` = 'tracker'"); |
53 | result = MySql_Query(this.db_resource_, "SELECT * FROM `Links` WHERE `node_id_down` = " + tracker_node_id + " AND `Role` = 'tracker'"); |
| 54 | 54 | ||
| 55 | if (result === false || result.length == 0) |
55 | if (result === false || result.length == 0) |
| 56 | { |
56 | { |
| 57 | 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!"); |
| 58 | 58 | ||
| 59 | return tracker_node_id; |
59 | return tracker_node_id; |
| 60 | } |
60 | } |
| 61 | else |
61 | else |
| 62 | { |
62 | { |
| 63 | result = MySql_Query(this.db_resource_, "SELECT * FROM `Nodes` WHERE `id` = " + result[0]["node_id_up"]); |
63 | result = MySql_Query(this.db_resource_, "SELECT * FROM `Nodes` WHERE `id` = " + result[0]["node_id_up"]); |
| 64 | 64 | ||
| 65 | if (result === false || result.length == 0) |
65 | if (result === false || result.length == 0) |
| 66 | { |
66 | { |
| 67 | 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"] + ")!"); |
| 68 | } |
68 | } |
| 69 | else |
69 | else |
| 70 | { |
70 | { |
| 71 | 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 + "\"."); |
| 72 | 72 | ||
| 73 | return result[0]["id"]; |
73 | return result[0]["id"]; |
| 74 | } |
74 | } |
| 75 | } |
75 | } |
| 76 | } |
76 | } |
| 77 | 77 | ||
| 78 | return false; |
78 | return false; |
| 79 | } |
79 | } |
| 80 | 80 | ||
| 81 | /* Function for inserting new position in database */ |
81 | /* Function for inserting new position in database */ |
| 82 | this._InsertPosition = function(tracker_node_id, data) |
82 | this._InsertPosition = function(tracker_node_id, data) |
| 83 | { |
83 | { |
| 84 |
|
84 | var result = false; |
| 85 |
|
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 ("; |
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 + ","; |
87 | query += tracker_node_id + ","; |
| 88 | query += "'gps',"; |
88 | query += "'gps',"; |
| 89 | query += "'" + data.Type + "',"; |
89 | query += "'" + data.Type + "',"; |
| 90 | query += "GeomFromText('POINT(" + data.Latitude + " " + data.Longitude + ")'),"; |
90 | query += "GeomFromText('POINT(" + data.Latitude + " " + data.Longitude + ")'),"; |
| 91 |
|
91 | query += "'20" + data.Datetime + "',"; |
| 92 | query += data.Pdop + ","; |
92 | query += data.Pdop + ","; |
| 93 | query += data.Hdop + ","; |
93 | query += data.Hdop + ","; |
| 94 | query += data.Vdop + ","; |
94 | query += data.Vdop + ","; |
| 95 | query += data.Satellites + ","; |
95 | query += data.Satellites + ","; |
| 96 | query += data.Altitude + ","; |
96 | query += data.Altitude + ","; |
| 97 | query += data.HeightOfGeoid + ","; |
97 | query += data.HeightOfGeoid + ","; |
| 98 | query += data.Speed + ","; |
98 | query += data.Speed + ","; |
| 99 | query += data.Angle; |
99 | query += data.Angle; |
| 100 | query += ");"; |
100 | query += ");"; |
| 101 | 101 | ||
| 102 |
|
102 | result = MySql_Query(this.db_resource_, query); |
| - | 103 | ||
| - | 104 | if (!result) |
|
| - | 105 | { |
|
| - | 106 | Log("Insert failed will try to reconnect to MySql database!"); |
|
| 103 |
|
107 | |
| 104 | if (this._ConnectToDatabase()) |
108 | if (this._ConnectToDatabase()) |
| 105 | { |
109 | { |
| 106 | Log("Reconnected to MySql database!"); |
110 | Log("Reconnected to MySql database!"); |
| 107 | 111 | ||
| 108 |
|
112 | result = MySql_Query(this.db_resource_, query); |
| 109 | } |
- | |
| 110 | } |
113 | } |
| 111 | 114 | } |
|
| - | 115 | ||
| 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"; |
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"; |
| 113 | |
117 | |
| 114 | Log("From DB:" + JSON.stringify(MySql_Query(this.db_resource_, query)));*/ |
118 | Log("From DB:" + JSON.stringify(MySql_Query(this.db_resource_, query)));*/ |
| 115 | } |
- | |
| 116 | 119 | ||
| 117 | Log |
120 | //Log(JSON.stringify(data)); |
| - | 121 | ||
| - | 122 | return !(result === false); |
|
| 118 | } |
123 | } |
| 119 | 124 | ||
| 120 | /* Function for handling tracker connected disconnected */ |
125 | /* Function for handling tracker connected disconnected */ |
| 121 | this._HandleClientStatusUpdate = function(client_id, state) |
126 | this._HandleClientStatusUpdate = function(client_id, state) |
| 122 | { |
127 | { |
| 123 | switch (state) |
128 | switch (state) |
| 124 | { |
129 | { |
| 125 | case SOCKET_STATE_CONNECTED: |
130 | case SOCKET_STATE_CONNECTED: |
| 126 | { |
131 | { |
| 127 | Log("Client " + client_id + " connected!"); |
132 | Log("Client " + client_id + " connected!"); |
| 128 | 133 | ||
| 129 | this.tracked_node_ids_[client_id] = true; |
134 | this.tracked_node_ids_[client_id] = true; |
| 130 | 135 | ||
| 131 | break; |
136 | break; |
| 132 | } |
137 | } |
| 133 | case SOCKET_STATE_DISCONNECTED: |
138 | case SOCKET_STATE_DISCONNECTED: |
| 134 | { |
139 | { |
| 135 | Log("Client " + client_id + " disconnected!"); |
140 | Log("Client " + client_id + " disconnected!"); |
| 136 | 141 | ||
| 137 | if (this.tracked_node_ids_[client_id]) |
142 | if (this.tracked_node_ids_[client_id]) |
| 138 | { |
143 | { |
| 139 | delete this.tracked_node_ids_[client_id]; |
144 | delete this.tracked_node_ids_[client_id]; |
| 140 | } |
145 | } |
| 141 | 146 | ||
| 142 | break; |
147 | break; |
| 143 | } |
148 | } |
| 144 | default: |
149 | default: |
| 145 | { |
150 | { |
| 146 | Log("Unknown state (" + state + ") for client " + client_id + "!"); |
151 | Log("Unknown state (" + state + ") for client " + client_id + "!"); |
| 147 | break; |
152 | break; |
| 148 | } |
153 | } |
| 149 | } |
154 | } |
| 150 | } |
155 | } |
| 151 | 156 | ||
| 152 | /* Functio for handling tracker data */ |
- | |
| 153 | this. |
157 | this._SendResponse = function(client_id, id, success) |
| 154 | { |
158 | { |
| 155 | if (this.tracked_node_ids_[client_id]) |
- | |
| 156 | { |
- | |
| 157 |
|
159 | var data = ""; |
| 158 | - | ||
| 159 |
|
160 | var json_data = {}; |
| 160 | 161 | ||
| 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); |
|
| 162 |
|
167 | |
| - | 168 | Log("Answer:" + data); |
|
| - | 169 | ||
| - | 170 | Socket_Send(client_id, data); |
|
| - | 171 | } |
|
| - | 172 | ||
| - | 173 | /* Functio for handling tracker data */ |
|
| 163 |
|
174 | this._HandleClientDataReceived = function(client_id, data) |
| 164 | { |
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 | { |
|
| 165 | this._InsertPosition(this.tracked_node_ids_[client_id], json_data.params); |
188 | result = this._InsertPosition(this.tracked_node_ids_[client_id], json_data.params); |
| 166 | } |
189 | } |
| 167 | } |
190 | } |
| 168 | else if (json_data.method == " |
191 | else if (json_data.method == "Tracker.Identify") |
| 169 | { |
192 | { |
| 170 | this.tracked_node_ids_[client_id] = this. |
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; |
|
| 171 | } |
200 | } |
| 172 | else |
201 | else |
| 173 | { |
202 | { |
| 174 | Log("Got unknown data \"" + data + "\" from client " + client_id); |
203 | Log("Got unknown data \"" + data + "\" from client " + client_id); |
| 175 | } |
204 | } |
| - | 205 | ||
| - | 206 | this._SendResponse(client_id, json_data.id, result); |
|
| 176 | } |
207 | } |
| - | 208 | } |
|
| - | 209 | ||
| - | 210 | this._HandleNewClient = function(client_id, server_id) |
|
| - | 211 | { |
|
| - | 212 | Log("Client " + client_id + " connected to tracker server " + server_id); |
|
| 177 | } |
213 | } |
| 178 | 214 | ||
| 179 | this._StartServer = function() |
215 | this._StartServer = function() |
| 180 | { |
216 | { |
| 181 | var self = this; |
217 | var self = this; |
| 182 | 218 | ||
| 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); }); |
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); }); |
| 184 | 220 | ||
| 185 | if (this.server_id_ === false) |
221 | if (this.server_id_ === false) |
| 186 | { |
222 | { |
| 187 | return false; |
223 | return false; |
| 188 | } |
224 | } |
| 189 | 225 | ||
| 190 | return true; |
226 | return true; |
| 191 | } |
227 | } |
| 192 | 228 | ||
| 193 | /* Initialize connection */ |
229 | /* Initialize connection */ |
| 194 | this._ConnectToDatabase(); |
230 | this._ConnectToDatabase(); |
| 195 | this._StartServer(); |
231 | this._StartServer(); |
| 196 | } |
232 | } |
| 197 | 233 | ||