Class: TwoNet::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/two_net.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activityRequest_xml(opts = {}) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/two_net.rb', line 298

def self.activityRequest_xml(opts={})
  guid = opts[:guid]
  track_guid = opts[:track_guid]
  timezone = opts[:timezone]
  builder = Builder::XmlMarkup.new
  xml = builder.activityRequest do |xml|
    xml.guid guid
    xml.trackGuid track_guid
    xml.timezone timezone
  end
  xml
end

.activityRequestFilter_xml(opts = {}) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/two_net.rb', line 373

def self.activityRequestFilter_xml(opts={})
  guid = opts[:guid]
  track_guid = opts[:track_guid]
  start_date = opts[:start_date]
  end_date = opts[:end_date]
  timezone = opts[:timezone]

  builder = Builder::XmlMarkup.new
  xml = builder.activityRequest  do |xml|
    xml.guid guid
    xml.trackGuid track_guid
    xml.filter do |xml2|
      xml2.startDate start_date.to_i
      xml2.endDate end_date.to_i
      xml2.aggregateLevel 'EPOCH'
  end
    xml.timezone timezone
  end
  xml
end

.add_oauth(opts = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/two_net.rb', line 86

def self.add_oauth(opts={})
  guid = opts[:type]
  type = opts[:type]
  body = {:guid=>guid,
          :type =>type,
          "registerType" => "oauth" }
  response = TwoNet::Client.post('/partner/user/track/register',:body=>body.to_xml(:root=>'trackRegistrationRequest'))
  return false if @error = response["errorStatus"].blank? == false
  return nil if response["trackRegistrationResponse"]["status"]["code"].to_s != "1"
  return response["trackRegistrationResponse"]["oauthAuthorizationUrl"]
end

.add_sensor(guid, properties) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/two_net.rb', line 74

def self.add_sensor(guid,properties)
  # https://twonetcom.qualcomm.com/kernel/api/objects/trackRegistrationRequest.jsp 
  body = {:guid=>guid ,
          :type =>"2net",
          :properties => properties,
          "registerType" => "properties" }
  response = TwoNet::Client.post('/partner/user/track/register',:body=>TwoNet::Client.trackRegistrationRequest_xml(body))
  return false if @error = response["errorStatus"].blank? == false
  return nil if response["trackRegistrationResponse"]["status"]["code"].to_s != "1"
  return response["trackRegistrationResponse"]["trackDetail"]["guid"]
end

.arrayify(object) ⇒ Object



394
395
396
# File 'lib/two_net.rb', line 394

def self.arrayify(object)
  object.is_a?(Array) ? object : [object]
end

.check_response(object, response, data) ⇒ Object



265
266
267
268
269
# File 'lib/two_net.rb', line 265

def self.check_response(object, response, data)
  return false if @error = response["errorStatus"].blank? == false ||  response[object].nil?
  return nil if response[object]["status"]["code"].to_s != "1"
  return response[object][data]
end

.check_status_response(response) ⇒ Object



260
261
262
263
# File 'lib/two_net.rb', line 260

def self.check_status_response(response)
  return false if @error = response["errorStatus"].blank? == false
  return response["statusResponse"]["status"]["code"].to_s == "1"
end

.check_user_track_details_response(response) ⇒ Object



271
272
273
274
275
# File 'lib/two_net.rb', line 271

def self.check_user_track_details_response(response)
  return false if @error = response["errorStatus"].blank? == false
  return nil if response["trackDetailsResponse"]["status"]["code"].to_s != "1"
  return response["trackDetailsResponse"]["trackDetails"]
end

.check_user_track_list_response(response) ⇒ Object



276
277
278
279
# File 'lib/two_net.rb', line 276

def self.check_user_track_list_response(response)
  return false if @error = response["errorStatus"].blank? == false
  return response["trackGuidsResponse"]
end

.clear_errorObject



21
22
23
# File 'lib/two_net.rb', line 21

def self.clear_error()
	@error = nil
end

.debug_user_filtered(opts = {}) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/two_net.rb', line 236

def self.debug_user_filtered(opts={})
  measurement = opts[:measurement]
  guid = opts[:guid]
  track_guid = opts[:track_guid]
  start_date = opts[:start_date]
  end_date = opts[:end_date]
  timezone = opts[:timezone]
	responses = Hash.new
  
  body = TwoNet::Client.measureRequest_xml(guid: guid,  track_guid: track_guid, timezone: timezone, start_date: start_date,  end_date: end_date)
  
    responses["blood"] = TwoNet::Client.post('/partner/measure/blood/filtered', :body=>body)
  
    responses["body"] = TwoNet::Client.post('/partner/measure/body/filtered', :body=>body)
  
    responses["breath"] = TwoNet::Client.post('/partner/measure/breath/filtered',:body=>body)

 

  return  responses
end

.delete_user(guid) ⇒ Object



30
31
32
33
34
# File 'lib/two_net.rb', line 30

def self.delete_user(guid)
  response = TwoNet::Client.delete("/partner/user/delete/#{guid}")
  return false if @error = response["errorStatus"].blank? == false
  return check_status_response response
end

.filtered_activity(opts = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/two_net.rb', line 123

def self.filtered_activity(opts={})
  guid = opts[:guid]
  track_guid = opts[:track_guid]
  timezone = opts[:timezone]
  start_date = opts[:start_date]
  end_date = opts[:end_date]

  body = TwoNet::Client.activityRequestFilter_xml(guid: guid, track_guid: track_guid, 
                          start_date: start_date.to_i, end_date: end_date.to_i, 
                            timezone: timezone )
  response = TwoNet::Client.post('/partner/activity/filtered',:body=>body)
end

.filtered_track(opts = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/two_net.rb', line 136

def self.filtered_track(opts={})
  guid = opts[:guid]
  track_guid = opts[:track_guid]
  timezone = opts[:timezone]
  start_date = opts[:start_date]
  end_date = opts[:end_date]

  body = TwoNet::Client.trackRequestFilter_xml(guid: guid, track_guid: track_guid, 
                          start_date: start_date.to_i, end_date: end_date.to_i, 
                            timezone: timezone )
  response = TwoNet::Client.post('/partner/user/track/filtered',:body=>body)
end

.generate_guidObject



103
104
105
# File 'lib/two_net.rb', line 103

def self.generate_guid()
  SecureRandom.uuid
end

.get_errorObject

timezone = ‘Canada/Atlantic’ Or “Atlantic Time (Canada)”



18
19
20
# File 'lib/two_net.rb', line 18

def self.get_error()
	return @error;
end

.get_guidsObject

return array



42
43
44
45
46
47
# File 'lib/two_net.rb', line 42

def self.get_guids()
  audit_response = TwoNet::Client.get('/partner/audit/guids')

  return nil if audit_response["auditResponse"]["status"]["code"] != "1"
  return audit_response["auditResponse"]["guids"]["guid"]
end

.latest_activity(opts = {}) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/two_net.rb', line 114

def self.latest_activity(opts={})
  guid = opts[:guid]
  track_guid = opts[:track_guid]
  timezone = opts[:timezone]
   #timezone = 'Canada/Atlantic'
  body = activityRequest_xml(guid: guid,  track_guid: track_guid, timezone: timezone)
  response = TwoNet::Client.post('/partner/activity/day/latest',:body=>body)
end

.latest_blood(opts = {}) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/two_net.rb', line 149

def self.latest_blood(opts={})
  guid = opts[:guid]
  track_guid = opts[:track_guid]

  body = measureRequest_xml(guid: guid, track_guid: track_guid)
  response = TwoNet::Client.post('/partner/measure/blood/latest',:body=>body)
  return nil if @error = response["measureResponse"]["status"]["code"].to_s != "1"
  return response["measureResponse"]
end

.latest_body(opts = {}) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'lib/two_net.rb', line 169

def self.latest_body(opts={})
  guid = opts[:guid]
  track_guid = opts[:track_guid]
  timezone = opts[:timezone]

  body = TwoNet::Client.measureRequest_xml(guid: guid, track_guid: track_guid, timezone: timezone)
  response = TwoNet::Client.post('/partner/measure/body/latest',:body=>body)
  return nil if @error = response["measureResponse"]["status"]["code"] != "1"
  return response["measureResponse"]
end

.latest_breath(opts = {}) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/two_net.rb', line 159

def self.latest_breath(opts={})
  guid = opts[:guid]
  track_guid = opts[:track_guid]

  body = measureRequest_xml(guid: guid, track_guid: track_guid)
  response = TwoNet::Client.post('/partner/measure/breath/latest',:body=>body)
  return nil if response["measureResponse"]["status"]["code"].to_s != "1"
  return response["measureResponse"]
end

.latest_reading(opts = {}) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/two_net.rb', line 106

def self.latest_reading(opts={})
  guid = opts[:guid]
  track_guid = opts[:track_guid]
   #timezone = 'Canada/Atlantic'
  body = TwoNet::Client.trackRequest_xml(guid: guid, track_guid: track_guid)
  response = TwoNet::Client.post('/partner/user/track/latest',:body=>body)
end

.list_all_sensors(guid) ⇒ Object

TODO clean up the hash that arrives back



60
61
62
63
# File 'lib/two_net.rb', line 60

def self.list_all_sensors(guid)
  response = TwoNet::Client.get("/partner/user/tracks/registerable/#{guid}")
  response["trackRegistrationTemplateResponse"]
end

.measureRequest_xml(opts = {}) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/two_net.rb', line 311

def self.measureRequest_xml(opts={})
  guid = opts[:guid]
  track_guid = opts[:track_guid]
  timezone = opts[:timezone]
  start_date = opts[:start_date]
  end_date = opts[:end_date]


  builder = Builder::XmlMarkup.new
  xml = builder.measureRequest do |xml|
    xml.guid guid
    xml.trackGuid track_guid
    xml.filter do |xml2|
      xml2.startDate start_date.to_i
      xml2.endDate end_date.to_i
      xml2.aggregateLevel 'EPOCH'
  end
    xml.timezone timezone
  end
  xml
end

.register_user(guid) ⇒ Object



24
25
26
27
28
# File 'lib/two_net.rb', line 24

def self.register_user(guid)
  response = TwoNet::Client.post('/partner/register', :body=>{:guid=>guid }.to_xml(:root=>:registerRequest))
  return false if @error = response["errorStatus"].blank? == false
  return  response["trackGuidsResponse"]["status"]["code"] == "1"
end

.remove_sensor(guid, track_id) ⇒ Object



98
99
100
101
# File 'lib/two_net.rb', line 98

def self.remove_sensor(guid,track_id)
  response = TwoNet::Client.delete("/partner/user/track/unregister/#{guid}/#{track_id}")
  return TwoNet::Client.check_status_response response
end

.trackRegistrationRequest_xml(body) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/two_net.rb', line 280

def self.trackRegistrationRequest_xml(body)
  builder = Builder::XmlMarkup.new
  xml = builder.trackRegistrationRequest do |xml|
    xml.guid body[:guid]
    xml.type body[:type]
    xml.registerType body[:registerType]
    xml.properties do
      body[:properties].each do |property|
        xml.property do
          xml.name property[:name].to_s
          xml.value property[:value]
        end
      end
    end
  end
  xml
end

.trackRequest_xml(opts = {}) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/two_net.rb', line 333

def self.trackRequest_xml(opts={})
  guid = opts[:guid]
  track_guid = opts[:track_guid]
  start_date = opts[:start_date]
  end_date = opts[:end_date]

  builder = Builder::XmlMarkup.new
  xml = builder.trackRequest  do |xml|
    xml.guid guid
    xml.trackGuid track_guid
    xml.filter do |xml2|
      xml2.startDate start_date.to_i
      xml2.endDate end_date.to_i
      xml2.aggregateLevel 'EPOCH'
    end
  end
  xml
end

.trackRequestFilter_xml(opts = {}) ⇒ Object



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/two_net.rb', line 351

def self.trackRequestFilter_xml(opts={})
  guid = opts[:guid]
  track_guid = opts[:track_guid]
  start_date = opts[:start_date]
  end_date = opts[:end_date]
  timezone = opts[:timezone]

  builder = Builder::XmlMarkup.new
  xml = builder.trackRequest  do |xml|
    xml.guid guid
    xml.trackGuid track_guid
    xml.filter do |xml2|
      xml2.startDate start_date.to_i
      xml2.endDate end_date.to_i
      xml2.aggregateLevel 'EPOCH'
      xml.timezone timezone 
  end
  
  end
  xml
end

.user_exists?(guid) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/two_net.rb', line 36

def self.user_exists?(guid)
  response = TwoNet::Client.get("/partner/user/exists/#{guid}")
  return check_status_response response
end

.user_filtered(opts = {}) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/two_net.rb', line 213

def self.user_filtered(opts={})
  measurement = opts[:measurement]
  guid = opts[:guid]
  track_guid = opts[:track_guid]
  start_date = opts[:start_date]
  end_date = opts[:end_date]
  timezone = opts[:timezone]

  body = TwoNet::Client.measureRequest_xml(guid: guid,  track_guid: track_guid, timezone: timezone, start_date: start_date,  end_date: end_date)
  if measurement == "blood"
    response = TwoNet::Client.post('/partner/measure/blood/filtered', :body=>body)
  elsif measurement == "body"
    response = TwoNet::Client.post('/partner/measure/body/filtered', :body=>body)
  elsif measurement == "breath"
    response = TwoNet::Client.post('/partner/measure/breath/filtered',:body=>body)
  elsif measurement == "activity"
response = TwoNet::Client.post('/partner/measure/activity/filtered',:body=>body)
  end

  return nil if response["measureResponse"]["status"]["code"] != "1"
  return response["measureResponse"]
end

.user_latest(opts = {}) ⇒ Object

measurement in blood, breath, blood



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/two_net.rb', line 181

def self.user_latest(opts={})
  measurement = opts[:measurement]
  patient_guid = opts[:patient_guid]
  sensor_guid = opts[:sensor_guid]
  timezone = opts[:timezone]

  if measurement == :blood
    results = TwoNet::Client.latest_blood(guid: patient_guid,track_guid: sensor_guid)
  elsif measurement == :body
    results = TwoNet::Client.latest_body(guid: patient_guid,track_guid: sensor_guid, timezone: timezone)
  elsif measurement == :breath
    results = TwoNet::Client.latest_breath(guid: patient_guid,track_guid: sensor_guid)
  end
return results
end

.user_latest_debug(opts = {}) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/two_net.rb', line 197

def self.user_latest_debug(opts={})
  measurement = opts[:measurement]
  patient_guid = opts[:patient_guid]
  sensor_guid = opts[:sensor_guid]
  timezone = opts[:timezone]
  results = Hash.new
  
    results["blood"] = TwoNet::Client.latest_blood(guid: patient_guid,track_guid: sensor_guid)
  
    results["body"] = TwoNet::Client.latest_body(guid: patient_guid,track_guid: sensor_guid, timezone: timezone)
  
    results["breath"] = TwoNet::Client.latest_breath(guid: patient_guid,track_guid: sensor_guid)
  
return results
end

.user_track_details(guid) ⇒ Object



49
50
51
52
# File 'lib/two_net.rb', line 49

def self.user_track_details(guid)
  response = TwoNet::Client.get("/partner/user/tracks/details/#{guid}")
  return check_user_track_details_response response
end

.user_track_list(guid) ⇒ Object



54
55
56
57
# File 'lib/two_net.rb', line 54

def self.user_track_list(guid)
  response = TwoNet::Client.get("/partner/user/tracks/guids/#{guid}")
  return check_user_track_list_response response
end

Instance Method Details

#add_fake_sensors(guid, properties) ⇒ Object



442
443
444
445
446
447
448
449
450
451
452
# File 'lib/two_net.rb', line 442

def add_fake_sensors(guid, properties)
  # https://twonetcom.qualcomm.com/kernel/api/objects/trackRegistrationRequest.jsp 
  body = {:guid=>guid ,
          :type =>"2net",
          :properties => properties,
          "registerType" => "properties" }
  response = TwoNet::Client.post('/partner/user/track/register',:body=>TwoNet::Client.trackRegistrationRequest_xml(body))
  return false if response["errorStatus"].blank? == false
  return nil if response["trackRegistrationResponse"]["status"]["code"].to_s != "1"
  return response["trackRegistrationResponse"]["trackDetail"]["guid"]
end

#compress_track(track_details) ⇒ Object



454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/two_net.rb', line 454

def compress_track(track_details)
  h = Hash.new
 # return nil if track_details.nil? == true

  track_detail = track_details["trackDetail"]
  return [] if track_detail.blank?
  track_detail = TwoNet::Client.arrayify(track_detail)
  track_detail.each do |track_detail|
    h[ track_detail["guid"] ] = track_detail["properties"]
  end
  return h
end

#fake_blood_pressureObject



426
427
428
429
430
431
432
433
# File 'lib/two_net.rb', line 426

def fake_blood_pressure
 properties = [ {:name=>:make, :value=>'A&D'},
   {:name=>:model, :value=>'UA-767PBT'},
    {:name=>:serialNumber, :value=>'2NET00004'},
    {:name=>:qualifier, :value=>1}
    ]
  return properties
end

#fake_glucometerObject



399
400
401
402
403
404
405
406
# File 'lib/two_net.rb', line 399

def fake_glucometer
 properties = [ {:name=>:make, :value=>'Entra'},
   {:name=>:model, :value=>'MGH-BT1'},
    {:name=>:serialNumber, :value=>'2NET00001'},
    {:name=>:qualifier, :value=>1}
    ]
  return properties
end

#fake_inhalerObject



434
435
436
437
438
439
440
441
# File 'lib/two_net.rb', line 434

def fake_inhaler
 properties = [ {:name=>:make, :value=>'Asthmapolis'},
   {:name=>:model, :value=>'Rev B'},
    {:name=>:serialNumber, :value=>'2NET00005'},
    {:name=>:qualifier, :value=>1}
    ]
  return properties
end

#fake_pulse_oximeterObject



408
409
410
411
412
413
414
415
# File 'lib/two_net.rb', line 408

def fake_pulse_oximeter
 properties = [ {:name=>:make, :value=>'Nonin'},
   {:name=>:model, :value=>'9560 Onyx II'},
    {:name=>:serialNumber, :value=>'2NET00002'},
    {:name=>:qualifier, :value=>1}
    ]
  return properties
end

#fake_weigh_scaleObject



417
418
419
420
421
422
423
424
# File 'lib/two_net.rb', line 417

def fake_weigh_scale
 properties = [ {:name=>:make, :value=>'A&D'},
   {:name=>:model, :value=>'UC-321PBT'},
    {:name=>:serialNumber, :value=>'2NET00003'},
    {:name=>:qualifier, :value=>1}
    ]
  return properties
end


467
468
469
470
471
472
473
474
475
476
# File 'lib/two_net.rb', line 467

def print_uid_sensors(guids = nil)
  guids = TwoNet::Client.get_guids() if guids == nil
 	guids= TwoNet::Client.arrayify(guids)
  h = Hash.new
  guids.each do |guid|
   results =  TwoNet::Client.user_track_details(guid)
    h[guid] =  compress_track(results)
  end
  return h
end