extract up/down from topic
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 4 Oct 2020 08:22:20 +0000 (10:22 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 4 Oct 2020 08:22:20 +0000 (10:22 +0200)
zc-mqtt

diff --git a/zc-mqtt b/zc-mqtt
index f7bd2b0..9706b5b 100755 (executable)
--- a/zc-mqtt
+++ b/zc-mqtt
@@ -83,8 +83,10 @@ while (1) {
       }
 
        my $topic = $msg->topic;
-       my $dir = $topic;
-       $dir =~ s{\w+/\w+/(\w+)/\w+}{$1};
+       # Inclinometer/ZCT330Ex_SWP_N_YK/869858031634109/up
+       my $dir = $topic; # leave imei in dir
+       $dir =~ s{\w+/\w+/(\w+)/(\w+)$}{$1};
+       my $up_down = $2;
        
        mkdir "$queue" if ( ! -e "$queue" );
        mkdir "$queue/$dir" if ( ! -e "$queue/$dir" );
@@ -92,9 +94,8 @@ while (1) {
 
        my $function_code = unpack('C',substr($msg->message,2,1));
 
-       write_file "$queue/$dir/$t.up.$function_code", $msg->message;
+       write_file "$queue/$dir/$t.$up_down.$function_code", $msg->message;
 
-#      if ( $msg->topic =~ m{Inclinometer/ZCT330Ex_SWP_N_YK/869858031634109/up} ) {
        if ( substr($msg->message,2,1) eq "\x07" ) { # heartbeat
                $topic =~ s/up$/down/;
 
@@ -109,7 +110,7 @@ while (1) {
                                topic => $topic,
                                message => $raw);
                        $pending =~ s{$queue/$dir/.pending/}{};
-                       rename "$queue/$dir/.pending/$pending", "$queue/$dir/$pending.down.$function_code";
+                       rename "$queue/$dir/.pending/$pending", "$queue/$dir/$pending.sent.$pending_function_code";
                }
       }