Using API to setup cams

Forum for questions and support relating to the 1.28.x releases only.
BiloxiGeek
Posts: 271
Joined: Tue Feb 11, 2014 2:04 pm

Using API to setup cams

Post by BiloxiGeek »

If given this information collected about each of my cameras from before I screwed up my install. Is it reasonable to use the API to rebuild my setup?

I'm working on fixing my live stream issue but I'm gonna need to reconfigure all my cameras once I figure that out. Am I correct in thinking I can just turn this text into a script of "curl -XPOST http://server/zm/api/monitors.json -d "Monitor[Name]=Front-low" and all the other settings to rebuild my system setup?

Code: Select all

**** Monitor ID 2 ****
Monitor 2(Front-low)
Id : 2
Name : Front-low
Type : File
Path : rtsp://192.168.1.231/4?tcp
Width : 320
Height : 176
Colours : 3
Subpixel Order : 6
Event Prefix : Front-Low-
Label Format : Front 1 - %d/%m/%y %H:%M:%S
Label Coord : 0,0
Image Buffer Count : 50
Warmup Count : 25
Pre Event Count : 25
Post Event Count : 300
Stream Replay Buffer : 1000
Alarm Frame Count : 3
Section Length : 600
Maximum FPS : 0.00
Alarm Maximum FPS : 0.00
Reference Blend %ge : 6
Alarm Reference Blend %ge : 6
Track Motion : 0
Function: 3 - Motion Detection
Zones : 1
  Id : 17
  Label : Front
  Type: 1 - Active
  Shape : 13 points
    0: 18,41
    1: 85,22
    2: 98,6
    3: 125,14
    4: 211,17
    5: 318,44
    6: 319,175
    7: 132,174
    8: 125,144
    9: 116,125
    10: 91,97
    11: 53,94
    12: 26,100
  Alarm RGB : c30000ff
  Check Method: 3 - Blobs
  Min Pixel Threshold : 50
  Max Pixel Threshold : 0
  Min Alarm Pixels : 350
  Max Alarm Pixels : 0
  Filter Box : 3,3
  Min Filter Pixels : 350
  Max Filter Pixels : 0
  Min Blob Pixels : 350
  Max Blob Pixels : 0
  Min Blobs : 1
  Max Blobs : 0
BiloxiGeek
Posts: 271
Joined: Tue Feb 11, 2014 2:04 pm

Re: Using API to setup cams

Post by BiloxiGeek »

Just tried to use curl and monitors.json in the API to create a new monitor. (Just to test I used the example in the API docs, just changed the IP address)

Code: Select all

curl -k -XPOST https://localhost/zm/api/monitors.json -d "Monitor[Name]Cliff-Burton \
&Monitor[Function]=Modect \
&Monitor[Protocol]=http \
&Monitor[Method]=simple \
&Monitor[Host]=usr:pass@192.168.1.231 \
&Monitor[Port]=80 \
&Monitor[Path]=/mjpg/video.mjpg \
&Monitor[Width]=704 \
&Monitor[Height]=480 \
&Monitor[Colours]=4"
I get an error: "message": "Database connection \"Mysql\" is missing, or could not be created."

Brought up the web interface, it looks like I expect it to, a brand new zm system with no monitors defined yet. Tried /zm/api/getVersion.json and I get that same Database connection error.
User avatar
asker
Posts: 1553
Joined: Sun Mar 01, 2015 12:12 pm

Re: Using API to setup cams

Post by asker »

I think your problem about DB is the same as the question you asked a few weeks ago. My response was viewtopic.php?f=32&t=23665&p=90451#p90451

Please see if that helps.
I no longer work on zmNinja, zmeventnotification, pyzm or mlapi. I may respond on occasion based on my available time/interest.

Please read before posting:
How to set up logging properly
How to troubleshoot and report - ES
How to troubleshoot and report - zmNinja
ES docs
zmNinja docs
BiloxiGeek
Posts: 271
Joined: Tue Feb 11, 2014 2:04 pm

Re: Using API to setup cams

Post by BiloxiGeek »

Yep, that was it. I had forgotten I had to change that. Getting API access now.

Ok, one more hurdle crossed. Only a few hundred to go! LOL
BiloxiGeek
Posts: 271
Joined: Tue Feb 11, 2014 2:04 pm

Re: Using API to setup cams

Post by BiloxiGeek »

I've got some scripts that let me add monitors from a curl command. Getting all the settings for a new monitor on one command is a bit unwieldy but at least it works. Just now read a bit more in the API docs and figured out that I can create the monitor initially and then adjust it's settings one or a few attributes at a time.

To add one monitor my first attempts used a command that was almost 1000 characters, a bit much for one command. But I've got that issue fixed now. I'm hoping someone has some tips and/or examples of how to best use the API for these kind of functions.
User avatar
asker
Posts: 1553
Joined: Sun Mar 01, 2015 12:12 pm

Re: Using API to setup cams

Post by asker »

What kind of functions do you have in mind? looks like you have figured this one out.
Also, please read http://zoneminder.readthedocs.org/en/latest/api.html for the latest. I'll keep updating it.

Here are some additional tips that I did not document yet. (We all have Kyle Johnson to thank for making the APIs in the first place)

Code: Select all

https://server/zm/api/events/consoleEvents/5%20minute.json
The above returns a list of how many events per monitor we got in the last 5 minutes. You can change it to minute,day,hour,week whatever
(The %20 == url encoded space)

The APIs have powerful search built in (for some, typically using the index path)
For example ,you know this works:

Code: Select all

https://server.com/zm/api/events.json
Now look at the output. What if you wanted only events with 1 or more alarmed frames? (i.e. no continuous events without alarms)

Code: Select all

https://server.com/zm/api/events/index/AlarmFrames >=: 1.json
What if you wanted to combine conditions?

Code: Select all

https://server.com/zm/api/events/index/AlarmFrames >=: 1/Length <=: 10.json
This one outputs events with alarmed frames >= 1 and length <= 10 secs.

Note if you are using curl, url escaping may be needed - please read up on that

Keep fiddling - lots of nice stuff hidden and you always have zmNinja's own source code to look for if you wonder how it does stuff
I no longer work on zmNinja, zmeventnotification, pyzm or mlapi. I may respond on occasion based on my available time/interest.

Please read before posting:
How to set up logging properly
How to troubleshoot and report - ES
How to troubleshoot and report - zmNinja
ES docs
zmNinja docs
BiloxiGeek
Posts: 271
Joined: Tue Feb 11, 2014 2:04 pm

Re: Using API to setup cams

Post by BiloxiGeek »

I can see some real useful stuff from tracking # of events per time frame. Gonna look into setting up a service in my check_mk stuff. Probably nothing practical there, but geeky cool nonetheless.

Just about got my build up script ready, I'd be working on that since it's my day off, but the wife has other ideas damnit. :)

One question about using the API: the docs say that output can be in json or xml, but so far every time I've tried to get xml it's failed.

Code: Select all

Fatal error: Class 'DOMDocument' not found in /usr/share/zoneminder/www/api/lib/Cake/Utility/Xml.php on line 213
Maybe a missing library or something?
User avatar
asker
Posts: 1553
Joined: Sun Mar 01, 2015 12:12 pm

Re: Using API to setup cams

Post by asker »

I think Cake automatically handles JSON or XML but I tried with a few URLs and in some cases XML worked and some cases it did not.

XML support seems spotty - I'd recommend sticking with JSON.
I no longer work on zmNinja, zmeventnotification, pyzm or mlapi. I may respond on occasion based on my available time/interest.

Please read before posting:
How to set up logging properly
How to troubleshoot and report - ES
How to troubleshoot and report - zmNinja
ES docs
zmNinja docs
BiloxiGeek
Posts: 271
Joined: Tue Feb 11, 2014 2:04 pm

Re: Using API to setup cams

Post by BiloxiGeek »

asker wrote: Here are some additional tips that I did not document yet. (We all have Kyle Johnson to thank for making the APIs in the first place)

Code: Select all

https://server/zm/api/events/consoleEvents/5%20minute.json
The above returns a list of how many events per monitor we got in the last 5 minutes. You can change it to minute,day,hour,week whatever

Code: Select all

{
    "results": {
        "105": "1",
        "108": "1",
        "205": "1",
        "208": "1"
    }
}
Is there a way to have it return all monitors even if the event count is zero? I've looked through the source but I don't know php well enough to have more than just a vague understanding of what I'm seeing.
User avatar
asker
Posts: 1553
Joined: Sun Mar 01, 2015 12:12 pm

Re: Using API to setup cams

Post by asker »

No, if you look at the code, specifically this line https://github.com/ZoneMinder/ZoneMinde ... r.php#L205 you'll know why that is so. (Hint: its picking up from the events table)

Not sure why you'd want it to do so --> the monitor IDs it does not return are the ones with 0 events in that time period. If you are doing some correlation for representation, the total list of monitors can always be retrieved from monitors.json to figure out which monitors were not included in this list.
I no longer work on zmNinja, zmeventnotification, pyzm or mlapi. I may respond on occasion based on my available time/interest.

Please read before posting:
How to set up logging properly
How to troubleshoot and report - ES
How to troubleshoot and report - zmNinja
ES docs
zmNinja docs
BiloxiGeek
Posts: 271
Joined: Tue Feb 11, 2014 2:04 pm

Re: Using API to setup cams

Post by BiloxiGeek »

I did end up with a perl script that lets me build my monitors in one command line. If anyone is interested in seeing what I did I can share it here or on the wiki. It's not particularly elegant but it did the job. There's almost certainly better ways to do it. I was curious if it could be done so I wouldn't have to spend a few hours clicking around the web interface since I have 10 Cameras, all Grandstream, each one has two feeds (high and low definition) and the configuration is identical across all 10 cams.
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Using API to setup cams

Post by rockedge »

BiloxiGeek wrote: If anyone is interested in seeing what I did I can share it here or on the wiki.
I am interested in seeing your code!
BiloxiGeek
Posts: 271
Joined: Tue Feb 11, 2014 2:04 pm

Re: Using API to setup cams

Post by BiloxiGeek »

rockedge wrote:
BiloxiGeek wrote: If anyone is interested in seeing what I did I can share it here or on the wiki.
I am interested in seeing your code!
I'll clean it up for readability and get it on the wiki this afternoon when I get home from work.
BiloxiGeek
Posts: 271
Joined: Tue Feb 11, 2014 2:04 pm

Post by BiloxiGeek »

rockedge wrote:I am interested in seeing your code!
Quick and dirty write up: https://wiki.zoneminder.com/API_Tricks/Create_Monitors
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Using API to setup cams

Post by rockedge »

@BiloxiGeek Thanks for the write up!
Locked