Class: Arlo::API
- Inherits:
-
Object
- Object
- Arlo::API
- Defined in:
- lib/arlo/init.rb,
lib/arlo/devices.rb,
lib/arlo/library.rb,
lib/arlo/profile.rb,
lib/arlo/network_helper.rb
Instance Method Summary collapse
- #arm_device(device, armed) ⇒ Object
- #devices ⇒ Object
- #get(url) ⇒ Object
- #get_device_info(device_name) ⇒ Object
- #get_devices ⇒ Object
- #get_library(from_date, to_date) ⇒ Object
- #get_profile ⇒ Object
-
#initialize ⇒ API
constructor
A new instance of API.
- #login ⇒ Object
- #post(url, payload, xheaders = nil) ⇒ Object
- #profile ⇒ Object
- #record_video(camera, duration) ⇒ Object
- #set_siren_on(device, duration) ⇒ Object
- #take_snapshot(camera) ⇒ Object
- #token ⇒ Object
Constructor Details
#initialize ⇒ API
Returns a new instance of API.
4 5 6 7 8 |
# File 'lib/arlo/init.rb', line 4 def initialize @token = login @devices = get_devices @profile = get_profile end |
Instance Method Details
#arm_device(device, armed) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/arlo/devices.rb', line 15 def arm_device(device, armed) device_id = device['deviceId'] payload = { 'from': 'arlogem', 'to': device_id, 'action': 'set', 'resource': 'modes', 'transId': SecureRandom.uuid, 'publishResponse': true, 'properties': { 'active': armed ? 'mode1' : 'mode0' } } ret_val = post("https://arlo.netgear.com/hmsweb/users/devices/notify/#{device_id}", payload, 'xcloudId': device['xCloudId']) JSON.parse(ret_val.body) end |
#devices ⇒ Object
14 15 16 |
# File 'lib/arlo/init.rb', line 14 def devices @devices end |
#get(url) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/arlo/network_helper.rb', line 8 def get url uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Get.new(uri) request.add_field('Content-Type', 'application/json;charset=UTF-8') request.add_field('Authorization', @token) http.request(request) end |
#get_device_info(device_name) ⇒ Object
10 11 12 13 |
# File 'lib/arlo/devices.rb', line 10 def get_device_info(device_name) raise 'Missing device_name' unless device_name @devices['data'].select {|device| device['deviceName'] == device_name }[0] end |
#get_devices ⇒ Object
5 6 7 8 |
# File 'lib/arlo/devices.rb', line 5 def get_devices devices = get 'https://arlo.netgear.com/hmsweb/users/devices' @devices = JSON.parse(devices.body) end |
#get_library(from_date, to_date) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/arlo/library.rb', line 3 def get_library(from_date, to_date) payload = { 'dateFrom': from_date, 'dateTo': to_date } library = post('https://arlo.netgear.com/hmsweb/users/library', payload) JSON.parse(library.body) end |
#get_profile ⇒ Object
3 4 5 6 |
# File 'lib/arlo/profile.rb', line 3 def get_profile profile = get 'https://arlo.netgear.com/hmsweb/users/profile' @profile = JSON.parse(profile.body) end |
#login ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/arlo/init.rb', line 22 def login email = ENV['ARLO_EMAIL'] raise 'Missing ARLO_EMAIL environment variable' unless email password = ENV['ARLO_PASSWORD'] raise 'Missing ARLO_PASSWORD environment variable' unless password payload = { 'email': email, 'password': password } response = post('https://arlo.netgear.com/hmsweb/login/v2', payload) JSON.parse(response.body)['data']['token'] end |
#post(url, payload, xheaders = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/arlo/network_helper.rb', line 20 def post url, payload, xheaders = nil uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Post.new(uri) headers = { 'Content-Type': 'application/json;charset=UTF-8', 'Authorization': @token } headers.merge!(xheaders) if xheaders headers.each do |key, value| request.add_field(key, value) end request.body = payload.to_json http.request(request) end |
#profile ⇒ Object
18 19 20 |
# File 'lib/arlo/init.rb', line 18 def profile @profile end |
#record_video(camera, duration) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/arlo/devices.rb', line 81 def record_video(camera, duration) camera_id = camera['deviceId'] parent_id = camera['parentId'] ret_val = start_stream(camera, parent_id) return ret_val unless ret_val['success'] # ret_val['data']['url'] payload = { 'xcloudId': camera['xCloudId'], 'parentId': parent_id, 'deviceId': camera_id, 'olsonTimeZone': camera['properties']['olsonTimeZone'] } ret_val = post('https://arlo.netgear.com/hmsweb/users/devices/startRecord', payload, 'xcloudId': camera['xCloudId']) sleep(duration) stop_stream(camera) JSON.parse(ret_val.body) end |
#set_siren_on(device, duration) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/arlo/devices.rb', line 35 def set_siren_on(device, duration) device_id = device['deviceId'] payload = { 'from': 'arlogem', 'to': device_id, 'action': 'set', 'transId': SecureRandom.uuid, 'publishResponse': true, 'resource': 'siren', 'properties': { 'duration': duration, 'pattern': 'alarm', 'sirenState': 'on', 'volume': 8 } } ret_val = post("https://arlo.netgear.com/hmsweb/users/devices/notify/#{device_id}", payload, 'xcloudId': device['xCloudId']) JSON.parse(ret_val.body) end |
#take_snapshot(camera) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/arlo/devices.rb', line 59 def take_snapshot(camera) camera_id = camera['deviceId'] ret_val = start_stream(camera, camera_id) return ret_val unless ret_val['success'] payload = { 'cameraId': camera_id, 'parentId': camera_id, 'deviceId': camera_id, 'olsonTimeZone': camera['properties']['olsonTimeZone'] } ret_val = post('https://arlo.netgear.com/hmsweb/users/devices/takeSnapshot', payload, 'xcloudId': camera['xCloudId']) snapshot_ret_val = JSON.parse(ret_val.body) stop_stream(camera) snapshot_ret_val end |
#token ⇒ Object
10 11 12 |
# File 'lib/arlo/init.rb', line 10 def token @token end |