From 1d05cd83ccb6ab3d2efbda8a6981204966c2c6d3 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Fri, 6 Jan 2017 00:07:55 +0100
Subject: use real pool and jingle name


diff --git a/src/qlistener.lua b/src/qlistener.lua
index d3fc9fd..86004ee 100644
--- a/src/qlistener.lua
+++ b/src/qlistener.lua
@@ -39,12 +39,12 @@ function handle_now(timestamp, nowcart, nowlen)
    local results, err = rddb:getCartInfo(nowcart)
    if results == nil then
       io.stderr:write("ERROR: can't fetch cart info: " .. err .. "\n")
-      return nil
+      return true
    else
-      local showtitle, carttype, err = rddb:getCartShowName(nowcart)
+      local showtitle, carttype = rddb:getCartShowName(nowcart)
       if showtitle == nil then
-         io.stderr:write("ERROR: can't fetch show/pool name: " .. err .. "\n")
-         return nil
+         io.stderr:write("ERROR: can't fetch show/pool name: " .. carttype .. "\n")
+         return true
       else
          local ret, err = playlog:insertNow(timestamp, nowcart, nowlen, showtitle, results.TITLE, results.ARTIST, results.ALBUM, carttype)
          if ret == nil then
diff --git a/src/rddb.lua b/src/rddb.lua
index b18ae67..0827f62 100644
--- a/src/rddb.lua
+++ b/src/rddb.lua
@@ -65,9 +65,51 @@ function rddb:getCartInfo(cartnum)
    return results, err
 end
 
+function rddb:getGroupNameAndCartType(cartnum)
+   local cur, err = self.con:execute("select NAME,DESCRIPTION from GROUPS where DEFAULT_LOW_CART <= " .. self.con:escape(cartnum) .. " and DEFAULT_HIGH_CART >= " .. self.con:escape(cartnum))
+   if cur == nil then
+      return nil, err
+   end
+
+   local groupname = nil
+   local carttype = nil
+   local groupdesc = nil
+   while true do
+      local data = {}
+      data = cur:fetch(data, "a")
+      if data == nil then break end
+      if data.NAME == "ALL_SHOWS" then
+         carttype = "show"
+      elseif data.NAME == "ALL_POOLS" then
+         carttype = "pool"
+      elseif data.NAME == "ALL_JINGLE" then
+         carttype = "jingle"
+      else
+         groupname = data.NAME
+         groupdesc = data.DESCRIPTION
+      end
+      if groupname and carttype then break end
+   end
+   cur:close()
+
+   if carttype == nil then
+      return nil, "unable to detect group type for cart: " .. cartnum
+   end
+   if groupname == nil then
+      return nil, "can't find any group for cart: " .. cartnum
+   end
+   return carttype, groupname, groupdesc
+end
+
 function rddb:getCartShowName(cartnum)
-   --TODO: implement this
-   return "unknown", "music", nil
+   local carttype, groupname, groupdesc = self:getGroupNameAndCartType(cartnum)
+   if carttype == nil then
+      return nil, groupname
+   end
+   if carttype == "show" then
+      return nil, "show carts not yet supported"
+   end
+   return groupdesc, carttype
 end
 
 function rddb:close()
-- 
cgit v0.10.2