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
/
switch.rb
77 lines (64 loc) · 1.96 KB
/
switch.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
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'open-uri'
require 'nokogiri'
require 'logger'
require 'yaml'
require 'time'
require 'sun_times'
class Switch
attr_reader :status, :nigthly
def initialize(id)
@id = id
### Fitzbox data
@@sid = Nokogiri::XML(open("http://fritz.box/login_sid.lua")).at_xpath('//SID/text()').to_s
@name = open("http://fritz.box/webservices/homeautoswitch.lua?sid=#{@@sid}&ain=#{@id}&switchcmd=getswitchname").read
@status = self.get_status
@config = YAML.load_file("config/default.yml")
@on_time = Time.parse(@config['switches']["#{@id}"]['on_time'])
@off_time = Time.parse(@config['switches']["#{@id}"]['off_time'])
@nightly = @config['switches']["#{@id}"]['nightly']
### Suntimes
@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
if @nightly == true
@on_time = @sunset
end
### Log
@logger = Logger.new(@config['log_path'])
@logger.level = Logger::INFO
end
def switch_on
if @status == false
@logger.info("enable Switch: #{@name}")
#open("http://fritz.box/webservices/homeautoswitch.lua?sid=#{@@sid}&ain=#{@id}&switchcmd=setswitchon")
@status = true
end
end
def switch_on_by_time
if Time.now > @on_time and Time.now < @off_time
self.switch_on
end
end
def switch_off
if @status == true
@logger.info("disable Switch: #{@name}")
#open("http://fritz.box/webservices/homeautoswitch.lua?sid=#{@@sid}&ain=#{@id}&switchcmd=setswitchoff")
@status = false
end
end
def switch_off_by_time
if Time.now > @off_time
self.switch_off
end
end
protected
def get_status
if open("http://fritz.box/webservices/homeautoswitch.lua?sid=#{@@sid}&ain=#{@id}&switchcmd=getswitchstate").read.strip == "1"
return true
else
return false
end
end
end