Script support

If you have a question or need help, this is the place to be.

Script support

Postby krambriw » Mon Aug 20, 2007 9:58 am

Lets say I have a main script. Can I call functions in other scripts (to avoid having a huge main script...)?

Walter
krambriw
Plugin Developer
 
Posts: 1144
Joined: Sat Jun 30, 2007 2:51 pm

Re: Script support

Postby Bitmonster » Mon Aug 20, 2007 12:29 pm

To use functions/methods/classes/whatever in other scripts you have to export them through eg.globals.

First script (in Autostart for example):
Code: Select all
def TestFunc():
    print "Hello world!"

eg.globals.TestFunc = TestFunc


Second script:
Code: Select all
eg.globals.TestFunc()


or you can then even use a PythonCommand with "TestFunc()" as statement.
Please post software-related questions in the forum - PMs will only be answered, if really private, thanks!
User avatar
Bitmonster
Site Admin
 
Posts: 2239
Joined: Mon Feb 06, 2006 10:28 pm

Re: Script support

Postby krambriw » Mon Aug 20, 2007 6:37 pm

Thanks a lot,

My script for the SunSet/SunRise is slowly becoming real...some +1500 lines...

It compiles/runs and does more or less what I want now :D

At the moment when I'm testing, I have created a simple while loop with a time.sleep(x)

my script structure:
import ...
user settings (many :mrgreen: )
initial variable settings
def functions....
.
.
.
Code: Select all
ind = 1
while ind < 5:
    SunUpDown() #(my main function)
    remain = int(time.strftime("%S", time.localtime()))
    time.sleep(60-remain)


But is there a way to avoid that the whole eg also sleeps during the sleep period?
Thread???

Best regards, Walter
krambriw
Plugin Developer
 
Posts: 1144
Joined: Sat Jun 30, 2007 2:51 pm

Re: Script support

Postby Bartman » Mon Aug 20, 2007 8:10 pm

krambriw wrote:But is there a way to avoid that the whole eg also sleeps during the sleep period?
Thread???
An additional thread would to. You could also trigger an event aftter x seconds with thr TriggerEvent action class, or use the newly introduced scheduler.
Bartman
Plugin Developer
 
Posts: 881
Joined: Sun Feb 12, 2006 9:03 am

Re: Script support

Postby krambriw » Wed Aug 22, 2007 9:40 am

Hello, please have a look (and test) this simple script. I have a problem that events triggered from scripts that then enters into a sleep i suspect
- execution might be delayed until the sleep ends
- or its only the logging of it that is delayed

In my sample I have added control of the sound muting and I see that it seems to work fine so it might be a logging problem

I'm still with the 1102 build

....same with 1103

Best regards, Walter

Code: Select all
import time

try:
    i
except NameError:
    i = 0

def MyFunc():
    if i == 0:
        print "ok to print"


def MyTestFunc(myArgument):
    print "MyTestFunc was called with:", repr(myArgument)
    eg.TriggerEvent("egEvent_OFF")

ind = 1
while ind < 3:
    MyFunc()
    i = 1
    print ind
    eg.plugins.System.MuteOn()
#    eg.plugins.TellStick.TurnOn(2)
    eg.TriggerEvent("egEvent_ON")
    eg.scheduler.AddTask(5.0, MyTestFunc, "Setting device off")
    remain = int(time.strftime("%S", time.localtime()))
    print 60-remain
    time.sleep(60-remain)
    eg.plugins.System.MuteOff()
    ind += 1
    time.sleep(5)
   
eg.StopMacro()
krambriw
Plugin Developer
 
Posts: 1144
Joined: Sat Jun 30, 2007 2:51 pm

Re: Script support

Postby krambriw » Thu Aug 23, 2007 9:30 am

Just a thought;

I'm using time.sleep() but maybe we would need a kind of eg.delay() function that gives cpu time to other processes during the sleep interval. I do not know "how to" in Python.

In C++ it would look like the below

You call the Delay(milliseconds)

It breaks it down into small sleep segments and releases the cpu to other processes using the SafeYield function

Also found this in the meantime ;)
http://pyds.muensterland.org/stories/23.html

Best regards, Walter

Code: Select all
//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
void myApp::Delay(int ms)
{
   bDelay = true;
   parent->SetTimer(TIMER_DELAY, ms, 0);
   while (bDelay){
      Sleep(10);
      SafeYield();
   }
}


//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
void myApp::SafeYield()
{
   tagMSG    Msg;
   while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)){
      TranslateMessage(&Msg);
      DispatchMessage(&Msg);
   }
}


//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
void myApp::OnTimer(UINT nIDEvent)
{
   switch(nIDEvent)
   {
   case TIMER_DELAY: //Delay timer
      KillTimer(nIDEvent);
      if(workObj)
         workObj->bDelay = false;
      break;

.
.
.
.



krambriw
Plugin Developer
 
Posts: 1144
Joined: Sat Jun 30, 2007 2:51 pm

Re: Script support

Postby Bitmonster » Thu Aug 23, 2007 2:13 pm

I haven't got the point, why you need/want to loop at all?

With eg.scheduler you can simply delay the execution of your code till the wanted time, without blocking.
Please post software-related questions in the forum - PMs will only be answered, if really private, thanks!
User avatar
Bitmonster
Site Admin
 
Posts: 2239
Joined: Mon Feb 06, 2006 10:28 pm

Re: Script support

Postby krambriw » Thu Aug 23, 2007 3:03 pm

How can I do that?

I have a loop calculates everything and check against variables. The script also has counters that I need to keep alive.

Do you mean

1. My script shall execute without loop
2. The scheduler or timer decides when
3. If I need counters to stay alive I make them global with eg.globals....?

(when is eg.globals destroyed?)


Thanks, Walter
krambriw
Plugin Developer
 
Posts: 1144
Joined: Sat Jun 30, 2007 2:51 pm

Re: Script support

Postby Bitmonster » Thu Aug 23, 2007 3:30 pm

If I get it right, you simply want to do something at a calculated time.

So why don't you:
1. Calculate the times that are interesting (eg. sunset/sunrise) with an Autostart script.
2. Schedule function calls with these times to functions inside your script.
3. If a function is called it checks what it might have to check also, calculate and schedule the next times it needs to be called (eg. the next day) and triggers an event so you can do something else with the result (eg. turn on the lights).

Pseudo-Script-Code:
Code: Select all
def MySunsetFunc():
    if EverythingIsAlright():
        eg.TriggerEvent("SunsetLightOn")
    nextTime = CalculateNextSunsetFromNow()
    eg.scheduler.AddTaskAbsolute(nextTime, MySunsetFunc)

nextTime = CalculateNextSunsetFromNow()
eg.scheduler.AddTaskAbsolute(nextTime, MySunsetFunc)

No loop, not more than one script.

The script is only executed once (at startup of EG), but it "lives" as long as it isn't re-edited or deleted. All functions inside can be called in the future with eg.scheduler, because it stays alive after it was executed once. (Technically speaking: the name-space of a script is not destroyed after it is executed).

eg.globals lives as long as the EXE is running.
Please post software-related questions in the forum - PMs will only be answered, if really private, thanks!
User avatar
Bitmonster
Site Admin
 
Posts: 2239
Joined: Mon Feb 06, 2006 10:28 pm

Re: Script support

Postby krambriw » Fri Aug 24, 2007 11:01 am

Dear Bitmonster,

My script runs good now, thanks, but currently I'm using a timer to trig an event that starts the script every minute.

I would like to schedule it from the script itself using the scheduler and that works ok also but I have still one little problem left;

I would like to initialize (re-read) some variables each time the script runs. I put them all in function that I also could call but then I would have to declare them as eg.globals. Can I do that in a simpler way thus avoiding declaring them as eg.globals?

Reason for this is that I would like to have possibility to change/edit variable values while the script is running. If I use the timer, the script is restarted after every edit.

With the scheduler I get

13:01:09 AttributeError: 'NoneType' object has no attribute 'globals'

when I apply the changes



Best regards, Walter
krambriw
Plugin Developer
 
Posts: 1144
Joined: Sat Jun 30, 2007 2:51 pm


Return to General Support

Who is online

Users browsing this forum: No registered users and 2 guests