bechad 1.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/bin/bechad +216 -0
  3. metadata +45 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YzU1NGQxZTdlMTJkZDEzNmJkOGQxODE1NTA3NjU2NjczOTA2ZTM3Yw==
5
+ data.tar.gz: !binary |-
6
+ MjU4YWZlZGQ4ZGYzZDdiZGRkYmM2YjkyZmY2Y2I4ZmQzODg3ZjZmYg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NmZhZWQ2NGVlNTdjYzRhMGIyNTk5ODc0M2I1MTFlYjkxNjY1NDQ1NzIxOTJk
10
+ MzE5ZjBhZjA5M2M4ZWNhYTczOGJhYWNhN2NhNDUxOTgyZjgyNmRkNDVlMjNl
11
+ NTI4NWI5OWEzNDc4OTczMzUzZDU3NjdjOWFhNTNjMzU3NDEyOGE=
12
+ data.tar.gz: !binary |-
13
+ MjA2YjlkM2Q1NWY0ODJlNTk0YmIyMDA4M2QxNmE0NWYwZTYyYzA4ZGEzMzMx
14
+ YjQ5NmUzMGNhY2U3Mzg4N2JmMmJmMjkzMGJjNGY4MWYwNzk5ZWE0MTcwOWZm
15
+ NjgxNTJhZmQyMDhmMTBkMThhZDA5ZjMxMzFlNzZjYzU3ZWU1OTU=
@@ -0,0 +1,216 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+ require 'optparse'
5
+ require 'optparse/time'
6
+ require 'ostruct'
7
+ require 'socket'
8
+ require 'yaml'
9
+
10
+
11
+ class Pathname
12
+ def extname= (extension)
13
+ @path = @path[0..@path.length-extname.length-1]
14
+ if extension.length > 0
15
+ @path << '.' if extension[0] != '.'
16
+ @path << extension.to_s
17
+ end
18
+ end
19
+
20
+ def chext (extension)
21
+ res = self.clone
22
+ res.extname = extension
23
+
24
+ res
25
+ end
26
+ end
27
+
28
+
29
+ APP_VERSION = 'BECHA-server v1.0.0'
30
+ ARGV_COUNT = 1
31
+
32
+
33
+ def parse_options(args)
34
+ options = OpenStruct.new
35
+
36
+ opt_parser = OptionParser.new do |opts|
37
+ opts.banner = "Usage: becha-server [options] cmd [args]\n\n" +
38
+ "Avalaible commands:\n" +
39
+ " power-on\t\t- Power ON the tape recorder\n" +
40
+ " power-off\t\t- Power OFF the tape recorder\n" +
41
+ " record track-path\t\t- Record track into the tape recorder"
42
+
43
+ options.verbose = false
44
+ options.playlist_path = Pathname.new('./becha.playlist.conf').expand_path
45
+ options.log_path = Pathname.new('./becha.log').expand_path
46
+ options.errlog_path = Pathname.new('./becha.error.log').expand_path
47
+ options.port = 2014
48
+
49
+ opts.separator ""
50
+ opts.separator "Common options:"
51
+
52
+ opts.on("-l", "--playlist path", String, "Define playlist information") do |path|
53
+ options.playlist_path = Pathname.new path
54
+ end
55
+
56
+ opts.on("-p", "--port num", Integer, "Define UDP port number") do |port|
57
+ options.port = port
58
+ end
59
+
60
+ opts.on("-e", "--log path", String, "Define log file") do |path|
61
+ options.log_path = Pathname.new path
62
+ end
63
+
64
+ opts.on("-E", "--errlog path", String, "Define log file for errors") do |path|
65
+ options.errlog_path = Pathname.new path
66
+ end
67
+
68
+ opts.on("--verbose", "Print extra information") do
69
+ options.verbose = true
70
+ end
71
+
72
+ opts.on_tail("-h", "--help", "Show this message") do
73
+ puts opts
74
+ exit
75
+ end
76
+
77
+ opts.on_tail("--version", "Show version") do
78
+ puts APP_VERSION
79
+ exit
80
+ end
81
+ end
82
+
83
+ opt_parser.parse!(args)
84
+ [options, opt_parser]
85
+ end
86
+
87
+ res = parse_options(ARGV)
88
+ options = res[0]
89
+ opt_parser = res[1]
90
+
91
+ if ARGV.count < ARGV_COUNT
92
+ p opt_parser
93
+
94
+ exit 1
95
+ end
96
+
97
+ argv = ARGV.reverse
98
+
99
+ cmd = argv.pop
100
+
101
+
102
+ case cmd
103
+ when 'power-on'
104
+ playlist_path = Pathname.new(options.playlist_path.expand_path)
105
+ playlist = YAML::load(IO.read playlist_path.expand_path)
106
+
107
+ raise 'Config file is corrupted' unless playlist.class == Hash
108
+
109
+ puts "Starting daemon..."
110
+
111
+ Process.daemon
112
+
113
+ STDOUT.reopen options.log_path.expand_path, 'a'
114
+ STDERR.reopen options.errlog_path.expand_path, 'a'
115
+
116
+ $stdout = STDOUT
117
+ $stderr = STDERR
118
+
119
+ STDOUT.sync = true
120
+ STDERR.sync = true
121
+
122
+ sock = UDPSocket.new
123
+ sock.bind '', options.port
124
+
125
+ puts "[#{Time.now}] Server started!"
126
+
127
+ loop do
128
+ packet = sock.recvfrom 32
129
+
130
+ if packet[0] == "quit"
131
+ puts "[#{Time.now}] Exiting..."
132
+ Process.kill "KILL", $mplayer_pid unless $mplayer_pid.nil?
133
+ exit 0
134
+ end
135
+
136
+ puts "[#{Time.now}] Received data: #{packet[0].to_s}"
137
+
138
+ if playlist.has_key? packet[0].to_s
139
+ puts "[#{Time.now}] Start playing: #{playlist[packet[0].to_s]}"
140
+
141
+ begin
142
+ Process.kill "KILL", $mplayer_pid unless $mplayer_pid.nil?
143
+ $mplayer_pid = fork do
144
+ `mplayer "#{playlist[packet[0].to_s]}"`
145
+ end
146
+ rescue => error
147
+ puts 'Error playing'
148
+ p error
149
+ p "#{playlist[packet[0].to_s]}"
150
+ $mplayer_pid = nil
151
+ end
152
+
153
+ else
154
+ puts "[#{Time.now}] Item not found"
155
+
156
+ begin
157
+ Process.kill "KILL", $mplayer_pid unless $mplayer_pid.nil?
158
+ $mplayer_pid = nil
159
+ rescue
160
+ $mplayer_pid = nil
161
+ end
162
+ end
163
+ end
164
+ loop { }
165
+ when 'power-off'
166
+ sock = UDPSocket.new
167
+ sock.send 'quit', 0, 'localhost', options.port
168
+ when 'record'
169
+ playlist_path = Pathname.new options.playlist_path.expand_path
170
+ begin
171
+ playlist = YAML::load IO.read playlist_path.expand_path
172
+ rescue
173
+ playlist = {}
174
+ end
175
+
176
+ raise 'Config file is corrupted' unless playlist.class == Hash
177
+
178
+ track_path = argv.pop
179
+
180
+ p opt_parser if track_path.nil?
181
+
182
+ track_path = Pathname.new(track_path).expand_path
183
+
184
+ sock = UDPSocket.new
185
+ sock.bind '', 2000
186
+
187
+ loop do
188
+ packet = sock.recvfrom 24
189
+
190
+ if packet[0].length == 0
191
+ puts "Received error packet, retrying..."
192
+ else
193
+ playlist[packet[0]] = track_path
194
+ IO.write playlist_path.expand_path, YAML::dump(playlist)
195
+
196
+ if playlist == YAML::load(IO.read playlist_path.expand_path)
197
+ puts "Recording successful: #{packet[0]} -> #{track_path}"
198
+ break
199
+ else
200
+ puts "Recording error!"
201
+ p playlist
202
+ p '---'
203
+ p YAML::load(IO.read playlist_path.expand_path)
204
+ end
205
+ end
206
+ end
207
+ else
208
+ p 'Undefined command'
209
+ p opt_parser
210
+ exit 1
211
+ end
212
+
213
+ exit 0
214
+
215
+ # vim: sw=2 sts=2 ts=8:
216
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bechad
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Artem Levenkov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: BECHA RFID audio server
14
+ email: artem@amperka.ru
15
+ executables:
16
+ - bechad
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/bechad
21
+ homepage: http://github.com/amperka-projects/becha-rfid
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.0.3
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: bechad
45
+ test_files: []