Class: SDN::CLI::Simulator::MockMotor

Inherits:
Object
  • Object
show all
Defined in:
lib/sdn/cli/simulator.rb

Constant Summary collapse

ALLOWED_MOVE_TYPES =
%i[up_limit
down_limit
ip
position_pulses
position_percent].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ MockMotor

Returns a new instance of MockMotor.



25
26
27
28
29
30
31
32
33
34
# File 'lib/sdn/cli/simulator.rb', line 25

def initialize(client)
  @client = client
  self.address = Message.parse_address("00.00.00")
  self.node_type = :st30
  self.label = ""
  self.ips = Array.new(16)
  self.groups = Array.new(16)
  self.ir_channels = 0
  self.lock_priority = 0
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



7
8
9
# File 'lib/sdn/cli/simulator.rb', line 7

def address
  @address
end

#down_limitObject

Returns the value of attribute down_limit.



7
8
9
# File 'lib/sdn/cli/simulator.rb', line 7

def down_limit
  @down_limit
end

#groupsObject

Returns the value of attribute groups.



7
8
9
# File 'lib/sdn/cli/simulator.rb', line 7

def groups
  @groups
end

#ipsObject

Returns the value of attribute ips.



7
8
9
# File 'lib/sdn/cli/simulator.rb', line 7

def ips
  @ips
end

#ir_channelsObject

Returns the value of attribute ir_channels.



7
8
9
# File 'lib/sdn/cli/simulator.rb', line 7

def ir_channels
  @ir_channels
end

#labelObject

Returns the value of attribute label.



7
8
9
# File 'lib/sdn/cli/simulator.rb', line 7

def label
  @label
end

#lock_priorityObject

Returns the value of attribute lock_priority.



7
8
9
# File 'lib/sdn/cli/simulator.rb', line 7

def lock_priority
  @lock_priority
end

#network_lock_priorityObject

Returns the value of attribute network_lock_priority.



7
8
9
# File 'lib/sdn/cli/simulator.rb', line 7

def network_lock_priority
  @network_lock_priority
end

#node_typeObject

Returns the value of attribute node_type.



7
8
9
# File 'lib/sdn/cli/simulator.rb', line 7

def node_type
  @node_type
end

#position_pulsesObject

Returns the value of attribute position_pulses.



7
8
9
# File 'lib/sdn/cli/simulator.rb', line 7

def position_pulses
  @position_pulses
end

#up_limitObject

Returns the value of attribute up_limit.



7
8
9
# File 'lib/sdn/cli/simulator.rb', line 7

def up_limit
  @up_limit
end

Instance Method Details

#ack(message) ⇒ Object



202
203
204
205
206
# File 'lib/sdn/cli/simulator.rb', line 202

def ack(message)
  return unless message.ack_requested

  respond(Message::Ack.new(message.dest))
end

#nack(message, _error_code = nil) ⇒ Object



208
209
210
211
212
# File 'lib/sdn/cli/simulator.rb', line 208

def nack(message, _error_code = nil)
  return unless message.ack_requested

  respond(Message::Nack.new(message.dest))
end

#processObject



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
# File 'lib/sdn/cli/simulator.rb', line 36

def process
  loop do
    @client.receive do |message|
      SDN.logger.info "Received #{message.inspect}"
      next unless message.is_a?(Message::ILT2::MasterControl) ||
                  message.dest == address ||
                  message.dest == BROADCAST_ADDRESS

      case message
      when Message::GetGroupAddr
        next nack(message) unless (1..16).cover?(message.group_index)

        respond(message.src, Message::PostGroupAddr.new(message.group_index, groups[message.group_index - 1]))
      when Message::GetMotorIP
        next nack(message) unless (1..16).cover?(message.ip)

        respond(message.src,
                Message::PostMotorIP.new(message.ip, ips[message.ip - 1], to_percent(ips[message.ip - 1])))
      when Message::GetMotorLimits
        respond(message.src, Message::PostMotorLimits.new(up_limit, down_limit))
      when Message::GetMotorPosition
        respond(message.src, Message::PostMotorPosition.new(
                               position_pulses,
                               to_percent(position_pulses),
                               ips.index(position_pulses)&.+(1)
                             ))
      when Message::GetNodeAddr then respond(message.src, Message::PostNodeAddr.new)
      when Message::GetNodeLabel then respond(message.src, Message::PostNodeLabel.new(label))
      when Message::ILT2::GetIRConfig then respond(message.src, Message::ILT2::PostIRConfig.new(ir_channels))
      when Message::ILT2::GetLockStatus then respond(message.src,
                                                     Message::ILT2::PostLockStatus.new(lock_priority))
      when Message::ILT2::GetMotorIP
        respond(message.src, Message::ILT2::PostMotorIP.new(message.ip, ips[message.ip - 1]))
      when Message::ILT2::GetMotorPosition
        respond(message.src, Message::ILT2::PostMotorPosition.new(position_pulses, to_percent(position_pulses)))
      when Message::ILT2::GetMotorSettings
        respond(message.src, Message::ILT2::PostMotorSettings.new(down_limit))
      when Message::ILT2::SetIRConfig then self.ir_channels = message.channels
      when Message::ILT2::SetLockStatus then self.lock_priority = message.priority
      when Message::ILT2::SetMotorIP
        next nack(message) unless (1..16).cover?(message.ip)

        ips[message.ip - 1] = message.value
        ack(message)
      when Message::ILT2::SetMotorPosition
        next nack(message) unless down_limit

        self.position_pulses = case message.target_type
                               when :up_limit then 0
                               when :down_limit then down_limit
                               when :ip
                                 next nack(message) unless (1..16).cover?(message.target)
                                 next nack(message) unless ips[message.target]

                                 ips[message.target]
                               when :position_pulses
                                 next nack(message) if message.target - 1 > down_limit

                                 message.target - 1
                               when :jog_up_pulses then [0, position_pulses - message.target].max
                               when :jog_down_pulses then [down_limit, position_pulses + message.target].min
                               when :position_percent
                                 next nack(message) if message.target > 100

                                 to_pulses(message.target.to_f)
                               end
        ack(message)
      when Message::ILT2::SetMotorSettings
        if message.down_limit != 0
          self.down_limit = message.down_limit
          self.position_pulses = message.position_pulses
        end
      when Message::MoveTo
        next nack(message) unless down_limit
        next nack(message) unless ALLOWED_MOVE_TYPES.include?(message.target_type)

        self.position_pulses = case message.target_type
                               when :up_limit then 0
                               when :down_limit then down_limit
                               when :ip
                                 next nack(message) unless (1..16).cover?(message.target)
                                 next nack(message) unless ips[message.target - 1]

                                 ips[message.target - 1]
                               when :position_pulses
                                 next nack(message) if message.target > down_limit

                                 message.target
                               when :position_percent
                                 next nack(message) if message.target > 100

                                 to_pulses(message.target)
                               end
        ack(message)
      when Message::SetGroupAddr
        next nack(message) unless (1..16).cover?(message.group_index)

        groups[message.group_index - 1] = (message.group_address == [0, 0, 0]) ? nil : message.group_address
        ack(message)
      when Message::SetMotorIP
        next nack(message) unless (1..16).cover?(message.ip) || message.type == :distribute

        case message.type
        when :delete
          ips[message.ip - 1] = nil
          ack(message)
        when :current_position
          ips[message.ip - 1] = position_pulses
          ack(message)
        when :position_pulses
          ips[message.ip - 1] = message.value
          ack(message)
        when :position_percent
          pulses = to_pulses(message.value)
          if pulses
            ips[message.ip - 1] = pulses
            ack(message)
          else
            nack(message)
          end
        when :distribute
          next nack(message) unless down_limit
          next nack(message) unless (1..15).cover?(message.value)

          span = down_limit / (message.value + 1)
          current = 0
          (0...message.value).each do |ip|
            ips[ip] = (current += span)
          end
          (message.value...16).each do |ip|
            ips[ip] = nil
          end
          ack(message)
        end
      when Message::SetMotorLimits
        next nack(message) unless Message::SetMotorLimits::TARGET.key?(message.target)
        next nack(message) unless message.type == :jog_pulses

        self.up_limit ||= 0
        self.down_limit ||= 0
        self.position_pulses ||= 0

        next nack(message) if message.target == :up && position_pulses != 0
        next nack(message) if message.target == :down && position_pulses != down_limit

        case message.type
        when :jog_pulses
          self.down_limit += message.value
          self.position_pulses += message.value if message.target == :down
        end
        ack(message)
      when Message::SetNodeLabel then self.label = message.label
                                      ack(message)
      end
    end
  end
end

#respond(dest, message) ⇒ Object



214
215
216
217
218
219
# File 'lib/sdn/cli/simulator.rb', line 214

def respond(dest, message)
  message.src = address
  message.node_type = node_type
  message.dest = dest
  @client.send(message)
end

#to_percent(pulses) ⇒ Object



194
195
196
# File 'lib/sdn/cli/simulator.rb', line 194

def to_percent(pulses)
  (pulses && down_limit) ? 100.0 * pulses / down_limit : nil
end

#to_pulses(percent) ⇒ Object



198
199
200
# File 'lib/sdn/cli/simulator.rb', line 198

def to_pulses(percent)
  (percent && down_limit) ? down_limit * percent / 100 : nil
end