Class: ConfigController

Inherits:
Shoes::Main
  • Object
show all
Includes:
ConfigHelper
Defined in:
lib/controllers/config_controller.rb

Overview

for some reason, when a scrollable slot has native controls, the native controls render even when they should be scrolled out of view.

Instance Method Summary collapse

Methods included from ConfigHelper

#upload

Instance Method Details

#appearanceObject


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
# File 'lib/controllers/config_controller.rb', line 35

def appearance
  config_setup
  if RUBY_PLATFORM =~ /linux/
    width,height = `xrandr | grep '*'`.split[0].split('x')
  else
    width,height = 1024,768
  end
  @header.clear { title "Appearance" }

  @center.clear do
    flow(:height => @center.height-50, :width => 1.0) do
      container
      flow(:height => @center.height-150, :scroll => true) do
        stack(:width => 0.4) do
          stack(:margin => 10) do
            inscription 'title (e.g. RockySprints):'
            edit_line(@prefs['title']) do |edit|
              @prefs['title'] = edit.text
            end
          end

          stack(:margin => 10, :padding => 0) do
            inscription "usable window height (e.g. #{[(height.to_i - 100),550].max}):"
            edit_line(@prefs['usable_window_height']) do |edit|
              @prefs['usable_window_height'] = edit.text
            end
            inscription(link("(what's this?)",:click => "http://wiki.opensprints.org/index.php?title=Usable_Screen_Height"))
          end

          stack(:margin => 10, :padding => 0) do
            inscription "Race Interface"
            list_box(:items => ['clock', 'progress_bars'], :choose => @prefs['track']) do |changed| 
              @prefs['track'] = changed.text
            end
          end
        end
        stack(:width => 0.6) do
          stack(:margin => 10) do

            inscription "window height (e.g. #{height.to_i - 100}):"
            edit_line(@prefs['window_height']) do |edit|
              @prefs['window_height'] = edit.text
            end
            inscription "window width (e.g. #{width.to_i - 50}):"
            edit_line(@prefs['window_width']) do |edit|
              @prefs['window_width'] = edit.text
            end
          end

          stack(:margin => 10) do
            inscription "audience window height (e.g. #{height.to_i - 100}):"
            edit_line(@prefs['audience_window_height']) do |edit|
              @prefs['audience_window_height'] = edit.text
            end
            inscription "audience window width (e.g. #{width.to_i - 50}):"
            edit_line(@prefs['audience_window_width']) do |edit|
              @prefs['audience_window_width'] = edit.text
            end
          end
        end
      end
      stack do
        save_button
      end
    end
  end
end

#bikesObject


218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/controllers/config_controller.rb', line 218

def bikes
  config_setup
  @header.clear { title "Bike Setup" }
  @center.clear do
    stack(:height => @center.height-50, :width => 0.8) do
      container
      stack(:height => @center.height-150, :scroll => true) do
        stack(:margin => 10) do
          inscription 'Bikes:'
          @prefs['bikes']||=[]
          @r = 4.times do |i|
            flow do
              inscription "Bike #{i+1} Color:"
              color_edit = edit_line(@prefs['bikes'][i]) do |edit|
                @prefs['bikes'][i] = edit.text
              end
              button "pick color" do
                color_edit.text = ask_color('pick...')
                @prefs['bikes'][i] = color_edit.text
              end
              box = check { |change|
                if change.checked?
                  color_edit.state = nil
                else
                  color_edit.state = "disabled"
                  @prefs['bikes'][i] = nil
                end
              }
              if @prefs['bikes'][i] && color_edit.text != ""
                box.checked = true
                color_edit.state = nil
              else
                color_edit.state = "disabled"
              end
              inscription "active?"
            end
          end
        end
      end
      stack do
        save_button
      end

    end
  end
end

#config_setupObject


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/controllers/config_controller.rb', line 15

def config_setup
  layout(:menu)
  @left.clear {
    left_button("Appearance") { visit '/configuration/appearance' }
    left_button("Skin") { visit '/configuration/skin' }
    left_button("Hardware") { visit '/configuration/hardware' }
    left_button("Bikes") { visit '/configuration/bikes' }
    left_button("Locale") { visit '/configuration/locale' }
    left_button("Data") { visit '/configuration/data_file' }
    if(PLATFORM =~ /linux/)
      left_button("Upgrade") { visit '/configuration/upgrade' }
    end
  }
  if File.exists?(File.join(LIB_DIR,'opensprints_conf.yml'))
    @prefs = YAML::load_file(File.join(LIB_DIR,'opensprints_conf.yml'))
  else
    @prefs = YAML::load_file('conf-sample.yml')
  end
end

#data_fileObject


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/controllers/config_controller.rb', line 159

def data_file
  config_setup
  @header.clear { title "Data" }
  @center.clear do
    container
    stack do
      button("Delete all data", :width => 200) do
        DataMapper.auto_migrate! if confirm("Are you sure? There's no going back.")
      end

      button("Export to MySQL", :width => 200) do
        location = ask_save_file
        if location != nil
            File.copy(File.join(LIB_DIR,'opensprints_data.db'),"/tmp/tmp-opensprints.db")
            system("sqlite3 /tmp/tmp-opensprints.db .dump .quit | sed s/\\\"/\\\`/g | grep -v \"BEGIN TRANSACTION\" | grep -v \"COMMIT\" | sed s/AUTOINCREMENT/AUTO_INCREMENT/g > /tmp/tmp-opensprints.sql")
            File.copy("/tmp/tmp-opensprints.sql",location)
        end
        location = nil
      end

      button("Export SQLite", :width => 200) do
        location = ask_save_file
        if location != nil
          File.copy(File.join(LIB_DIR,'opensprints_data.db'),location)
        end
        location = nil
      end
      para "stats.opensprints.org:" 
      inscription "Email:"
      edit_line do |edit|
        @stats_email = edit.text
      end
      
      inscription "Password:"
      edit_line(:secret => true) do |edit|
        @stats_pass = edit.text
      end
      inscription(link("sign up!",:click => "http://stats.opensprints.org/users/new"))
      button("Upload to server", :width => 200) do
        begin
          upload("http://stats.opensprints.org/config_files.xml","opensprints_conf.yml","config_file[config]",@stats_email,@stats_pass)
          response = upload("http://stats.opensprints.org/data_uploads.xml","opensprints_data.db","data_upload[database]",@stats_email,@stats_pass)
        rescue
          alert("Sorry, there was a problem!")
        end
        if (Hpricot(response)/:id).inner_html.to_i.nonzero?
          alert("Your stats have been sent!")
        end
      end
      inscription(link("(what's this?)",:click => "http://wiki.opensprints.org/index.php?title=Upload_To_Server"))
    end
  end
end

#hardwareObject


265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/controllers/config_controller.rb', line 265

def hardware
  config_setup
  @header.clear { title "Hardware & Rollers" }
  
  @center.clear do
    stack(:height => @center.height-50, :width => 0.8) do
      container
      flow(:height => @center.height-150, :scroll => true) do
        stack(:width => 0.5) do
          stack(:margin => 10) do
            inscription 'Race distance (METERS):'
            edit_line(@prefs['race_distance']) do |edit|
              @prefs['race_distance'] = edit.text.to_f
            end
          end

          stack(:margin => 10) do
            inscription 'Roller circumference (METERS):'
            edit_line(@prefs['roller_circumference']) do |edit|
              @prefs['roller_circumference'] = edit.text.to_f
            end
            inscription "e.g. 4.5 in. roller drum = 0.35908404 meters"
            inscription "e.g. 3.0 in. roller drum = 0.23938936 meters"
          end
        end

        stack(:width => 0.5) do
          stack(:margin => 10) do
            inscription 'Sensor type:'
            sensors = Dir.glob('lib/sensors/*_sensor.rb').map do |s|
              s.gsub(/lib\/sensors\/(.*)_sensor\.rb/, '\1')
            end
            list_box(:items => sensors,
              :choose => @prefs['sensor']['type']) do |changed|
                @prefs['sensor']['type'] = changed.text
                if (changed.text=="mock")||(changed.text=="network")
                  @sensor_location_edit.state = "disabled"
                else
                  @sensor_location_edit.state = nil
                end
            end
          end

          stack(:margin => 10) do
            inscription 'Sensor location:'
            @sensor_location_edit = edit_line(@prefs['sensor']['device']) do |edit|
              @prefs['sensor']['device'] = edit.text
            end
            inscription "e.g. Linux: /dev/ttyUSB0"
          end
        end
      end
      stack do
        save_button
      end

    end
  end
end

#indexObject


213
214
215
216
# File 'lib/controllers/config_controller.rb', line 213

def index
  config_setup
  @header.clear { title "Configuration" }
end

#localeObject


383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/controllers/config_controller.rb', line 383

def locale
  config_setup
  @header.clear { title "Appearance" }

  @center.clear do
    flow(:height => @center.height-50, :width => 1.0) do
      container
      flow(:height => @center.height-150, :scroll => true) do
        stack(:width => 0.6) do
          stack(:margin => 10, :padding => 0) do
            stack(:margin => 0) do
              inscription "Display speed in:", :margin => 0
              metric = nil; standard = nil;
              flow(:margin => 0) do
                standard = radio(:units){ @prefs['units'] = 'standard'}
                inscription 'standard', :margin => 2
              end
              flow(:margin => 0) do
                metric = radio(:units){ @prefs['units'] = 'metric'}
                inscription 'metric', :margin => 2
              end
              if @prefs['units'] == 'metric'
                metric.checked = true
              else
                standard.checked = true
              end
            end
          end
        end
        stack(:width => 0.6) do
          stack(:margin => 10) do
            inscription 'Locale:'
            sensors = Dir.glob('lib/translations/*').map do |s|
              s.gsub(/lib\/translations\/(.*).yml/, '\1')
            end
            list_box(:items => [nil]+sensors,
              :choose => @prefs['locale']) do |changed|
                @prefs['locale'] = changed.text
            end
          end
        end
      end
      stack do
        save_button
      end
    end
  end
end

#save_buttonObject


325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/controllers/config_controller.rb', line 325

def save_button
  button "Save!" do
    @prefs['bikes'].compact!
    old_width, old_height = WIDTH, HEIGHT
    old_audience_width, old_audience_height = WIDTH_AUDIENCE, HEIGHT_AUDIENCE
    old_skin = SKIN
    File.open(File.join(LIB_DIR,'opensprints_conf.yml'), 'w+') do |f|
      f << @prefs.to_yaml

      # Collect env information for troubleshooting via stats site
      f << `ls ~/.shoes/+gem/gems`.gsub(/^/,"#")
    end

    load "lib/setup.rb"
    alert('Preferences saved!')
    if(old_width!=WIDTH||old_height!=HEIGHT) || (old_audience_width!=WIDTH_AUDIENCE||old_audience_height!=HEIGHT_AUDIENCE)
      if((PLATFORM =~ /linux/)&&!(`which opensprints`).empty?)
        alert("window dimensions have changed, restarting...")
        fork ? exit : exec("opensprints")
      else
        alert("window dimensions have changed, please restart opensprints for this to take effect.")
        exit
      end
    end
    if(old_skin!=SKIN)
      if((PLATFORM =~ /linux/)&&!(`which opensprints`).empty?)
        alert("Skin has changed, restarting...")
        fork ? exit : exec("opensprints")
      else
        alert("Skin has changed, please restart opensprints for this to take effect.")
        exit
      end
    end
    
    visit '/configuration'
  end
end

#skinObject


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
# File 'lib/controllers/config_controller.rb', line 103

def skin
  config_setup
  @header.clear { title "Skin" }

  @center.clear do
    flow(:height => @center.height-50, :width => 1.0) do
      container
      flow(:height => @center.height-150, :scroll => true) do
        stack(:width => 0.4) do
          stack do
            inscription 'Background color:'
            color_edit = edit_line(@prefs['background_color']) do |edit|
              @prefs['background_color'] = edit.text
            end
            button "pick color" do
              color_edit.text = ask_color('pick...')
              @prefs['background_color'] = color_edit.text
            end
            inscription 'Background image:'
            image_edit = edit_line(@prefs['background_image']) do |edit|
              @prefs['background_image'] = edit.text
            end
            button "pick file" do
              image_edit.text = ask_open_file
              @prefs['background_image'] = image_edit.text
            end
            inscription 'Menu Background image:'
            m_image_edit = edit_line(@prefs['menu_background_image']) do |edit|
              @prefs['menu_background_image'] = edit.text
            end
            button "pick file" do
              m_image_edit.text = ask_open_file
              @prefs['menu_background_image'] = m_image_edit.text
            end
          end
        end
        stack(:width => 0.6) do
          stack(:margin => 10) do
            inscription 'Skin:'
            sensors = Dir.glob('media/skins/*').map do |s|
              s.gsub(/media\/skins\/(.*)/, '\1')
            end
            list_box(:items => [nil]+sensors,
              :choose => @prefs['skin']) do |changed|
                @prefs['skin'] = changed.text
            end
          end
        end
      end
      stack do
        save_button
      end
    end
  end
end

#upgradeObject


363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/controllers/config_controller.rb', line 363

def upgrade
  config_setup
  @header.clear { title "Upgrade" }
  @center.clear do
    container
    stack do
      version = `dpkg-query -W -f='${Version}' opensprints`
      version = File.readlines('build/debian/changelog').first.gsub(/.*\((.*)\).*/,'\1').strip if version.empty?
      para "You're using software version: #{version}"
      button("Update the software!", :width => 200) do
        `update-manager`
      end
      para "You're using firmware version: #{SENSOR.version}"
      button("Update the firmware", :width => 200) do
        `arduino-ide`
      end
    end
  end
end