-
Notifications
You must be signed in to change notification settings - Fork 0
/
zm_alarm_handling.pl
executable file
·200 lines (177 loc) · 6.25 KB
/
zm_alarm_handling.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/perl -w
#---------------- Configurations ------------------------------------------------------------------------------------
use constant OPENHAB_URL => "http://openhab2:8080"; #Base URL of Openhab
use constant MONITOR_RELOAD_INTERVAL => 300; #Time in second for reload Monitor from ZM
use constant SLEEP_DELAY=>2; #Time in second for checking loop
use constant ALARM_COUNT => 3; #Number of alarm event to raise action to OpenHab
#---------------- End of Configurations ------------------------------------------------------------------------------
use strict;
use ZoneMinder;
use warnings;
use DBI;
$| = 1;
$SIG{INT} = \&signal_handler;
$SIG{TERM} = \&signal_handler;
my %monitors;
my $dbh = zmDbConnect(); #ZM function to create DB object
my $monitor_reload_time = 0;
my $apns_feedback_time = 0;
my $proxy_reach_time=0;
my $wss;
my @events=();
my @active_connections=();
my $alarm_header="";
my $alarm_mid="";
my $alarmEventId = 1; # tags the event id along with the alarm - useful for correlation
my @camstatus = ("NONE","NONE","NONE","NONE","NONE","NONE","OFF","OFF","OFF","OFF","OFF","OFF"); #Array with alarm status
my @firstrun = (0,0,0,0,0,0,0,0,0,0,0,0);
my @alarmcount = (0,0,0,0,0,0,0,0,0,0,0,0);
Info( "ALL - Alarm Monitor Handling module starting" );
while( 1 )
{
my $eventFound = 0;
#Every MONITOR_RELOAD_INTERVAL reload monitor from database
if ( (time() - $monitor_reload_time) > MONITOR_RELOAD_INTERVAL )
{
Info ("ALL - Reloading Monitors...\n");
foreach my $monitor (values(%monitors))
{
zmMemInvalidate( $monitor );
}
loadMonitors();
Info ("ALL - Firstrun status= @firstrun\n");
Info ("ALL - Alarm Status= @camstatus\n");
}
@events = ();
$alarm_header = "";
$alarm_mid="";
foreach my $monitor ( values(%monitors) )
{
next if ( !zmMemVerify( $monitor ) );
my ( $state, $last_event )
= zmMemRead( $monitor,
[ "shared_data:state",
"shared_data:last_event"
]
);
#Decodifica Stati
#STATE_IDLE => 0;
#STATE_PREALARM => 1;
#STATE_ALARM => 2;
#STATE_ALERT => 3;
#STATE_TAPE => 4;
#If an alarm status is detected
if ($state == STATE_ALARM || $state == STATE_ALERT)
{
#Info ("Alarm Detected on cam=".$monitor->{Name});
my $count = @alarmcount[$monitor->{Id}]; #Retrieve count from array
$count = $count+1; #Increase count of one
@alarmcount[$monitor->{Id}] = $count; #Update the array
Info ($monitor->{Name}." - Alarm Detected");
if ($count >= ALARM_COUNT) {
if (@camstatus[$monitor->{Id}] ne decodeState($state)) #If CAM status is not equal to previus status
{
&sendtoOH($monitor->{Id},$state,$monitor->{Name}); #Send notification ro OpenHab
@camstatus[$monitor->{Id}] = decodeState($state); #Update the array with last status
Info ($monitor->{Name}." - Global Alarm Count= @alarmcount\n"); #Log alarmcount
}
}
}
#If an alarm is rearmed
if ($state == STATE_IDLE || $state == STATE_TAPE)
{
my $count = @alarmcount[$monitor->{Id}];
if (@camstatus[$monitor->{Id}] ne decodeState($state))
{
@alarmcount[$monitor->{Id}] = 0; #reset alarmcount array
if ($count >= ALARM_COUNT) { #if alarm count greater the threashold, send the notification to OH
&sendtoOH($monitor->{Id},$state,$monitor->{Name}); #send off command to OH
}
@camstatus[$monitor->{Id}] = decodeState($state); #Update the array with last status
Info ($monitor->{Name}." - Alarm Rearmed");
Info ($monitor->{Name}." - Global Alarm Count= @alarmcount\n");
}
}
}
sleep( SLEEP_DELAY );
}
#This sub decode numeric state of ZM in OH state for the Alarm's Switch
sub decodeState
{
use Switch;
my ($stato) = @_;
my $oh_state = "NONE";
switch ($stato) {
case 2 { $oh_state = "ON"; }
case 3 { $oh_state = "ON"; }
case 4 { $oh_state = "OFF"; }
case 0 { $oh_state = "OFF"; }
else { $oh_state = "NONE"; }
}
return ($oh_state);
}
#Compose the command for OH
sub sendtoOH
{
my ($monid, $stato,$monname) = @_;
my $oh_state = decodeState($stato);
if ($oh_state ne "NONE")
{
use REST::Client;
my $host = OPENHAB_URL;
my $client = REST::Client->new(host => $host);
my $url = "/rest/items/CAM_ID".$monid."_ALARM/state"; #Composing Item Name into REST URL
Info($monname." - Send Command: ".OPENHAB_URL.$url." ".$oh_state);
#Write stuff to HTTP with REST Client
$client->PUT($url, $oh_state);
Info($monname." - Client Result: ".$client);
}
}
# Refreshes list of monitors from DB
sub loadMonitors
{
Info( "ALL - Loading monitors\n" );
$monitor_reload_time = time();
my %new_monitors = ();
my $sql = "SELECT * FROM Monitors
WHERE find_in_set( Function, 'Modect,Mocord,Nodect' )".
( $Config{ZM_SERVER_ID} ? 'AND ServerId=?' : '' );
Debug ("SQL to be executed is :$sql");
my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID} : () )
or Fatal( "Can't execute: ".$sth->errstr() );
while( my $monitor = $sth->fetchrow_hashref() )
{
next if ( !zmMemVerify( $monitor ) ); # Check shared memory ok
if ( defined($monitors{$monitor->{Id}}->{LastState}) )
{
$monitor->{LastState} = $monitors{$monitor->{Id}}->{LastState};
}
else
{
$monitor->{LastState} = zmGetMonitorState( $monitor );
}
if ( defined($monitors{$monitor->{Id}}->{LastEvent}) )
{
$monitor->{LastEvent} = $monitors{$monitor->{Id}}->{LastEvent};
}
else
{
$monitor->{LastEvent} = zmGetLastEvent( $monitor );
}
$new_monitors{$monitor->{Id}} = $monitor;
#For each monitor check if is first run and if yes, send OFF to OpenHab
if (@firstrun[$monitor->{Id}] == 0)
{
Info ($monitor->{Name}." - First Run - Sendig OFF to cam ID".$monitor->{Id});
&sendtoOH($monitor->{Id},0,,$monitor->{Name});
@firstrun[$monitor->{Id}] = 1;
}
}
%monitors = %new_monitors;
}
sub signal_handler {
Info( "ALL - ZM Alarm Monitor Handling module stopping" );
exit 0
}