Subversion Repositories HomeAutomation

Rev

Rev 1962 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1962 Rev 1986
Line 33... Line 33...
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._IdentifyTracker = function(data)
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
    {
Line 79... Line 79...
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
    if (data.Type != "NO")
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
      query     +=  "'" + data.Datetime + "',";
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
      if (!MySql_Query(this.db_resource_, query))
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
          MySql_Query(this.db_resource_, query);
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(JSON.stringify(data));
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
  {
Line 125... Line 130...
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._HandleClientDataReceived = function(client_id, data)
157
  this._SendResponse = function(client_id, id, success)
154
  {
158
  {
155
    if (this.tracked_node_ids_[client_id])
-
 
156
    {
-
 
157
      Log(data);
159
    var data = "";
158
   
-
 
159
      var json_data = eval("(" + data + ")");
160
    var json_data = {};
160
     
161
   
161
      if (json_data.method == "MBB.OnPosition")
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
        if (this.tracked_node_ids_[client_id])
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 == "MBB.OnConnected")
191
      else if (json_data.method == "Tracker.Identify")
169
      {
192
      {
170
        this.tracked_node_ids_[client_id] = this._IdentifyTracker(json_data.params);
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
    }