Class: PlcUtil::IntouchPrettyPrintRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/plcutil/wonderware/intouch_pretty_print_runner.rb

Constant Summary collapse

MAX_WW_TAGLENGTH =
32

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ IntouchPrettyPrintRunner

Returns a new instance of IntouchPrettyPrintRunner.



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
# File 'lib/plcutil/wonderware/intouch_pretty_print_runner.rb', line 9

def initialize(arguments)
  # Standard options
  @mode = :io

  # Parse command line options
			option_parser.parse! arguments
			if arguments.size > 1
show_help
exit
			end
  filename, = arguments



  # Read from intouch file
  @intouch_file = IntouchFile.new
			if filename
File.open filename do |f|
	@intouch_file.read_csv f
end
			else
    @intouch_file.read_csv $stdin
			end

  # print the tags in intouch
  case @mode
  when :io
    print_io
  when :duplicates
    print_duplicates
  when :missing
    print_alarms_missing_text
  when :alarm_groups
    print_alarm_groups
  when :access_names
    print_access_names
  when :tag
    print_tag @tag
  end
end

Instance Method Details

#addr_signature(tag) ⇒ Object



50
51
52
# File 'lib/plcutil/wonderware/intouch_pretty_print_runner.rb', line 50

def addr_signature(tag) 
  get_tag_field(:access_name, tag) + get_tag_field(:item_name, tag)
end

#fix_string(column, tag) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/plcutil/wonderware/intouch_pretty_print_runner.rb', line 141

def fix_string(column, tag)
  str = column[:gen].call(tag)
  if str.size > column[:adjusted]
    str[0..(column[:adjusted] - 4)] + '...'
  else
    str
  end
end

#get_tag_field(field, tag) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/plcutil/wonderware/intouch_pretty_print_runner.rb', line 133

def get_tag_field(field, tag)
  if tag.respond_to? field
    tag.method(field).call || ''
  else
    ''
  end
end

#max_str_len(tags, abs_max, abs_min) ⇒ Object



127
128
129
# File 'lib/plcutil/wonderware/intouch_pretty_print_runner.rb', line 127

def max_str_len(tags, abs_max, abs_min) 
  [abs_max, tags.reduce(abs_min) {|max, tag| max = [max, (yield tag).size].max }].min
end

#option_parserObject



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
# File 'lib/plcutil/wonderware/intouch_pretty_print_runner.rb', line 182

def option_parser
	OptionParser.new do |opts|
		opts.banner = "Usage: intouchreader [options] [DBFILE]"
		opts.on("-c", "--access-names", "Show access name") do
        @mode = :access_names
		end
		opts.on("-t", "--tag TAGNAME", "Show all fields for the specified tag") do |tag|
        @mode = :tag
        @tag = tag
		end
		opts.on("-w", "--wide", "Print wider than console width") do
        @column_width = 9999
		end
		opts.on("-a", "--alarm-groups", "Show alarm groups") do
        @mode = :alarm_groups
		end
		opts.on("-m", "--missing", "Show only alarms with missing text") do
        @mode = :missing
		end
		opts.on("-d", "--duplicates", "Show only duplicated tags (shares address)") do
        @mode = :duplicates
		end
		opts.on_tail("-h", "--help", "Show this message") do
			puts opts
			exit
		end
	end	
end


155
156
157
# File 'lib/plcutil/wonderware/intouch_pretty_print_runner.rb', line 155

def print_access_names
  print_four_column_tags ':IOAccess'
end


151
152
153
# File 'lib/plcutil/wonderware/intouch_pretty_print_runner.rb', line 151

def print_alarm_groups
  print_four_column_tags ':AlarmGroup'
end


74
75
76
77
78
# File 'lib/plcutil/wonderware/intouch_pretty_print_runner.rb', line 74

def print_alarms_missing_text
  print_io do |tags| 
    tags.select {|tag| tag.alarm? && (!tag.alarm_comment || !tag.alarm_comment.match(/\S/)) }
  end
end


59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/plcutil/wonderware/intouch_pretty_print_runner.rb', line 59

def print_duplicates
  print_io do |tags| 
    addresscount = {}

    tags.each do |tag| 
      addr = addr_signature tag

      addresscount[addr] ||= 0
      addresscount[addr] += 1 if tag_is_io?(tag)
    end
    tmp = tags.select {|tag| addresscount[addr_signature tag] > 1 }
    tmp.sort {|a, b| addr_signature(a) <=> addr_signature(b) }
  end
end


159
160
161
162
163
164
165
166
167
168
# File 'lib/plcutil/wonderware/intouch_pretty_print_runner.rb', line 159

def print_four_column_tags(section)
  lines = []
  @intouch_file.each_tag section do |tag|
    lines << tag.tag
  end
  lines.each_slice(4) do |slice|
    slice[3] ||= nil
    puts ('%-20s' * 4) % slice
  end
end


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
# File 'lib/plcutil/wonderware/intouch_pretty_print_runner.rb', line 80

def print_io
    ss = %w( :IODisc :IOReal :IndirectAnalog :MemoryReal :IOMsg :IndirectMsg 
              :MemoryMsg :MemoryDisc :IndirectDisc :IOInt :MemoryInt)
    tags = []
    ss.each {|section| @intouch_file.each_tag(section) {|tag| tags << tag } }

    if block_given?
      tags = yield tags
    end

    columns = []
    columns << {:max => 1, :min => 1, :nospace => true, :gen => Proc.new { |tag| alarm_icon(tag) } }
    columns << {:max => MAX_WW_TAGLENGTH, :min => 15, :gen => lambda {|tag| tag.tag } }
    columns << {:max => 20, :min => 10, :gen => Proc.new {|tag| get_tag_field(:item_name, tag) } }
    columns << {:max => 20, :min => 10, :gen => Proc.new {|tag| get_tag_field(:access_name, tag) } }
    columns << {:rest => true, :gen => Proc.new {|tag| comment_for_tag(tag) } }

    # shrink columns
    columns.each do |c| 
      if c[:max]
        c[:adjusted] = max_str_len(tags, c[:max], c[:min]) {|tag| c[:gen].call(tag) }
      end
    end

    # use remaining space for :rest tag
    spaces = columns.map {|c| c[:nospace] ? 0 : 1 }.reduce(:+) - 1
    wasted = columns.map {|c| c[:adjusted] || 0 }.reduce(:+) - spaces
    rest = columns.find {|c| c[:rest] }
    @column_width ||= console_width
    rest[:adjusted] = @column_width - wasted if rest

    first_columns = columns - [columns.last]
    tags.each do |tag|
      cs = first_columns.map do |c| 
        fix_string(c, tag).ljust(c[:adjusted]) + (c[:nospace] ? '' : ' ') 
      end
      cs << fix_string(columns.last, tag)
      str = cs.join
      if tag.tag.size > MAX_WW_TAGLENGTH
        str = red(str)
      else
        str = yellow(str) if tag.alarm?
      end
      puts str
    end
end


170
171
172
173
174
175
176
177
178
179
180
# File 'lib/plcutil/wonderware/intouch_pretty_print_runner.rb', line 170

def print_tag(tag)
  t = @intouch_file.find_tag(tag)
  if t
    puts 'Tag: ' + tag
    t.intouch_fields.each do |field|
      puts '%-30s%s' % [field.to_s, t.method(field).call]
    end
  else
    puts 'Tag %s was not found' % tag
  end
end

#show_helpObject



211
212
213
# File 'lib/plcutil/wonderware/intouch_pretty_print_runner.rb', line 211

def show_help
	puts option_parser
end

#tag_is_io?(tag) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/plcutil/wonderware/intouch_pretty_print_runner.rb', line 54

def tag_is_io?(tag)
  tag.respond_to?(:item_name) && tag.item_name &&
    tag.respond_to?(:access_name) && tag.access_name
end