Class: NetLinx::System

Inherits:
Object
  • Object
show all
Defined in:
lib/netlinx/workspace/system.rb

Overview

A collection of resources loaded onto a NetLinx master. Workspace -> Project -> System

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ System

Returns a new instance of System.

Parameters:

  • kwargs (Hash)

    a customizable set of options

Options Hash (**kwargs):

  • :project (NetLinx::Project)

    This system’s parent project node.

  • :name (String) — default: ''

    System name.

  • :description (String) — default: ''
  • :active (Boolean) — default: false

    True if this is the active system in the workspace.

  • :id (Integer) — default: 0

    Master controller system ID. 0 connects to any master at the given communication settings. Or in other words, 0 prevents disconnection from a master with a different ID.

  • :ip_address (String) — default: '0.0.0.0'
  • :ip_port (String) — default: 1319

    ICSLan port.

  • :ensure_availability (String) — default: true

    Ping the master controller to ensure availability before connecting.

  • :com_port (Symbol) — default: :com1
  • :baud_rate (Integer) — default: 38400
  • :data_bits (Integer) — default: 8
  • :parity (:none, :even, :odd, :mark, :space) — default: :none
  • :stop_bits (Integer) — default: 1
  • :flow_control (:none) — default: :none


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
# File 'lib/netlinx/workspace/system.rb', line 49

def initialize **kwargs
  @project     = kwargs.fetch :project,     nil
  
  @name        = kwargs.fetch :name,        ''
  @id          = kwargs.fetch :id,          0
  @active      = kwargs.fetch :active,      false
  @description = kwargs.fetch :description, ''
  
  @ip_address  = kwargs.fetch :ip_address, '0.0.0.0'
  @ip_port     = kwargs.fetch :ip_port, 1319
  @ensure_availability = kwargs.fetch :ensure_availability, true
  
  @com_port     = kwargs.fetch :com_port,     :com1
  @baud_rate    = kwargs.fetch :baud_rate,    38400
  @data_bits    = kwargs.fetch :data_bits,    8
  @parity       = kwargs.fetch :parity,       :none
  @stop_bits    = kwargs.fetch :stop_bits,    1
  @flow_control = kwargs.fetch :flow_control, :none
  
  @files = []
  
  @compiler_target_files  = []
  @compiler_include_paths = []
  @compiler_module_paths  = []
  @compiler_library_paths = []
  
  system_element = kwargs.fetch :element, nil
  parse_xml_element system_element if system_element
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



13
14
15
# File 'lib/netlinx/workspace/system.rb', line 13

def active
  @active
end

#baud_rateObject

Returns the value of attribute baud_rate.



22
23
24
# File 'lib/netlinx/workspace/system.rb', line 22

def baud_rate
  @baud_rate
end

#com_portObject

Returns the value of attribute com_port.



21
22
23
# File 'lib/netlinx/workspace/system.rb', line 21

def com_port
  @com_port
end

#data_bitsObject

Returns the value of attribute data_bits.



23
24
25
# File 'lib/netlinx/workspace/system.rb', line 23

def data_bits
  @data_bits
end

#descriptionObject

Returns the value of attribute description.



15
16
17
# File 'lib/netlinx/workspace/system.rb', line 15

def description
  @description
end

#ensure_availabilityObject

Returns the value of attribute ensure_availability.



19
20
21
# File 'lib/netlinx/workspace/system.rb', line 19

def ensure_availability
  @ensure_availability
end

#filesObject

Returns the value of attribute files.



10
11
12
# File 'lib/netlinx/workspace/system.rb', line 10

def files
  @files
end

#flow_controlObject

Returns the value of attribute flow_control.



26
27
28
# File 'lib/netlinx/workspace/system.rb', line 26

def flow_control
  @flow_control
end

#idObject

Returns the value of attribute id.



14
15
16
# File 'lib/netlinx/workspace/system.rb', line 14

def id
  @id
end

#ip_addressObject

Returns the value of attribute ip_address.



17
18
19
# File 'lib/netlinx/workspace/system.rb', line 17

def ip_address
  @ip_address
end

#ip_portObject

Returns the value of attribute ip_port.



18
19
20
# File 'lib/netlinx/workspace/system.rb', line 18

def ip_port
  @ip_port
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/netlinx/workspace/system.rb', line 12

def name
  @name
end

#parityObject

Returns the value of attribute parity.



24
25
26
# File 'lib/netlinx/workspace/system.rb', line 24

def parity
  @parity
end

#projectObject

A reference to the system’s parent project.



9
10
11
# File 'lib/netlinx/workspace/system.rb', line 9

def project
  @project
end

#stop_bitsObject

Returns the value of attribute stop_bits.



25
26
27
# File 'lib/netlinx/workspace/system.rb', line 25

def stop_bits
  @stop_bits
end

Instance Method Details

#<<(file) ⇒ Object

Alias to add a file.



80
81
82
83
# File 'lib/netlinx/workspace/system.rb', line 80

def << file
  @files << file
  file.system = self
end

#compileObject

Compile this system.



180
181
182
183
184
185
186
# File 'lib/netlinx/workspace/system.rb', line 180

def compile
  # The compiler dependency is only needed if this method is called.
  require 'netlinx/compiler'
  
  compiler = NetLinx::Compiler.new
  compiler.compile self
end

#compiler_include_pathsObject

See Also:

  • Test::NetLinx::Compilable.


137
138
139
140
141
142
143
144
# File 'lib/netlinx/workspace/system.rb', line 137

def compiler_include_paths
  @files
    .select {|f| f.type == :include}
    .map {|f| File.expand_path \
      File.dirname(f.path.gsub('\\', '/')),
      f.system.project.workspace.path
    }.uniq
end

#compiler_library_pathsObject

See Also:

  • Test::NetLinx::Compilable.


157
158
159
# File 'lib/netlinx/workspace/system.rb', line 157

def compiler_library_paths
  []
end

#compiler_module_pathsObject

See Also:

  • Test::NetLinx::Compilable.


147
148
149
150
151
152
153
154
# File 'lib/netlinx/workspace/system.rb', line 147

def compiler_module_paths
  @files
    .select {|f| f.type == :module || f.type == :tko || f.type == :duet}
    .map {|f| File.expand_path \
      File.dirname(f.path.gsub('\\', '/')),
      f.system.project.workspace.path
    }.uniq
end

#compiler_target_filesObject

See Also:

  • Test::NetLinx::Compilable.


127
128
129
130
131
132
133
134
# File 'lib/netlinx/workspace/system.rb', line 127

def compiler_target_files
  @files
    .select {|f| f.type == :master}
    .map {|f| File.expand_path \
      f.path.gsub('\\', '/'),
      f.system.project.workspace.path
    }.uniq
end

#include?(file) ⇒ Boolean

Returns true if the project contains the specified file.

Returns:

  • (Boolean)

    true if the project contains the specified file.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/netlinx/workspace/system.rb', line 162

def include? file
  included = false
  
  @files.each do |f|
    name_included = f.name.downcase.eql? file.downcase
    
    # TODO: This should probably be relative to the workspace path,
    #       which can be found by traversing @project, @workspace.
    path_included = file.gsub(/\\/, '/').include? f.path.gsub(/\\/, '/')
    
    included = name_included || path_included
    break if included
  end
  
  included
end

#to_sObject

Returns the system name.

Returns:

  • the system name.



86
87
88
# File 'lib/netlinx/workspace/system.rb', line 86

def to_s
  @name
end

#to_xml_elementREXML::Element

Returns an XML element representing this system.

Returns:

  • (REXML::Element)

    an XML element representing this system.



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
# File 'lib/netlinx/workspace/system.rb', line 91

def to_xml_element
  REXML::Element.new('System').tap do |system|
    system.attributes['IsActive']  = active
    system.attributes['Platform']  = 'Netlinx'
    transport = (ip_address == '0.0.0.0') ? 'Serial' : 'TCPIP'
    system.attributes['Transport'] = transport
    system.attributes['TransportEx'] = transport
      
    
    system.add_element('Identifier').tap { |e| e.text = name }
    system.add_element('SysID').tap { |e| e.text = id }
    system.add_element('Comments').tap { |e| e.text = description }
    
    # These don't seem to change in NetLinx Studio 4.0; possibly 3.x legacy.
    # The 'Ex' suffixes are used.
    system.add_element('TransTCPIP').tap { |e| e.text = "#{ip_address},#{ip_port},#{ensure_availability ? 1 : 0},,," }
    system.add_element('TransSerial').tap { |e| e.text = "#{com_port.upcase},#{baud_rate},#{data_bits},#{parity.capitalize},#{stop_bits},,," }
    
    # TODO: Generate communication settings.
    system.add_element('TransTCPIPEx').tap { |e|
      e.text = "#{ip_address}|#{ip_port}|#{ensure_availability ? 1 : 0}|||"
    }
    system.add_element('TransSerialEx').tap { |e|
      e.text = "#{com_port.upcase}|#{baud_rate}|#{data_bits}|#{parity.capitalize}|#{stop_bits}|||"
    }
    system.add_element('TransUSBEx').tap { |e| e.text = '|||||' }
    system.add_element('TransVNMEx').tap { |e| e.text = '||' }
    
    system.add_element('UserName').tap { |e| e.text = '' }
    system.add_element('Password').tap { |e| e.text = '' }
    
    @files.each { |file| system << file.to_xml_element }
  end
end