PDA

View Full Version : Reset Scripting Engine from within a Lua script?



FearTheDentist
February 27th, 2008, 09:41 PM
Is there a way to do it? The reason I ask is every now and the G5 drops communication with my Denon AVR3808-CI receiver or my Pioneer 607CMX Plasma TV. When it does it takes a reset of the scripting engine to reconnect. As these are the central fixtures in my setup, this creates a significant issue. The plugin for the receiver polls it every 60 sec for status. I'd like to add code so that if a communication error is encountered when the unit is polled the scripting engine is reset. Obviously we'd want to add a counter so if it didn't re-establish communications after, say, 5 attempts it would kick out an error and stop trying. Here is the polling code:



StartPollingState = function (self)

self.PollingStateTimer = gir.CreateTimer (
nil,
function ()
self:PowerState ()
end,
nil,
1
)
self.PollingStateTimer:Arm (60000)
end,


any ideas how to go about this? If there was a reset scripting engine action in G5 I could do this in the .gml with a string.find script looking for comm. errors, but I really think this would be more suitably placed in the plugin.

Rob H
February 28th, 2008, 02:24 AM
As far as I know there is no action to reset the scripting engine.

However, you shouldn't really need to do this - you could instead close and reopen the serial connection.

FearTheDentist
February 28th, 2008, 08:21 PM
That sounds like a much better way to do it. Unfortunately I have little idea how to go about it. I've been reading the help file on the serial library, unfortunately I'm somewhat lacking in the fundamentals and have little idea where to start.

From the serial library, I'm guessing this is some of what I want to use:


[Port], [err] = serial.Open([comport])
[res], [err] = [Port]:Close()


I'm a little confused on the structure. Is the first line defining "ComX" as the command to open that com port eg:
Com5 = serial.Open(5)

Then, if I call "Com5" later, it tells G5 to open Com 5?

I'm thinking I'm wrong though, because the "close" part is structured differently.- I'd think if my reasoning were correct, the structure of the second line of code above would be the same as the first.

Without taking your time for a whole Lua tutorial, can you help me get started on this?

Marquis
February 29th, 2008, 06:15 AM
However,

I still have the need to to this feature also as you can see here. Hope it is still on the feature list:rolleyes:

http://www.promixis.com/forums/showthread.php?t=17330

Kind regards,
Marc

Rob H
February 29th, 2008, 07:29 AM
There is a way to do this using the Girder Watchdog (which you will find in the Girder directory).

With the watchdog running you should be able to effect a reset with a Lua scripting action as follows.


while true do
end

Note that you will lose any unsaved changes to your GMLs if you use this.

FearTheDentist
February 29th, 2008, 11:36 PM
Well, progress...I think I was making it far more complicated than it needs to be.

Straight from init.lua:


Reset = function (self)
self.Serial:Close ()
self:Open ()
end,


So, add this to your serial device definition and create a "Reset" action in G5 and now you can trigger it by the event of your choice.

my thought was rather than an event, watch for comm. error in the lua console but I can't figure out the syntax. My thought was something like this:


ResetComPort = function(self, data)
if string.find(err,"No response to last command") then
self.Serial:Close ()
self:Open ()
gir.LogMessage(self.Name, "Reset Com " .. (self.ComPort),3)
end
end


Maybe add a counter so you need X consecutive errors to trigger a reset.


ResetComPort = function(self, data)
local ComState = 0
if string.find(err,"No response to last command") do
ComState = ComState + 1
end
if ComState = 5 then
self.Serial:Close ()
self:Open ()
gir.LogMessage(self.Name, "Reset Com " .. (self.ComPort),3)
end
end


Unfortunately, no variation I've tried has yielded any success. I'm sure I'm butchering the code all over the place, any help would be appreciated!

Rob H
March 1st, 2008, 03:03 AM
You can monitor for errors in your ReceiveResponse method - it depends what sort of errors you're seeing though.

Something like this would do it


if math.band(code, serial.NORESPONSETIMEOUT) > 0 then
self.ErrorCount = (self.ErrorCount or 0) + 1
if self.ErrorCount > 5 then
self.ErrorCount = 0
self:Reset()
end
end

FearTheDentist
March 2nd, 2008, 10:34 PM
Your code works perfectly, thanks!

Is there a way to do the same thing for the connection to my Denon receiver? It's connected via ethernet, and when the system resumes from standby I have to exit and restart G5 to re-establish communications. I think it's the communications server that has to be reset, but haven't figured out a method. I looked in the comserv library, no luck. Suggestions?

Rob H
March 3rd, 2008, 01:29 AM
You could try disabling and enabling the comserver plugin, or even unloading and reloading it. I'm not very keen on that idea though.

FearTheDentist
March 3rd, 2008, 01:10 PM
I imagine this issue could affect more than just Denon receivers, I think I'll punt it over to the bug thread.