diff --git a/scripts/zmtrigger.pl.z b/scripts/zmtrigger.pl.z index 7034e1933..a578a961d 100755 --- a/scripts/zmtrigger.pl.z +++ b/scripts/zmtrigger.pl.z @@ -156,7 +156,8 @@ foreach my $source ( @sources ) { local *sfh; #sysopen( *sfh, $source->{path}, O_NONBLOCK|O_RDONLY ) or die( "Can't sysopen: $!" ); - open( *sfh, "<".$source->{path} ) or die( "Can't open: $!" ); + #open( *sfh, "<".$source->{path} ) or die( "Can't open: $!" ); + open( *sfh, "+<".$source->{path} ) or die( "Can't open: $!" ); $source->{handle} = *sfh; vec( $base_rin, fileno($source->{handle}),1) = 1; } @@ -278,7 +279,10 @@ sub handleMessage { next if ( !$message ); print( "Processing message '$message'\n" ) if ( VERBOSE ); - my ( $id, $action, $score, $cause, $text ) = split( /\|/, $message ); + my ( $id, $action, $score, $cause, $text, $showtext ) = split( /\|/, $message ); + $score = 0 if ( !defined($score) ); + $cause = 0 if ( !defined($cause) ); + $text = 0 if ( !defined($text) ); my $res = $sth->execute( $id, $id ) or die( "Can't execute '$sql': ".$sth->errstr() ); my $monitor = $sth->fetchrow_hashref(); @@ -289,7 +293,7 @@ sub handleMessage next; } print( "Found monitor for id '$id'\n" ) if ( VERBOSE ); - my $size = 512; # We only need the first 512 bytes really for the alarm state and forced alarm + my $size = 512; # We only need the first 512 bytes really for the shared data and trigger section $monitor->{ShmKey} = hex(ZM_SHM_KEY)|$monitor->{Id}; $monitor->{ShmId} = shmget( $monitor->{ShmKey}, $size, 0 ); if ( !defined($monitor->{ShmId}) ) @@ -298,13 +302,30 @@ sub handleMessage next; } + my $shm_data_size; + if ( !shmread( $monitor->{ShmId}, $shm_data_size, 0, 4 ) ) + { + print( "Can't read from shared memory: $!\n" ); + exit( -1 ); + } + $shm_data_size = unpack( "l", $shm_data_size ); + my $trigger_data_offset = $shm_data_size+4; # Allow for 'size' member of trigger data + print( "Handling action '$action'\n" ) if ( VERBOSE ); if ( $action =~ /^(on|off)(?:\+(\d+))?$/ ) { my $trigger = $1; my $delay = $2; - my $force_data = pack( "llZ32Z256", $trigger eq "on"?1:2, $trigger eq "on"?$score:0, $cause, $text ); - if ( !shmwrite( $monitor->{ShmId}, $force_data, 60, 4+4+32+256 ) ) + my $trigger_data; + if ( defined($showtext) ) + { + $trigger_data = pack( "llZ32Z256Z32", $trigger eq "on"?1:2, $trigger eq "on"?$score:0, $cause, $text, $showtext ); + } + else + { + $trigger_data = pack( "llZ32Z256", $trigger eq "on"?1:2, $trigger eq "on"?$score:0, $cause, $text ); + } + if ( !shmwrite( $monitor->{ShmId}, $trigger_data, $trigger_data_offset, length($trigger_data) ) ) { print( "Can't write to shared memory: $!\n" ); } @@ -312,7 +333,7 @@ sub handleMessage if ( $delay ) { my $action_time = time()+$delay; - my $action_text = $id."|cancel|".$cause."|".$text; + my $action_text = $id."|cancel|0|".$cause."|".$text; my $action_array = $actions{$action_time}; if ( !$action_array ) { @@ -324,13 +345,30 @@ sub handleMessage } elsif( $action eq "cancel" ) { - my $force_data = pack( "llZ32Z256", 0, 0, "", "" ); - if ( !shmwrite( $monitor->{ShmId}, $force_data, 60, 4+4+32+256 ) ) + my $trigger_data; + if ( defined($showtext) ) + { + $trigger_data = pack( "llZ32Z256Z32", 0, 0, "", "", $showtext ); + } + else + { + $trigger_data = pack( "llZ32Z256", 0, 0, "", "" ); + } + if ( !shmwrite( $monitor->{ShmId}, $trigger_data, $trigger_data_offset, length($trigger_data) ) ) { print( "Can't write to shared memory: $!\n" ); } print( "Cancelled event '$cause'\n" ); } + elsif( $action eq "show" ) + { + my $trigger_data = pack( "Z32", $showtext ); + if ( !shmwrite( $monitor->{ShmId}, $trigger_data, $trigger_data_offset, length($trigger_data) ) ) + { + print( "Can't write to shared memory: $!\n" ); + } + print( "Updated show text to '$showtext'\n" ); + } else { print( "Unrecognised action '$action' in message '$message'\n" ); diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 9d901a6b8..f1d288c77 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -263,6 +263,7 @@ void Monitor::Setup() trigger_data->trigger_score = 0; trigger_data->trigger_cause[0] = 0; trigger_data->trigger_text[0] = 0; + trigger_data->trigger_showtext[0] = 0; } if ( !shared_data->valid ) { diff --git a/src/zm_monitor.h b/src/zm_monitor.h index ac41fe9ce..b9c8e7cf4 100644 --- a/src/zm_monitor.h +++ b/src/zm_monitor.h @@ -149,6 +149,7 @@ protected: int trigger_score; char trigger_cause[32]; char trigger_text[256]; + char trigger_showtext[32]; } TriggerData; SharedData *shared_data; @@ -200,11 +201,36 @@ public: { if ( label_format[0] ) { + static int token_count = -1; static char label_time_text[256]; static char label_text[256]; + if ( token_count < 0 ) + { + const char *token_ptr = label_format; + const char *token_string = "%%s"; + token_count = 0; + while( token_ptr = strstr( token_ptr, token_string ) ) + { + token_count++; + token_ptr += strlen(token_string); + } + Info(( "Found %d tokens, in %s", token_count, label_format )); + } strftime( label_time_text, sizeof(label_time_text), label_format, localtime( &ts_time ) ); - snprintf( label_text, sizeof(label_text), label_time_text, name ); + switch ( token_count ) + { + case 1: + { + snprintf( label_text, sizeof(label_text), label_time_text, name ); + break; + } + case 2: + { + snprintf( label_text, sizeof(label_text), label_time_text, name, trigger_data->trigger_showtext ); + break; + } + } ts_image->Annotate( label_text, label_coord ); }