Class: LadderDrive::Emulator::PluginTriggerState

Inherits:
Object
  • Object
show all
Defined in:
lib/plc/emulator/plugin_trigger_state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#configObject (readonly)

Returns the value of attribute config.



37
38
39
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 37

def config
  @config
end

#deviceObject (readonly)

Returns the value of attribute device.



38
39
40
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 38

def device
  @device
end

#plcObject (readonly)

Returns the value of attribute plc.



36
37
38
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 36

def plc
  @plc
end

#valueObject

Returns the value of attribute value.



40
41
42
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 40

def value
  @value
end

#value_typeObject (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

Returns:

  • (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

Returns:

  • (Boolean)


75
76
77
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 75

def fallen?
  !!@fallen
end

#keyObject



50
51
52
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 50

def key
  object_id
end

#raised?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 71

def raised?
  !!@raised
end

#resetObject



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

Returns:

  • (Boolean)


79
80
81
# File 'lib/plc/emulator/plugin_trigger_state.rb', line 79

def triggered?
  !!@triggered
end

#updateObject



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