This repository has been archived by the owner on Jul 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
house.rb
64 lines (55 loc) · 1.4 KB
/
house.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require 'sun_times'
require 'open-uri'
require 'nokogiri'
require_relative 'switch.rb'
class House
attr_reader :switches
def initialize()
@sid = Nokogiri::XML(open("http://fritz.box/login_sid.lua")).at_xpath('//SID/text()').to_s
ains = open("http://fritz.box/webservices/homeautoswitch.lua?sid=#{@sid}&switchcmd=getswitchlist").read.strip.split(",")
@switches = []
ains.each do |ain|
@switches.push(Switch.new(ain))
end
@sun_times = SunTimes.new
@latitude = 52.633056
@longitude = 13.55
@sunrise = @sun_times.rise(Date.today, @latitude, @longitude).localtime
@sunset = @sun_times.set(Date.today, @latitude, @longitude).localtime
@global_turn_off = Time.new(Date.today.year,Date.today.month,Date.today.day,23,0,0)
end
def switch_all_on
@switches.each do |switch|
switch.switch_on_by_time
end
end
def switch_all_on_at_night
if !daytime?
@switches.each do |switch|
switch.switch_on_by_time
end
end
end
def switch_all_off
@switches.each do |switch|
switch.switch_off_by_time
end
end
def global_turn_off
if Time.now >= @global_turn_off
switch_all_off
end
end
def switch_outdoor_lights_on
if Time.now <= @global_turn_off
switch_all_on
end
end
def daytime?
if Time.now > @sunrise and Time.now < @sunset
return true
else
return false
end
end
end