PDA

View Full Version : weird issue with addEventHandler



yonu
October 19th, 2015, 07:31 PM
I am not sure what is going on with the addEventHandler I am using but each time an event comes it that matches it multiplies by 2 to the point that it repeated the code 40times any ideas here is my code.



handlerId, err = gir.addEventHandler( ".*", 23, 65000, function( eventString ,eventDevice, keyMod, payloads, captures )


Thanks,
Yonu

Ron
October 20th, 2015, 10:00 AM
can you post the full code? It sounds like you are re-adding the eventhandler on each event. Just add it once. (maybe add a print statement so you can track 'adds').

yonu
October 20th, 2015, 10:54 AM
Here is the full code I have tried it 2 different way witht he end) where is is and with it and the very end of the code also if you use starting as a reference it increases by 2 every run




local tostring = tostring
handlerId, err = gir.addEventHandler( "Media", 23, 65000, function( eventString ,eventDevice, keyMod, payloads, captures )
print("starting")
--[[print("Event: ", eventString, eventDevice, keyMod)
--table.print(payloads)
--print(payloads[1])]]--
end)

if eventString == "Media" then
rawspc = payloads[1]
--print (rawspc)
--[[linespc = (string.gsub(rawspc, "%.", "%\n"))
_, _, pwrstate, pwrpercentage = string.find(rawpwr, "state:%s(.-)%spercentage:%s(.%d%d)")
print("state ", pwrstate)
print("percent ",pwrpercentage)]]--
else
print ("spc")
end

if eventString == "Mediapwr" then
print("bat")
rawpwr = payloads[1]
print (rawpwr)
--print(string.gsub(rawpwr, "%.", "%\n"))
--linepwr = string.gsub(rawpwr, "%.", "%\n")

_, _, pwrstate, pwrpercentage = string.find(rawpwr, "state:%s(.-)%spercentage:%s(.%d%d)")
print("state ", pwrstate)
print("percent ",pwrpercentage)
deviceManager.requestControlValueChange( 165, pwrstate, "lua")
deviceManager.requestControlValueChange( 166, pwrpercentage, "lua")

--[[ test control anme
if control then
--deviceManager.requestControlValueChange( 165,pwrstate, "lua")
print("yes")
else
-- local v = "0"
--deviceManager.requestControlValueChange( control,"0" , "lua")
print("no")
end
--]]
end


Thanks,
Yonu

yonu
October 20th, 2015, 11:13 AM
The problem is fixed after I thought about what you said about adding a print statement so I looked at where I had my print statements for testing and they were reprinting like crazy but the rest of the script wasn't rerunning.

Thanks,
Yonu

Ron
October 20th, 2015, 11:17 AM
Excellent!! Nice work. print is your friend :)

yonu
October 20th, 2015, 11:24 AM
except when they are in the wrong places lol

Yonu