-- afkdata bitcoin ticker
-- LICENCE: http://opensource.org/licenses/MIT
-- Steven Cole <steven.j.cole@gmail.com>
local json = require "cjson"
lcd.clear()
poll = 60 * 1000
secondTimer = 0 
secondLine = ""
slLoc = 1
function findJson(input)
     posStart =  string.find(input, "\r\n{", nil, true)
     --print("posStart:"..posStart)
     posEnd =  string.find(input, "}\r\n", nil, true)
     --print("posEnd:"..posEnd)
     if posStart == nil then
        return nil
     end
     
     if posEnd == nil then
        return nil
     end
     return string.sub(input, posStart, posEnd + 1)
end
function restart()
    node.restart()  
end
function connectionLost()
        lcd.clear()
        lcd.put(lcd.locate(0, 0), "Connection Lost")
        lcd.put(lcd.locate(1, 0), "Restarting..")
        tmr.alarm( 4 , 5000 , 0 , restart)
end
function TickTock()
    secondTimer = secondTimer + 1
    --print("ticktock "..secondTimer)
    if secondTimer > 300 then -- 5 minutes of no data
        connectionLost()
        return
    end
    
    tmr.alarm( 5 , 1000 , 0 , TickTock)
end
tmr.alarm( 5 , 1000 , 0 , TickTock)
function sendRequest()
    conn=net.createConnection(net.TCP, 30) 
    conn:on("receive", function(conn, payload) 
        --print(payload)
        
        tempString = findJson(payload)
        if tempString == nil then
            print("Json not found")
            return
        end
        
        record = json.decode(tempString)
          
        lcd.put(lcd.locate(0, 0), record["display"]["Line1"]) 
        if(type(record["display"]["Line2"]) == "string") then
            lcd.put(lcd.locate(1, 0), record["display"]["Line2"]) 
        else
            secondLine = record["display"]["Line2"]
            fancySecondLine() -- do the first one now
            tmr.alarm( 6 , 5000 , 0 , fancySecondLine)  -- do n'th one in 5 seconds
        end
        
        secondTimer = 0
        
         if MODE == "2" then
           lcd.light(1)
         end
         
         --print (record["poll"] * 1000)
         poll = record["poll"] * 1000
         --print("Going to deep sleep for "..(poll/1000).." seconds")
         --node.dsleep((poll-1000)*1000)             
         
      end)
    conn:on("connection", function(sck) 
        sck:send("GET /api/screen?@Code="..CODE.."&@SecondLine="..SECOND.." HTTP/1.1\r\nHost: cryptofolio.info\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") 
      end)
    conn:connect(80,"cryptofolio.info")
end
function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end
function fancySecondLine()
    lcd.put(lcd.locate(1, 0), secondLine[slLoc]) 
    slLoc = slLoc + 1
    if (slLoc > tablelength(secondLine)) then
        slLoc = 1
    end
    
    tmr.alarm( 6 , 5000 , 0 , fancySecondLine)
end
function getPrices()
    --lcd.clear()
    
    print(MODE)
    if wifi.sta.status() ~= 5 then
        connectionLost()
        return
    end
        
    if MODE == "2" then
            lcd.light(0)
    end
    if MODE == "3" then
            lcd.put(lcd.locate(0, 0), "*")
            lcd.put(lcd.locate(0, 15), "*")
            lcd.put(lcd.locate(1, 0), "*")
            lcd.put(lcd.locate(1, 15), "*")
    end
    
sendRequest()
tmr.alarm( 3 , poll , 0 , getPrices)
   
end
getPrices()   -- Get the price now.
