Class: Device::ParamsDat

Inherits:
Object
  • Object
show all
Includes:
DaFunk::Helper
Defined in:
lib/device/params_dat.rb

Constant Summary collapse

FILE_NAME =
"./main/params.dat"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.appsObject

Returns the value of attribute apps.



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

def apps
  @apps
end

.fileObject

Returns the value of attribute file.



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

def file
  @file
end

.filesObject

Returns the value of attribute files.



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

def files
  @files
end

.validObject

Returns the value of attribute valid.



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

def valid
  @valid
end

Class Method Details

.application_menuObject



211
212
213
214
215
# File 'lib/device/params_dat.rb', line 211

def self.application_menu
  options = Hash.new
  executable_apps.each { |app| options[app.label] = app }
  menu("Application Menu", options, {:number => true})
end

.downloadObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/device/params_dat.rb', line 105

def self.download
  if attach
    parse
    ret = try(3) do |attempt|
      Device::Display.clear
      I18n.pt(:downloading_content, :args => ["PARAMS", 1, 1])
      ret = Device::Transaction::Download.request_param_file(FILE_NAME)
      unless check_download_error(ret)
        sleep(2)
        false
      else
        true
      end
    end
    parse if ret
    ret
  end
end

.executable_appObject



200
201
202
203
204
205
# File 'lib/device/params_dat.rb', line 200

def self.executable_app
  selected = self.executable_apps
  if selected && selected.size == 1
    selected.first
  end
end

.executable_appsObject



207
208
209
# File 'lib/device/params_dat.rb', line 207

def self.executable_apps
  self.apps.select{|app| app.label != "X"}
end

.exists?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/device/params_dat.rb', line 26

def self.exists?
  File.exists?(FILE_NAME)
end

.file_deletable?(path, keep_config_files, keep_files) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
158
159
160
161
162
163
# File 'lib/device/params_dat.rb', line 155

def self.file_deletable?(path, keep_config_files, keep_files)
  keep = false
  if keep_config_files
    keep = [".bmp", ".jpeg", ".jpg", ".png"].find {|ext| path.include?(ext) && path.include?(Device::System.model) }
    keep ||= path.include?(Device::Display::MAIN_BMP)
    keep ||= keep_files.include?(path)
  end
  File.file?(path) && ! keep
end

.format!(keep_config_files = false, keep_files = []) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/device/params_dat.rb', line 141

def self.format!(keep_config_files = false, keep_files = [])
  Device::Application.delete(self.apps)
  DaFunk::FileParameter.delete(self.files)
  File.delete(FILE_NAME) if exists?
  @apps, @files = [], []
  Dir.entries("./shared/").each do |f|
    begin
      path = "./shared/#{f}"
      File.delete(path) if self.file_deletable?(path, keep_config_files, keep_files)
    rescue
    end
  end
end

.get_app(name) ⇒ Object



95
96
97
98
# File 'lib/device/params_dat.rb', line 95

def self.get_app(name)
  @apps.each {|app| return app if app.original == name }
  nil
end

.get_file(name) ⇒ Object



100
101
102
103
# File 'lib/device/params_dat.rb', line 100

def self.get_file(name)
  @files.each {|file_| return file_ if file_.original == name}
  nil
end

.outdated_appsObject



37
38
39
# File 'lib/device/params_dat.rb', line 37

def self.outdated_apps
  self.apps.select{|app| app.outdated? }
end

.outdated_filesObject



41
42
43
# File 'lib/device/params_dat.rb', line 41

def self.outdated_files
  self.files.select{|f| f.outdated? }
end

.parseObject

TODO Scalone: Change after @bmsatierf change the format For each apps on apps_list We’ll have: Today: <label>,<arquivo>,<pages>,<crc>; After: <label>,<arquivo>,<type>,<crc>; Today: “1 - App,pc2_app.posxml,1,E0A0;” After: “1 - App,pc2_app.posxml,posxml,E0A0;” After: “1 - App,pc2_app.zip,ruby,E0A0;”



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/device/params_dat.rb', line 82

def self.parse
  return unless self.setup
  parse_apps
  parse_files

  if (@apps.size >= 1)
    self.valid = true
  else
    self.valid = false
  end
  self.valid
end

.parse_appsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/device/params_dat.rb', line 45

def self.parse_apps
  new_apps = []
  self.file["apps_list"].to_s.gsub("\"", "").split(";").each do |app|
    label, name, type, crc = app.split(",")
    if application = get_app(name)
      application.crc = crc
    else
      application = Device::Application.new(label, name, type, crc)
    end
    new_apps << application
  end
  Device::Application.delete(@apps - new_apps)
  @apps = new_apps
end

.parse_filesObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/device/params_dat.rb', line 60

def self.parse_files
  new_files = []
  self.file["files_list"].to_s.gsub("\"", "").split(";").each do |f|
    name, crc = f.split(",")
    if file_ = get_file(name)
      file_.crc = crc
    else
      file_ = DaFunk::FileParameter.new(name, crc)
    end
    new_files << file_
  end
  Device::Application.delete(@files - new_files)
  @files = new_files
end

.ready?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/device/params_dat.rb', line 30

def self.ready?
  return unless exists?
  apps.each {|app| return false if app.outdated? } if apps.size > 0
  files.each {|f| return false if f.outdated? } if files.size > 0
  true
end

.setupObject



22
23
24
# File 'lib/device/params_dat.rb', line 22

def self.setup
  @file = FileDb.new(FILE_NAME)
end

.update_app(application, index = 1, all = 1, force = false) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/device/params_dat.rb', line 165

def self.update_app(application, index = 1, all = 1, force = false)
  if attach && application
    try(3) do |attempt|
      Device::Display.clear
      I18n.pt(:downloading_content, :args => [I18n.t(:apps), index, all])
      ret = check_download_error(application.download(force))
      sleep(1)
      ret
    end
  end
end

.update_apps(force = false) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/device/params_dat.rb', line 124

def self.update_apps(force = false)
  self.download if force || ! self.valid
  if self.valid
    apps_to_update = self.outdated_apps
    size_apps = apps_to_update.size
    apps_to_update.each_with_index do |app, index|
      self.update_app(app, index+1, size_apps)
    end

    files_to_update = self.outdated_files
    size_files = files_to_update.size
    files_to_update.each_with_index do |file_, index|
      self.update_file(file_, index+1, size_files)
    end
  end
end

.update_file(file_parameter, index = 1, all = 1, force = false) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/device/params_dat.rb', line 177

def self.update_file(file_parameter, index = 1, all = 1, force = false)
  if attach && file_parameter
    try(3) do |attempt|
      Device::Display.clear
      I18n.pt(:downloading_content, :args => [I18n.t(:files), index, all])
      ret = check_download_error(file_parameter.download(force))
      file_parameter.unzip if ret
      sleep(1)
      ret
    end
  end
end