Class: Eufycam::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/eufycam/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(email:, password:) ⇒ Client

Returns a new instance of Client.



9
10
11
12
# File 'lib/eufycam/client.rb', line 9

def initialize(email:, password:)
  @email = email
  @password = password
end

Instance Method Details

#capture_image(url, filename) ⇒ Object



61
62
63
# File 'lib/eufycam/client.rb', line 61

def capture_image(url, filename)
  system('ffmpeg', '-hide_banner', '-loglevel', 'panic', '-i', url.to_s, '-r', '5', filename.to_s)
end

#generate_auth_tokenObject



31
32
33
34
35
36
37
38
# File 'lib/eufycam/client.rb', line 31

def generate_auth_token
  post('passport/login', nil, {
         email: ENV.fetch('EUFYCAM_EMAIL'),
         password: ENV.fetch('EUFYCAM_PASSWORD')
       }) do |response|
    JSON.parse(response.body)['data']['auth_token']
  end
end

#list_devices(auth_token = generate_auth_token) ⇒ Object



40
41
42
43
44
# File 'lib/eufycam/client.rb', line 40

def list_devices(auth_token = generate_auth_token)
  post('app/get_devs_list', auth_token) do |response|
    JSON.parse(response.body)['data']
  end
end

#post(path, access_token, body = nil) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/eufycam/client.rb', line 14

def post(path, access_token, body = nil)
  uri = URI("https://mysecurity.eufylife.com/api/v1/#{path}")

  Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    yield http.request(request(path, access_token, body))
  end
end

#request(path, auth_token, body = nil) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/eufycam/client.rb', line 22

def request(path, auth_token, body = nil)
  uri = URI("https://mysecurity.eufylife.com/api/v1/#{path}")

  Net::HTTP::Post.new(uri).tap do |post|
    post.body = body.to_json
    post['x-auth-token'] = auth_token unless auth_token.nil?
  end
end

#start_stream(device_name:) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/eufycam/client.rb', line 46

def start_stream(device_name:)
  auth_token = generate_auth_token
  body = list_devices(auth_token)
         .detect { |d| d['device_name'] == device_name }
         .slice('station_sn', 'device_sn')

  post(
    'web/equipment/start_stream',
    auth_token,
    body
  ) do |response|
    JSON.parse(response.body)['data']
  end
end

#timelapse(device_name, directory) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/eufycam/client.rb', line 65

def timelapse(device_name, directory)
  url = start_stream(device_name)

  loop do
    capture_image(url, File.expand_path("#{directory}/#{Time.now.to_i}.png"))
    print '.'
    sleep(1)
  end
end