Class: LadderDrive::Emulator::PluginTriggerState
- Inherits:
-
Object
- Object
- LadderDrive::Emulator::PluginTriggerState
- Defined in:
- lib/plc/emulator/plugin_trigger_state.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#device ⇒ Object
readonly
Returns the value of attribute device.
-
#plc ⇒ Object
readonly
Returns the value of attribute plc.
-
#value ⇒ Object
Returns the value of attribute value.
-
#value_type ⇒ Object
readonly
Returns the value of attribute value_type.
Instance Method Summary collapse
- #changed? ⇒ Boolean
- #device_with_trigger(trigger) ⇒ Object
- #fallen? ⇒ Boolean
-
#initialize(plc, config) ⇒ PluginTriggerState
constructor
A new instance of PluginTriggerState.
- #key ⇒ Object
- #raised? ⇒ Boolean
- #reset ⇒ Object
- #triggered? ⇒ Boolean
- #update ⇒ Object
Constructor Details
#initialize(plc, config) ⇒ PluginTriggerState
Returns a new instance of PluginTriggerState.
42 43 44 45 46 47 48 |
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 42 def initialize plc, config @plc = plc @config = config @value_type = config[:value_type] @devices = {} @next_trigger_times = {} end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
37 38 39 |
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 37 def config @config end |
#device ⇒ Object (readonly)
Returns the value of attribute device.
38 39 40 |
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 38 def device @device end |
#plc ⇒ Object (readonly)
Returns the value of attribute plc.
36 37 38 |
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 36 def plc @plc end |
#value ⇒ Object
Returns the value of attribute value.
40 41 42 |
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 40 def value @value end |
#value_type ⇒ Object (readonly)
Returns the value of attribute value_type.
39 40 41 |
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 39 def value_type @value_type end |
Instance Method Details
#changed? ⇒ Boolean
67 68 69 |
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 67 def changed? !!@changed end |
#device_with_trigger(trigger) ⇒ Object
145 146 147 |
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 145 def device_with_trigger trigger @devices[trigger.object_id] ||= @plc.device_by_name(trigger[:device]) end |
#fallen? ⇒ Boolean
75 76 77 |
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 75 def fallen? !!@fallen end |
#key ⇒ Object
50 51 52 |
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 50 def key object_id end |
#raised? ⇒ Boolean
71 72 73 |
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 71 def raised? !!@raised end |
#reset ⇒ Object
138 139 140 141 142 143 |
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 138 def reset @changed = nil @raised = nil @fallen = nil @triggered = nil end |
#triggered? ⇒ Boolean
79 80 81 |
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 79 def triggered? !!@triggered end |
#update ⇒ Object
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 |
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 87 def update return unless @triggered.nil? triggers = config[:triggers] triggers ||= [config[:trigger]] @triggered = false triggers.each do |trigger| case trigger[:type] when "changed", "raise", "fall", "raise_and_fall" device = device_with_trigger(trigger) value = device.send(@value_type || :value) # update flags @changed = @value != value case value when true, false, nil @raised = @changed && !!value @fallen = @changed && !value else @fallen = @changed && value == 0 @raised = @changed && !@fallen end @value = value # judgement triggered case trigger[:type] when "raise" @triggered = true if @raised when "fall" @triggered = true if @fallen else @triggered = true if @changed end when "interval" now = Time.now t = @next_trigger_times[trigger.object_id] || now while now >= t @triggered ||= true t += trigger[:interval] end @next_trigger_times[trigger.object_id] = t else @triggered = false end end @triggered end |