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



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

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



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/two_net.rb', line 338

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



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

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



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

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



359
360
361
# File 'lib/two_net.rb', line 359

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

.check_response(object, response, data) ⇒ Object



251
252
253
254
255
# File 'lib/two_net.rb', line 251

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



246
247
248
249
# File 'lib/two_net.rb', line 246

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



257
258
259
260
261
# File 'lib/two_net.rb', line 257

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



262
263
264
265
# File 'lib/two_net.rb', line 262

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



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/two_net.rb', line 222

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



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

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

.generate_guidObject



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

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
# 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



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

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



135
136
137
138
139
140
141
142
143
# File 'lib/two_net.rb', line 135

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



155
156
157
158
159
160
161
162
163
164
# File 'lib/two_net.rb', line 155

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



145
146
147
148
149
150
151
152
153
# File 'lib/two_net.rb', line 145

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



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

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



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

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

.measureRequest_xml(opts = {}) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/two_net.rb', line 297

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



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

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



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/two_net.rb', line 266

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



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/two_net.rb', line 319

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

.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



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/two_net.rb', line 199

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



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

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



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

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



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

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



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

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



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

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



419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/two_net.rb', line 419

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



391
392
393
394
395
396
397
398
# File 'lib/two_net.rb', line 391

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



364
365
366
367
368
369
370
371
# File 'lib/two_net.rb', line 364

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



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

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



373
374
375
376
377
378
379
380
# File 'lib/two_net.rb', line 373

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



382
383
384
385
386
387
388
389
# File 'lib/two_net.rb', line 382

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


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

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