Class: SDN::CLI::Simulator::MockMotor
- Inherits:
-
Object
- Object
- SDN::CLI::Simulator::MockMotor
- Defined in:
- lib/sdn/cli/simulator.rb
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
-
#down_limit ⇒ Object
Returns the value of attribute down_limit.
-
#groups ⇒ Object
Returns the value of attribute groups.
-
#ips ⇒ Object
Returns the value of attribute ips.
-
#ir_channels ⇒ Object
Returns the value of attribute ir_channels.
-
#label ⇒ Object
Returns the value of attribute label.
-
#lock_priority ⇒ Object
Returns the value of attribute lock_priority.
-
#network_lock_priority ⇒ Object
Returns the value of attribute network_lock_priority.
-
#node_type ⇒ Object
Returns the value of attribute node_type.
-
#position_pulses ⇒ Object
Returns the value of attribute position_pulses.
-
#up_limit ⇒ Object
Returns the value of attribute up_limit.
Instance Method Summary collapse
- #ack(message) ⇒ Object
-
#initialize(client) ⇒ MockMotor
constructor
A new instance of MockMotor.
- #nack(message, error_code = nil) ⇒ Object
- #process ⇒ Object
- #respond(dest, message) ⇒ Object
- #to_percent(pulses) ⇒ Object
- #to_pulses(percent) ⇒ Object
Constructor Details
#initialize(client) ⇒ MockMotor
Returns a new instance of MockMotor.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/sdn/cli/simulator.rb', line 7 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
#address ⇒ Object
Returns the value of attribute address.
5 6 7 |
# File 'lib/sdn/cli/simulator.rb', line 5 def address @address end |
#down_limit ⇒ Object
Returns the value of attribute down_limit.
5 6 7 |
# File 'lib/sdn/cli/simulator.rb', line 5 def down_limit @down_limit end |
#groups ⇒ Object
Returns the value of attribute groups.
5 6 7 |
# File 'lib/sdn/cli/simulator.rb', line 5 def groups @groups end |
#ips ⇒ Object
Returns the value of attribute ips.
5 6 7 |
# File 'lib/sdn/cli/simulator.rb', line 5 def ips @ips end |
#ir_channels ⇒ Object
Returns the value of attribute ir_channels.
5 6 7 |
# File 'lib/sdn/cli/simulator.rb', line 5 def ir_channels @ir_channels end |
#label ⇒ Object
Returns the value of attribute label.
5 6 7 |
# File 'lib/sdn/cli/simulator.rb', line 5 def label @label end |
#lock_priority ⇒ Object
Returns the value of attribute lock_priority.
5 6 7 |
# File 'lib/sdn/cli/simulator.rb', line 5 def lock_priority @lock_priority end |
#network_lock_priority ⇒ Object
Returns the value of attribute network_lock_priority.
5 6 7 |
# File 'lib/sdn/cli/simulator.rb', line 5 def network_lock_priority @network_lock_priority end |
#node_type ⇒ Object
Returns the value of attribute node_type.
5 6 7 |
# File 'lib/sdn/cli/simulator.rb', line 5 def node_type @node_type end |
#position_pulses ⇒ Object
Returns the value of attribute position_pulses.
5 6 7 |
# File 'lib/sdn/cli/simulator.rb', line 5 def position_pulses @position_pulses end |
#up_limit ⇒ Object
Returns the value of attribute up_limit.
5 6 7 |
# File 'lib/sdn/cli/simulator.rb', line 5 def up_limit @up_limit end |
Instance Method Details
#ack(message) ⇒ Object
167 168 169 170 |
# File 'lib/sdn/cli/simulator.rb', line 167 def ack() return unless .ack_requested respond(Message::Ack.new(.dest)) end |
#nack(message, error_code = nil) ⇒ Object
172 173 174 175 |
# File 'lib/sdn/cli/simulator.rb', line 172 def nack(, error_code = nil) return unless .ack_requested respond(Message::Nack.new(.dest)) end |
#process ⇒ Object
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 |
# File 'lib/sdn/cli/simulator.rb', line 18 def process loop do @client.receive do || SDN.logger.info "Received #{.inspect}" next unless .is_a?(Message::ILT2::MasterControl) || .dest == address || .dest == BROADCAST_ADDRESS case when Message::GetGroupAddr next nack() unless (1..16).include?(.group_index) respond(.src, Message::PostGroupAddr.new(.group_index, groups[.group_index - 1])) when Message::GetMotorIP next nack() unless (1..16).include?(.ip) respond(.src, Message::PostMotorIP.new(.ip, ips[.ip - 1], to_percent(ips[.ip - 1]))) when Message::GetMotorLimits respond(.src, Message::PostMotorLimits.new(up_limit, down_limit)) when Message::GetMotorPosition respond(.src, Message::PostMotorPosition.new( position_pulses, to_percent(position_pulses), ips.index(position_pulses)&.+(1) )) when Message::GetNodeAddr; respond(.src, Message::PostNodeAddr.new) when Message::GetNodeLabel; respond(.src, Message::PostNodeLabel.new(label)) when Message::ILT2::GetIRConfig; respond(.src, Message::ILT2::PostIRConfig.new(ir_channels)) when Message::ILT2::GetLockStatus; respond(.src, Message::ILT2::PostLockStatus.new(lock_priority)) when Message::ILT2::GetMotorIP; respond(.src, Message::ILT2::PostMotorIP.new(.ip, ips[.ip - 1])) when Message::ILT2::GetMotorPosition; respond(.src, Message::ILT2::PostMotorPosition.new(position_pulses, to_percent(position_pulses))) when Message::ILT2::GetMotorSettings; respond(.src, Message::ILT2::PostMotorSettings.new(down_limit)) when Message::ILT2::SetIRConfig; self.ir_channels = .channels when Message::ILT2::SetLockStatus; self.lock_priority = .priority when Message::ILT2::SetMotorIP next nack() unless (1..16).include?(.ip) ips[.ip - 1] = .value ack() when Message::ILT2::SetMotorPosition next nack() unless down_limit self.position_pulses = case .target_type when :up_limit; 0 when :down_limit; down_limit when :ip next nack() unless (1..16).include?(.target) next nack() unless ips[.target] ips[.target] when :position_pulses next nack() if .target - 1 > down_limit .target - 1 when :jog_up_pulses; [0, position_pulses - .target].max when :jog_down_pulses; [down_limit, position_pulses + .target].min when :position_percent next nack() if .target > 100 to_pulses(.target.to_f) end ack() when Message::ILT2::SetMotorSettings if .down_limit != 0 self.down_limit = .down_limit self.position_pulses = .position_pulses end when Message::MoveTo next nack() unless down_limit next nack() unless %I{up_limit down_limit ip position_pulses position_percent}.include?(.target_type) self.position_pulses = case .target_type when :up_limit; 0 when :down_limit; down_limit; when :ip next nack() unless (1..16).include?(.target) next nack() unless ips[.target - 1] ips[.target - 1] when :position_pulses next nack() if .target > down_limit .target when :position_percent next nack() if .target > 100 to_pulses(.target) end ack() when Message::SetGroupAddr next nack() unless (1..16).include?(.group_index) groups[.group_index - 1] = .group_address == [0, 0, 0] ? nil : .group_address ack() when Message::SetMotorIP next nack() unless (1..16).include?(.ip) || .type == :distribute case .type when :delete ips[.ip - 1] = nil ack() when :current_position ips[.ip - 1] = position_pulses ack() when :position_pulses ips[.ip - 1] = .value ack() when :position_percent pulses = to_pulses(.value) if pulses ips[.ip - 1] = pulses ack() else nack() end when :distribute next nack() unless down_limit next nack() unless (1..15).include?(.value) span = down_limit / (.value + 1) current = 0 (0....value).each do |ip| ips[ip] = (current += span) end (.value...16).each do |ip| ips[ip] = nil end ack() end when Message::SetMotorLimits next nack() unless [:up, :down].include?(.target) next nack() unless [:jog_pulses].include?(.type) self.up_limit ||= 0 self.down_limit ||= 0 self.position_pulses ||= 0 next nack() if .target == :up && position_pulses != 0 next nack() if .target == :down && position_pulses != down_limit case .type when :jog_pulses self.down_limit += .value self.position_pulses += .value if .target == :down end ack() when Message::SetNodeLabel; self.label = .label; ack() end end end end |
#respond(dest, message) ⇒ Object
177 178 179 180 181 182 183 |
# File 'lib/sdn/cli/simulator.rb', line 177 def respond(dest, ) .src = address .node_type = node_type .dest = dest SDN.logger.info "Sending #{.inspect}" @client.send() end |
#to_percent(pulses) ⇒ Object
159 160 161 |
# File 'lib/sdn/cli/simulator.rb', line 159 def to_percent(pulses) pulses && down_limit ? 100.0 * pulses / down_limit : nil end |
#to_pulses(percent) ⇒ Object
163 164 165 |
# File 'lib/sdn/cli/simulator.rb', line 163 def to_pulses(percent) percent && down_limit ? down_limit * percent / 100 : nil end |