trigger_switch_d 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +74 -57
- data/bin/ts_setup +68 -0
- data/bin/{teach → ts_teach} +1 -1
- data/bin/{turn_off → ts_turn_off} +1 -1
- data/bin/{turn_on → ts_turn_on} +1 -1
- data/bin/tsd +1 -1
- data/config/environment.rb.sample +42 -0
- data/config/lookup.dsl +7 -0
- data/lib/tellstick/code_converter.rb +56 -0
- data/lib/tellstick/protocol/nexa.rb +25 -10
- data/lib/tellstick/protocol/not_connected.rb +6 -14
- data/lib/tellstick/protocol/null_protocol.rb +6 -14
- data/lib/tellstick/protocol/proove.rb +60 -35
- data/lib/tellstick/protocol/protocol_loader.rb +2 -3
- data/lib/tellstick/protocol/rising_sun.rb +8 -4
- data/lib/tellstick/proxy_io.rb +32 -14
- data/lib/tellstick/tellstick.rb +4 -12
- data/lib/trigger_switch_d/action.rb +54 -19
- data/lib/trigger_switch_d/application.rb +77 -34
- data/lib/trigger_switch_d/config.rb +85 -35
- data/lib/trigger_switch_d/daylight.rb +135 -52
- data/lib/trigger_switch_d/ipc.rb +44 -12
- data/lib/trigger_switch_d/schedule.rb +20 -13
- data/lib/trigger_switch_d/switch.rb +6 -0
- data/lib/trigger_switch_d/trigger_switch_d_helper.rb +14 -13
- data/samples/devices +14 -0
- data/samples/devices.rising_sun +6 -0
- data/samples/environment.rb +44 -0
- data/samples/lookup.dsl +7 -0
- data/samples/scheduled_actions +6 -0
- data/samples/scheduled_actions.rising_sun +4 -0
- metadata +41 -116
- data/doc/classes/Tellstick/Tellstick.html +0 -117
- data/doc/classes/Tellstick/TellstickIO.html +0 -265
- data/doc/classes/Tellstick/TellstickIO.src/M000020.html +0 -18
- data/doc/classes/Tellstick/TellstickIO.src/M000021.html +0 -20
- data/doc/classes/Tellstick/TellstickIO.src/M000022.html +0 -26
- data/doc/classes/Tellstick/TellstickIO.src/M000023.html +0 -19
- data/doc/classes/Tellstick/TellstickIO.src/M000024.html +0 -26
- data/doc/classes/Tellstick/TellstickIO.src/M000025.html +0 -16
- data/doc/classes/Tellstick/TellstickIO.src/M000026.html +0 -16
- data/doc/classes/Tellstick/TellstickIO.src/M000027.html +0 -24
- data/doc/classes/Tellstick.html +0 -204
- data/doc/classes/Tellstick.src/M000019.html +0 -25
- data/doc/classes/TriggerSwitchD/Action.html +0 -165
- data/doc/classes/TriggerSwitchD/Action.src/M000015.html +0 -28
- data/doc/classes/TriggerSwitchD/Action.src/M000016.html +0 -18
- data/doc/classes/TriggerSwitchD/Application.html +0 -203
- data/doc/classes/TriggerSwitchD/Application.src/M000017.html +0 -21
- data/doc/classes/TriggerSwitchD/Application.src/M000018.html +0 -40
- data/doc/classes/TriggerSwitchD/ConfigFactory/Config.html +0 -127
- data/doc/classes/TriggerSwitchD/ConfigFactory/Config.src/M000014.html +0 -23
- data/doc/classes/TriggerSwitchD/ConfigFactory.html +0 -141
- data/doc/classes/TriggerSwitchD/ConfigFactory.src/M000013.html +0 -24
- data/doc/classes/TriggerSwitchD/IPC.html +0 -134
- data/doc/classes/TriggerSwitchD/IPC.src/M000012.html +0 -32
- data/doc/classes/TriggerSwitchD/Schedule/InstanceMethods.html +0 -242
- data/doc/classes/TriggerSwitchD/Schedule/InstanceMethods.src/M000005.html +0 -19
- data/doc/classes/TriggerSwitchD/Schedule/InstanceMethods.src/M000006.html +0 -18
- data/doc/classes/TriggerSwitchD/Schedule/InstanceMethods.src/M000007.html +0 -23
- data/doc/classes/TriggerSwitchD/Schedule/InstanceMethods.src/M000008.html +0 -19
- data/doc/classes/TriggerSwitchD/Schedule/InstanceMethods.src/M000009.html +0 -19
- data/doc/classes/TriggerSwitchD/Schedule/InstanceMethods.src/M000010.html +0 -18
- data/doc/classes/TriggerSwitchD/Schedule/InstanceMethods.src/M000011.html +0 -18
- data/doc/classes/TriggerSwitchD/Schedule.html +0 -147
- data/doc/classes/TriggerSwitchD/Schedule.src/M000004.html +0 -19
- data/doc/classes/TriggerSwitchD/SwitchFactory.html +0 -135
- data/doc/classes/TriggerSwitchD/SwitchFactory.src/M000003.html +0 -20
- data/doc/classes/TriggerSwitchD.html +0 -212
- data/doc/classes/TriggerSwitchD.src/M000001.html +0 -22
- data/doc/classes/TriggerSwitchD.src/M000002.html +0 -27
- data/doc/created.rid +0 -1
- data/doc/files/lib/tellstick/protocol/nexa_rb.html +0 -126
- data/doc/files/lib/tellstick/protocol/not_connected_rb.html +0 -126
- data/doc/files/lib/tellstick/protocol/null_protocol_rb.html +0 -126
- data/doc/files/lib/tellstick/protocol/plain_text_rb.html +0 -126
- data/doc/files/lib/tellstick/protocol/proove_rb.html +0 -126
- data/doc/files/lib/tellstick/protocol/protocol_loader_rb.html +0 -126
- data/doc/files/lib/tellstick/protocol/rising_sun_rb.html +0 -126
- data/doc/files/lib/tellstick/protocol/waveman_rb.html +0 -126
- data/doc/files/lib/tellstick/tellstick_io_rb.html +0 -135
- data/doc/files/lib/tellstick/tellstick_rb.html +0 -135
- data/doc/files/lib/tellstick_rb.html +0 -134
- data/doc/files/lib/trigger_switch_d/action_rb.html +0 -126
- data/doc/files/lib/trigger_switch_d/application_rb.html +0 -136
- data/doc/files/lib/trigger_switch_d/config_rb.html +0 -126
- data/doc/files/lib/trigger_switch_d/daylight_rb.html +0 -126
- data/doc/files/lib/trigger_switch_d/ipc_rb.html +0 -126
- data/doc/files/lib/trigger_switch_d/schedule_rb.html +0 -126
- data/doc/files/lib/trigger_switch_d/switch_rb.html +0 -126
- data/doc/files/lib/trigger_switch_d/trigger_switch_d_helper_rb.html +0 -126
- data/doc/files/lib/trigger_switch_d_rb.html +0 -140
- data/doc/fr_class_index.html +0 -38
- data/doc/fr_file_index.html +0 -46
- data/doc/fr_method_index.html +0 -53
- data/doc/index.html +0 -24
- data/features/execute_schedule.feature +0 -20
- data/features/group_aliases.feature +0 -31
- data/features/handle_switches.feature +0 -36
- data/features/listen_for_client_call.feature +0 -23
- data/features/persistent_schedule.feature +0 -70
- data/features/proove_protocol.feature +0 -20
- data/features/schedule.feature +0 -25
- data/features/simple_string_protocol.feature +0 -35
- data/features/step_definitions/execute_schedule_steps.rb +0 -16
- data/features/step_definitions/group_aliases_steps.rb +0 -24
- data/features/step_definitions/handle_switches_steps.rb +0 -21
- data/features/step_definitions/listen_for_client_call_steps.rb +0 -34
- data/features/step_definitions/persistent_schedule_steps.rb +0 -18
- data/features/step_definitions/proove_protocol_steps.rb +0 -3
- data/features/step_definitions/schedule_steps.rb +0 -9
- data/features/step_definitions/shared_steps.rb +0 -75
- data/features/step_definitions/simple_string_protocol_steps.rb +0 -3
- data/features/step_definitions/sunrise_steps.rb +0 -22
- data/features/step_definitions/tellstick_connected_steps.rb +0 -21
- data/features/sunrise.feature +0 -22
- data/features/sunset.feature +0 -22
- data/features/support/action_helpers.rb +0 -10
- data/features/support/env.rb +0 -7
- data/features/support/listen_for_client_call_helper.rb +0 -71
- data/features/support/proove_helpers.rb +0 -6
- data/features/support/sunrise_helper.rb +0 -8
- data/features/tellstick_connected.feature +0 -14
- data/spec/spec_helper.rb +0 -22
- data/spec/tellstick/protocol/nexa_spec.rb +0 -57
- data/spec/tellstick/protocol/proove_spec.rb +0 -61
- data/spec/tellstick/protocol/rising_sun_spec.rb +0 -56
- data/spec/tellstick/protocol/waveman_spec.rb +0 -57
- data/spec/tellstick/proxy_io_spec.rb +0 -34
- data/spec/tellstick/tellstick_spec.rb +0 -86
- data/spec/trigger_switch_d/application_spec.rb +0 -147
- data/spec/trigger_switch_d/config_spec.rb +0 -154
- data/spec/trigger_switch_d/daylight_spec.rb +0 -39
- data/spec/trigger_switch_d/ipc_spec.rb +0 -119
- data/spec/trigger_switch_d/schedule_spec.rb +0 -103
- data/spec/trigger_switch_d/trigger_switch_d_spec_helpers.rb +0 -24
- data/trigger_switch_d.gemspec +0 -43
@@ -16,6 +16,7 @@
|
|
16
16
|
|
17
17
|
|
18
18
|
require File.join(File.dirname(__FILE__),"protocol_loader")
|
19
|
+
require File.join(File.dirname(__FILE__),"..","code_converter")
|
19
20
|
|
20
21
|
module Tellstick
|
21
22
|
module RisingSun #:nodoc:
|
@@ -30,12 +31,15 @@ module Tellstick
|
|
30
31
|
@output
|
31
32
|
end
|
32
33
|
private
|
34
|
+
|
35
|
+
attr_reader :convert_it
|
36
|
+
def convert_it
|
37
|
+
@convert_it ||= CodeConverter.new([/[1234]/,/[A-D]/,/[a-d]/],-1)
|
38
|
+
end
|
39
|
+
|
33
40
|
def codify(code)
|
34
|
-
tmp_code = ((code =~ /[1234]/) == 0) ? code.to_i - 1: 0
|
35
|
-
tmp_code = code[0]-"A"[0] if ((code =~ /[A-D]/) == 0)
|
36
|
-
tmp_code = code[0]-"a"[0] if ((code =~ /[a-d]/) == 0)
|
37
41
|
result = ["e..e", "e..e", "e..e", "e..e"]
|
38
|
-
result[
|
42
|
+
result[convert_it.to_i(code)] = ".e.e"
|
39
43
|
result
|
40
44
|
end
|
41
45
|
end
|
data/lib/tellstick/proxy_io.rb
CHANGED
@@ -15,44 +15,62 @@
|
|
15
15
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
16
|
|
17
17
|
module Tellstick
|
18
|
-
|
18
|
+
#Proxy between usb dongle and driver
|
19
19
|
class ProxyIO #:nodoc:all
|
20
20
|
public
|
21
21
|
def initialize(klass,*args)
|
22
22
|
@init_args = args
|
23
23
|
@class_to_proxy = klass
|
24
24
|
@tio = nil
|
25
|
-
open
|
25
|
+
# open
|
26
|
+
end
|
27
|
+
|
28
|
+
def method_missing(method_id)
|
29
|
+
@tio.send(method_id)
|
26
30
|
end
|
31
|
+
|
27
32
|
def open
|
28
33
|
return unless @tio == nil
|
29
|
-
|
30
|
-
|
31
|
-
else
|
32
|
-
@tio = @class_to_proxy.new(@init_args[0],@init_args[1])
|
33
|
-
@init_args[2].call(@tio)
|
34
|
-
end
|
35
|
-
if @tio.methods.include?("connected?")
|
36
|
-
def self.connected?
|
37
|
-
open
|
38
|
-
@tio.connected?
|
39
|
-
end
|
40
|
-
end
|
34
|
+
create_io_instance
|
35
|
+
attach_connected
|
41
36
|
return if @tio.private_methods.include?("open")
|
42
37
|
@tio.open
|
43
38
|
end
|
39
|
+
|
44
40
|
def close
|
45
41
|
@tio.close unless @tio == nil
|
46
42
|
@tio = nil
|
47
43
|
end
|
44
|
+
|
48
45
|
def puts(input)
|
49
46
|
open
|
50
47
|
@tio.puts(input)
|
51
48
|
end
|
49
|
+
|
52
50
|
def gets
|
53
51
|
open
|
54
52
|
@tio.gets
|
55
53
|
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def create_io_instance
|
58
|
+
if (@init_args.empty?)
|
59
|
+
@tio = @class_to_proxy.new
|
60
|
+
else
|
61
|
+
@tio = @class_to_proxy.new(@init_args[0],@init_args[1])
|
62
|
+
@init_args[2].call(@tio)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def attach_connected
|
67
|
+
if @tio.methods.include?("connected?")
|
68
|
+
def self.connected?
|
69
|
+
open
|
70
|
+
@tio.connected?
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
56
74
|
end
|
57
75
|
end
|
58
76
|
|
data/lib/tellstick/tellstick.rb
CHANGED
@@ -57,17 +57,9 @@ module Tellstick
|
|
57
57
|
not output.methods.include?("connected?")
|
58
58
|
end
|
59
59
|
def self.load(name)
|
60
|
-
$protocol ||=
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
if File.exists?("#{filename}.rb")
|
65
|
-
require filename
|
66
|
-
protocol = $protocol[name]
|
67
|
-
else
|
68
|
-
protocol = NullProtocol
|
69
|
-
end
|
70
|
-
end
|
71
|
-
protocol
|
60
|
+
$protocol ||= Hash.new(NullProtocol)
|
61
|
+
filename = File.join(File.dirname(__FILE__),"protocol",name)
|
62
|
+
require filename if File.exists?("#{filename}.rb")
|
63
|
+
return $protocol[name]
|
72
64
|
end
|
73
65
|
end
|
@@ -29,28 +29,27 @@ module TriggerSwitchD
|
|
29
29
|
@at_location = environment[:at_location]
|
30
30
|
@lookup = environment[:lookup]
|
31
31
|
tokens = code_line.scan(/(\w+) (\w+) (\w+) ([\w\d:]+)/u).flatten
|
32
|
-
(class << self;self;end)
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
@at, executable = send(code[3], send(code[2], send(code[1], code[0])))
|
37
|
-
(class << self;self;end).methodize_executable(executable)
|
32
|
+
ghost = (class << self;self;end)
|
33
|
+
ghost.methodize_device(tokens[1],devices,@lookup)
|
34
|
+
@at, executable = __build_executable_at__(tokens)
|
35
|
+
ghost.methodize_executable(executable)
|
38
36
|
end
|
39
37
|
|
40
38
|
def activate(*args) #:nodoc:
|
41
|
-
action(args
|
39
|
+
Action.action(:turn_on,*args)
|
42
40
|
end
|
43
41
|
|
44
42
|
def deactivate(*args) #:nodoc:
|
45
|
-
action(args
|
43
|
+
Action.action(:turn_off,*args)
|
46
44
|
end
|
47
45
|
|
48
46
|
def at(*args) #:nodoc:
|
49
|
-
|
50
|
-
|
51
|
-
|
47
|
+
time = *args
|
48
|
+
return @at if time == nil
|
49
|
+
if (time =~ /(?:2[0-3]|[01]\d):[0-5]\d/) == 0
|
50
|
+
return time
|
52
51
|
end
|
53
|
-
|
52
|
+
at_calculated_time(*args)
|
54
53
|
end
|
55
54
|
|
56
55
|
def to_s
|
@@ -59,24 +58,60 @@ module TriggerSwitchD
|
|
59
58
|
|
60
59
|
private
|
61
60
|
|
62
|
-
def
|
63
|
-
|
61
|
+
def __build_executable_at__(tokens)
|
62
|
+
code = __translate__(tokens).reverse
|
63
|
+
code[1..3].inject(code[0]) do |part,val|
|
64
|
+
send(val, *part)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def __translate__(tokens)
|
69
|
+
tokens.map do |token|
|
70
|
+
(@lookup.include? token) ? @lookup[token] : token
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def at_calculated_time(*args) #:nodoc:
|
75
|
+
method_name = *args
|
76
|
+
@date.extend(Daylight)
|
77
|
+
time = @date.send(method_name,@at_location)
|
78
|
+
time.localtime.strftime("%H:%M")
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.action(*args)
|
82
|
+
event, switches, at_time = *args
|
83
|
+
call_args = Action.__build_action_args__(event,switches)
|
64
84
|
code = Proc.new do |output|
|
65
|
-
|
66
|
-
Tellstick::create(output,switch.model_name).send(event,switch.house_code, switch.unit_code)
|
67
|
-
end
|
85
|
+
Action.__action_gut__(output,*call_args)
|
68
86
|
end
|
69
87
|
[at_time,code]
|
70
88
|
end
|
71
89
|
|
90
|
+
def self.__build_action_args__(event,switches)
|
91
|
+
switches.map do |switch|
|
92
|
+
switch_array = switch.to_a_by_keys(%w{model_name house_code unit_code})
|
93
|
+
switch_array.insert(1,event)
|
94
|
+
switch_array
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.__action_gut__(*args)
|
99
|
+
args.each do |stick_call|
|
100
|
+
output, model_name, *params = args.flatten
|
101
|
+
Tellstick::create(output,model_name).send(*params)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
72
105
|
def self.methodize_executable(executable)
|
73
106
|
define_method(:execute,&executable)
|
74
107
|
end
|
75
108
|
|
76
|
-
def self.methodize_device(name,
|
109
|
+
def self.methodize_device(name,devices,lookup)
|
110
|
+
return unless devices.include? name
|
77
111
|
lookup[name] = :remote_device.to_s
|
78
112
|
define_method(:remote_device) do |*args|
|
79
|
-
|
113
|
+
time = *args
|
114
|
+
[devices[name],time]
|
80
115
|
end
|
81
116
|
end
|
82
117
|
end
|
@@ -36,32 +36,16 @@ module TriggerSwitchD
|
|
36
36
|
logger.info("Environment prepared")
|
37
37
|
end
|
38
38
|
|
39
|
+
ANYTHING_STR = "anything"
|
40
|
+
RESPONSE_STR = "response"
|
41
|
+
RECEIVED_STR = "received"
|
39
42
|
#Runs the daemon untill process killed or it receives
|
40
43
|
#stop from client connected via unix socket
|
41
44
|
def start
|
42
|
-
|
43
|
-
logger.info("IPC started")
|
44
|
-
|
45
|
-
|
46
|
-
now_time = Time.new
|
47
|
-
load_db(now_time.strftime("%Y-%m-%d %H:%M:%S"))
|
48
|
-
hhmm_now_time = now_time.strftime("%H:%M")
|
49
|
-
if methods.include? command.to_s
|
50
|
-
com_thread["response"] = send(command).to_yaml
|
51
|
-
logger.info("Received #{command} via IPC")
|
52
|
-
Config.log_object.flush
|
53
|
-
com_thread["received"] = "anything"
|
54
|
-
com_thread.run
|
55
|
-
end
|
56
|
-
if (schedule.cronological.keys.include? hhmm_now_time)
|
57
|
-
logger.info("Executed #{schedule.cronological[hhmm_now_time].length} action(s) at #{hhmm_now_time}")
|
58
|
-
Config.log_object.flush
|
59
|
-
schedule.execute(hhmm_now_time,Config.output)
|
60
|
-
end
|
61
|
-
Kernel.sleep(1)
|
62
|
-
com_thread["received"] = "anything" if com_thread["received"] == ""
|
63
|
-
end
|
64
|
-
logger.info("Received #{command} via IPC")
|
45
|
+
@ipc = IPC.create(Config.unix_socket_path)
|
46
|
+
@logger.info("IPC started")
|
47
|
+
__run_loop__
|
48
|
+
@logger.info("Received stop via IPC")
|
65
49
|
end
|
66
50
|
|
67
51
|
def list_schedule #:nodoc:
|
@@ -70,19 +54,78 @@ module TriggerSwitchD
|
|
70
54
|
|
71
55
|
private
|
72
56
|
|
73
|
-
def
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
57
|
+
def load_db_at(time)
|
58
|
+
return unless did_set_current_date?(time)
|
59
|
+
load_db
|
60
|
+
logger.info("Loaded #{devices.length} devices and #{schedule.by_name.length} actions")
|
61
|
+
Config::flush_log
|
62
|
+
end
|
63
|
+
|
64
|
+
def did_set_current_date?(time)
|
65
|
+
date = Date.parse(time.strftime("%Y-%m-%d %H:%M:%S"))
|
66
|
+
set_current_date = (@current_date != date)
|
67
|
+
@current_date = date if set_current_date
|
68
|
+
set_current_date
|
69
|
+
end
|
70
|
+
|
71
|
+
def load_db
|
79
72
|
@schedule = Schedule.create
|
80
|
-
file =
|
81
|
-
|
73
|
+
Application.load_db_file(:devices) { |file| @devices = TriggerSwitchD::load_devices(file) }
|
74
|
+
inner_block = Proc.new do |code_line, date|
|
75
|
+
@schedule.schedule_action(Action.new(code_line,devices,Config.environment,date))
|
76
|
+
end
|
77
|
+
Application.load_db_file(:schedule) do |file|
|
78
|
+
TriggerSwitchD::load_scheduled_actions(file,&inner_block)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.load_db_file(filename_symbol)
|
83
|
+
lookup = {:devices => 0, :schedule => 1}
|
84
|
+
file = File.open(Config.db_path[lookup[filename_symbol]],"r")
|
85
|
+
yield(file)
|
82
86
|
file.close
|
83
|
-
|
84
|
-
|
85
|
-
|
87
|
+
end
|
88
|
+
|
89
|
+
def received
|
90
|
+
result = @ipc[RECEIVED_STR]
|
91
|
+
return result if result != ""
|
92
|
+
reset_received
|
93
|
+
ANYTHING_STR
|
94
|
+
end
|
95
|
+
|
96
|
+
def reset_received
|
97
|
+
@ipc[RECEIVED_STR] = ANYTHING_STR
|
98
|
+
end
|
99
|
+
|
100
|
+
def __run_loop__
|
101
|
+
command = ANYTHING_STR
|
102
|
+
while ((command = received).to_sym != :stop)
|
103
|
+
__inner_run_loop__(command)
|
104
|
+
Kernel.sleep(1)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def __inner_run_loop__(command)
|
109
|
+
now_time = Time.new
|
110
|
+
load_db_at(now_time)
|
111
|
+
__respond_to_ipc__(command) if methods.include? command
|
112
|
+
__execute_action__(now_time.strftime("%H:%M"))
|
113
|
+
end
|
114
|
+
|
115
|
+
def __respond_to_ipc__(command)
|
116
|
+
@ipc[RESPONSE_STR] = send(command).to_yaml
|
117
|
+
logger.info("Received #{command} via IPC")
|
118
|
+
Config::flush_log
|
119
|
+
reset_received
|
120
|
+
@ipc.run
|
121
|
+
end
|
122
|
+
|
123
|
+
def __execute_action__(hhmm_now_time)
|
124
|
+
cronolog = @schedule.cronological
|
125
|
+
return unless (cronolog.keys.include? hhmm_now_time)
|
126
|
+
logger.info("Executed #{cronolog[hhmm_now_time].length} action(s) at #{hhmm_now_time}")
|
127
|
+
Config::flush_log
|
128
|
+
@schedule.execute(hhmm_now_time,Config.output)
|
86
129
|
end
|
87
130
|
|
88
131
|
end
|
@@ -17,71 +17,121 @@ require "serialport"
|
|
17
17
|
|
18
18
|
module TriggerSwitchD
|
19
19
|
module ConfigFactory
|
20
|
-
|
20
|
+
|
21
|
+
#Verifies proper path to environment.rb
|
22
|
+
class ProperEnvironmentFile
|
23
|
+
def initialize
|
24
|
+
@paths_to_look = %w{~/.trigger_switch_d/config/ /etc/trigger_switch_d/config/ config/}
|
25
|
+
@paths_to_look[0].gsub!("~",File.expand_path("~"))
|
26
|
+
@env_name = "environment.rb"
|
27
|
+
end
|
28
|
+
|
29
|
+
def path
|
30
|
+
env_path = @paths_to_look.find do |path|
|
31
|
+
File.exist?("#{path}#{@env_name}")
|
32
|
+
end
|
33
|
+
return offer_to_create if env_path == nil
|
34
|
+
"#{env_path}#{@env_name}"
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def offer_to_create
|
39
|
+
return "" if ask == false
|
40
|
+
create
|
41
|
+
"#{@paths_to_look[0]}#{@env_name}"
|
42
|
+
end
|
43
|
+
|
44
|
+
def create
|
45
|
+
home_path = @paths_to_look[0]
|
46
|
+
Kernel.system("mkdir -p #{home_path}")
|
47
|
+
Kernel.system("cp samples/environment.rb #{home_path}#{@env_name}")
|
48
|
+
end
|
49
|
+
|
50
|
+
def ask
|
51
|
+
question = <<-EOS
|
52
|
+
Could't find the environment.rb file. I would like to create one for you at
|
53
|
+
#{@paths_to_look[0]} , do you want me to do that now [Yn]?
|
54
|
+
EOS
|
55
|
+
STDOUT.puts question
|
56
|
+
(STDIN.gets =~ /[nN]/) == nil
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
21
60
|
|
22
61
|
#Creates the Config class from the config/config.rb file
|
23
62
|
#See samples/config.rb for more documentation
|
24
63
|
def self.new(file=StringIO.new)
|
25
64
|
env = file.readlines
|
26
|
-
attributes = env
|
27
|
-
|
28
|
-
end
|
29
|
-
attributes.compact!
|
30
|
-
cls = <<-EOS
|
31
|
-
class Config
|
32
|
-
#{attributes.map {|name| "def self.#{name}=(value); #{append_filenames(name)}; end"}.join("\n")}
|
33
|
-
|
34
|
-
#{attributes.map {|name| "def self.#{name}; @#{name}; end"}.join("\n")}
|
35
|
-
|
36
|
-
def self.default_environment
|
37
|
-
{:lookup => {"tänd" => "activate",
|
38
|
-
"släck" => "deactivate",
|
39
|
-
"soluppgång" => "sunrise",
|
40
|
-
"solnedgång" => "sunset",
|
41
|
-
"klockan" => "at",
|
42
|
-
"vid" => "at"},
|
43
|
-
:at_location => {:north => 0.0, :west => 0.0}}
|
44
|
-
end
|
45
|
-
end
|
46
|
-
EOS
|
47
|
-
::TriggerSwitchD.module_eval(cls)
|
65
|
+
attributes = map_attributes(env)
|
66
|
+
eval_config_code_definition(attributes)
|
48
67
|
setup_environment(env)
|
49
68
|
end
|
50
69
|
|
51
70
|
private
|
71
|
+
def self.map_attributes(env_list)
|
72
|
+
env_list.map do |attribute|
|
73
|
+
attribute.scan(/Config.([\w_]+) =/).flatten[0]
|
74
|
+
end.compact
|
75
|
+
end
|
76
|
+
|
52
77
|
def self.setup_environment(env)
|
53
78
|
env.map do |attribute|
|
54
79
|
::TriggerSwitchD.module_eval(attribute)
|
55
80
|
end
|
56
81
|
def Config.environment
|
57
82
|
result = default_environment
|
58
|
-
|
59
|
-
|
60
|
-
|
83
|
+
File.open(Config.dsl_path,"r") do |file|
|
84
|
+
result[:lookup] = YAML::load(file)
|
85
|
+
end
|
61
86
|
result[:at_location] = Config.geo_position
|
62
87
|
result
|
63
88
|
end
|
64
89
|
end
|
65
90
|
|
66
91
|
def self.set_type(name)
|
67
|
-
types = {"log_object" => "{:spec => StringIO.new, :development => STDOUT,
|
68
|
-
|
92
|
+
types = {"log_object" => "Hash.new(STDOUT).merge({:spec => StringIO.new, :development => STDOUT,
|
93
|
+
:file => Tellstick::ProxyIO.new(File,Config.log_path,\"a+\",Proc.new { })})",
|
69
94
|
"output" => "Hash.new(STDOUT).merge({:spec => StringIO.new, :development => STDOUT,
|
70
95
|
:vcp => Tellstick::ProxyIO.new(SerialPort,Config.vcp_port,4800,Proc.new { |sp| sp.puts(\"~^M~AT S7=45 S0=0 L1 V1 X4 &c1 E1 Q0^M\"); sp.puts(\"S+\"); sp.gets })})"}
|
71
96
|
return "@#{name}=value" unless types.keys.include? name
|
72
|
-
"@#{name}=#{types[name]}[value]"
|
97
|
+
"@#{name}=#{types[name]}[value]"#.gsub("log_path","{log_path")
|
73
98
|
end
|
74
99
|
|
100
|
+
PATH_FILENAMES = {"log_path" => ["tsd.log"], "db_path" => ["devices","scheduled_actions"],
|
101
|
+
"dsl_path" => ["lookup.dsl"], "unix_socket_path" => ["com.adhocskill.trigger_switch_d"]}
|
102
|
+
|
75
103
|
def self.append_filenames(name)
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
result = filenames[name].map do |fname|
|
104
|
+
return set_type(name) unless PATH_FILENAMES.keys.include? name
|
105
|
+
filename = PATH_FILENAMES[name]
|
106
|
+
result = filename.map do |fname|
|
80
107
|
"#value}/#{fname}"
|
81
|
-
end.inspect.gsub("value","{value")
|
82
|
-
result = result.gsub("[","").gsub("]","") if
|
108
|
+
end.inspect.gsub("value}","value.gsub(\"~\",File.expand_path(\"~\"))}").gsub("value","{value")
|
109
|
+
result = result.gsub("[","").gsub("]","") if filename.length == 1
|
83
110
|
"@#{name}=#{result}"
|
84
111
|
end
|
112
|
+
|
113
|
+
def self.eval_config_code_definition(attributes)
|
114
|
+
cls = <<-EOS
|
115
|
+
class Config
|
116
|
+
#{attributes.map {|name| "def self.#{name}=(value); #{append_filenames(name)}; end; ; def self.#{name}; @#{name}; end "}.join("\n")}
|
117
|
+
|
118
|
+
def self.default_environment
|
119
|
+
{:lookup => {"tänd" => "activate",
|
120
|
+
"släck" => "deactivate",
|
121
|
+
"soluppgång" => "sunrise",
|
122
|
+
"solnedgång" => "sunset",
|
123
|
+
"klockan" => "at",
|
124
|
+
"vid" => "at"},
|
125
|
+
:at_location => {:north => 0.0, :west => 0.0}}
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.flush_log
|
129
|
+
log_object.flush
|
130
|
+
end
|
131
|
+
end
|
132
|
+
EOS
|
133
|
+
::TriggerSwitchD.module_eval(cls)
|
134
|
+
end
|
85
135
|
end
|
86
136
|
end
|
87
137
|
|