File: update-fixture-map.rb

package info (click to toggle)
qlcplus 4.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 58,644 kB
  • sloc: cpp: 182,867; javascript: 7,764; xml: 2,453; ansic: 2,120; sh: 1,716; python: 634; ruby: 606; makefile: 23
file content (649 lines) | stat: -rwxr-xr-x 18,583 bytes parent folder | download
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
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
212
213
214
215
216
217
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
264
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
324
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#!/usr/bin/env ruby

# Script to automatically update the fixtures map.
# Run it every time some fixtures are added to this folder.
# The packages 'ruby' and 'ruby-libxml' are required to run this

# When invoked with additional parameter (filename), it will create
# HTML overview of the fixtures e.g. ./update-fixtures-map.rb index.html

# rubocop: disable Style/Documentation, Metrics/MethodLength
# rubocop: disable Metrics/ClassLength, Metrics/AbcSize
# rubocop: disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity,
# Metrics/LineLength,

require 'libxml'

NS_URI = 'http://www.qlcplus.org/FixtureDefinition'
OLD_NS_URI = 'http://qlcplus.sourceforge.net/FixtureDefinition'

NS = ['xmlns:' + NS_URI]

class FixtureDef
  class Creator
    attr_accessor :name, :version, :author
    def initialize(node)
      return if node.empty?
      @name = node.find_first('xmlns:Name', NS).content
      @version = node.find_first('xmlns:Version', NS).content
      @author = node.find_first('xmlns:Author', NS).content
    end
  end

  class Capability
    attr_accessor :min, :max, :name, :res, :color, :color2
    def initialize(node)
      return if node.empty?
      @min = node.attributes['Min'].to_i
      @max = node.attributes['Max'].to_i
      @res = node.attributes['Res']
      @color = node.attributes['Color']
      @color2 = node.attributes['Color2']
      @name = node.content
    end
  end

  class Group
    attr_accessor :byte, :name

    MSB = 0
    LSB = 1

    def initialize(node)
      return if node.empty?
      @name = node.content
      @byte = node.attributes['Byte'].to_i
    end

    PAN = 'Pan'
    TILT = 'Tilt'
    COLOUR = 'Colour'
    EFFECT = 'Effect'
    GOBO = 'Gobo'
    SHUTTER = 'Shutter'
    SPEED = 'Speed'
    PRISM = 'Prism'
    MAINTENANCE = 'Maintenance'
    INTENSITY = 'Intensity'
    BEAM = 'Beam'

    def icon
      case @name
      when PAN
        'pan.png'
      when TILT
        'tilt.png'
      when COLOUR
        'colorwheel.png'
      when EFFECT
        'star.png'
      when GOBO
        'gobo.png'
      when SHUTTER
        'shutter.png'
      when SPEED
        'speed.png'
      when PRISM
        'prism.png'
      when MAINTENANCE
        'configure.png'
      when INTENSITY
        'intensity.png'
      when BEAM
        'beam.png'
      end
    end

    def byte_str
      case @group
      when MSB
        'MSB'
      when LSB
        'LSB'
      end
    end
  end

  class Channel
    attr_accessor :name, :group, :color, :capabilities
    def initialize(node)
      @capabilities = []
      return if node.empty?
      @name = node.attributes['Name']
      @group = Group.new(node.find_first('xmlns:Group', NS))
      n = node.find_first('xmlns:Colour', NS)
      @color = n.content unless n.nil?
      n = node.find('xmlns:Capability', NS)
      @capabilities = n.map { |c| Capability.new(c) } unless n.empty?
    end

    RED = 'Red'
    GREEN = 'Green'
    BLUE = 'Blue'
    CYAN = 'Cyan'
    MAGENTA = 'Magenta'
    YELLOW = 'Yellow'
    AMBER = 'Amber'
    WHITE = 'White'
    UV = 'UV'
    LIME = 'Lime'
    INDIGO = 'Indigo'

    def rgb_color
      case color
      when RED
        '#FF0000'
      when GREEN
        '#00FF00'
      when BLUE
        '#0000FF'
      when CYAN
        '#00FFFF'
      when MAGENTA
        '#FF00FF'
      when YELLOW
        '#FFFF00'
      when AMBER
        '#FF7E00'
      when WHITE
        '#FFFFFF'
      when UV
        '#9400D3'
      when LIME
        '#ADFF2F'
      when INDIGO
        '#4B0082'
      end
    end
  end

  class Bulb
    attr_accessor :lumens, :type, :color_temp
    def initialize(node)
      return if node.empty?
      @lumens = node.attributes['Lumens']
      @type = node.attributes['Type']
      @color_temp = node.attributes['ColourTemperature']
    end
  end

  class Dimensions
    attr_accessor :width, :height, :depth, :weight
    def initialize(node)
      return if node.empty?
      @width = node.attributes['Width'].to_i
      @height = node.attributes['Height'].to_i
      @depth = node.attributes['Depth'].to_i
      @weight = node.attributes['Weight'].to_f
    end
  end

  class Lens
    attr_accessor :degrees_min, :degrees_max, :name
    def initialize(node)
      return if node.empty?
      @degrees_min = node.attributes['DegreesMin'].to_f
      @degrees_max = node.attributes['DegreesMax'].to_f
      @name = node.attributes['Name']
    end
  end

  class Focus
    attr_accessor :pan_max, :tilt_max, :type
    def initialize(node)
      return if node.empty?
      @pan_max = node.attributes['PanMax'].to_i
      @tilt_max = node.attributes['TiltMax'].to_i
      @type = node.attributes['Type']
    end
  end

  class Technical
    attr_accessor :power_consumption, :dmx_connector
    def initialize(node)
      return if node.nil?
      return if node.empty?
      @power_consumption = node.attributes['PowerConsumption']
      @dmx_connector = node.attributes['DmxConnector']
    end
  end

  class Physical
    attr_accessor :bulb, :dimensions, :lens, :focus, :technical
    def initialize(node)
      return if node.empty?
      @bulb = Bulb.new(node.find_first('xmlns:Bulb', NS))
      @dimensions = Dimensions.new(node.find_first('xmlns:Dimensions', NS))
      @lens = Lens.new(node.find_first('xmlns:Lens', NS))
      @focus = Focus.new(node.find_first('xmlns:Focus', NS))
      @technical = Technical.new(node.find_first('xmlns:Technical', NS))
    end
  end

  class ChannelRef
    attr_accessor :number, :name
    def initialize(node)
      return if node.empty?
      @name = node.content
      @number = node.attributes['Number'].to_i
    end
  end

  class Head
    attr_accessor :channels, :index
    def initialize(node)
      @channels = []
      return if node.empty?
      @channels = node.find('xmlns:Channel', NS).map { |c| c.content.to_i }
    end
  end

  class Mode
    attr_accessor :name, :physical, :channels, :heads
    def initialize(node)
      @channels = []
      @heads = []
      return if node.empty?
      @name = node.attributes['Name']
      @physical = Physical.new(node.find_first('xmlns:Physical', NS))
      n = node.find('xmlns:Channel', NS)
      @channels = n.map { |c| ChannelRef.new(c) } unless n.empty?
      n = node.find('xmlns:Head', NS)
      @heads = n.map { |h| Head.new(h) } unless n.empty?
      @heads.each_with_index { |h, i| h.index = i + 1 }
    end
  end

  attr_accessor :path, :manufacturer, :model, :type, :creator, :channels, :modes

  def initialize(path)
    @channel = []
    @modes = []

    load(path)
  end

  COLOR_CHANGER = 'Color Changer'
  DIMMER = 'Dimmer'
  EFFECT = 'Effect'
  FAN = 'Fan'
  FLOWER = 'Flower'
  HAZER = 'Hazer'
  LASER = 'Laser'
  MOVING_HEAD = 'Moving Head'
  OTHER = 'Other'
  SCANNER = 'Scanner'
  SMOKE = 'Smoke'
  STROBE = 'Strobe'

  def type_icon
    case @type
    when COLOR_CHANGER
      'fixture.png'
    when DIMMER
      'dimmer.png'
    when EFFECT
      'effect.png'
    when FAN
      'fan.png'
    when FLOWER
      'flower.png'
    when HAZER
      'hazer.png'
    when LASER
      'laser.png'
    when MOVING_HEAD
      'movinghead.png'
    when OTHER
      'other.png'
    when SCANNER
      'scanner.png'
    when SMOKE
      'smoke.png'
    when STROBE
      'strobe.png'
    else
      'other.png'
    end
  end

  AUTHORS_TO_FIX = {
    'hjunnila' => 'Heikki Junnila',
    'jlgriffin' => 'JL Griffin',
    'griffinwebnet' => 'JL Griffin',
    ',,,' => ''
  }

  def load(path)
    qxf = File.read(path)
    if qxf.include?(OLD_NS_URI)
      qxf.gsub!(OLD_NS_URI, NS_URI)
      File.open(path, 'w') { |f| f.write(qxf) }
    end

    @doc = LibXML::XML::Document.string(qxf)
    @doc.root.find('//xmlns:Dimensions', NS).each do |node|
      node['Weight'] = node['Weight'].tr(',', '.')
    end

    @doc.root.find('//xmlns:Lens', NS).each do |node|
      node['DegreesMin'] = node['DegreesMin'].tr(',', '.')
    end

    @doc.root.find('//xmlns:Lens', NS).each do |node|
      node['DegreesMax'] = node['DegreesMax'].tr(',', '.')
    end

    @doc.root.find('//xmlns:Author', NS).each do |node|
      AUTHORS_TO_FIX.each do |old, new|
        node.content = node.content.gsub(old, new)
      end
    end

    if @doc.root.namespaces.default.nil?
      @doc.root.namespaces.namespace = LibXML::XML::Namespace.new(@doc.root, nil, NS_URI)
    end

    qxf2 = @doc.to_s(indent: true, encoding: LibXML::XML::Encoding::UTF_8)

    if qxf != qxf2
      File.open(path, 'w') { |f| f.write(qxf2) }
      @doc = LibXML::XML::Document.string(qxf2)
    end

    @path = path
    @manufacturer = @doc.find_first('/xmlns:FixtureDefinition/xmlns:Manufacturer', NS).content
    @model = @doc.find_first('/xmlns:FixtureDefinition/xmlns:Model', NS).content
    @type = @doc.find_first('/xmlns:FixtureDefinition/xmlns:Type', NS).content
    @creator = Creator.new(@doc.find_first('/xmlns:FixtureDefinition/xmlns:Creator', NS))
    @channels = @doc.find('/xmlns:FixtureDefinition/xmlns:Channel', NS).map { |c| Channel.new(c) }
    @modes = @doc.find('/xmlns:FixtureDefinition/xmlns:Mode', NS).map { |m| Mode.new(m) }
  end
end

class Fixtures
  attr_reader :fixtures

  def initialize
    @fixtures = []
  end

  def load_fixtures(path)
    Dir.chdir(path) do
      Dir.glob('*.qxf').sort.each do |f|
        @fixtures << FixtureDef.new(f)
      end
    end
  end

  def heads_have_common_channels(mode)
    all_head_channels = mode.heads.inject([]) { |a, e| a + e.channels }.sort
    unique_head_channels = all_head_channels.uniq.sort

    all_head_channels != unique_head_channels
  end

  def update_fixtures_map(filename = 'FixturesMap.xml')
    orig_data = File.read(filename)
    doc = LibXML::XML::Document.string(orig_data)
    doc.root.children.map(&:remove!)
    total_wrong = 0
    @fixtures.sort_by { |f| f.path.downcase }.each do |f|
      node = LibXML::XML::Node.new('fixture')
      LibXML::XML::Attr.new(node, 'path', f.path)
      LibXML::XML::Attr.new(node, 'mf', f.manufacturer)
      LibXML::XML::Attr.new(node, 'md', f.model)

      wrong_pan = f.modes.find { |m| m.physical.focus.pan_max == 0 }
      wrong_tilt = f.modes.find { |m| m.physical.focus.tilt_max == 0 }
      has_pan = f.channels.find { |c| c.group.name == FixtureDef::Group::PAN }
      has_tilt = f.channels.find { |c| c.group.name == FixtureDef::Group::TILT }
      wrong_width = f.modes.find { |m| m.physical.dimensions.width == 0 }
      wrong_height = f.modes.find { |m| m.physical.dimensions.height == 0 }
      wrong_depth = f.modes.find { |m| m.physical.dimensions.depth == 0 }
      wrong_weight = f.modes.find { |m| m.physical.dimensions.weight == 0 }
      wrong_heads = f.modes.find { |m| heads_have_common_channels(m) }
      wrong_msb = f.channels.select do |c|
        c.group.byte == FixtureDef::Group::MSB &&
        (c.name.downcase.include?('fine') || c.name.downcase.include?('least')) &&
        (f.model != 'Event Bar LED' || c.name != 'Movement (Fine)')
      end
      wrong_lsb = f.channels.select { |c| c.group.byte == FixtureDef::Group::LSB && c.name.downcase.include?('coarse') }

      problems = []
      problems << 'PAN' if wrong_pan && has_pan
      problems << 'TILT' if wrong_tilt && has_tilt
      problems << 'WIDTH' if wrong_width
      problems << 'HEIGHT' if wrong_height
      problems << 'DEPTH' if wrong_depth
      problems << 'WEIGHT' if wrong_weight
      problems << 'HEADS' if wrong_heads
      problems << "MSB: #{wrong_msb.map(&:name).join(',')}" unless wrong_msb.empty?
      problems << "LSB: #{wrong_msb.map(&:name).join(',')}" unless wrong_lsb.empty?

      unless problems.empty?
        puts "#{f.path}: #{problems.join ' '}"
        total_wrong += 1
      end

      doc.root << node
    end

    puts "wrong: #{total_wrong}"

    if doc.root.namespaces.default.nil?
      doc.root.namespaces.namespace = LibXML::XML::Namespace.new(doc.root, nil, 'http://www.qlcplus.org/FixturesMap')
    end
    new_data = doc.to_s(indent: true, encoding: LibXML::XML::Encoding::UTF_8).gsub('http://qlcplus.sourceforge.net/FixturesMap', 'http://www.qlcplus.org/FixturesMap')
    return if orig_data == new_data

    File.open(filename, 'w') { |f| f.write(new_data) }
  end

  def make_overview(filename = 'index.html')
    File.open(filename, 'w') do |f|
      f << <<-EOF
<html>
<head>
    <meta charset='utf-8'>
</head>
<body>
  <table border=1>
    <tr>
      <th rowspan=3>Manuf</th>
      <th rowspan=3>Model</th>
      <th rowspan=3>Version</th>
      <th rowspan=3>Author</th>
      <th rowspan=3>Type</th>
      <th rowspan=3>Mode</th>
      <th rowspan=3>Heads</th>
      <th colspan=15>Physical</th>
    </tr>
    <tr>
      <th colspan=3>Bulb</th>
      <th colspan=4>Dim</th>
      <th colspan=3>Lens</th>
      <th colspan=3>Focus</th>
      <th colspan=2>Tech</th>
    </tr>
    <tr>
      <th>Lm</th>
      <th>Type</th>
      <th>ColTemp</th>
      <th>Wid</th>
      <th>Hei</th>
      <th>Dep</th>
      <th>Wei</th>
      <th>Min</th>
      <th>Max</th>
      <th>Name</th>
      <th>Pan</th>
      <th>Tilt</th>
      <th>Type</th>
      <th>Power</th>
      <th>Conn</th>
    </tr>
EOF
      @fixtures.sort_by { |fixture| fixture.path.downcase }.each do |fixture|
        f << <<-EOF
    <tr>
      <td rowspan=#{fixture.modes.size}>#{fixture.manufacturer}</td>
      <td rowspan=#{fixture.modes.size}>#{fixture.model}</td>
      <td rowspan=#{fixture.modes.size}>#{fixture.creator.version}</td>
      <td rowspan=#{fixture.modes.size}>#{fixture.creator.author}</td>
      <td rowspan=#{fixture.modes.size}><img src="gfx/#{fixture.type_icon}" /> #{fixture.type}</td>

EOF
        fixture.modes.each_with_index do |mode, index|
          f << "    <tr>\n" if index > 0
          f << <<-EOF
      <td>#{mode.name}</td>
      <td>#{mode.heads.size}</td>
      <td>#{mode.physical.bulb.lumens}</td>
      <td>#{mode.physical.bulb.type}</td>
      <td>#{mode.physical.bulb.color_temp}</td>
      <td>#{mode.physical.dimensions.width}</td>
      <td>#{mode.physical.dimensions.height}</td>
      <td>#{mode.physical.dimensions.depth}</td>
      <td>#{mode.physical.dimensions.weight}</td>
      <td>#{mode.physical.lens.degrees_min}</td>
      <td>#{mode.physical.lens.degrees_max}</td>
      <td>#{mode.physical.lens.name}</td>
      <td>#{mode.physical.focus.pan_max}</td>
      <td>#{mode.physical.focus.tilt_max}</td>
      <td>#{mode.physical.focus.type}</td>
      <td>#{mode.physical.technical.power_consumption}</td>
      <td>#{mode.physical.technical.dmx_connector}</td>
    </tr>
EOF
        end

        f << <<-EOF
    <tr>
      <td colspan=22>
        <table border=1>
          <tr>
            <th>&nbsp;</th>
            <th colspan="2">group</th>
            <th>byte</th>
            <th>color</th>
EOF
        fixture.modes.each do |mode|
          f << "            <th colspan=2> #{mode.name}</th>\n"
        end

        f << "          </tr>\n"

        fixture.channels.each do |channel|
          f << "          <tr>\n"
          f << "            <td>#{channel.name}</td>\n"
          icon = channel.group.icon
          if icon.nil?
            f << "            <td></td>\n"
          else
            f << "            <td><img src=\"gfx/#{icon}\" /></td>\n"
          end
          f << "            <td>#{channel.group.name}</td>\n"
          f << "            <td>#{channel.group.byte_str}</td>\n"
          rgb_color = channel.rgb_color
          if rgb_color.nil?
            f << "            <td>#{channel.color}</td>\n"
          else
            f << "            <td style=\"background-color: #{rgb_color}\">#{channel.color}</td>\n"
          end

          fixture.modes.each do |mode|
            mch = mode.channels.find { |mc| mc.name == channel.name }
            if mch.nil?
              f << "            <td></td>\n"
              f << "            <td></td>\n"
            else
              heads = mode.heads.map { |head| head.index if head.channels.include? mch.number }.compact.join(',')
              f << "            <td>#{mch.number}</td>\n"
              f << "            <td>#{heads.empty? ? '&nbsp;' : heads}</td>\n"
            end
          end

          f << "          </tr>\n"
          if false
            channel.capabilities.each do |capability|
              f << "          <tr>\n"
              f << "            <td colspan=\"#{5 + fixture.modes.size * 2}\"></td>\n"
              f << "            <td>#{capability.min}</td>\n"
              f << "            <td>#{capability.max}</td>\n"
              f << "            <td>#{capability.name}</td>\n"
              f << "            <td>#{capability.res}</td>\n"
              if capability.color.nil?
                f << "            <td>&nbsp;</td>\n"
              else
                f << "            <td style=\"background-color: #{capability.color}\">#{capability.color}</td>\n"
              end
              if capability.color2.nil?
                f << "            <td>&nbsp;</td>\n"
              else
                f << "            <td style=\"background-color: #{capability.color2}\">#{capability.color2}</td>\n"
              end
              f << "          </tr>\n"
            end
          end
        end

        f << <<-EOF
          </tr>
        </table>
      </td>
EOF
      end

      f << <<-EOF
</table>
</body>
</html>
EOF
    end
  end

  def shutter(filename = 'index.html')
    File.open(filename, 'w') do |f|
      @fixtures.sort_by { |fixture| fixture.path.downcase }.each do |fixture|
        shutter_channels = fixture.channels.select { |channel| channel.group.name == FixtureDef::Group::Shutter && channel.group.byte == FixtureDef::Group::MSB }
        next if shutter_channels.empty?

        f << <<-EOF
=== #{fixture.manufacturer} #{fixture.model}
#{fixture.type}

EOF
        shutter_channels.each do |channel|
          f << "-- #{channel.name}\n"
          channel.capabilities.each do |capability|
            shutter = 'open'
            if capability.name =~ /close|blackout|off/i
              shutter = 'closed'
            elsif capability.name =~ /strob|pulse/i
              shutter = 'strobe'
            end

            f << "#{capability.min} - #{capability.max} #{shutter} #{capability.name}\n"
          end
        end
      end
    end
  end
end

# LibXML::XML::Error.set_handler(&LibXML::XML::Error::VERBOSE_HANDLER)
LibXML::XML::Error.set_handler do |error|
  puts error.to_s
end

fm = Fixtures.new
fm.load_fixtures('.')
fm.update_fixtures_map

puts "Total fixtures: #{fm.fixtures.size}"

if ARGV.size > 0
  fm.make_overview(ARGV[0])
  # fm.shutter(ARGV[0])
end