From 428b776c5748cdfe60245fba4336c01b4f62064b Mon Sep 17 00:00:00 2001
From: Roman Bazalevsky <rvb@rvb.name>
Date: Sat, 25 Jan 2020 10:30:10 +0300
Subject: [PATCH] =?utf8?q?=D0=A0=D0=B0=D0=B7=D0=B1=D0=BB=D0=BE=D0=BA=D0=B8?=
 =?utf8?q?=D1=80=D0=BE=D0=B2=D0=BA=D0=B0=20=D0=91=D0=94=20=D0=B2=20=D1=81?=
 =?utf8?q?=D0=BB=D1=83=D1=87=D0=B0=D0=B5=20=D0=BE=D0=B1=D0=BD=D0=B0=D1=80?=
 =?utf8?q?=D1=83=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=20"=D0=B7=D0=B0=D0=B2=D0=B8?=
 =?utf8?q?=D1=81=D1=88=D0=B5=D0=B9"=20=D0=B1=D0=BB=D0=BE=D0=BA=D0=B8=D1=80?=
 =?utf8?q?=D0=BE=D0=B2=D0=BA=D0=B8.=20=D0=9F=D1=80=D0=B8=D1=87=D0=B8=D0=BD?=
 =?utf8?q?=D0=B0=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD?=
 =?utf8?q?=D0=B8=D1=8F=20=D0=BF=D0=BE=D0=BA=D0=B0=20=D0=BD=D0=B5=D0=BF?=
 =?utf8?q?=D0=BE=D0=BD=D1=8F=D1=82=D0=BD=D0=B0=20-=20=D0=B2=D1=81=D0=B5=20?=
 =?utf8?q?=D0=BF=D1=80=D0=BE=D1=86=D0=B5=D1=81=D1=81=D1=8B,=20=D1=80=D0=B0?=
 =?utf8?q?=D0=B1=D0=BE=D1=82=D0=B0=D0=B2=D1=88=D0=B8=D0=B5=20=D1=81=20?=
 =?utf8?q?=D0=91=D0=94,=20=D0=B7=D0=B0=D0=B2=D0=B5=D1=80=D1=88=D0=B8=D0=BB?=
 =?utf8?q?=D0=B8=D1=81=D1=8C,=20=D0=B1=D0=BB=D0=BE=D0=BA=20=D0=BD=D0=B5=20?=
 =?utf8?q?=D1=81=D0=BD=D1=8F=D1=82.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

---
 bin/weathermon | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/bin/weathermon b/bin/weathermon
index d2b5374..2c5419c 100755
--- a/bin/weathermon
+++ b/bin/weathermon
@@ -89,6 +89,14 @@ function printLog(str)
   end 
 end
 
+function unlock_db(file)
+
+  print("Unlocking DB "..file)
+  os.execute("sqlite3 -readonly \""..file.."\" \".backup /tmp/weathermon.db\"")
+  os.execute("mv /tmp/weathermon.db \""..file.."\"")
+
+end
+
 function submitValue(type,id,param,val)
 
   val = tonumber(val)
@@ -108,18 +116,31 @@ function submitValue(type,id,param,val)
       local backlog_con = assert(env:connect(backlogdb))
       backlog_con:execute('BEGIN TRANSACTION')
       backlog_con:execute(string.format("INSERT INTO queue(time_stamp,sensor_id,sensor,param,value) VALUES (datetime('now','localtime'),'%s','%s','%s',%f)",id,type,param,val))
-      backlog_con:execute('COMMIT')
+      local n,err = backlog_con:execute('COMMIT TRANSACTION')
       backlog_con:close()
+      
+      if err == "LuaSQL: database is locked" then
+        unlock_db(backlogdb);
+      end
+      
     end
 
   end
   
   if logdb then
+    print(logdb)
     local log_con = assert(env:connect(logdb))
     log_con:execute('BEGIN TRANSACTION')
     log_con:execute(string.format("INSERT INTO log(time_stamp,sensor_id,sensor,param,value) VALUES (datetime('now','localtime'),'%s','%s','%s',%f)",id,type,param,val))
-    log_con:execute('COMMIT')
+    local n,err = log_con:execute('COMMIT TRANSACTION')
     log_con:close()
+
+    print(n,err)
+
+    if err == "LuaSQL: database is locked" then
+      unlock_db(logdb);
+    end
+
   end
 
 end
@@ -395,7 +416,7 @@ while 1 do
     break
   end
 
-  pcall(function ()
+--  pcall(function ()
   
     printLog("Received: "..line)
     if startswith(line,'{') then
@@ -418,6 +439,6 @@ while 1 do
         io.close(f)
       end)  
     end
-  end)
+--  end)
     
 end
-- 
2.34.1