Module: ImportCode

Defined in:
lib/import_code.rb

Constant Summary collapse

DEFAULT_UNCLUDE =

不是按键编码的KEY

["FileFormat","Remotecontroldescripe","Device","Brand","Model","Button'scounts"]
DEFAULT_NOIMPORT =

不进行导入的对应KEY

["FileFormat","Remotecontroldescripe","Button'scounts"]
METHOD_NAME =
"method_name"
DEFAULT_CODENAME =

用于将按键编码中的KEY的中文转为英文表示

{"机顶盒电源"=>"power","静音"=>"voice_quiet","菜单"=>"menu",
"上"=>"up","下"=>"down","左"=>"left","右"=>"right",
"音量+"=>'voice_up',"音量-"=>"voice_down",
"频道+"=>"channel_up","频道-"=>"channel_down","上一页"=>"previous_page",
"下一页"=>"next_page","返回"=>"return","快进"=>"fast_forward",
"快退"=>"rew","播放/暂停"=>"play","停止播放"=>"stop","回看"=>"look_back",
"删除"=>"delete","TV/AV"=>"tv_av","-/--"=>"switch","确认"=>"sure","确定"=>"sure"}
DEFAULT_VALUE =

需要将属性值进行转换的

{ "电视"=>"tv","机顶盒"=>"stb"}
BRAND =
"Brand"
MODEL =
"model"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_path_arrayObject

To change this template use File | Settings | File Templates.



5
6
7
# File 'lib/import_code.rb', line 5

def file_path_array
  @file_path_array
end

#protocol_hashObject

Returns the value of attribute protocol_hash.



7
8
9
# File 'lib/import_code.rb', line 7

def protocol_hash
  @protocol_hash
end

#remote_code_arrayObject

Returns the value of attribute remote_code_array.



6
7
8
# File 'lib/import_code.rb', line 6

def remote_code_array
  @remote_code_array
end

#statisticsObject

Returns the value of attribute statistics.



8
9
10
# File 'lib/import_code.rb', line 8

def statistics
  @statistics
end

Class Method Details

.disposeProtocolCode(codes, brand) ⇒ Object

对按键的编码进行了处理(按协议码进行的处理)



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/import_code.rb', line 201

def self.disposeProtocolCode(codes,brand)
  protocol = ""
  begin
  codelist = codes.split(" ")
  protocol_name = codelist[0]
  statisticsProtocol(@protocol_hash[protocol_name])
  protocol = @protocol_hash[protocol_name] if @protocol_hash[protocol_name]
  code = ""
  1.upto(codelist.size-1).each do |index|
    code =code + codelist[index]
    code =code + ":" unless index == codelist.size-1
  end
  rescue => err
    raise ArgumentError, get_error_msg(err,{METHOD_NAME => "disposeProtocolCode"})
  end
  {protocol_name:protocol,code:code}
end

.get_error_msg(error, options = {}) ⇒ Object

获取错误信息用于抛出错误



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/import_code.rb', line 43

def self.get_error_msg(error,options={})
  ret = ""
  ret = ret + "ClassName: import_remotecontrol gem     "
  if options["method_name"]
    ret = ret + "MethodName: #{options["method_name"]}     "
  end
  if error
    ret = ret + "Error Msg: #{error.to_s}"
  end
  ret
end

.getFileNames(dir_path) ⇒ Object

遍历文件夹获取全部的文件名



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
# File 'lib/import_code.rb', line 56

def self.getFileNames(dir_path)
  @file_path_array = []
  begin
    Find.find(dir_path).each do |path|
      if FileTest.directory?(path)
        Dir.foreach(path) do |file_path|
          if(file_path != "."&&file_path!= "..")
            if FileTest.directory?("#{path}/#{file_path}")
              self.getFileNames("#{path}/#{file_path}")
            else
              if(file_path.eql?("protocol"))
                getProtocolList("#{path}/#{file_path}")
              else
                @file_path_array << {"file_path"=>"#{path}/#{file_path}","file_name"=>"#{file_path}"}
              end
            end
          end
        end
      end
    end
  rescue => err
    raise ArgumentError, get_error_msg(err,{METHOD_NAME => "getFileNames"})
  end
  p "File_path: #{@file_path_array}"
  @file_path_array
end

.getHashRemoteCode(file_path) ⇒ Object

从文件中读入编码并转换为hash的格式来处理



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
165
166
167
168
169
170
171
172
# File 'lib/import_code.rb', line 123

def self.getHashRemoteCode(file_path)
  begin
  ret = {}
  brand = ""
  remote_code_line = File.read(file_path)
  remote_code_line.split("\r\n").each do |code|
    i = code.split("=")
    if i.count>=2
      i[0] = "" unless i[0]
      key = i[0].gsub(" ","")
      if isBrand?(key)
        brand = i[1]
      end
      if DEFAULT_VALUE.has_key?(i[1])
        i[1] = DEFAULT_VALUE[i[1]]
      end
      if i
        if i[0]
          if(i[0].split(",").count > 1)
            key = i[0].split(",")[1]
            if DEFAULT_CODENAME.has_key?(key)
              key = DEFAULT_CODENAME[key]
            end
            unless isProcolCode?(key)
              ret.merge!({key => i[1]})  unless DEFAULT_NOIMPORT.include?(key)
            else
              #codehash = disposeProtocolCode(i[1],brand)
              ret.merge!({key=>i[1]}) unless DEFAULT_NOIMPORT.include?(key)
            end
          else
            ret.merge!({key=>i[1]}) unless DEFAULT_NOIMPORT.include?(key)
          end
        end
      end
      _key =""
      _value = -1
      @statistics.each do |key,value|
        if(_value < value)
          _key = key
          _value = value
        end
      end
      ret.merge!({"statistics"=>_key})
    end
  end
  rescue => err
    raise ArgumentError, get_error_msg(err,{METHOD_NAME => "getHashRemoteCode"})
  end
  ret
end

.getProtocolList(file_path) ⇒ Object

获取协议码对应协议(忽视品牌直接按协议码进行的处理)



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/import_code.rb', line 176

def self.getProtocolList(file_path)
  @protocol_hash = {}
  begin
  protocol_lines = File.read(file_path)
  protocol_lines.split("\n").each do |protocol_line|
    protocol = protocol_line.split(":")
    if protocol.count ==2
      protocol<<" "
    end
    unless @protocol_hash.has_key?(protocol[1])
      @protocol_hash.merge!({protocol[1]=>protocol[2]}) if protocol[2]!=nil&&protocol[2]!=""
    else
      unless @protocol_hash[protocol[1]]==nil||@protocol_hash[protocol[1]]==""
        @protocol_hash[protocol[1]]=protocol[2] if protocol[2]!=""&&protocol[2]!=nil
      end
    end
  end
  rescue => err
    raise ArgumentError, get_error_msg(err,{METHOD_NAME => "getProtocolList"})
  end
  p @protocol_hash
  @protocol_hash
end

.getRemoteCodeArray(dir_path) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/import_code.rb', line 256

def self.getRemoteCodeArray(dir_path)
  @remote_code_array = []
  @statistics = {}
  if FileTest.directory?(dir_path)
    filenames =  getFileNames(dir_path)
    filenames.each do |file_msg|
      p "File_Path : #{file_msg["file_path"]}"
      @statistics = {}
      @remote_code_array<<importRemoteCode(file_msg)
    end
  end
  @remote_code_array
end

.importRemoteCode(file_msg) ⇒ Object

导入遥控编码



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/import_code.rb', line 234

def self.importRemoteCode(file_msg)
  remote_code_hash = {}
  begin
    if file_msg
      file_path = nil
      file_path = file_msg["file_path"]
      file_name = nil
      file_name = file_msg["file_name"]
      if file_path
        remote_code_hash.merge!(getHashRemoteCode(file_path))
      else

      end
    end
  rescue => err
    raise ArgumentError, get_error_msg(err,{METHOD_NAME => "importRemoteCode"})
  end
  remote_code_hash
end

.isBrand?(key) ⇒ Boolean

判断是不是品牌对应的KEY



118
119
120
# File 'lib/import_code.rb', line 118

def self.isBrand?(key)
  key.eql?("Brand")
end

.isProcolCode?(key) ⇒ Boolean

判断是否是按键编码对应的KEY



113
114
115
# File 'lib/import_code.rb', line 113

def self.isProcolCode?(key)
  !DEFAULT_UNCLUDE.include?(key)
end

.statisticsProtocol(protocol_name) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/import_code.rb', line 220

def self.statisticsProtocol(protocol_name)
  begin
  unless @statistics.has_key?(protocol_name)
    @statistics.merge!({protocol_name=>1})
  else
    @statistics[protocol_name]=@statistics[protocol_name]+1
  end
  rescue => err
    raise ArgumentError, get_error_msg(err,{METHOD_NAME => "statisticsProtocol"})
  end
  @statistics
end

Instance Method Details

#initializeObject



10
11
12
13
14
15
# File 'lib/import_code.rb', line 10

def initialize
  self.file_path_array=[]
  self.remote_code_array=[]
  self.protocol_hash={}
  self.statistics = {}
end