Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details
If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.
If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.
Script support
-
krambriw
- Plugin Developer
- Posts: 2570
- Joined: Sat Jun 30, 2007 2:51 pm
- Location: Stockholm, Sweden
- Contact:
Script support
Lets say I have a main script. Can I call functions in other scripts (to avoid having a huge main script...)?
Walter
Walter
My released plugins
https://drive.google.com/drive/folders/ ... y01eVBKeHM
https://drive.google.com/drive/folders/ ... y01eVBKeHM
- Bitmonster
- Site Admin
- Posts: 2239
- Joined: Mon Feb 06, 2006 10:28 pm
Re: Script support
To use functions/methods/classes/whatever in other scripts you have to export them through eg.globals.
First script (in Autostart for example):
Second script:
or you can then even use a PythonCommand with "TestFunc()" as statement.
First script (in Autostart for example):
Code: Select all
def TestFunc():
print "Hello world!"
eg.globals.TestFunc = TestFunc
Code: Select all
eg.globals.TestFunc()
Please post software-related questions in the forum - PMs will only be answered, if really private, thanks!
-
krambriw
- Plugin Developer
- Posts: 2570
- Joined: Sat Jun 30, 2007 2:51 pm
- Location: Stockholm, Sweden
- Contact:
Re: Script support
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
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
)
initial variable settings
def functions....
.
.
.
But is there a way to avoid that the whole eg also sleeps during the sleep period?
Thread???
Best regards, Walter
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
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
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)
Thread???
Best regards, Walter
My released plugins
https://drive.google.com/drive/folders/ ... y01eVBKeHM
https://drive.google.com/drive/folders/ ... y01eVBKeHM
Re: Script support
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.krambriw wrote:But is there a way to avoid that the whole eg also sleeps during the sleep period?
Thread???
-
krambriw
- Plugin Developer
- Posts: 2570
- Joined: Sat Jun 30, 2007 2:51 pm
- Location: Stockholm, Sweden
- Contact:
Re: Script support
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
- 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()
My released plugins
https://drive.google.com/drive/folders/ ... y01eVBKeHM
https://drive.google.com/drive/folders/ ... y01eVBKeHM
-
krambriw
- Plugin Developer
- Posts: 2570
- Joined: Sat Jun 30, 2007 2:51 pm
- Location: Stockholm, Sweden
- Contact:
Re: Script support
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
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;
.
.
.
.
My released plugins
https://drive.google.com/drive/folders/ ... y01eVBKeHM
https://drive.google.com/drive/folders/ ... y01eVBKeHM
- Bitmonster
- Site Admin
- Posts: 2239
- Joined: Mon Feb 06, 2006 10:28 pm
Re: Script support
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.
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!
-
krambriw
- Plugin Developer
- Posts: 2570
- Joined: Sat Jun 30, 2007 2:51 pm
- Location: Stockholm, Sweden
- Contact:
Re: Script support
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
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
My released plugins
https://drive.google.com/drive/folders/ ... y01eVBKeHM
https://drive.google.com/drive/folders/ ... y01eVBKeHM
- Bitmonster
- Site Admin
- Posts: 2239
- Joined: Mon Feb 06, 2006 10:28 pm
Re: Script support
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:
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.
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)
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!
-
krambriw
- Plugin Developer
- Posts: 2570
- Joined: Sat Jun 30, 2007 2:51 pm
- Location: Stockholm, Sweden
- Contact:
Re: Script support
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
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
My released plugins
https://drive.google.com/drive/folders/ ... y01eVBKeHM
https://drive.google.com/drive/folders/ ... y01eVBKeHM