Class: Foscam::Schedule::Week

Inherits:
Object
  • Object
show all
Defined in:
lib/foscam/schedule/week.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Week

Returns a new instance of Week.

Parameters:

  • params (Hash) (defaults to: {})

    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.

Options Hash (params):

  • :fri_0 (Fixnum)
  • :fri_1 (Fixnum)
  • :fri_2 (Fixnum)
  • :mon_0 (Fixnum)
  • :mon_1 (Fixnum)
  • :mon_2 (Fixnum)
  • :sat_0 (Fixnum)
  • :sat_1 (Fixnum)
  • :sat_2 (Fixnum)
  • :sun_0 (Fixnum)
  • :sun_1 (Fixnum)
  • :sun_2 (Fixnum)
  • :thu_0 (Fixnum)
  • :thu_1 (Fixnum)
  • :thu_2 (Fixnum)
  • :tue_0 (Fixnum)
  • :tue_1 (Fixnum)
  • :tue_2 (Fixnum)
  • :wed_0 (Fixnum)
  • :wed_1 (Fixnum)
  • :wed_2 (Fixnum)


30
31
32
33
34
35
36
37
38
39
# File 'lib/foscam/schedule/week.rb', line 30

def initialize(params = {})
	self.days = {}
	[:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday].each do |day|
		abbrev = day.to_s[0..2]
		bit0 = params.has_key?("#{abbrev}_0".to_sym) ? params["#{abbrev}_0".to_sym] : 0
		bit1 = params.has_key?("#{abbrev}_1".to_sym) ? params["#{abbrev}_1".to_sym] : 0
		bit2 = params.has_key?("#{abbrev}_2".to_sym) ? params["#{abbrev}_2".to_sym] : 0
		self.days.merge!( day => Day.new(bit0,bit1,bit2))
	end
end

Instance Attribute Details

#daysObject

attr_accessor :sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday



5
6
7
# File 'lib/foscam/schedule/week.rb', line 5

def days
  @days
end

Instance Method Details

#busy_at?(time) ⇒ FalseClass, TrueClass

Determine if the the schedule is true at the given date time

Parameters:

  • time (Time, DateTime)

Returns:

  • (FalseClass, TrueClass)

    Whether the schedule is true at that time



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/foscam/schedule/week.rb', line 45

def busy_at?(time)
	time = time.to_time if time.is_a?(DateTime)
	if time.sunday?
		days[:sunday].busy_at?(time)
	elsif time.monday?
		days[:monday].busy_at?(time)
	elsif time.tuesday?
		days[:tuesday].busy_at?(time)
	elsif time.wednesday?
		days[:wednesday].busy_at?(time)
	elsif time.thursday?
		days[:thursday].busy_at?(time)
	elsif time.friday?
		days[:friday].busy_at?(time)
	elsif time.saturday?
		days[:saturday].busy_at?(time)
	end
end

#to_hashHash

Convert the Week to a nested hash with the day of the week as the key and and time as the second key

Returns:

  • (Hash)


67
68
69
70
71
72
73
# File 'lib/foscam/schedule/week.rb', line 67

def to_hash
	h = {}
	self.days.each do |name, day|
		h.merge!(name => day.to_hash)
	end
	h
end

#to_paramHash

Convert the Week to the form of the input hash

Returns:

  • (Hash)


77
78
79
80
81
82
83
84
85
86
# File 'lib/foscam/schedule/week.rb', line 77

def to_param
	params = {}
	self.days.each do |name, day|
		abbrev = name.to_s[0..2]
		day.bits.each_index do |i|
			params.merge!("#{abbrev}_#{i}".to_sym => day.bits[i].bit)
		end
	end
	params
end