Class: UnifiProtect::Camera

Inherits:
Object
  • Object
show all
Defined in:
lib/unifi_protect/camera.rb

Defined Under Namespace

Classes: SnapshotError, VideoExportError

Constant Summary collapse

TIME_FIELDS =
i[upSince connectedSince lastSeen lastMotion lastRing].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, camera:) ⇒ Camera

Returns a new instance of Camera.



12
13
14
15
# File 'lib/unifi_protect/camera.rb', line 12

def initialize(client:, camera:)
  @client = client
  @camera = camera
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



29
30
31
32
33
34
# File 'lib/unifi_protect/camera.rb', line 29

def method_missing(method_name, *args)
  value = @camera.send(method_name, *args)
  return Time.at(value / 1000) if value && TIME_FIELDS.include?(method_name)

  value
end

Instance Attribute Details

#cameraObject (readonly)

Returns the value of attribute camera.



10
11
12
# File 'lib/unifi_protect/camera.rb', line 10

def camera
  @camera
end

Instance Method Details

#inspectObject



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

def inspect
  to_s
end

#match(name, matcher) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/unifi_protect/camera.rb', line 36

def match(name, matcher)
  return matcher.match(send(name)) if matcher.is_a?(Regexp)
  return send(name) == matcher if matcher.is_a?(String) || [true, false].include?(matcher)
  return matcher.any? { |item| match(name, item) } if matcher.is_a?(Array)

  if matcher.is_a?(Hash)
    value = send(name).send(matcher.first[0])
    pattern = matcher.first[1]
    return pattern.match(value)
  end

  false
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/unifi_protect/camera.rb', line 25

def respond_to_missing?(method_name, include_private = false)
  @camera.respond_to?(method_name) || super
end

#snapshot(local_file: nil) ⇒ Object



50
51
52
53
54
# File 'lib/unifi_protect/camera.rb', line 50

def snapshot(local_file: nil)
  @client.api.camera_snapshot(camera: id, local_file: local_file)
rescue UnifiProtect::API::RequestError => e
  raise SnapshotError, e
end

#to_sObject



17
18
19
# File 'lib/unifi_protect/camera.rb', line 17

def to_s
  "#<#{self.class.name} id=#{@camera.id.inspect} name=#{@camera.name.inspect}>"
end

#video_export(start_time:, end_time:, local_file: nil) ⇒ Object



56
57
58
59
60
# File 'lib/unifi_protect/camera.rb', line 56

def video_export(start_time:, end_time:, local_file: nil)
  @client.api.video_export(camera: id, start_time: start_time, end_time: end_time, local_file: local_file)
rescue UnifiProtect::API::RequestError => e
  raise VideoExportError, e
end