Class: Foscam::Client
- Inherits:
-
Object
- Object
- Foscam::Client
- Defined in:
- lib/foscam/client.rb
Overview
TODO: put in some documentation for this class
Instance Attribute Summary collapse
-
#connection ⇒ Faraday
The HTTP connection object to the camera.
-
#password ⇒ String
The password for authentication to the camera.
-
#url ⇒ String
The url to the camera.
-
#username ⇒ String
The username for authentication to the camera.
Instance Method Summary collapse
-
#camera_control(params) ⇒ FalseClass, TrueClass
Sets the camera sensor parameters.
-
#comm_write(params) ⇒ FalseClass, TrueClass
Write to comm.
-
#connect(url, username = nil, password = nil) ⇒ Object
Connects to the foscam webcam.
-
#decoder_control(action) ⇒ FalseClass, TrueClass
Controls the pan and tilt of the camera.
-
#get_camera_params ⇒ Hash
Obtains the cameras parameters (orientation, resolution, contrast, brightness).
-
#get_forbidden ⇒ Schedule::Week?
Returns the forbidden schedule for the camera.
-
#get_misc ⇒ Hash
Get miscellaneous parameters.
-
#get_params ⇒ Hash
Returns all the parameters for the camera.
-
#get_status ⇒ Hash
Obtains the cameras status information.
-
#initialize(args = {}) ⇒ Client
constructor
A new instance of Client.
-
#reboot ⇒ FalseClass, TrueClass
Reboots the camera.
-
#restore_factory ⇒ FalseClass, TrueClass
Restore settings to the factory defaults.
-
#set_alarm(params) ⇒ FalseClass, TrueClass
Set alarm parameters.
-
#set_alias(name) ⇒ FalseClass, TrueClass
Set the name of the camera.
-
#set_datetime(params) ⇒ FalseClass, TrueClass
Set the datetime of the camera.
-
#set_ddns(params) ⇒ FalseClass, TrueClass
Set the ddns parameters.
-
#set_decoder(baud) ⇒ FalseClass, TrueClass
Set decoder baud.
-
#set_forbidden(week) ⇒ FalseClass, TrueClass
Set Forbidden.
-
#set_ftp(params) ⇒ FalseClass, TrueClass
Set the ftp parameters.
-
#set_mail(params) ⇒ FalseClass, TrueClass
Set the smtp server mail notification parameters.
-
#set_misc(params) ⇒ FalseClass, TrueClass
Set miscellaneous parameters.
-
#set_network(params) ⇒ FalseClass, TrueClass
Set usernames, passwords and privilages.
-
#set_pppoe(params) ⇒ FalseClass, TrueClass
Set the pppoe parameters.
-
#set_upnp(flag) ⇒ FalseClass, TrueClass
Enable or disable upnp.
-
#set_users(params) ⇒ FalseClass, TrueClass
Set usernames, passwords and privilages.
-
#set_wifi(params) ⇒ FalseClass, TrueClass
Set the wifi parameters.
-
#snapshot ⇒ nil, ::MiniMagick::Image
Obtains a snapshot of the current image.
-
#upgrade_firmware ⇒ FalseClass, TrueClass
Upgrade the camera firmware.
-
#upgrade_htmls ⇒ FalseClass, TrueClass
Upgrade the cameras html pages.
Constructor Details
#initialize(args = {}) ⇒ Client
Returns a new instance of Client.
22 23 24 25 26 27 |
# File 'lib/foscam/client.rb', line 22 def initialize(args = {}) @url = args.delete(:url) @username = args.delete(:username) @password = args.delete(:password) connect(@url, @username, @password) end |
Instance Attribute Details
#connection ⇒ Faraday
Returns The HTTP connection object to the camera.
20 21 22 |
# File 'lib/foscam/client.rb', line 20 def connection @connection end |
#password ⇒ String
Returns The password for authentication to the camera.
16 17 18 |
# File 'lib/foscam/client.rb', line 16 def password @password end |
#url ⇒ String
Returns the url to the camera.
10 11 12 |
# File 'lib/foscam/client.rb', line 10 def url @url end |
#username ⇒ String
Returns The username for authentication to the camera.
13 14 15 |
# File 'lib/foscam/client.rb', line 13 def username @username end |
Instance Method Details
#camera_control(params) ⇒ FalseClass, TrueClass
Sets the camera sensor parameters
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/foscam/client.rb', line 133 def camera_control(params) params.all? do |key, value| # validation case key when :resolution case value when Integer throw "invalid parameter value" unless [8,32].include?(value) when String || Symbol throw "invalid parameter value" unless ["vga","qvga"].include?(value.to_s.downcase) if value.to_s.downcase == "vga" value = 32 elsif value.to_s.downcase == "qvga" value = 8 end else throw "invalid parameter value type" end when :brightness throw "invalid parameter value" if value.to_i < 0 || value.to_i > 255 when :contrast throw "invalid parameter value" if value.to_i < 0 || value.to_i > 6 when :mode case value when Integer throw "invalid parameter value" if value.to_i < 0 || value.to_i > 2 when Symbol || String throw "invalid parameter value" unless CAMERA_CONTROL_MODE.keys.include?(value.to_s.downcase.to_sym) value = CAMERA_CONTROL_MODE[value.to_s.downcase.to_sym] else throw "invalid parameter value type" end when :flip case value when Integer throw "invalid parameter value" if value.to_i < 0 || value.to_i > 3 when String || Symbol throw "invalid parameter value" unless CAMERA_CONTROL_ORIENTATION.keys.include?(value.to_s.downcase.to_sym) value = CAMERA_CONTROL_ORIENTATION[value.to_s.downcase.to_sym] else throw "invalid parameter value type" end else throw "invalid parameter" end response = @connection.get("camera_control.cgi?param=#{CAMERA_CONTROLS[key.to_sym]}&value=#{value}") response.success? end end |
#comm_write(params) ⇒ FalseClass, TrueClass
Write to comm
541 542 543 544 |
# File 'lib/foscam/client.rb', line 541 def comm_write(params) response = @connection.get("comm_write.cgi?#{params.to_query}") response.success? end |
#connect(url, username = nil, password = nil) ⇒ Object
Connects to the foscam webcam
37 38 39 40 41 42 43 |
# File 'lib/foscam/client.rb', line 37 def connect(url, username = nil, password = nil) @url = url @username = username @password = password @connection = Faraday.new( :url => @url) unless @url.nil? @connection.basic_auth(@username, @password) unless @username.nil? && @password.nil? end |
#decoder_control(action) ⇒ FalseClass, TrueClass
Controls the pan and tilt of the camera
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/foscam/client.rb', line 110 def decoder_control(action) case action when Symbol || String action_id = DECODER_CONTROLS[action.to_sym] else action_id = action end response = @connection.get("decoder_control.cgi?command=#{action_id}") response.success? end |
#get_camera_params ⇒ Hash
Obtains the cameras parameters (orientation, resolution, contrast, brightness)
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/foscam/client.rb', line 93 def get_camera_params response = @connection.get('get_camera_params.cgi') response = response.success? ? parse_response(response) : {} unless response.empty? response[:flip] = CAMERA_PARAMS_ORIENTATION[response[:flip].to_i] response[:mode] = CAMERA_PARAMS_MODE[response[:mode].to_i] response[:resolution] = CAMERA_PARAMS_RESOLUTION[response[:resolution].to_i] [:brightness, :contrast].each {|field| response[field] = response[field].to_i} end response end |
#get_forbidden ⇒ Schedule::Week?
Returns the forbidden schedule for the camera
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'lib/foscam/client.rb', line 566 def get_forbidden response = @connection.get("get_forbidden.cgi") params = response.success? ? parse_response(response) : {} schedule = {} unless params.empty? params.keys.each do |field| match = field.to_s.match(/schedule_(.+)_(\d)/) unless match.nil? schedule.merge!("#{match[1]}_#{match[2]}".to_sym => params[field].to_i) end end ::Foscam::Schedule::Week.new(schedule) else nil end end |
#get_misc ⇒ Hash
Get miscellaneous parameters
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 |
# File 'lib/foscam/client.rb', line 634 def get_misc response = @connection.get("get_misc.cgi") response = response.success? ? parse_response(response) : {} unless response.empty? [:ptz_disable_preset, :ptz_preset_onstart, :ptz_center_onstart].each do |field| response[field] = response[field].to_i > 0 end [:ptz_auto_patrol_interval, :ptz_patrol_h_rounds, :ptz_patrol_v_rounds, :ptz_patrol_rate, :ptz_patrol_up_rate, :ptz_patrol_down_rate, :ptz_patrol_left_rate, :ptz_patrol_right_rate].each do |field| response[field] = response[field].to_i end response[:led_mode] = LED_MODE[response[:led_mode].to_i] response[:ptz_auto_patrol_type] = PTZ_AUTO_PATROL_TYPE[response[:ptz_auto_patrol_type].to_i] response end response end |
#get_params ⇒ Hash
Returns all the parameters for the camera
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/foscam/client.rb', line 311 def get_params response = @connection.get("get_params.cgi") response = response.success? ? parse_response(response) : {} unless response.empty? alarm_schedule = {} ftp_schedule = {} response.keys.each do |field| ftp_match = field.to_s.match(/ftp_schedule_(.+)_(\d)/) unless ftp_match.nil? value = response.delete(field) ftp_schedule.merge!("#{ftp_match[1]}_#{ftp_match[2]}".to_sym => value.to_i) end alarm_match = field.to_s.match(/alarm_schedule_(.+)_(\d)/) unless alarm_match.nil? value = response.delete(field) alarm_schedule.merge!("#{alarm_match[1]}_#{alarm_match[2]}".to_sym => value.to_i) end end response[:ftp_schedule] = ::Foscam::Schedule::Week.new(ftp_schedule) response[:alarm_schedule] = ::Foscam::Schedule::Week.new(alarm_schedule) response[:now] = ::DateTime.strptime(response[:now],'%s') [:ntp_enable, :wifi_enable, :pppoe_enable, :upnp_enable, :alarm_schedule_enable, :ftp_schedule_enable].each do |field| response[field] = response[field].to_i > 0 end [:daylight_savings_time, :ddns_proxy_port, :ftp_port, :mail_port, :port, :dev2_port, :dev3_port, :dev4_port, :dev5_port, :dev6_port, :dev7_port, :dev8_port, :dev9_port].each do |field| response[field] = response[field].to_i end [:user1_pri, :user2_pri, :user3_pri, :user4_pri, :user5_pri, :user6_pri, :user7_pri, :user8_pri].each do |key| response[key] = USER_PERMISSIONS[response[key].to_i] end end response end |
#get_status ⇒ Hash
Obtains the cameras status information
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/foscam/client.rb', line 69 def get_status response = @connection.get('get_status.cgi') response = response.success? ? parse_response(response) : {} unless response.empty? response[:ddns_status] = DDNS_STATUS[response[:ddns_status].to_i] response[:upnp_status] = UPNP_STATUS[response[:upnp_status].to_i] response[:alarm_status] = ALARM_STATUS[response[:alarm_status].to_i] response[:now] = ::DateTime.strptime(response[:now],'%s') end response end |
#reboot ⇒ FalseClass, TrueClass
Reboots the camera
187 188 189 190 |
# File 'lib/foscam/client.rb', line 187 def reboot response = @connection.get("reboot.cgi") response.success? end |
#restore_factory ⇒ FalseClass, TrueClass
Restore settings to the factory defaults
195 196 197 198 |
# File 'lib/foscam/client.rb', line 195 def restore_factory response = @connection.get("restore_factory.cgi") response.success? end |
#set_alarm(params) ⇒ FalseClass, TrueClass
Set alarm parameters
528 529 530 531 |
# File 'lib/foscam/client.rb', line 528 def set_alarm(params) response = @connection.get("set_alarm.cgi?#{params.to_query}") response.success? end |
#set_alias(name) ⇒ FalseClass, TrueClass
Set the name of the camera
369 370 371 372 373 |
# File 'lib/foscam/client.rb', line 369 def set_alias(name) throw "invalid parameter value" if name.length > 20 response = @connection.get("set_alias.cgi?alias=#{name}") response.success? end |
#set_datetime(params) ⇒ FalseClass, TrueClass
Set the datetime of the camera
379 380 381 382 383 384 |
# File 'lib/foscam/client.rb', line 379 def set_datetime(params) # Extract the time zone throw "invalid parameter value" if params.has_key?(:ntp_svr) && params[:ntp_svr].length > 64 response = @connection.get("set_datetime.cgi?#{params.to_query}") response.success? end |
#set_ddns(params) ⇒ FalseClass, TrueClass
Set the ddns parameters
472 473 474 475 476 477 478 |
# File 'lib/foscam/client.rb', line 472 def set_ddns(params) throw "invalid parameter value" if params.has_key?(:user) && params[:user].length > 20 throw "invalid parameter value" if params.has_key?(:pwd) && params[:pwd].length > 20 throw "invalid parameter value" if params.has_key?(:host) && params[:host].length > 40 response = @connection.get("set_ddns.cgi?#{params.to_query}") response.success? end |
#set_decoder(baud) ⇒ FalseClass, TrueClass
Set decoder baud
656 657 658 659 660 |
# File 'lib/foscam/client.rb', line 656 def set_decoder(baud) baud = DECODER_BAUD_ID[baud.to_sym] if baud.is_a?(String) || baud.is_a?(Symbol) response = @connection.get("set_decoder.cgi?baud=#{baud}") response.success? end |
#set_forbidden(week) ⇒ FalseClass, TrueClass
Set Forbidden
550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'lib/foscam/client.rb', line 550 def set_forbidden(week) if week.is_a?(Schedule::Week) params = {} week.to_param.each_pair do |key, value| params.merge!("schedule_#{key}" => value) end response = @connection.get("set_forbidden.cgi?#{params.to_query}") response.success? else false end end |
#set_ftp(params) ⇒ FalseClass, TrueClass
Set the ftp parameters
490 491 492 493 |
# File 'lib/foscam/client.rb', line 490 def set_ftp(params) response = @connection.get("set_ftp.cgi?#{params.to_query}") response.success? end |
#set_mail(params) ⇒ FalseClass, TrueClass
Set the smtp server mail notification parameters
508 509 510 511 512 513 514 515 516 |
# File 'lib/foscam/client.rb', line 508 def set_mail(params) throw "invalid parameter value" if params.has_key?(:user) && params[:user].length > 20 throw "invalid parameter value" if params.has_key?(:pwd) && params[:pwd].length > 20 [:sender, :receiver1, :receiver2, :receiver3, :receiver4].each do |key| throw "invalid parameter value" if params.has_key?(key) && params[key].length > 40 end response = @connection.get("set_mail.cgi?#{params.to_query}") response.success? end |
#set_misc(params) ⇒ FalseClass, TrueClass
Set miscellaneous parameters
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 |
# File 'lib/foscam/client.rb', line 600 def set_misc(params) url_params = params.clone [:ptz_patrol_rate, :ptz_patrol_up_rate, :ptz_patrol_down_rate, :ptz_patrol_left_rate, :ptz_patrol_right_rate].each do |key| throw "invalid parameter value" if (url_params.has_key?(key) && (url_params[key].to_i < 0 || url_params[key].to_i > 100)) end [:ptz_auto_patrol_interval, :ptz_patrol_h_rounds, :ptz_patrol_v_rounds].each do |key| throw "invalid parameter value" if url_params.has_key?(key) && url_params[key].to_i < 0 end [:ptz_disable_preset, :ptz_preset_onstart, :ptz_center_onstart].each do |key| url_params[key] = handle_boolean(url_params[key]) if url_params.has_key?(key) end url_params[:led_mode] = LED_MODE_ID[url_params[:led_mode]] if url_params[:led_mode].is_a?(String) || url_params[:led_mode].is_a?(Symbol) url_params[:ptz_auto_patrol_type] = PTZ_AUTO_PATROL_TYPE_ID[url_params[:ptz_auto_patrol_type]] if url_params[:ptz_auto_patrol_type].is_a?(String) || url_params[:ptz_auto_patrol_type].is_a?(Symbol) response = @connection.get("set_misc.cgi?#{url_params.to_query}") response.success? end |
#set_network(params) ⇒ FalseClass, TrueClass
Set usernames, passwords and privilages
423 424 425 426 |
# File 'lib/foscam/client.rb', line 423 def set_network(params) response = @connection.get("set_network.cgi?#{params.to_query}") response.success? end |
#set_pppoe(params) ⇒ FalseClass, TrueClass
Set the pppoe parameters
447 448 449 450 451 452 |
# File 'lib/foscam/client.rb', line 447 def set_pppoe(params) throw "invalid parameter value" if params.has_key?(:user) && params[:user].length > 20 throw "invalid parameter value" if params.has_key?(:pwd) && params[:pwd].length > 20 response = @connection.get("set_pppoe.cgi?#{params.to_query}") response.success? end |
#set_upnp(flag) ⇒ FalseClass, TrueClass
Enable or disable upnp
458 459 460 461 |
# File 'lib/foscam/client.rb', line 458 def set_upnp(flag) response = @connection.get("set_upnp.cgi?enable=#{handle_boolean(flag)}") response.success? end |
#set_users(params) ⇒ FalseClass, TrueClass
Set usernames, passwords and privilages
397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/foscam/client.rb', line 397 def set_users(params) [:user1, :pwd1, :user2, :pwd2, :user3, :pwd3, :user4, :pwd4, :user5, :pwd5, :user6, :pwd6, :user7, :pwd7, :user8, :pwd8].each do |key| throw "invalid parameter value" if params.has_key?(key) && params[key].length > 12 end # if it is one of the matching symbols then set it to the corresponding integer [:pri1, :pri2, :pri3, :pri4, :pri5, :pri6, :pri7, :pri8].each do |key| params[key] = USER_PERMISSIONS_ID[key] if params.has_key?(key) && params[key].is_a?(Symbol) end response = @connection.get("set_users.cgi?#{params.to_query}") response.success? end |
#set_wifi(params) ⇒ FalseClass, TrueClass
Set the wifi parameters
435 436 437 438 |
# File 'lib/foscam/client.rb', line 435 def set_wifi(params) response = @connection.get("set_wifi.cgi?#{params.to_query}") response.success? end |
#snapshot ⇒ nil, ::MiniMagick::Image
Obtains a snapshot of the current image
54 55 56 57 |
# File 'lib/foscam/client.rb', line 54 def snapshot response = @connection.get('snapshot.cgi') response.success? ? ::MiniMagick::Image.read(response.body) : nil end |
#upgrade_firmware ⇒ FalseClass, TrueClass
Upgrade the camera firmware
353 354 355 |
# File 'lib/foscam/client.rb', line 353 def upgrade_firmware response = @connection.post("upgrade_firmware.cgi") end |
#upgrade_htmls ⇒ FalseClass, TrueClass
Upgrade the cameras html pages.
360 361 362 |
# File 'lib/foscam/client.rb', line 360 def upgrade_htmls response = @connection.post("upgrade_htmls.cgi") end |