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.

Wait until file transfer is complete?

If you have a question or need help, this is the place to be.
mercraus
Posts: 1
Joined: Fri Sep 03, 2010 5:05 am

Wait until file transfer is complete?

Post by mercraus »

Sorry if this is already in here somewhere, but I couldn't find an answer in the search.

What I'm trying to do:
Launch a program when a new file or folder is added to a directory

How I'm currently doing it:
Using a macro with this tree -
DirectoryWatcher.Updated
Wait 10 sec
Launch Program (don't continue until program closes)
Wait 10 sec

Problem I'm Having:
When multiple files are being transfered all at once, this macro loops repeatedly. I would like to have it wait until all the files have transfered, then launch the program. I was thinking I would do something like start the macro when DirectoryWatcher.Updated triggers, wait 30 seconds, then see if it triggers again. The macro would continue that loop until DirectoryWatcher.Updated stops tiggering, then launch the program. Any suggestions to implement this?

Thanks in advance for the help!
User avatar
jinxdone
Plugin Developer
Posts: 443
Joined: Tue Jan 02, 2007 4:08 pm

Re: Wait until file transfer is complete?

Post by jinxdone »

Try this quick example. copy&paste the following xml into your EG window, it will paste the macros in your tree.

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
<EventGhost Version="1397">
    <Folder Name="Example" Expanded="True">
        <Macro Name="MyDirectory updated" Expanded="True">
            <Action>
                EventGhost.PythonCommand(u'eg.globals.myDirectoryLastUpdated = eg.Utils.time.time()')
            </Action>
            <Action>
                EventGhost.TriggerEvent(u'myDirupdated', 5.0)
            </Action>
        </Macro>
        <Macro Name="Check elapsed time" Expanded="True">
            <Event Name="myDirupdated" />
            <Action>
                EventGhost.PythonCommand(u'if eg.globals.myDirectoryLastUpdated < eg.Utils.time.time() - 5: eg.result = True')
            </Action>
            <Action>
                EventGhost.NewJumpIf(XmlIdLink(170), 0, False)
            </Action>
        </Macro>
        <Macro Name="Run Action" id="170" Expanded="True">
            <Action>
                EventGhost.ShowOSD(u'Enough time has passed since since last udate.. run any actions here', u'0;-24;0;0;0;700;0;0;0;1;0;0;2;32;Arial', (255, 255, 255), (0, 0, 0), 0, (0, 0), 0, 3.0, False)
            </Action>
        </Macro>
    </Folder>
</EventGhost>
So you would use this by triggering the "MyDirectory udpated" macro with your directory watcher updated event. Then you will want to adjust the time on TriggerEvent and the time offset on the check condition "if eg.globals.myDirectoryLastUpdated < eg.Utils.time.time() - 5: eg.result = True". I had the times set to 5 seconds while I tested. Finally put any actions you want to run in the "Run Action" macro.

You could do the same things more neatly in a single Python script, but this way you can use normal EG config tree to run your actions.

Btw, it's important to use "Trigger event" with timer rather than "Wait some time", because the wait action will cause EG to block other macros or events from processing while it's waiting.

/jinxdone
feerlessleadr
Posts: 4
Joined: Thu Apr 02, 2009 9:49 pm

Re: Wait until file transfer is complete?

Post by feerlessleadr »

sorry for bumping such an old thread, but this may help me accomplish exactly what I was looking for.

jinxdone - do you think you could explain the code to me?

Do the two times need to be the same (ie 5 seconds and 5 seconds) or should they be different?
User avatar
jinxdone
Plugin Developer
Posts: 443
Joined: Tue Jan 02, 2007 4:08 pm

Re: Wait until file transfer is complete?

Post by jinxdone »

It's np. Yes, the times should be set to the same value. You should actually use <= (greater than or equal) in the comparison to make sure. The timer is not that precise though, it will never run any earlier than the time specified, but usually some milliseconds later.

When you run the "MyDirectory updated" macro:
- The current time is stored in a variable
- A timer is started to run a second macro after 5 seconds

When the second macro is run (by the timer from the first one):
- It checks if 5 seconds have passed since the time stored in the variable
- If 5 seconds have NOT passed:
* it means the first macro was run since the timer began and the directory has been updated since this timer began
* stop the macro and let the following run to do the action, since this is not the last one
- If 5 seconds have passed, run the third macro, which is the actual things you want to do.

It is a bit of a hack, but might be easier for new users since you can do it using the EG config tree only.

Normally to do the same in a python script you would start a timer, store it in a variable, then if your script runs again check if the timer already exists and if so, move it's runtime to 5 seconds in the future. That way it keeps pushing the timer forwards until it finally runs, because the directory was not updated.
agrowe
Posts: 6
Joined: Tue Dec 28, 2010 7:17 am

Re: Wait until file transfer is complete?

Post by agrowe »

Hi Jinxdone,
I have been using some of your code to try and get the an XMBC auto update to work when a file has finished being transfered. I have been having lots of trouble trying to get it to work. You also mentioned a python script should be able to do the job better. Ive done programming at Uni but havent touched python in years, would it be hard to make?
Are you able to see any errors in my Eventghost layout.
Any help would be greatly appreciated. And sorry in advance if i brought up an old thread.

Regards

NB. the xbmc update has been disabled on purpose while testing, I was just using your OSD command.
Attachments
Eventghost screenshot
Eventghost screenshot
User avatar
jinxdone
Plugin Developer
Posts: 443
Joined: Tue Jan 02, 2007 4:08 pm

Re: Wait until file transfer is complete?

Post by jinxdone »

Hi,

Check the attached picture for what it should look like.. Also, you can copy&paste the earlier piece of XML into your EG tree, it will insert the proper macros and actions.

And no, it shouldn't be difficult to make a better version of this as a python script.
Attachments
dirwatcher_example.jpg
agrowe
Posts: 6
Joined: Tue Dec 28, 2010 7:17 am

Re: Wait until file transfer is complete?

Post by agrowe »

Thanks for the reply,
in the first xml, the python test is:

Code: Select all

if eg.globals.myDirectoryLastUpdated < eg.Utils.time.time() - 5: eg.result = True
but in the second it says it should be
You should actually use <= (greater than or equal) in the comparison to make sure

So should it be <= or >=?

Thanks
User avatar
jinxdone
Plugin Developer
Posts: 443
Joined: Tue Jan 02, 2007 4:08 pm

Re: Wait until file transfer is complete?

Post by jinxdone »

Whoops.. i must have ment less than or equal, operator <= is the one.. In practice it shouldn't make a difference though sincr the timer is not accurate to that extent..
agrowe
Posts: 6
Joined: Tue Dec 28, 2010 7:17 am

Re: Wait until file transfer is complete?

Post by agrowe »

Hi, thanks for the clarification.
I am still having troubles though. It will show the OSD while the file is still copying.
Below is the log file for Eventghost

Code: Select all

12:37:31 PM   DirectoryWatcher.Created (u'D:\\Media\\Movies\\Top Gear - 15x07.avi',)
12:37:31 PM   DirectoryWatcher.Updated (u'D:\\Media\\Movies\\Top Gear - 15x07.avi',)
12:37:31 PM      MyDirectory updated
12:37:31 PM         eg.globals.myDirectoryLastUpdated = eg.Utils.time.time()
12:37:31 PM         Trigger event "myDirupdated" after 5.00 seconds
12:37:36 PM   Main.myDirupdated
12:37:36 PM      Check elapsed Time
12:37:36 PM         if eg.globals.myDirectoryLastUpdated <= eg.Utils.time.time() - 5: eg.result = True
12:37:36 PM         If successful jump to "Run Action"
12:37:36 PM            Run Action
12:37:36 PM               Show OSD: Enough time has passed since since last udate.. run any actions here [File is still being copied]
12:37:39 PM   DirectoryWatcher.Updated (u'D:\\Media\\Movies\\Top Gear - 15x07.avi',)
12:37:39 PM      MyDirectory updated
12:37:39 PM         eg.globals.myDirectoryLastUpdated = eg.Utils.time.time()
12:37:39 PM         Trigger event "myDirupdated" after 5.00 seconds
12:37:39 PM   DirectoryWatcher.Updated (u'D:\\Media\\Movies\\Top Gear - 15x07.avi',)
12:37:39 PM      MyDirectory updated
12:37:39 PM         eg.globals.myDirectoryLastUpdated = eg.Utils.time.time()
12:37:39 PM         Trigger event "myDirupdated" after 5.00 seconds
12:37:39 PM   DirectoryWatcher.Updated (u'D:\\Media\\Movies\\Top Gear - 15x07.avi',)
12:37:39 PM      MyDirectory updated
12:37:39 PM         eg.globals.myDirectoryLastUpdated = eg.Utils.time.time()
12:37:39 PM         Trigger event "myDirupdated" after 5.00 seconds
12:37:39 PM   DirectoryWatcher.Updated (u'D:\\Media\\Movies\\Top Gear - 15x07.avi',)
12:37:39 PM      MyDirectory updated
12:37:39 PM         eg.globals.myDirectoryLastUpdated = eg.Utils.time.time()
12:37:39 PM         Trigger event "myDirupdated" after 5.00 seconds
12:37:44 PM   Main.myDirupdated
12:37:44 PM      Check elapsed Time
12:37:44 PM         if eg.globals.myDirectoryLastUpdated <= eg.Utils.time.time() - 5: eg.result = True
12:37:44 PM         If successful jump to "Run Action"
12:37:44 PM   Main.myDirupdated
12:37:44 PM      Check elapsed Time
12:37:44 PM         if eg.globals.myDirectoryLastUpdated <= eg.Utils.time.time() - 5: eg.result = True
12:37:44 PM         If successful jump to "Run Action"
12:37:44 PM   Main.myDirupdated
12:37:44 PM      Check elapsed Time
12:37:44 PM         if eg.globals.myDirectoryLastUpdated <= eg.Utils.time.time() - 5: eg.result = True
12:37:44 PM         If successful jump to "Run Action"
12:37:44 PM   Main.myDirupdated
12:37:44 PM      Check elapsed Time
12:37:44 PM         if eg.globals.myDirectoryLastUpdated <= eg.Utils.time.time() - 5: eg.result = True
12:37:44 PM         If successful jump to "Run Action"
12:37:44 PM            Run Action
12:37:44 PM               Show OSD: Enough time has passed since since last udate.. run any actions here
The file has now been copied but I only need it to do it once, only after the file has been completely copied.
Ive been looking at it for ages and cant figure it out. Does eventghost have a debugging tool?
One other question, is it possible to make directory watcher skip certain files such as thumbs.db.

Thanks again
User avatar
jinxdone
Plugin Developer
Posts: 443
Joined: Tue Jan 02, 2007 4:08 pm

Re: Wait until file transfer is complete?

Post by jinxdone »

Well, it should work better if you set the two times that are now 5 seconds to something more lax, like 10 minutes or 1 hour.. that way you shouldn't get the false results.

Sure, you could filter filenames. But I think if you want to add these kinds of features to it I think it would be a better idea to start making it as a python script or an EG plugin instead of a bunch of macros in EG's config tree..
piert
Experienced User
Posts: 321
Joined: Tue Jun 14, 2011 2:53 pm

Re: Wait until file transfer is complete?

Post by piert »

When the second macro is run (by the timer from the first one):
- It checks if 5 seconds have passed since the time stored in the variable
- If 5 seconds have NOT passed:
* it means the first macro was run since the timer began and the directory has been updated since this timer began
* stop the macro and let the following run to do the action, since this is not the last one
- If 5 seconds have passed, run the third macro, which is the actual things you want to do.

It is a bit of a hack, but might be easier for new users since you can do it using the EG config tree only.
Ok, I AM using this solution now and it works nicely, BUT it is a bit cumbersome to build it like this if using in several places. It also uses a lot of screen area....
Normally to do the same in a python script you would start a timer, store it in a variable, then if your script runs again check if the timer already exists and if so, move it's runtime to 5 seconds in the future. That way it keeps pushing the timer forwards until it finally runs, because the directory was not updated.
Would anyone be willing to show an example of this? It would be nicer to have this available as a tool for such occasions.

Thanks in advance
Perry
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Wait until file transfer is complete?

Post by Pako »

Please see the topic Directory Watcher.
I think there you will find a solution to your problem.

Pako
piert
Experienced User
Posts: 321
Joined: Tue Jun 14, 2011 2:53 pm

Re: Wait until file transfer is complete?

Post by piert »

Thank you Pako.

However, my problem is much simpler, I think.

An example:

I have several motion sensors that can trigger an alarm sound on my PC.
If somebody (or an animal) is moving in the same place in front of the motion sensor many times, I get many times the sound on my PC.

EDIT: I would like to add that there are multiple different events that can trigger the same action (for instance a door contact, a door bell, etc.).

I would like to use some Python code that, even if there are many Event triggers in a short amount of time, only causes the Macro to trigger in a predictable, slow manner.
For instance, even if there are 100 events within one minute, I want to have the Macro action occur on the first event and then, for instance, again only every 20 seconds. This means I would get only 3 times an alarm sound on my PC.

It seems such a simple problem that I think it must be possible to achieve this with only a few lines of Python Code, but I don't know how.

It is not the world's biggest problem, but it would certainly be a 'nice to have'.

Best regards,
Perry
hellow
Posts: 11
Joined: Tue Sep 27, 2011 4:36 pm

Re: Wait until file transfer is complete?

Post by hellow »

I have to agree with perry on this one.This is exacly what im after. Seems like there are loads of questions on the forum from xbmc users about the same thing over and over. We all want to stop the loops and have "timer" in the events so it doesnt get fired off every DirWatch.Update.

What we all want if a py script to do the job

Any decent programmers that can make one? :P
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Wait until file transfer is complete?

Post by Pako »

piert wrote:For instance, even if there are 100 events within one minute, I want to have the Macro action occur on the first event and then, for instance, again only every 20 seconds. This means I would get only 3 times an alarm sound on my PC.
It seems such a simple problem that I think it must be possible to achieve this with only a few lines of Python Code, but I don't know how.
This task is really not difficult.
Here is a script that does the required work:

Code: Select all

if not "myTimerFlag" in eg.globals.__dict__:
    eg.globals.myTimerFlag = False
    def clearMyTimerFlag():
        eg.globals.myTimerFlag = False
        
if eg.globals.myTimerFlag:
    eg.StopMacro()
eg.globals.myTimerFlag = True
eg.scheduler.AddTask(20.0, clearMyTimerFlag))
You must to place the action "Python Script" with that code to the top of your macro.

Pako
Post Reply