-
Notifications
You must be signed in to change notification settings - Fork 2
/
comm.rb
165 lines (144 loc) · 4.53 KB
/
comm.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/env/bin/ruby
# encoding: utf-8
require File.dirname(__FILE__) + '/config.rb'
class MacSpeaker
# text is word to be said
# paraX is useless, only to make speaker() can accept two parameters
def speak text, paraX
Thread.start do
`say #{text} -v kyoko`
end
end
end
module COMM
configMgr = ConfigManager.new
if not configMgr.user_conf_exist?
configMgr.make_user_conf
end
configMgr.format_user_conf
default_setting = configMgr.get_default_json
user_setting = configMgr.get_user_json
## User defined constants
MAX_MEM_TIMES = 8
MAIN_WIN_WIDTH = configMgr.get_settings "MAIN_WIN_WIDTH"
MAIN_WIN_HEIGHT = configMgr.get_settings "MAIN_WIN_HEIGHT"
WB_WIN_WIDTH = configMgr.get_settings "WB_WIN_WIDTH"
WB_WIN_HEIGHT = configMgr.get_settings "WB_WIN_HEIGHT"
AUTO_CHECK_UPDATE = configMgr.get_settings "AUTO_CHECK_UPDATE"
DEBUG_MODE = configMgr.get_settings "DEBUG_MODE"
MMRZ_BUILD_WINDOWS_EXE = configMgr.get_settings "MMRZ_BUILD_WINDOWS_EXE"
USE_TTS_ENGINE = configMgr.get_settings "USE_TTS_ENGINE"
AUTO_SPEAK = configMgr.get_settings "AUTO_SPEAK"
REVERSE_MODE = configMgr.get_settings "REVERSE_MODE"
REVERSE_MODE_TIMES = configMgr.get_settings "REVERSE_MODE_TIMES"
SMART_IMPORT = configMgr.get_settings "SMART_IMPORT"
RANDOM_PICK_UP = configMgr.get_settings "RANDOM_PICK_UP"
IMPORT_QUANTITY = configMgr.get_settings "IMPORT_QUANTITY"
## Constants
PLATFORM_WINDOWS = RbConfig::CONFIG['target_os'] == "mingw32"
PLATFORM_MAC = RUBY_PLATFORM.include? "darwin"
PLATFORM_LINUX = RUBY_PLATFORM.include? "linux"
SERVERADDR = true ? "http://115.29.192.240:2603" : "http://127.0.0.1:2603"
module_function # public functions begin
def cal_remind_time memTimes, type
curTime = Time.now
case memTimes
when 0
remindTime = curTime + (60 * 5) # 5 minutes
remindTime = curTime if COMM::DEBUG_MODE # 0 minutes, debug mode
when 1
remindTime = curTime + (60 * 30) # 30 minutes
when 2
remindTime = curTime + (60 * 60 * 12) # 12 hours
when 3
remindTime = curTime + (60 * 60 * 24) # 1 day
when 4
remindTime = curTime + (60 * 60 * 24 * 2) # 2 days
when 5
remindTime = curTime + (60 * 60 * 24 * 4) # 4 days
when 6
remindTime = curTime + (60 * 60 * 24 * 7) # 7 days
when 7
remindTime = curTime + (60 * 60 * 24 * 15) # 15 days
else
remindTime = curTime
end
case type
when "int"
return remindTime.to_i
when "str"
return remindTime.to_s[0..-7]
end
end
def split_remindTime remindTime, adjust = false
if remindTime > 0
remindTime += 59 if adjust
day = remindTime / (60 * 60 * 24)
hour = remindTime % (60 * 60 * 24) / (60 * 60)
min = remindTime % (60 * 60 * 24) % (60 * 60) / 60
sec = remindTime % (60 * 60 * 24) % (60 * 60) % 60
else
day = hour = min = sec = 0
end
return day, hour, min, sec
end
def get_shortest_remind
dbMgr = MmrzDBManager.new
rows = dbMgr.readDB
dbMgr.closeDB
return "No words in schedule" if rows == []
rows.sort! { |r1, r2| r1[3] <=> r2[3] } # remindTime from short to long
word = rows[0][0]
pronounce = rows[0][1]
memTimes = rows[0][2]
remindTime = rows[0][3]
remindTimeStr = rows[0][4]
wordID = rows[0][5]
remindTime -= Time.now.to_i
day, hour, min, sec = split_remindTime remindTime, true
remindTimeStr = format("%sd-%sh-%sm", day, hour, min)
format("Next after %s", remindTimeStr)
end
def get_OS
if RUBY_PLATFORM.include? "darwin"
return "mac"
elsif RUBY_PLATFORM.include? "linux"
return "linux"
else
return "win"
end
end
def find_misaki?
if COMM::PLATFORM_WINDOWS
speaker = WIN32OLE.new('Sapi.SpVoice')
speaker.GetVoices().each do |engine|
if engine.GetDescription().include? "Misaki"
begin
speaker.speak "", 1
return true
rescue
p "audio device not available"
end
end
end
end
return false
end
def make_speaker
if COMM::PLATFORM_WINDOWS
speaker = WIN32OLE.new('Sapi.SpVoice')
speaker.GetVoices().each do |engine|
if engine.GetDescription().include? "Misaki"
speaker.Voice = engine
speaker.volume = 100 # range 0(low) - 100(loud)
speaker.rate = -3 # range -10(slow) - 10(fast)
return speaker
end
end
end
if COMM::PLATFORM_MAC
speaker = MacSpeaker.new
return speaker
end
end
end # end of module COMM