From 52680074ae2143b5d92e1c99c36a53d4a9c01471 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Fri, 6 Jan 2017 22:49:35 +0100
Subject: fix last cart check


diff --git a/src/noprml b/src/noprml
index 596b2f6..16171d4 100755
--- a/src/noprml
+++ b/src/noprml
@@ -35,7 +35,7 @@ local s, ns = assert(string.match(time, "([0-9]+) ([0-9]+)"))
 local timestamp = s .. string.format("%06d", math.floor(ns/1000))
 
 -- check arguments
-if #arg < 4 then
+if #arg < 5 then
    io.stderr:write("too few parameters\n")
    os.exit(1)
 end
@@ -55,7 +55,7 @@ if q == nil then
 end
 
 -- send out message to nopfetchd
-local result, err = mq.send(q, timestamp .. " " .. arg[1] .. " " .. arg[2] .. " " .. arg[3] .. " " .. arg[4], 0)
+local result, err = mq.send(q, timestamp .. " " .. arg[1] .. " " .. arg[2] .. " " .. arg[3] .. " " .. arg[4] .. " " .. arg[5], 0)
 if result == nil then
    io.stderr:write("sending message failed: " .. err .. "\n")
    os.exit(2)
diff --git a/src/playlog.lua b/src/playlog.lua
index b93a3cc..591468f 100644
--- a/src/playlog.lua
+++ b/src/playlog.lua
@@ -25,7 +25,7 @@ luasql = require "luasql.mysql"
 -- GRANT select,insert,update ON rhnop.* TO 'nopfetchd' IDENTIFIED BY '<password>';
 -- GRANT select ON rhnop.* TO 'nopsyncd' IDENTIFIED BY '<password>';
 -- USE rhnop
--- CREATE TABLE IF NOT EXISTS now (timestamp BIGINT UNSIGNED PRIMARY KEY NOT NULL, cart INT NOT NULL, len INT, showtitle VARCHAR(255), title VARCHAR(255), artist VARCHAR(255), album VARCHAR(255), carttype ENUM('show','pool','jingle'));
+-- CREATE TABLE IF NOT EXISTS now (timestamp BIGINT UNSIGNED PRIMARY KEY NOT NULL, cart INT NOT NULL, len INT, showtitle VARCHAR(255), title VARCHAR(255), artist VARCHAR(255), album VARCHAR(255), carttype ENUM('show','pool','jingle'), output INT NOT NULL);
 -- ALTER TABLE now CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
 
 local playlog = {}
@@ -56,8 +56,8 @@ function playlog:init(cnf)
    return true
 end
 
-function playlog:getLastCart()
-   local cur, err = self.con:execute("SELECT cart FROM now WHERE timestamp = (SELECT MAX(timestamp) FROM now)")
+function playlog:getLastCart(output, timestamp)
+   local cur, err = self.con:execute("SELECT cart FROM now WHERE timestamp = " .. timestamp .. " and output = " .. output)
    if cur == nil then
       return nil, err
    end
@@ -67,10 +67,30 @@ function playlog:getLastCart()
    return cart
 end
 
-function playlog:insertNow(timestamp, cart, len, showtitle, title, artist, album, carttype)
+function playlog:getLastCarts()
+   local cur, err = self.con:execute("select output,max(timestamp) as timestamp from now GROUP BY output")
+   if cur == nil then
+      return nil, err
+   end
+
+   local carts = {}
+   while true do
+      local data = {}
+      data = cur:fetch(data, "a")
+      if data == nil then break end
+
+      t[data.output] = self:getLastCart(data.output, data.timestamp)
+   end
+   cur:close()
+
+   return t
+end
+
+function playlog:insertNow(timestamp, cart, len, showtitle, title, artist, album, carttype, output)
    cart = tonumber(cart)
    len = tonumber(len)
-   local cur, err = self.con:execute("INSERT into now VALUES(" .. self.con:escape(timestamp) .. ", " .. cart .. ", " .. len .. ", '" .. self.con:escape(showtitle) .. "', '" .. self.con:escape(title) .. "', '" .. self.con:escape(artist) .."', '" .. self.con:escape(album) .. "', '" .. self.con:escape(carttype) .. "')")
+   output = tonumber(output)
+   local cur, err = self.con:execute("INSERT into now VALUES(" .. self.con:escape(timestamp) .. ", " .. cart .. ", " .. len .. ", '" .. self.con:escape(showtitle) .. "', '" .. self.con:escape(title) .. "', '" .. self.con:escape(artist) .."', '" .. self.con:escape(album) .. "', '" .. self.con:escape(carttype) .. "', " .. output .. ")")
    if cur == nil then
       return nil, err
    end
diff --git a/src/qlistener.lua b/src/qlistener.lua
index 86004ee..5e31890 100644
--- a/src/qlistener.lua
+++ b/src/qlistener.lua
@@ -19,7 +19,7 @@
 --  along with rhnop. If not, see <http://www.gnu.org/licenses/>.
 --
 
-local last_cart = nil
+local last_carts = nil
 
 posix = require "posix"
 mq = require "mq"
@@ -59,18 +59,19 @@ function handle_now(timestamp, nowcart, nowlen)
 end
 
 function handle_message(msg, q)
-   local timestamp, nowcart, nowlen, nextcart, nextlen = string.match(msg, "^(%d+) (%d+) (%d+) (%d+) (%d+)$");
-   if not timestamp or not nowcart or not nowlen or not nextcart or not nextlen then
+   local timestamp, output, nowcart, nowlen, nextcart, nextlen = string.match(msg, "^(%d+) (%d+) (%d+) (%d+) (%d+) (%d+)$");
+   if not timestamp or not output or not nowcart or not nowlen or not nextcart or not nextlen then
       io.stderr:write("WARN: ignoring malformed message\n")
    else
       nowcart = tonumber(nowcart)
       nowlen = tonumber(nowlen)
-      if last_cart ~= nowcart and nowlen > 0 then
-         last_cart = nowcart
+      if last_carts[output] == nil then last_carts[output] = 0 end
+      if last_carts[output] ~= nowcart and nowlen > 0 then
+         last_carts[output] = nowcart
          local ret = handle_now(timestamp, nowcart, nowlen)
          if ret == nil then
             io.stderr:write("INFO: trying to push last message back onto the queue - before exiting\n")
-            local result, err = mq.send(q, timestamp .. " " .. nowcart .. " " .. nowlen .. " " .. nextcart .. " " .. nextlen, 0)
+            local result, err = mq.send(q, timestamp .. " " .. output .. " " .. nowcart .. " " .. nowlen .. " " .. nextcart .. " " .. nextlen, 0)
             if result == nil then
                io.stderr:write("ERROR: sending message failed: " .. err .. "\n")
             end
@@ -100,7 +101,7 @@ function main_loop()
       io.stderr:write("ERROR: creation of playlog failed: " .. err .. "\n")
       os.exit(1)
    end
-   last_cart = assert(playlog:getLastCart())
+   last_carts = assert(playlog:getLastCarts())
    print("PLAYLOG: connected to " .. cnf.playlog_db .. "@" .. cnf.playlog_host .. " with user '" .. cnf.playlog_user .. "'")
 
    local ret, err = rddb:init(cnf)
-- 
cgit v0.10.2

