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.

Multithreading, python? Eventghost?

If you have a question or need help, this is the place to be.
scastano
Posts: 10
Joined: Mon May 20, 2013 6:07 am

Multithreading, python? Eventghost?

Post by scastano »

I've got a bunch of python scripts setup to control some PTZ foscam cameras.... it's all linked to my home automation systems.

So, when my ISY-994i detects motion when none of us are home, it automatically uses the CGI interface to the cameras to quickly move the camera and point it toward the area the motion was detected. It does this, then has a 1.5 second delay in it, then immediately runs a loop with the parameter "range(50)" in it to capture 50 jpg's from the camera and save them to disk naming them with the date, time, and a sequence number from 0 - 49..... very basic, but more than enough for what I need. Here is the code:

Code: Select all

print "Capturing 50 images..."
for item in range(50):
    #print item
    image1 = urllib2.urlopen("http://xxx.yyy.zzz.qqq:ppp/cgi-bin/CGIProxy.fcgi?usr=XXX&cmd=snapPicture2")
    increment = str(item)
    filename = sitename + "_" + today.strftime("%y%b%d_%H-%M-%S.%f") + "-" + increment + ".jpg"
    output1 = open(rptFolder + "\\" + filename,'wb')
    output1.write(image1.read())
    output1.close()
print "Captured!"
All of that works beautifully.... I've even got the macro doing some other stuff, emailing me, turning on some lights, playing a sound, etc...

The PROBLEM I have is that, intentionally, capturing the 50 images takes about 20 or so seconds and during that time, EventGhost doesn't do anything else. It runs that single macro, and only one at a time apparently. Even though the person maybe move to another room, trip another detector, flip a switch, etc... that I want to run some other events.....

Is there any way to get EventGhost to just start the image capture than move on right away and process it's other tasks at the same time?
Owel
Posts: 41
Joined: Thu May 31, 2012 5:51 am

Re: Multithreading, python? Eventghost?

Post by Owel »

Good Question, dont know it either!
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Multithreading, python? Eventghost?

Post by krambriw »

Yes, of course it is possible, you have already mentioned the way in your question, you have to use threads.

Basically, I would create a plugin for your 'Surveillance Cameras" and actions that can be defined & created for each camera. Each action you then define should start it's own thread when triggered (the thread will basically run your script code) and once done, terminate the thread.

In this way, per camera, each picture capturing will be ongoing and finish without disturbing other cameras picture capturing or EG's other functions. You will also be able to trigger multiple actions in parallel so that you can capture pictures from multiple cameras at the same time following the intruders movements.

Best, Walter
scastano
Posts: 10
Joined: Mon May 20, 2013 6:07 am

Re: Multithreading, python? Eventghost?

Post by scastano »

Basically, I would create a plugin for your 'Surveillance Cameras" and actions that can be defined & created for each camera. Each action you then define should start it's own thread when triggered (the thread will basically run your script code) and once done, terminate the thread.
Awesome, that's great news!!! First of all, thank's so much for the reply.... this will surely solve a lot of problemss for me, this one has been really hanging me up.

Just a few questions:

1) Does it HAVE to be a plugin? I've never written one before.... can I just make the python script action item I've got now do that?
2) If not, does anyone have any good basic guides out there on plugin writing?? I'm a pretty terrible programmer, but I surely don't mind learning and writing a foscam plugin for everyone to use! (at lease on the cameras I own and can test)
3) Are there any good tips/how-to's on reading responses from the camera API to report back if the command was successful or not?

*SMC
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Multithreading, python? Eventghost?

Post by krambriw »

I will make a first version for you, it is not that hard for me, that you can extend with more features later.

Think about what data you would like to have configurable. I have to suggest:
- nbr_of_images (default 50)
- path_to_camera (default "http://xxx.yyy.zzz.qqq:ppp/cgi-bin/CGIProxy.fcgi?usr=XXX&cmd=snapPicture2")
- sitename (default 'sitename')
- folder_for_images (default 'rptFolder')

Then to your Q's

1) Does it HAVE to be a plugin? I've never written one before.... can I just make the python script action item I've got now do that?
It will be much easier to have and to manage a plugin with proper settings for each camera (even if it is possible start threads from a python script)
2) If not, does anyone have any good basic guides out there on plugin writing?? I'm a pretty terrible programmer, but I surely don't mind learning and writing a foscam plugin for everyone to use! (at lease on the cameras I own and can test)
In the documentation section there is a basic guide how to write a plugin for EG
3) Are there any good tips/how-to's on reading responses from the camera API to report back if the command was successful or not?
If you have a page that shows a successful response, you can always read that and parse the content

Best, Walter
scastano
Posts: 10
Joined: Mon May 20, 2013 6:07 am

Re: Multithreading, python? Eventghost?

Post by scastano »

krambriw wrote:I will make a first version for you, it is not that hard for me, that you can extend with more features later.

Think about what data you would like to have configurable. I have to suggest:
- nbr_of_images (default 50)
- path_to_camera (default "http://xxx.yyy.zzz.qqq:ppp/cgi-bin/CGIProxy.fcgi?usr=XXX&cmd=snapPicture2")
- sitename (default 'sitename')
- folder_for_images (default 'rptFolder')
THANKS!! I will surely pick it up and run with it, and yes there are a ton of options... setting preset positions, number of images, changing resolutions, turning on and off the LEDs for night vision... and the great news is, I've been reading the FOSCAM CGI guide.... everything I need to know is in there along with examples of every command and they work on whole families of cameras, not just the 2 models I have.

We should end up with a pretty nifty plugin here.
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Multithreading, python? Eventghost?

Post by krambriw »

Hi,
Just a first quick one, I think it may work out of the box...cross my fingers
SurveillanceCameras.zip
(2.33 KiB) Downloaded 362 times
Image2.jpg
scastano
Posts: 10
Joined: Mon May 20, 2013 6:07 am

Re: Multithreading, python? Eventghost?

Post by scastano »

HELL YES!!! That's awesome, it's working already......

I was able to pull in the plugin, set it to the right info and run it while other things were happening...... Now what I'd like to do is start building in the options for more "config"

It's a hassle to have to know/update the CGI line everytime, so I'm going to read and figure out how you did the options window and put them into the python script, then I should at least for one camera model/CGI version at a time, ask meaningful questions in the config tab:

1) IP/hostname of the camera
2) port
3) username/pw

Also, give a few more actions, like moving to presets, turning on the IR LEDs, etc.

This is a great start, I'm going to work on it right now! Thanks again!!!

I did test the multi-threading already and it works perfectly, while its capturing I am surely able to process other events.
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Multithreading, python? Eventghost?

Post by krambriw »

Provide me with a link to the type of cameras you are using, this is indeed interesting

Best, Walter
scastano
Posts: 10
Joined: Mon May 20, 2013 6:07 am

Re: Multithreading, python? Eventghost?

Post by scastano »

krambriw wrote:Provide me with a link to the type of cameras you are using, this is indeed interesting

Best, Walter

I'm well on my way into writing out the additional config lines, then from there it should be VERY easy to program out the extra functions. What I'd really like to be able to do is enter the username, password, ports, etc in the plugin configuration, then just be able to pick and choose multiple actions from the list once they're created without having to configure everything each time.

The ones I have on the way right now are: http://foscam.com/prd_view.aspx?id=102

It's the Foscam FI8910w, I have some 8910E's on the way with the main difference being that the "E" series will do power over ethernet so I don't have to run multiple cords up to it.... also, I've tried the 8921, and the 9818. This ones I'm going to use now seem to be a good mix of features and usability. The 8921's that do "HD" via h264 encoding have a horrific time streaming to mobile phones, they don't store presets well, there are a ton of bugs in the web UI and in general are plagued with problems.... the older 9818's are pretty solid, though they have some problems broadcasting over WiFi (which is supposedly resolved now, for most people)... but uses the old LED control, video encoding, etc and results in working, but very washed out images....

The 8910W/E camera use the much new hardware with thr IR-cut filters, better motors, upgraded speaker on on-board mic as well as supporting wireless N on top of the others that only do B/G and the nice part is, they have a fully functional web interface (writen for Foscam by consultants) and uses the same command set as the older cameras.

So while they're not "HD", the get great color, the night vision works very well, it comes with a rock solid CGI API and comes in the Power-over-Ethernet version that I have been looking for.

There's 3 of them on their way to arrive today... so hopefully if I can this plugin basically working, I should be able to have the cameras setup tonight and run them through their paces tomorrow and through the weekend.
scastano
Posts: 10
Joined: Mon May 20, 2013 6:07 am

Re: Multithreading, python? Eventghost?

Post by scastano »

I've been smashing my way through this one.... I've made a bunch of edits, not JUST adding myself to the authors line! :-P hahahaha

Basically they are as follows:

1) I commented out the self.finished.wait line since I wanted to see how QUICKLY I could capture images if one just comes streaming in right after the other.
2) I added the individual config options so you don't have to paste in or even look at the CGI code. (This will limit the script to models using this version of the CGI API, currently FOSCAM v1.0.2)
3) I had to create individual variables to "assemble" the correct URL to hit
4) I cleaned up some of the default values that show up in the config screen.

ToDo List:
1) Create the structure I need to better sort the snapshots that come out. Currently, windows gets REALLY bogged down when you throw a ton of files in the same directory, which surely will happen if you leave this running for any length of time. So in my original script I had it set to use the path entered as the "root" path, then create sub folders for each "sitename" and date, so you'd wind up with a folder like C:\MyCameraPictures\sitename\23May13\CameraName-Time-01.jpg and in thinking about it, to be able to relate multiple images together, I think I want to change this to have the folder name the same, but to change the image name so it's TIME-PHOTOINDEXNUMBER-CAMERANAME.jpg so when they're all in the same folder for the day, you can follow the images in chronological order.... this will make it much easier if a person were to enter your home and move from one room to another.

2) I'd like to put the "self.finished.wait" line back in, but only as an option, so people can control how many images they capture per second if they DO want it slower.

3) I did notice something about the "multi-threading" stuff.... if it's running a "TakeSnapShots" action, and ANOTHER "TakeSnapShots" action starts in the same macro, it only seems to capture images for the new thread... the old one seems to be running, but I don't see any images in the folder anymore. i.e. I clicked the play button and started a capture, right at about image 13, I clicked the play button again and 13 was the last one I got... but a new series of 50 started. I'd like to work that out somehow... either it records both at the same time, waits for one to finish, or the goal would be... capture images constantly until no motion is sensed for 30 seconds.

4) Move cam info and login info to central variables so I can program other actions that use the same credentials rather than type them in over and over again.

5) Create a radio button/dropdown box or something of the sort to allow you to pick which model camera this is and set the URLs needed for the API accordingly.

I'm sure I'll come up with some more as I move along, but that's what's out there now.
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Multithreading, python? Eventghost?

Post by krambriw »

Not a bad start for day one!!!
scastano
Posts: 10
Joined: Mon May 20, 2013 6:07 am

Re: Multithreading, python? Eventghost?

Post by scastano »

krambriw wrote:Not a bad start for day one!!!
Thanks, I'm getting there!! I just created an SVN repo for the project also... since trying to make backups and such has been making me crazy, you can see it at: https://code.google.com/p/eventghost-fo ... _init__.py

It's version controlled, downloadable, etc.... and I'll keep working from there.
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Multithreading, python? Eventghost?

Post by krambriw »

Really nice!

This exercise shows in a very good way the power behind python and EventGhost. I have used it for years and it has been really reliable and stable.

You mentioned home automation. A couple of weeks ago I just finished another nice plugin for EG and for Z-Wave support. So far only for my private usage since interest seemed not to be that huge in her, but it is amazing how well it works.

So now I am completely lost in Z-Wave for various power switching devices and others. It's so good but unfortunately still very expensive.

Anyway, the plugin is for a Raspberry Pi with the RazBerry Z-Wave controller attached and when I switch devices on and off it is instant, really impressive. Z-wave is a mesh network so there are multiple communication paths and status feedback from all devices.

I think I will soon publish it anyway in the 'Plugin' section. At least to have it there as a backup...

Best, Walter
scastano
Posts: 10
Joined: Mon May 20, 2013 6:07 am

Re: Multithreading, python? Eventghost?

Post by scastano »

Yeah, I would love to see that actually!! My Insteon based system is working pretty well and I do have a pretty nice sum of money wrapped up in it, between switches, filters, controllers, etc... but it does basically the same thing. I've got it powering on lamps, kitchen lights, CFLs, most of them with dimmers.... it auto sets scene levels so at night lights only come on to certain levels, motion detection for lights in the kitchen, bathrooms and front hallway.

Now, this is all part of the effort to integrate different systems.I currently have a solution worked out for GPS notification from our iPhones to trigger scripts and along with some arp detection on my DD-WRT router I used the ISY controller to do "proximity" calculations. It knows who's home when, how many people are in the house, etc...

Along with that, my XBMC media centers are wired in too... especially in the living room where, when you hit play on any video or TV show the lights automatically dim to 30% then very slowly raise back up to 50 - 60% when you hit pause or stop. Of course, there's a button on a keypad to engage the auto lighting control if you want it, or disable it if you dont.....

This eventghost situation is in an effort to tie the much better motion detectors in the Insteon system into the network cameras since the motion detection on the cameras themselves just isn't that great. Plus, the cameras can only detect motion in what they can SEE so with this solution when a motion detector with a much broader view sees something moving it will automatically point the camera over that way.

Then.... THEN.... last but not least, I've recently put together my first voice control server and gotten almost all the lighting control to work via voice commands from our iPhones and iPads. Very soon, through the extensive Event Ghost plugins, I'll have voice control going for XBMC, the TV, network cameras and if I get very brave (since I'm a security nut), I may work on adding the Z-wave controller to my ISY and getting some door locks integrated also, possibly some HVAC control also.

It's all a lot of work, but the product isn't only "pretty cool" but it actually saves money in electric bills, makes the house much more secure!
Post Reply