Zone alarms vs monitor alarms via websocket JSON output

Discussion topics related to mobile applications and ZoneMinder Event Server (including machine learning)
hahobson
Posts: 34
Joined: Mon Jul 19, 2021 8:00 am

Zone alarms vs monitor alarms via websocket JSON output

Post by hahobson »

Hello;

Using Websockets, is it possible to ascertain which zone within a monitor had an alarm?
I need to know which zone had the alarm within a monitor. Is that possible based on the documentation below?https://zmeventnotification.readthedocs ... opers.html

Thank you!
hahobson
Posts: 34
Joined: Mon Jul 19, 2021 8:00 am

Re: Zone alarms vs monitor alarms via websocket JSON output

Post by hahobson »

From what I am seeing in the documents, it seems that alarms are associated with monitors generally and no zones specifically. I hope to be proven wrong.
hahobson
Posts: 34
Joined: Mon Jul 19, 2021 8:00 am

Re: Zone alarms vs monitor alarms via websocket JSON output

Post by hahobson »

In : https://zmeventnotification.readthedocs ... opers.html I am trying to get this JSON to work:
{"event":"control","data":{"type":"filter","monlist":"1,2,4,5,6", "intlist":"0,0,3600,60,0"}}

I only have one monitor up and running now. By mousing over it on the console, the number 4 comes up. So my code reflects that:

Code: Select all

import json
from websocket import create_connection
ws = create_connection("ws://127.0.0.1:9000")
ws.send(json.dumps({"event":"auth","data":{"user":"admin","password":"admin"}}))
result =  ws.recv()
print (result)
ws.send(json.dumps({"event":"control","data":{"type":"version"}}))
result =  ws.recv()
print (result)
ws.send(json.dumps({"event":"control","data":{"type":"filter","monlist":"4","intlist":"0"}}))
#ws.send(json.dumps({"event":"control","data":{"type":"filter","monlist":"1,2,4,5,6", "intlist":"0,0,3600,60,0">
result =  ws.recv()
print (result)
ws.close()
When I run the above code it gets stuck at the 3rd send.
Output:
{"reason":"","status":"Success","version":"6.1.27","type":"","event":"auth"}
{"status":"Success","reason":"","event":"control","type":"version","version":"6.1.27"}
ES running in debug mode:

Code: Select all

DBG-2:2021-12-01,15:52:59 PARENT: ----------> Tick START (active forks:0, total forks:2, active hooks: 0 running for:24 min)<--------------
DBG-2:2021-12-01,15:52:59 PARENT: After tick: TOTAL: 1,  ES_CONTROL: 1, FCM+WEB: 0, FCM: 0, WEB: 1, MQTT:0, invalid WEB: 0, PENDING: 0
DBG-2:2021-12-01,15:52:59 PARENT: There are 0 active child forks & 0 zm_detect processes running...
DBG-2:2021-12-01,15:52:59 PARENT: checkEvents() new events found=0
DBG-2:2021-12-01,15:52:59 PARENT: There are 0 new Events to process
DBG-2:2021-12-01,15:52:59 PARENT: ---------->Tick END (active forks:0, total forks:2, active hooks: 0)<--------------
When I hit ctrl C, I get the following errors:

Code: Select all

^CTraceback (most recent call last):
  File "soxz.py", line 12, in <module>
    result =  ws.recv()
  File "/usr/local/lib/python3.8/dist-packages/websocket/_core.py", line 354, in recv
    opcode, data = self.recv_data()
  File "/usr/local/lib/python3.8/dist-packages/websocket/_core.py", line 377, in recv_data
    opcode, frame = self.recv_data_frame(control_frame)
  File "/usr/local/lib/python3.8/dist-packages/websocket/_core.py", line 396, in recv_data_frame
    frame = self.recv_frame()
  File "/usr/local/lib/python3.8/dist-packages/websocket/_core.py", line 435, in recv_frame
    return self.frame_buffer.recv_frame()
  File "/usr/local/lib/python3.8/dist-packages/websocket/_abnf.py", line 337, in recv_frame
    self.recv_header()
  File "/usr/local/lib/python3.8/dist-packages/websocket/_abnf.py", line 293, in recv_header
    header = self.recv_strict(2)
  File "/usr/local/lib/python3.8/dist-packages/websocket/_abnf.py", line 372, in recv_strict
    bytes_ = self.recv(min(16384, shortage))
  File "/usr/local/lib/python3.8/dist-packages/websocket/_core.py", line 519, in _recv
    return recv(self.sock, bufsize)
  File "/usr/local/lib/python3.8/dist-packages/websocket/_socket.py", line 113, in recv
    bytes_ = _recv()
  File "/usr/local/lib/python3.8/dist-packages/websocket/_socket.py", line 90, in _recv
    return sock.recv(bufsize)
KeyboardInterrupt
Thank you!
User avatar
kitkat
Posts: 193
Joined: Sun Jan 27, 2019 5:17 pm

Re: Zone alarms vs monitor alarms via websocket JSON output

Post by kitkat »

Are you waiting for a reply that will never come?

"There is no response for this request, unless the payload did not have either monlist or intlist."
hahobson
Posts: 34
Joined: Mon Jul 19, 2021 8:00 am

Re: Zone alarms vs monitor alarms via websocket JSON output

Post by hahobson »

Yes, there is no reply to this json request:
ws.send(json.dumps({"event":"control","data":{"type":"filter","monlist":"4","intlist":"0"}}))

Code: Select all

import json
from websocket import create_connection
ws = create_connection("ws://127.0.0.1:9000")
ws.send(json.dumps({"event":"auth","data":{"user":"admin","password":"admin"}}))
result =  ws.recv()
print (result)
ws.send(json.dumps({"event":"control","data":{"type":"version"}}))
result =  ws.recv()
print (result)
ws.send(json.dumps({"event":"control","data":{"type":"filter","monlist":"4","intlist":"0"}}))
#ws.send(json.dumps({"event":"control","data":{"type":"filter","monlist":"1,2,4,5,6", "intlist":"0,0,3600,60,0">
result =  ws.recv()
print (result)
ws.close()

hahobson
Posts: 34
Joined: Mon Jul 19, 2021 8:00 am

Re: Zone alarms vs monitor alarms via websocket JSON output

Post by hahobson »

It just hangs.
User avatar
kitkat
Posts: 193
Joined: Sun Jan 27, 2019 5:17 pm

Re: Zone alarms vs monitor alarms via websocket JSON output

Post by kitkat »

I just thought it might be waiting forever in result = ws.recv() after doing ws.send(json.dumps({"event":"control","data":{"type":"filter","monlist":"4","intlist":"0"}})), and the traceback suggests it was in ws.recv when it was interrupted.
hahobson
Posts: 34
Joined: Mon Jul 19, 2021 8:00 am

Re: Zone alarms vs monitor alarms via websocket JSON output

Post by hahobson »

@Kitkat Thanks for your input. It is much appreciated. The first two seem to work fine. Just getting monitor information seems to be the problem.
hahobson
Posts: 34
Joined: Mon Jul 19, 2021 8:00 am

Re: Zone alarms vs monitor alarms via websocket JSON output

Post by hahobson »

@kitkat Interrupted by what I wonder?

I run the following code:

Code: Select all

import json
from websocket import create_connection
ws = create_connection("ws://127.0.0.1:9000")
ws.send(json.dumps({"event":"auth","data":{"user":"admin","password":"admin"}}))
result =  ws.recv()
print (result)
ws.send(json.dumps({"event":"control","data":{"type":"version"}}))
result =  ws.recv()
print (result)
ws.send(json.dumps({"event":"escontrol", "data":{"command":"unmute","monitors":[4]}}))
result =  ws.recv()
print (result)
ws.close()

Output:

Code: Select all

{"version":"6.1.27","event":"auth","type":"","reason":"","status":"Success"}
{"type":"version","status":"Success","reason":"","version":"6.1.27","event":"control"}
{"request":{"data":{"monitors":[4],"command":"unmute"},"event":"escontrol"},"type":"command","status":"Fail","reason":"NOTCONTROL","event":"escontrol"}
I only have one monitor running and it is called 4.
User avatar
kitkat
Posts: 193
Joined: Sun Jan 27, 2019 5:17 pm

Re: Zone alarms vs monitor alarms via websocket JSON output

Post by kitkat »

hahobson wrote: Thu Dec 02, 2021 2:52 am @kitkat Interrupted by what I wonder?
You pressing Ctrl+C ;)

I'm kind of shooting in the dark here (not used ws or zme) but it looks in the first case like you're sending a command that doesn't elicit a response ("There is no response for this request.." is from the docs) and then calling ws.recv() which sits there waiting for something to be sent:

Code: Select all

ws.send(json.dumps({"event":"control","data":{"type":"filter","monlist":"4","intlist":"0"}}))
#ws.send(json.dumps({"event":"control","data":{"type":"filter","monlist":"1,2,4,5,6", "intlist":"0,0,3600,60,0">
result =  ws.recv()
When you interrupt it, it looks like it's in that ws.recv() function:

Code: Select all

^CTraceback (most recent call last):
  File "soxz.py", line 12, in <module>
    result =  ws.recv()
And that it knows it had been interrupted manually:

Code: Select all

KeyboardInterrupt
The later script does get a response (OK, so it's not what you wanted, but it's something) and it does come back from recv() and print the result:

Code: Select all

ws.send(json.dumps({"event":"escontrol", "data":{"command":"unmute","monitors":[4]}}))
result =  ws.recv()
print (result)
So I don't think there's an actual error in the first one (although there may be), just that recv() is sitting there waiting for something that will never happen.

I'm not really sure what else you'd be expecting to see unless the server sends an alarm or some other self-initiated event whilst it's waiting.

What happens if you send the non-responsive filter message and then, without waiting for a reply to filter and omitting the call to ws.recv(), send another message that does get a response, such as version? If that works then I reckon just drop the call to ws.recv() after the filter command and carry on :)



The other difference is the #-commented line in the first one, but that'd be a really wild error!
hahobson
Posts: 34
Joined: Mon Jul 19, 2021 8:00 am

Re: Zone alarms vs monitor alarms via websocket JSON output

Post by hahobson »

Code: Select all

DBG-2:2021-12-02,10:44:29 PARENT: ----------> Tick START (active forks:0, total forks:0, active hooks: 0 running for:47 min)<--------------
DBG-2:2021-12-02,10:44:29 PARENT: After tick: TOTAL: 1,  ES_CONTROL: 1, FCM+WEB: 0, FCM: 0, WEB: 1, MQTT:0, invalid WEB: 0, PENDING: 0
DBG-2:2021-12-02,10:44:29 PARENT: There are 0 active child forks & 0 zm_detect processes running...
DBG-2:2021-12-02,10:44:29 PARENT: checkEvents() new events found=0
DBG-2:2021-12-02,10:44:29 PARENT: There are 0 new Events to process
DBG-2:2021-12-02,10:44:29 PARENT: ---------->Tick END (active forks:0, total forks:0, active hooks: 0)<--------------
There is an ES_CONTROL: 1

Code: Select all

ws.send(json.dumps({"event":"control","data":{"type":"filter","monlist":"4", "intlist":"30"}}))
The above hangs The below fails

Code: Select all

ws.send(json.dumps({"event":"escontrol", "data":{"command":"unmute","monitors":[4]}}))
Output:

Code: Select all

{"type":"command","status":"Fail","event":"escontrol","request":{"event":"escontrol","data":{"command":"get"}},"reason":"NOTCONTROL"}
hahobson
Posts: 34
Joined: Mon Jul 19, 2021 8:00 am

Re: Zone alarms vs monitor alarms via websocket JSON output

Post by hahobson »

Code: Select all

harry@harry-HP-Compaq-Pro-4300-SFF-PC:~$ sudo python3 soxx.py 
{"event":"auth","reason":"","version":"6.1.27","type":"","status":"Success"}
{"reason":"","event":"control","type":"version","status":"Success","version":"6.1.27"}
--{"event":"alarm","status":"Success","type":"","events":[{"DetectionJson":[],"RulesObject":{},"Name":"1","Cause":"","EventId":"4549","MonitorId":"4"}]}--
{"request":{"event":"escontrol","data":{"command":"get"}},"reason":"NOTCONTROL","event":"escontrol","type":"command","status":"Fail"}
A failure and some sort of success. :shock:
hahobson
Posts: 34
Joined: Mon Jul 19, 2021 8:00 am

Re: Zone alarms vs monitor alarms via websocket JSON output

Post by hahobson »

Output:

Code: Select all

{"status":"Success","type":"","events":[{"MonitorId":"4","EventId":"4560","Cause":"","Name":"1","RulesObject":{},"DetectionJson":[]}],"event":"alarm"}
The above output takes several minutes to be triggered. It does not reflect the zone but it does show the monitor ID, which might be enough. If I can get it work faster, that would be huge progress.

Code: Select all

import json
from websocket import create_connection
ws = create_connection("ws://127.0.0.1:9000")
ws.send(json.dumps({"event":"auth","data":{"user":"admin","password":"admin"}}))
result =  ws.recv()
print (result)
ws.send(json.dumps({"event":"control","data":{"type":"filter","monlist":"4", "intlist":"0"}}))
result =  ws.recv()
print (result)
ws.close()
User avatar
kitkat
Posts: 193
Joined: Sun Jan 27, 2019 5:17 pm

Re: Zone alarms vs monitor alarms via websocket JSON output

Post by kitkat »

Code: Select all

ws.send(json.dumps({"event":"control","data":{"type":"filter","monlist":"4", "intlist":"30"}}))
The above hangs The below fails

Code: Select all

ws.send(json.dumps({"event":"escontrol", "data":{"command":"unmute","monitors":[4]}}))
Output:

Code: Select all

{"type":"command","status":"Fail","event":"escontrol","request":{"event":"escontrol","data":{"command":"get"}},"reason":"NOTCONTROL"}
If I'm right about recv() waiting for a reply that will never come after issuing the filter command then the hang-up is to be expected.

As for the Fail response to the unmute command, I wonder... Is the monitor muted and if not, will trying to unmute a not-muted monitor throw an error? Perhaps try muting it first/instead?
Output:

Code: Select all

{"status":"Success","type":"","events":[{"MonitorId":"4","EventId":"4560","Cause":"","Name":"1","RulesObject":{},"DetectionJson":[]}],"event":"alarm"}
The above output takes several minutes to be triggered. It does not reflect the zone but it does show the monitor ID, which might be enough. If I can get it work faster, that would be huge progress.
When you say "takes several minutes to be triggered," do you mean after the alarm is triggered in ZM?

It's not a reply to the filter command, or at least I don't think it is anyway, but an alarm message which could appear at any random time (or never).
hahobson
Posts: 34
Joined: Mon Jul 19, 2021 8:00 am

Re: Zone alarms vs monitor alarms via websocket JSON output

Post by hahobson »

@Kitkat Zoneminder's web GUI shows an alarm within a reasonable time, and so does the ES, but the websocket output seems to randomly output the alarm.
Post Reply