Browse code

Modificat partea de update in getdevinfo cu insert / update on duplicate;

Liviu Pislaru authored on 05/03/2018 11:50:56
Showing 1 changed files
... ...
@@ -1,6 +1,6 @@
1 1
 #!/usr/bin/perl
2 2
 # parser pentru mesajele MQTT
3
-# Version: 1.0.4
3
+# Version: 1.0.5
4 4
 
5 5
 use strict;
6 6
 use warnings;
... ...
@@ -76,7 +76,6 @@ while (my $mqttMessage = <$SUB> ) {
76 76
 
77 77
       # aleg canalele de unde salvez date in DB:
78 78
       if ($queue eq "ENERGY"){
79
-        # tele/mng9184-1657/ENERGY {"Time":"2018-03-04T13:18:39","Total":1376.995,"Yesterday":23.704,"Today":16.450,"Period":102,"Power":1255,"Factor":0.99,"Voltage":237,"Current":5.298}
80 79
         my $decoded = decode_json($json);
81 80
         if (exists $decoded->{'Yesterday'}){
82 81
           addDB ($topic, "Yesterday", $decoded->{'Yesterday'});
... ...
@@ -135,15 +134,13 @@ sub addFILE {
135 135
   return;
136 136
 }
137 137
 
138
-# adaug in tabela getdevinfo din baza de date 
138
+# adaug/updatez in tabela getdevinfo din baza de date 
139 139
 sub addDB {                                                    
140 140
   (my $topic, my $camp, my $valoare) = @_;
141
-#  mysql_query("insert into getdevinfo values('$MQTTuser','$MQTTdevice','$MQTTcamp','$MQTTvaloare') on duplicate key update camp='$MQTTcamp',valoare='$MQTTvaloare';");
142
-#   my $query = "insert into getdevinfo values (?, ?, ?, ?) on duplicate key update camp=$camp,valoare='$valoare";
143 141
   (my $user, my $devid) = split /\-/, $topic;
144
-  my $query = "UPDATE getdevinfo SET valoare=? WHERE user=? and devid=? and camp=?";
142
+  my $query = "insert into getdevinfo values (?, ?, ?, ?) on duplicate key update camp=?, valoare=?";
145 143
   my $sth = $dbh->prepare($query) or die "Can't prepare $query: $dbh->errstr\n";
146
-  $sth->execute("$valoare", "$user", "$devid", "$camp");
144
+  $sth->execute("$user", "$devid", "$camp", "$valoare", "$camp", "$valoare");
147 145
   $sth->finish();
148 146
   return;
149 147
 }