Class: PlcUtil::PL7ToIntouchRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/plcutil/schneider/pl7_to_intouch_runner.rb

Overview

Command line tool to read and output an awl file

Instance Method Summary collapse

Constructor Details

#initialize(command_line_arguments) ⇒ PL7ToIntouchRunner

Returns a new instance of PL7ToIntouchRunner.



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
# File 'lib/plcutil/schneider/pl7_to_intouch_runner.rb', line 11

def initialize(command_line_arguments)
    # standard command line options
  @intouchoptions = {}
  @output = nil
  @input = nil
  @symlistfile = nil
    @access_name = nil
    @pl7_tags = []
    @prefix = nil
    @alarm = false
    @caps = false
    @alarm_group = '$System'

    # Parse command line options
  option_parser.parse! command_line_arguments


  if @input
    File.open @input do |f|
      read_pl7_file f
    end
  else
    read_pl7_file $stdin
  end

    # Write to intouch file
  if @output
    File.open @output, 'w' do |f|
      print_to_file f
    end
  else
    print_to_file $stdout
  end
end

Instance Method Details

#make_alarm(io) ⇒ Object



68
69
70
71
72
# File 'lib/plcutil/schneider/pl7_to_intouch_runner.rb', line 68

def make_alarm(io)
  io.alarm_state = 'On'
  io.alarm_comment = io.comment
  io.group = @alarm_group
end

#option_parserObject



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
# File 'lib/plcutil/schneider/pl7_to_intouch_runner.rb', line 107

def option_parser
  OptionParser.new do |opts|
    opts.banner = "Usage: pl7tointouch [options]"
    opts.on("-a", "--access ACCESSNAME", String, "Set access name for all tags") do |access_name|
        @intouchoptions[:access_name] = access_name
    end
    opts.on("-o", "--output FILE", String, "Output to specified file instead of", "standard output") do |output|
      @output = output
    end
    opts.on("-i", "--input FILE", String, "Input from specified file instead of", "standard input") do |input|
      @input = input
    end
    opts.on("--alarm", "Mark bit tags as alarms") do
      @alarm = true
    end
    opts.on("-g", "--alarm-group GROUP", String, "Select intouch alarm group") do |alg|
      @alarm_group = alg
        @alarm = true
    end
    opts.on("--caps", "tag name in capital letters") do
      @caps = true
    end
    opts.on("-p", "--prefix PREFIX", String, "Add PREFIX to tagname") do |prefix|
      @prefix = prefix
    end
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
      # --alarm --alarmgroup 
  end  
end


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
# File 'lib/plcutil/schneider/pl7_to_intouch_runner.rb', line 74

def print_to_file(f)
    @intouchfile = IntouchFile.new nil, @intouchoptions

    @pl7_tags.each do |item|
      data = {:item_use_tagname => 'No', :comment => item.comment }
      tag = (@prefix || '') + item.tagname
      tag = tag.upcase if @caps

      if item.bit_index
        @intouchfile.new_io_disc(tag, data) do |io|
          io.item_name = '%d:%02d' % [400001 + item.index, 16 - item.bit_index]
          make_alarm(io) if @alarm
        end
      elsif item.is_word
        @intouchfile.new_io_int(tag, data) do |io|
          io.item_name = '%d S' % (item.index + 400001)
        end
      elsif item.is_float
        @intouchfile.new_io_real(tag, data) do |io|
          io.item_name = '%d F' % (item.index + 400001)
        end
      else
        @intouchfile.new_io_disc(tag, data) do |io|
          io.item_name = (item.index + 1).to_s
          make_alarm(io) if @alarm
        end
      end
    end

    @intouchfile.write_csv f
end

#read_pl7_file(io) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/plcutil/schneider/pl7_to_intouch_runner.rb', line 47

def read_pl7_file(io)
  io.each_line do |l|
    mres = l.match(/^%M(W|F)?(\d+)(:X(\d+))?\t(\S+)\t(\S+)(\t\"?([^\t"]*))?/)
    if mres
      item = OpenStruct.new
      case mres[1]
      when 'W'
        item.is_word = true
      when 'F'
        item.is_float = true
      end
      item.index = mres[2].to_i
      item.bit_index = mres[4] && mres[4].to_i
      item.tagname = mres[5]
      item.comment = mres[8]
      @pl7_tags << item
    end
  end

end

#show_helpObject



140
141
142
# File 'lib/plcutil/schneider/pl7_to_intouch_runner.rb', line 140

def show_help
  puts option_parser
end