-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIREmitter.rb
119 lines (107 loc) · 3.13 KB
/
IREmitter.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Copyright (C) 2014 Wesleyan University
#
# This file is part of cmdr-devices.
#
# cmdr-devices is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cmdr-devices is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cmdr-devices. If not, see <http://www.gnu.org/licenses/>.
#---
#{
# "name": "IREmitter",
# "depends_on": "Device",
# "description": "Controls any IR device with a LIRC configuration",
# "author": "Micah Wylde",
# "email": "[email protected]",
# "type": "IR"
#}
#---
require 'socket'
#TODO: Rewrite using non-blocking socket library
class IREmitter < Cmdr::Device
configure do
remote1 :type => :string
remote2 :type => :string
remote3 :type => :string
remote4 :type => :string
end
command :pulse_command, :action => proc {|remote, button|
remote_name = @remotes[remote.to_i] rescue nil
next "No remote set for #{remote}" unless remote_name
_set_cmd = "set_transmitters #{remote.to_i}"
_command = "send_once #{remote_name} #{button}"
@_commands[_command] = nil
begin
@socket.write(_set_cmd + "\n")
@socket.write(_command + "\n")
rescue
begin
@socket = UNIXSocket.open(@port)
@socket.write_nonblock(_command + "\n")
rescue
next "Failed to communicate with IR emitter: #{$!}"
end
end
20.times {|t|
break if @_commands[_command]
sleep(0.1)
}
next @_commands[_command] if @_commands[_command]
next "Failed to communicate with IR emitter"
}
def initialize(name, options)
Thread.abort_on_exception = true
options = options.symbolize_keys
DaemonKit.logger.info "Initializing IR Emitter #{options[:name]} with remote #{options[:remote]}"
super(name, options)
@_commands = {}
@remotes = [options[:remote1],
options[:remote2],
options[:remote3],
options[:remote4]]
# @port = options[:port]
@port = "/var/run/lirc/lircd" unless @port
begin
@socket = UNIXSocket.open(@port)
rescue
throw "Failed to create socket: #{$!}"
end
end
def run
read()
super
end
def read
Thread.start do
buffer = []
line = ""
while true do
if @socket && [email protected]?
byte = @socket.recvfrom(1)[0]
unless byte == "\n"
line += byte
next
end
DaemonKit.logger.debug "READING: #{line}"
buffer = [] if line.strip == "BEGIN"
buffer.push(line)
if line.strip == "END"
DaemonKit.logger.debug "#{buffer[1]}: #{buffer[2]}"
@_commands[buffer[1]] = buffer[2]
end
line = ""
else
sleep(0.1)
end
end
end
end
end