foscam-ruby 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,89 @@
1
+ module Foscam
2
+ module Schedule
3
+ class Week
4
+ # attr_accessor :sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday
5
+ attr_accessor :days
6
+
7
+ ##
8
+ # @param [Hash] params Each input is a 32-bit bitmask representing a an 8 hour period divided into 15 minute block. Three inputs are needed for each day to to represent a full 24 hour of 96-bits.
9
+ # @option params [Fixnum] :fri_0
10
+ # @option params [Fixnum] :fri_1
11
+ # @option params [Fixnum] :fri_2
12
+ # @option params [Fixnum] :mon_0
13
+ # @option params [Fixnum] :mon_1
14
+ # @option params [Fixnum] :mon_2
15
+ # @option params [Fixnum] :sat_0
16
+ # @option params [Fixnum] :sat_1
17
+ # @option params [Fixnum] :sat_2
18
+ # @option params [Fixnum] :sun_0
19
+ # @option params [Fixnum] :sun_1
20
+ # @option params [Fixnum] :sun_2
21
+ # @option params [Fixnum] :thu_0
22
+ # @option params [Fixnum] :thu_1
23
+ # @option params [Fixnum] :thu_2
24
+ # @option params [Fixnum] :tue_0
25
+ # @option params [Fixnum] :tue_1
26
+ # @option params [Fixnum] :tue_2
27
+ # @option params [Fixnum] :wed_0
28
+ # @option params [Fixnum] :wed_1
29
+ # @option params [Fixnum] :wed_2
30
+ def initialize(params = {})
31
+ self.days = {}
32
+ [:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday].each do |day|
33
+ abbrev = day.to_s[0..2]
34
+ bit0 = params.has_key?("#{abbrev}_0".to_sym) ? params["#{abbrev}_0".to_sym] : 0
35
+ bit1 = params.has_key?("#{abbrev}_1".to_sym) ? params["#{abbrev}_1".to_sym] : 0
36
+ bit2 = params.has_key?("#{abbrev}_2".to_sym) ? params["#{abbrev}_2".to_sym] : 0
37
+ self.days.merge!( day => Day.new(bit0,bit1,bit2))
38
+ end
39
+ end
40
+
41
+ ##
42
+ # Determine if the the schedule is true at the given date time
43
+ # @param time [Time, DateTime]
44
+ # @return [FalseClass, TrueClass] Whether the schedule is true at that time
45
+ def busy_at?(time)
46
+ time = time.to_time if time.is_a?(DateTime)
47
+ if time.sunday?
48
+ days[:sunday].busy_at?(time)
49
+ elsif time.monday?
50
+ days[:monday].busy_at?(time)
51
+ elsif time.tuesday?
52
+ days[:tuesday].busy_at?(time)
53
+ elsif time.wednesday?
54
+ days[:wednesday].busy_at?(time)
55
+ elsif time.thursday?
56
+ days[:thursday].busy_at?(time)
57
+ elsif time.friday?
58
+ days[:friday].busy_at?(time)
59
+ elsif time.saturday?
60
+ days[:saturday].busy_at?(time)
61
+ end
62
+ end
63
+
64
+ ##
65
+ # Convert the Week to a nested hash with the day of the week as the key and and time as the second key
66
+ # @return [Hash]
67
+ def to_hash
68
+ h = {}
69
+ self.days.each do |name, day|
70
+ h.merge!(name => day.to_hash)
71
+ end
72
+ h
73
+ end
74
+ ##
75
+ # Convert the Week to the form of the input hash
76
+ # @return [Hash]
77
+ def to_param
78
+ params = {}
79
+ self.days.each do |name, day|
80
+ abbrev = name.to_s[0..2]
81
+ day.bits.each_index do |i|
82
+ params.merge!("#{abbrev}_#{i}".to_sym => day.bits[i].bit)
83
+ end
84
+ end
85
+ params
86
+ end
87
+ end
88
+ end
89
+ end
@@ -1,4 +1,4 @@
1
1
  module Foscam
2
2
  # Gem Version number
3
- VERSION = "0.0.3"
3
+ VERSION = "0.1.0"
4
4
  end
@@ -410,23 +410,9 @@ describe Foscam::Client do
410
410
  :mail_svr, :mail_port, :mail_tls, :mail_user, :mail_pwd, :mail_sender,
411
411
  :mail_receiver1, :mail_receiver2, :mail_receiver3, :mail_receiver4, :mail_inet_ip,
412
412
  :ftp_svr, :ftp_port, :ftp_user, :ftp_pwd, :ftp_dir, :ftp_mode, :ftp_upload_interval, :ftp_filename, :ftp_numberoffiles, :ftp_schedule_enable,
413
- :ftp_schedule_sun_0, :ftp_schedule_sun_1, :ftp_schedule_sun_2,
414
- :ftp_schedule_mon_0, :ftp_schedule_mon_1, :ftp_schedule_mon_2,
415
- :ftp_schedule_tue_0, :ftp_schedule_tue_1, :ftp_schedule_tue_2,
416
- :ftp_schedule_wed_0, :ftp_schedule_wed_1, :ftp_schedule_wed_2,
417
- :ftp_schedule_thu_0, :ftp_schedule_thu_1, :ftp_schedule_thu_2,
418
- :ftp_schedule_fri_0, :ftp_schedule_fri_1, :ftp_schedule_fri_2,
419
- :ftp_schedule_sat_0, :ftp_schedule_sat_1, :ftp_schedule_sat_2,
420
- :alarm_motion_armed, :alarm_motion_sensitivity, :alarm_motion_compensation,
413
+ :ftp_schedule,:alarm_motion_armed, :alarm_motion_sensitivity, :alarm_motion_compensation,
421
414
  :alarm_input_armed, :alarm_ioin_level, :alarm_iolinkage, :alarm_preset, :alarm_ioout_level, :alarm_mail, :alarm_upload_interval, :alarm_http, :alarm_msn, :alarm_http_url, :alarm_schedule_enable,
422
- :alarm_schedule_sun_0, :alarm_schedule_sun_1, :alarm_schedule_sun_2,
423
- :alarm_schedule_mon_0, :alarm_schedule_mon_1, :alarm_schedule_mon_2,
424
- :alarm_schedule_tue_0, :alarm_schedule_tue_1, :alarm_schedule_tue_2,
425
- :alarm_schedule_wed_0, :alarm_schedule_wed_1, :alarm_schedule_wed_2,
426
- :alarm_schedule_thu_0, :alarm_schedule_thu_1, :alarm_schedule_thu_2,
427
- :alarm_schedule_fri_0, :alarm_schedule_fri_1, :alarm_schedule_fri_2,
428
- :alarm_schedule_sat_0, :alarm_schedule_sat_1, :alarm_schedule_sat_2,
429
- :decoder_baud, :msn_user, :msn_pwd,
415
+ :alarm_schedule,:decoder_baud, :msn_user, :msn_pwd,
430
416
  :msn_friend1, :msn_friend2, :msn_friend3, :msn_friend4, :msn_friend5, :msn_friend6, :msn_friend7, :msn_friend8, :msn_friend9, :msn_friend10
431
417
  ].each do |key|
432
418
  response.should have_key(key)
@@ -484,34 +470,14 @@ describe Foscam::Client do
484
470
  response[:ftp_schedule_enable].should be_a_kind_of(Boolean)
485
471
  end
486
472
 
487
- it "sets the ftp_schedule as an integer" do
473
+ it "sets the ftp_schedule as an Week" do
488
474
  response = @service.get_params
489
- [
490
- :ftp_schedule_sun_0, :ftp_schedule_sun_1, :ftp_schedule_sun_2,
491
- :ftp_schedule_mon_0, :ftp_schedule_mon_1, :ftp_schedule_mon_2,
492
- :ftp_schedule_tue_0, :ftp_schedule_tue_1, :ftp_schedule_tue_2,
493
- :ftp_schedule_wed_0, :ftp_schedule_wed_1, :ftp_schedule_wed_2,
494
- :ftp_schedule_thu_0, :ftp_schedule_thu_1, :ftp_schedule_thu_2,
495
- :ftp_schedule_fri_0, :ftp_schedule_fri_1, :ftp_schedule_fri_2,
496
- :ftp_schedule_sat_0, :ftp_schedule_sat_1, :ftp_schedule_sat_2
497
- ].each do |key|
498
- response[key].should be_a_kind_of(Integer)
499
- end
475
+ response[:ftp_schedule].should be_an_instance_of(Foscam::Schedule::Week)
500
476
  end
501
477
 
502
- it "sets the alarm_schedule as an integer" do
478
+ it "sets the alarm_schedule as an Week" do
503
479
  response = @service.get_params
504
- [
505
- :alarm_schedule_sun_0, :alarm_schedule_sun_1, :alarm_schedule_sun_2,
506
- :alarm_schedule_mon_0, :alarm_schedule_mon_1, :alarm_schedule_mon_2,
507
- :alarm_schedule_tue_0, :alarm_schedule_tue_1, :alarm_schedule_tue_2,
508
- :alarm_schedule_wed_0, :alarm_schedule_wed_1, :alarm_schedule_wed_2,
509
- :alarm_schedule_thu_0, :alarm_schedule_thu_1, :alarm_schedule_thu_2,
510
- :alarm_schedule_fri_0, :alarm_schedule_fri_1, :alarm_schedule_fri_2,
511
- :alarm_schedule_sat_0, :alarm_schedule_sat_1, :alarm_schedule_sat_2
512
- ].each do |key|
513
- response[key].should be_a_kind_of(Integer)
514
- end
480
+ response[:alarm_schedule].should be_an_instance_of(Foscam::Schedule::Week)
515
481
  end
516
482
  end
517
483
 
@@ -1028,15 +994,16 @@ describe Foscam::Client do
1028
994
  end
1029
995
  it "returns a hash of variables" do
1030
996
  params = {
1031
- :schedule_sun_0 => 95, :schedule_sun_1 => 45, :schedule_sun_2 => 20,
1032
- :schedule_mon_0 => 95, :schedule_mon_1 => 45, :schedule_mon_2 => 20,
1033
- :schedule_tue_0 => 95, :schedule_tue_1 => 45, :schedule_tue_2 => 20,
1034
- :schedule_wed_0 => 95, :schedule_wed_1 => 45, :schedule_wed_2 => 20,
1035
- :schedule_thu_0 => 95, :schedule_thu_1 => 45, :schedule_thu_2 => 20,
1036
- :schedule_fri_0 => 95, :schedule_fri_1 => 45, :schedule_fri_2 => 20,
1037
- :schedule_sat_0 => 95, :schedule_sat_1 => 45, :schedule_sat_2 => 20
997
+ :sun_0 => 95, :sun_1 => 45, :sun_2 => 20,
998
+ :mon_0 => 95, :mon_1 => 45, :mon_2 => 20,
999
+ :tue_0 => 95, :tue_1 => 45, :tue_2 => 20,
1000
+ :wed_0 => 95, :wed_1 => 45, :wed_2 => 20,
1001
+ :thu_0 => 95, :thu_1 => 45, :thu_2 => 20,
1002
+ :fri_0 => 95, :fri_1 => 45, :fri_2 => 20,
1003
+ :sat_0 => 95, :sat_1 => 45, :sat_2 => 20
1038
1004
  }
1039
- response = @service.set_forbidden(params)
1005
+ week = Foscam::Schedule::Week.new(params)
1006
+ response = @service.set_forbidden(week)
1040
1007
  response.should be_true
1041
1008
  end
1042
1009
  end
@@ -1063,18 +1030,9 @@ describe Foscam::Client do
1063
1030
  spec.run
1064
1031
  end
1065
1032
  end
1066
- it "returns a hash of variables" do
1033
+ it "returns an Week" do
1067
1034
  response = @service.get_forbidden
1068
- [ :schedule_sun_0, :schedule_sun_1, :schedule_sun_2,
1069
- :schedule_mon_0, :schedule_mon_1, :schedule_mon_2,
1070
- :schedule_tue_0, :schedule_tue_1, :schedule_tue_2,
1071
- :schedule_wed_0, :schedule_wed_1, :schedule_wed_2,
1072
- :schedule_thu_0, :schedule_thu_1, :schedule_thu_2,
1073
- :schedule_fri_0, :schedule_fri_1, :schedule_fri_2,
1074
- :schedule_sat_0, :schedule_sat_1, :schedule_sat_2
1075
- ].each do |key|
1076
- response.should have_key(key)
1077
- end
1035
+ response.should be_an_instance_of(Foscam::Schedule::Week)
1078
1036
  end
1079
1037
  end
1080
1038
 
@@ -1087,7 +1045,7 @@ describe Foscam::Client do
1087
1045
  end
1088
1046
  it "returns a hash of variables" do
1089
1047
  response = @service.get_forbidden
1090
- response.should == {}
1048
+ response.should be_nil
1091
1049
  end
1092
1050
  end
1093
1051
  end
@@ -0,0 +1,164 @@
1
+ require 'spec_helper'
2
+
3
+ describe Foscam::Model::AlarmConfig do
4
+
5
+ def valid_client_params
6
+ {:url => "http://192.168.0.1/", :username => "foobar", :password => "secret"}
7
+ end
8
+
9
+ def default_get_params
10
+ {
11
+ :alarm_motion_armed => true,
12
+ :alarm_motion_sensitivity => 0,
13
+ :alarm_motion_compensation => 0,
14
+ :alarm_input_armed => true,
15
+ :alarm_ioin_level => 1,
16
+ :alarm_iolinkage => 0,
17
+ :alarm_preset => 0,
18
+ :alarm_ioout_level => 1,
19
+ :alarm_mail => false,
20
+ :alarm_upload_interval => 2,
21
+ :alarm_http => false,
22
+ :alarm_msn => false,
23
+ :alarm_http_url => '',
24
+ :alarm_schedule_enable => true,
25
+ :alarm_schedule => Foscam::Schedule::Week.new
26
+ }
27
+ end
28
+
29
+ before(:each) do
30
+ @client = ::Foscam::Client.new(valid_client_params)
31
+ @client.stub(:get_params).and_return(default_get_params)
32
+ end
33
+
34
+
35
+ describe "#client=" do
36
+ before(:each) do
37
+ @alarm = Foscam::Model::AlarmConfig.instance
38
+ end
39
+ it "sets the current Foscam::Client" do
40
+ @alarm.client = @client
41
+ @alarm.client.should eql(@client)
42
+ end
43
+
44
+ it "sets the alarm parameters" do
45
+ @alarm.client = @client
46
+ @alarm.motion_armed.should == default_get_params[:alarm_motion_armed]
47
+ @alarm.motion_sensitivity.should == default_get_params[:alarm_motion_sensitivity]
48
+ @alarm.motion_compensation.should == default_get_params[:alarm_motion_compensation]
49
+ @alarm.input_armed.should == default_get_params[:alarm_input_armed]
50
+ @alarm.ioin_level.should == default_get_params[:alarm_ioin_level]
51
+ @alarm.iolinkage.should == default_get_params[:alarm_iolinkage]
52
+ @alarm.preset.should == default_get_params[:alarm_preset]
53
+ @alarm.ioout_level.should == default_get_params[:alarm_ioout_level]
54
+ @alarm.mail.should == default_get_params[:alarm_mail]
55
+ @alarm.http.should == default_get_params[:alarm_http]
56
+ @alarm.msn.should == default_get_params[:alarm_msn]
57
+ @alarm.http_url.should == default_get_params[:alarm_http_url]
58
+ @alarm.schedule_enable.should == default_get_params[:alarm_schedule_enable]
59
+ end
60
+ end
61
+
62
+ describe "#save" do
63
+ before(:each) do
64
+ # @client.stub(:get_params).and_return(one_device_response)
65
+ @alarm = Foscam::Model::AlarmConfig.instance
66
+ end
67
+ context "with valid params" do
68
+ before(:each) do
69
+ @alarm.stub(:is_valid?).and_return(true)
70
+ end
71
+ context "is dirty" do
72
+ context "request is successful" do
73
+ before(:each) do
74
+ params = {
75
+ :motion_armed => false,
76
+ :motion_sensitivity => 10,
77
+ :motion_compensation => 20,
78
+ :input_armed => false,
79
+ :ioin_level=>1, :iolinkage=>0, :preset=>0, :ioout_level=>1,
80
+ :mail => true,
81
+ :http => true,
82
+ :msn => true,
83
+ # :http_url => "http://mydomain.com/",
84
+ :schedule_enable => false
85
+ }
86
+ # @alarm.stub(:changed?).and_return(true)
87
+ @client.should_receive(:set_alarm).with(params).once
88
+ @client.stub(:set_alarm).and_return(true)
89
+ end
90
+ it "updates the alarm attributes that changed" do
91
+ @alarm.client = @client
92
+ @alarm.motion_armed = false
93
+ @alarm.motion_sensitivity = 10
94
+ @alarm.motion_compensation = 20
95
+ @alarm.input_armed = false
96
+ @alarm.mail = true
97
+ @alarm.http = true
98
+ @alarm.msn = true
99
+ # @alarm.http_url = "http://mydomain.com/",
100
+ @alarm.schedule_enable = false
101
+ flag = @alarm.save
102
+ flag.should be_true
103
+ end
104
+ end
105
+ context "request is unsuccessful" do
106
+ before(:each) do
107
+ params = {
108
+ :motion_armed => false,
109
+ :motion_sensitivity => 10,
110
+ :motion_compensation => 20,
111
+ :input_armed => false,
112
+ :mail => true,
113
+ # :upload_interval => 5,
114
+ :http => true,
115
+ :msn => true,
116
+ # :http_url => "http://mydomain.com/",
117
+ :schedule_enable => false
118
+ }
119
+ # @alarm.stub(:changed?).and_return(true)
120
+ @client.should_receive(:set_alarm).with(params).once
121
+ @client.stub(:set_alarm).and_return(false)
122
+ end
123
+ it "fails to update the device attributes" do
124
+ @alarm.client = @client
125
+ @alarm.motion_armed = false
126
+ @alarm.motion_sensitivity = 10
127
+ @alarm.motion_compensation = 20
128
+ @alarm.input_armed = false
129
+ @alarm.mail = true
130
+ @alarm.http = true
131
+ @alarm.msn = true
132
+ @alarm.http_url = "http://mydomain.com/",
133
+ @alarm.schedule_enable = false
134
+
135
+ flag = @alarm.save
136
+ flag.should be_false
137
+ end
138
+ end
139
+ end
140
+ context "is not dirty" do
141
+ before(:each) do
142
+ @alarm.stub(:changed?).and_return(false)
143
+ end
144
+ it "skips updating since nothing changed" do
145
+ @client.should_not_receive(:set_alarm)
146
+ @alarm.client = @client
147
+ flag = @alarm.save
148
+ flag.should be_false
149
+ end
150
+ end
151
+ end
152
+ context "with invalid params" do
153
+ before(:each) do
154
+ @alarm.stub(:is_valid?).and_return(false)
155
+ end
156
+ it "skips updating since nothing changed" do
157
+ @client.should_not_receive(:set_alarm)
158
+ @alarm.client = @client
159
+ flag = @alarm.save
160
+ flag.should be_false
161
+ end
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Foscam::Model::Base do
4
+
5
+ def valid_params
6
+ {:url => "http://192.168.0.1/", :username => "foobar", :password => "secret"}
7
+ end
8
+
9
+ describe "#connect" do
10
+ before(:each) do
11
+ # @client.stub(:get_params).and_return(one_device_response)
12
+ @device = Foscam::Model::Base.new
13
+ end
14
+ it "returns an instance of a Foscam::Client" do
15
+ client = @device.connect(valid_params)
16
+ client.should be_an_instance_of(::Foscam::Client)
17
+ client.username.should == valid_params[:username]
18
+ client.url.should == valid_params[:url]
19
+ client.password.should == valid_params[:password]
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,136 @@
1
+ require 'spec_helper'
2
+
3
+ describe Foscam::Model::Device do
4
+
5
+ def valid_params
6
+ {:url => "http://192.168.0.1/", :username => "foobar", :password => "secret"}
7
+ end
8
+ before(:each) do
9
+ @client = ::Foscam::Client.new(valid_params)
10
+ @client.stub(:get_camera_params).and_return({:resolution => "qvga", :brightness => 169, :contrast => 2, :mode => "60hz", :flip => "flip", :fps => 0})
11
+ end
12
+
13
+ describe "#client=" do
14
+ before(:each) do
15
+ @device = Foscam::Model::Device.instance
16
+ end
17
+ it "sets the current Foscam::Client" do
18
+ @device.client = @client
19
+ @device.client.should eql(@client)
20
+ end
21
+
22
+ it "sets the camera parameters" do
23
+ @device.client = @client
24
+ @device.resolution.should == "qvga"
25
+ @device.brightness.should == 169
26
+ @device.contrast.should == 2
27
+ # @device.mode.should == "60Hz"
28
+ # @device.orientation.should == "default"
29
+ end
30
+ end
31
+
32
+ describe "#capture" do
33
+ before(:each) do
34
+ # @client.stub(:get_params).and_return(one_device_response)
35
+ @device = Foscam::Model::Device.instance
36
+ end
37
+ context "request is successful" do
38
+ it "returns an image" do
39
+ @client.should_receive(:snapshot)
40
+ @device.client = @client
41
+ image = @device.capture
42
+ end
43
+ end
44
+ end
45
+
46
+
47
+ # def action(value)
48
+ # # have an action map to map some subset to the foscam set
49
+ # @client.decoder_control(value)
50
+ # end
51
+ describe "#action" do
52
+ before(:each) do
53
+ # @client.stub(:get_params).and_return(one_device_response)
54
+ @device = Foscam::Model::Device.instance
55
+ end
56
+ context "request is successful" do
57
+ before(:each) do
58
+ @client.stub(:decoder_control).and_return(true)
59
+ end
60
+ it "processes the action" do
61
+ @client.should_receive(:decoder_control).with(:up).once
62
+ @device.client = @client
63
+ flag = @device.action(:up)
64
+ flag.should be_true
65
+ end
66
+ end
67
+ end
68
+
69
+ describe "#save" do
70
+ before(:each) do
71
+ # @client.stub(:get_params).and_return(one_device_response)
72
+ @device = Foscam::Model::Device.instance
73
+ end
74
+ context "with valid params" do
75
+ before(:each) do
76
+ @device.stub(:is_valid?).and_return(true)
77
+ end
78
+ context "is dirty" do
79
+ before(:each) do
80
+ params = {:resolution => "vga", :brightness => 230, :contrast => 3}
81
+ # @device.stub(:changed?).and_return(true)
82
+ @client.should_receive(:camera_control).with(params).once
83
+ end
84
+ context "request is successful" do
85
+ before(:each) do
86
+ @client.stub(:camera_control).and_return(true)
87
+ end
88
+ it "updates the device attributes that changed" do
89
+ @device.client = @client
90
+ @device.brightness = 230
91
+ @device.contrast = 3
92
+ @device.resolution = "vga"
93
+ flag = @device.save
94
+ flag.should be_true
95
+ end
96
+ end
97
+ context "request is unsuccessful" do
98
+ before(:each) do
99
+ @client.stub(:camera_control).and_return(false)
100
+ end
101
+ it "fails to update the device attributes" do
102
+ @device.client = @client
103
+ @device.brightness = 230
104
+ @device.contrast = 3
105
+ @device.resolution = "vga"
106
+ flag = @device.save
107
+ flag.should be_false
108
+ end
109
+ end
110
+ end
111
+ context "is not dirty" do
112
+ before(:each) do
113
+ @device.stub(:changed?).and_return(false)
114
+ end
115
+ it "skips updating since nothing changed" do
116
+ @client.should_not_receive(:camera_control)
117
+ @device.client = @client
118
+ flag = @device.save
119
+ flag.should be_false
120
+ end
121
+ end
122
+ end
123
+ context "with invalid params" do
124
+ before(:each) do
125
+ @device.stub(:is_valid?).and_return(false)
126
+ end
127
+ it "skips updating since nothing changed" do
128
+ @client.should_not_receive(:camera_control)
129
+ @device.client = @client
130
+ flag = @device.save
131
+ flag.should be_false
132
+ end
133
+ end
134
+ end
135
+
136
+ end