lib/plc/emulator/plc_plugins.rb in ladder_drive-0.6.4 vs lib/plc/emulator/plc_plugins.rb in ladder_drive-0.6.5

- old
+ new

@@ -1,7 +1,7 @@ # -# Copyright (c) 2018 ITO SOFT DESIGN Inc. +# Copyright (c) 201 ITO SOFT DESIGN Inc. # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, @@ -23,10 +23,12 @@ dir = Dir.pwd $:.unshift dir unless $:.include? dir require 'active_support' require 'active_support/core_ext' +require 'erb' +require 'plugin_trigger_state' module PlcPlugins #def self.included(klass) # load_plugins @@ -108,10 +110,12 @@ end def initialize plc @config = load_config @plc = plc + @device_states = {} + @trigger_states = {} end def name @name ||= self.class.name.split(":").last.underscore.scan(/(.*)_plugin$/).first.first end @@ -122,33 +126,36 @@ def run_cycle plc return false unless self.plc == plc end + def triggered? trigger_config + state = trigger_state_for trigger_config + state.reset + state.update + state.triggered? + end + + def trigger_state_for trigger_config + @trigger_states[trigger_config.object_id] ||= PluginTriggerState.new(plc, trigger_config) + end + + private def load_config h = {} path = File.join("config", "plugins", "#{name}.yml") if File.exist?(path) - h = YAML.load(File.read(path)) + erb = ERB.new File.read(path) + h = YAML.load(erb.result(binding)) h = JSON.parse(h.to_json, symbolize_names: true) end h end -end end -end -# @deprecated use LadderDrive::Emulator::Plugin class instead of this. -def load_plugin_config name - h = {} - path = File.join("config", "plugins", "#{name}.yml") - if File.exist?(path) - h = YAML.load(File.read(path)) - h = JSON.parse(h.to_json, symbolize_names: true) - end - h +end end