Logic Machine Forum
Get object name - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Get object name (/showthread.php?tid=3403)



Get object name - tomnord - 04.06.2021

Hi.
When using a event based script, I want to use event.getvalue() to read the actual value, but I also want to read the object name that triggers the script. 
I  will use this name to read other GA's using grp.find().


Code:
pseudo:


value = event.getvalue()
object = "getObjectName"


alarm_lim = grp.find(object+"_lim")
alarm_tag = grp.find(object+"_alarm")







if value > alarm_lim then
alarm == 1
else

alarm == 0
end



grp.write('alarm_tag',alarm)



RE: Get object name - admin - 04.06.2021

Like this:
Code:
name = grp.find(event.dst).name



RE: Get object name - tomnord - 04.06.2021

(04.06.2021, 08:42)admin Wrote: Like this:
Code:
name = grp.find(event.dst).name

Thank you. 
Any quick tip on how to replace characteres in a string? eg: "AA2233_PV" needs to be changed to "AA2233_Alarm"

Found a way, string.gsub()

How will the os.sleep() work in a event script? if timer is started, will it be reset on next event change? or will the script pause untill the first sleep command is finished?


RE: Get object name - admin - 04.06.2021

Each event script instance is completely separate from others. So if you have sleep calls there it's possible to have multiple instances running in parallel which can lead to unwanted behavior. What kind of script are you creating? Maybe there's a better solution but you need to give a more detailed explanation.


RE: Get object name - tomnord - 04.06.2021

Ah, I'm trying to make a script that uses a temperature value and checks if this value is under or over a setpoint that is read from another GA.
If over or under then set alarm GA to true. else false. And I want a delay on sending that is adjustable so that any minor fluctuations are eliminated.

my code so far:
Code:
value = event.getvalue()
name = grp.find(event.dst).name
--
--
alarm_Hlim  = string.gsub(name, '_PV', '_HAL')
alarm_Htag = string.gsub(name, '_PV', '_AHL')
alarm_Llim  = string.gsub(name, '_PV', '_LAL')
alarm_Ltag = string.gsub(name, '_PV', '_ALL')
alarm_delay = string.gsub(name, '_PV', '_AD')
--
--
--High Alarm
if value > alarm_Hlim then
  hAlarm == true
else
  hAlarm == false
end
--
--
--Low Alarm
if value < alarm_Llim then
  lAlarm == true
else
  lAlarm == false
end


--Some delay
grp.write('alarm_Htag',hAlarm)
grp.write('alarm_Ltag',lAlarm)

The thing is, that I have 2-300 values to check ?


RE: Get object name - admin - 04.06.2021

For multiple objects you can use a tag. This way you can use a single script for all objects.
Using os.sleep in this case won't harm but it's possible to overload the system by having a lot of event script processes.

The more correct approach is to have an event script that sets/resets the last time that alarm was triggered (via storage) and a resident script which checks if the alarm has been set for a certain period then writes the alarm value to an object.


RE: Get object name - tomnord - 04.06.2021

(04.06.2021, 12:42)admin Wrote: For multiple objects you can use a tag. This way you can use a single script for all objects.
Using os.sleep in this case won't harm but it's possible to overload the system by having a lot of event script processes.

The more correct approach is to have an event script that sets/resets the last time that alarm was triggered (via storage) and a resident script which checks if the alarm has been set for a certain period then writes the alarm value to an object.

Could you give a pseudo example to set me off in the right direction?


RE: Get object name - admin - 04.06.2021

Event script (attached to a tag):
Code:
value = event.getvalue()
name = grp.find(event.dst).name

now = os.time()

h_key = 'h_alarm'
l_key = 'l_alarm'

h_lim_obj  = string.gsub(name, '_PV', '_HAL')
l_lim_obj  = string.gsub(name, '_PV', '_LAL')

alarm_h_obj = string.gsub(name, '_PV', '_AHL')
alarm_l_obj = string.gsub(name, '_PV', '_ALL')

h_alarm = value > grp.getvalue(h_lim_obj)
l_alarm = value < grp.getvalue(l_lim_obj)

if h_alarm then
  storage.exec('hsetnx', h_key, name, now)
else
  storage.exec('hdel', h_key, name)
  grp.checkwrite(alarm_h_obj, false)
end

if l_alarm then
  storage.exec('hsetnx', l_key, name, now)
else
  storage.exec('hdel', l_key, name)
  grp.checkwrite(alarm_l_obj, false)
end

Resident script. Adjust the sleep time as needed (at least 5 seconds) depending on the average delay between objects value being detected as out of bounds and the alarm object change to 1.
Code:
if not check then
  storage.openconn()

  check = function(storage_key, alarm_obj_key)
    local now = os.time()
    local items = storage.exec('hgetall', storage_key)

    for name, time in pairs(items) do
      local delay_obj = string.gsub(name, '_PV', '_AD')
      time = time + grp.getvalue(delay_obj)

      if now >= time then
        local alarm_obj = string.gsub(name, '_PV', alarm_obj_key)
        grp.checkwrite(alarm_obj, true)
      end
    end
  end
end

check('h_alarm', '_AHL')
check('l_alarm', '_ALL')