autobudilnik/autobudilnik.rb

168 lines
3.4 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ruby autobudilnik.rb [-a <audiofilepath>] [-t <time>] [-c <configfilepath>]
#require 'io/console'
require 'reline'
za = [false,false,false]
nf = ''
da = ''
audiofilepath = "audio.opus"
waittimestr = "2h"
configfilepath = "config.txt"
i=0;while i<ARGV.size
if ARGV[i] == "-a"
if za[0] == false
i += 1
za[0] = true
audiofilepath = ARGV[i]
else
#da.push ARGV[i] + (i += 1;" " + ARGV[i])
da += " -a"
#raise "duplicate arguments"
end
elsif ARGV[i] == "-t"
if za[1] == false
i += 1
za[1] = true
waittimestr = ARGV[i]
else
#da.push ARGV[i] + (i += 1;" " + ARGV[i])
da += " -t"
#raise "duplicate arguments"
end
elsif ARGV[i] == "-c"
if za[2] == false
i += 1
za[2] = true
configfilepath = ARGV[i]
else
#da.push ARGV[i] + (i += 1;" " + ARGV[i])
da += " -c"
#raise "duplicate arguments"
end
else
#nf.push ARGV[i]
nf += " " + ARGV[i]
#ARGV[i..ARGV.size].join(' ')
end
i+=1
end
puts "not found:" + nf if nf != ''
if da.size != 0
raise 'duplicate arguments:' + da
end
if za[0] == false || za[1] == false
configfilepath = File.absolute_path(configfilepath)
if File.file? configfilepath
puts 'reading config'
File.readlines(configfilepath).each { |l|
lspl = l.split(' ')
if za[0] == false && ('audiofilepath'.start_with? lspl[0])
audiofilepath = l.delete_prefix(lspl[0] + ' ')
#split('"')[0].split('"')[0]
elsif za[1] == false && ('waittime'.start_with? lspl[0])
#waittime = lspl[1].to_i
waittimestr = lspl[1]
end
}
else
if za[2] == true
raise configfilepath + ' doesnt exist'
end
end
end
if audiofilepath[0] == '"'
audiofilepath = audiofilepath.split.pop[0]
audiofilepath = audiofilepath.join
end
#audiofilepath.delete_prefix!('"')
audiofilepath = File.absolute_path(audiofilepath)
raise audiofilepath + ' doesnt exist' unless File.file? audiofilepath.delete_suffix('"')
if audiofilepath[-1] != '"'
audiofilepath += '"'
end
audiofilepath = '"' + audiofilepath
puts 'audiofilepath:' + audiofilepath
suffix = waittimestr.delete('0-9')
case suffix
when ''
waittime = waittimestr.to_i
waittimestr += 's'
when 'd'
waittime = waittimestr.to_i * 86400
when 'm'
waittime = waittimestr.to_i * 60
when 'h'
waittime = waittimestr.to_i * 3600
when 's'
waittime = waittimestr.to_i
else
raise "'#{suffix}' suffix is not found"
end
if waittime > 18
puts 'waittime:' + waittimestr
waittime = waittime - 18 # 30 x 0.6 = 18
else
puts "19 seconds is the minimum\nwaittime:19s"
waittime = 1
end
nexttime = Time.new + waittime
=begin
STDIN.echo = false
STDIN.raw!
getchr = false
Thread.new {while true do sleep(0.1);puts 't';STDIN.getbyte; getchr = true end}
puts "rtstrt"
while true
sleep(1.4)
if getchr == true
nexttime = Time.new + waittime;puts nexttime
getchr = false
end
if nexttime < Time.new
puts "audioplay"
system(audiofilepath)
end
#thr.run
end
=end
puts 'autobudilnik running'
a=Reline::Windows::Win32API.new('user32','GetAsyncKeyState','I','I')
a2=Reline::Windows::Win32API.new('user32','keybd_event','I','I')
b = []
c = []
while true
255.times {|r|
b[r] = a.call(r)
}
if c != b
nexttime = Time.now + waittime
end
sleep(3.4)
255.times {|r|
c[r] = a.call(r)
}
if c != b
nexttime = Time.now + waittime
end
if nexttime < Time.now
30.times {
sleep(0.6) #НУЖНО УБАВИТЬ С ВЭЙТТАЙМА
a2.call(175)
}
puts "starts audio"
system('zadacha1.bat ' + audiofilepath)
end
sleep(3.4)
end