send to sensors on any message, not just heartbeat
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 22 Mar 2021 17:50:47 +0000 (18:50 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 22 Mar 2021 17:50:47 +0000 (18:50 +0100)
zc-mqtt

diff --git a/zc-mqtt b/zc-mqtt
index 5a6cd86..690010e 100755 (executable)
--- a/zc-mqtt
+++ b/zc-mqtt
@@ -133,28 +133,31 @@ while (1) {
        write_file "$queue/$dir/$date/$time.$t.$up_down.$function_code", $msg->message;
 
        if ( substr($msg->message,2,1) eq "\x07" ) { # heartbeat
-               $topic =~ s/up$/down/;
-
-               my @all_pending = sort glob "$queue/$dir/.pending/*";
-               if ( my $pending = shift @all_pending ) {
-                       my $raw = read_file $pending;
-                       my $pending_function_code = unpack('C',substr($msg->message,2,1));
-
-                       send_message($socket,
-                               message_type => MQTT_PUBLISH,
-                               retain => 0, #$retain,
-                               topic => $topic,
-                               message => $raw);
-                       $pending =~ s{$queue/$dir/.pending/}{};
-                       rename "$queue/$dir/.pending/$pending", "$queue/$dir/$pending.sent.$pending_function_code";
-               }
-
                my $hash = protocol_decode( $up_down, $msg->message );
                $sth->execute( map { $hash->{data_id}->{$_} } @insert_data_ids );
 
 
       }
 
+
+       # send pending messages on any connection
+       my @all_pending = sort glob "$queue/$dir/.pending/*";
+       if ( my $pending = shift @all_pending ) {
+               my $raw = read_file $pending;
+               my $pending_function_code = unpack('C',substr($msg->message,2,1));
+
+               $topic =~ s/up$/down/;
+
+               send_message($socket,
+                       message_type => MQTT_PUBLISH,
+                       retain => 0, #$retain,
+                       topic => $topic,
+                       message => $raw);
+               $pending =~ s{$queue/$dir/.pending/}{};
+               rename "$queue/$dir/.pending/$pending", "$queue/$dir/$pending.sent.$pending_function_code";
+       }
+
+
       if (defined $count && --$count == 0) {
         exit;
       }