changed sql insert

master
ademant 2019-12-22 21:14:06 +01:00
parent fd9eef4f16
commit 12f9028b1b
2 changed files with 7 additions and 4 deletions

View File

@ -48,6 +48,7 @@ begin
select min(timestamp) into @mts from (select timestamp from data_storage where topicid=tid order by timestamp desc limit 2) td ;
insert into data_retent select * from data_storage where topicid=tid and timestamp < @mts;
delete from data_storage where topicid=tid and timestamp < @mts;
commit;
end if;
until btopicdone end repeat;
close topic_curs;
@ -73,6 +74,7 @@ begin
end if;
if (select count(*) from topic_def where topic_def.topicid = newid and topic_def.feld=feldname) = 0 then
insert into topic_def (topicid,feld,inhalt) values (newid,feldname,@feldinhalt);
commit;
end if;
UNTIL bcfgDone END REPEAT;
close cfg_curs;
@ -105,13 +107,14 @@ BEGIN
insert into data_storage (timestamp,topicid,value) values (tstamp,@crctopic,invalue);
update last_data set value=invalue where topicid=@crctopic;
else
if (select count(distinct td.value) from (select data_storage.value from data_storage where topicid=@crctopic and (data_storage.timestamp > (unix_timestamp(now)-300)*1000) order by timestamp desc limit 2) td) = 1 then
if (select count(distinct td.value) from (select data_storage.value from data_storage where topicid=@crctopic and (data_storage.timestamp > (unix_timestamp(now())-300)*1000) order by timestamp desc limit 2) td) = 1 then
update data_storage set timestamp=tstamp where topicid=@crctopic and timestamp=(select max(timestamp) from data_storage where topicid=@crctopic);
else
insert into data_storage (timestamp,topicid,value) values (tstamp,@crctopic,invalue);
end if; # check if second last match
end if; # compare with last value
end if; # topicid is null
commit;
END
//
DELIMITER ;
@ -125,3 +128,4 @@ insert into tsensor.cfg_topic (feld,pos,minlength) values ('device',2,NULL);
insert into tsensor.cfg_topic (feld,pos,minlength) values ('quantity',-1,NULL);
insert into tsensor.cfg_topic (feld,pos,minlength) values ('place',3,NULL);
insert into tsensor.cfg_topic (feld,pos,minlength) values ('internal_id',4,5);
commit;

View File

@ -2,10 +2,9 @@
mosquitto_sub -h 172.24.42.2 -R -t 'tele/+/sens/#' -F "%t %p %U" | while read TOPIC VALU EPOCH; do
SEPOCH=$(echo $EPOCH|sed -e 's/\.//g'|cut -b -13)
LEPOCH=$(date +%s%N|cut -b -13)
TOPI=$(echo $TOPIC|sed -e 's/tele/sp/g')
IVAL=$(echo $VALU|cut -d\. -f1)
echo "call insert_data('$TOPI',$IVAL,$SEPOCH);"|mysql -t tsensor
echo "call insert_data('$TOPI',$IVAL,$EPOCH);"
echo "call insert_data('$TOPI',$IVAL,$SEPOCH);"|mysql -s -t tsensor
echo "call insert_data('$TOPI',$IVAL,$SEPOCH);"
done