From e72e1a185290683953d671ade05fc06591ca55e5 Mon Sep 17 00:00:00 2001 From: Piotr Dobrowolski Date: Fri, 24 Feb 2017 01:51:13 +0100 Subject: The most important screen this month. --- nodes/currency.lua | 55 +++++++++++++++++++++++++++++++++++++++++++++++ nodes/currency_thread.lua | 22 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 nodes/currency.lua create mode 100644 nodes/currency_thread.lua diff --git a/nodes/currency.lua b/nodes/currency.lua new file mode 100644 index 0000000..081f7a3 --- /dev/null +++ b/nodes/currency.lua @@ -0,0 +1,55 @@ +local node = ThreadNode:extend('node.currency', { + threadFile = 'nodes/currency_thread.lua', + threadChannel = 'currency', + + updateInterval = 10, + state = { + values = {}, + changes = {}, + }, +}) + +local inspect = require('vendor.inspect') + +local textFont = love.graphics.newFont('fonts/Lato-Light.ttf', 90) +local headFont = love.graphics.newFont('fonts/Lato-Regular.ttf', 90) +local smallFont = love.graphics.newFont('fonts/Lato-Light.ttf', 30) + +function node:render() + love.graphics.setColor( 0, 0, 0 ) + love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight()) + + if self.state.values and self.state.values[2] then + local pad = 20 + love.graphics.setColor( 255, 255, 255 ) + love.graphics.setFont(headFont) + love.graphics.printf("BTCPLN", 0, 180, love.graphics.getWidth()/2, 'right') + love.graphics.printf("TRYIDR", 0, 350, love.graphics.getWidth()/2, 'right') + love.graphics.setFont(textFont) + + love.graphics.setColor( 0, 255, 0 ) + love.graphics.printf(self.state.values[1], love.graphics.getWidth()/2 + 2*pad, 180, love.graphics.getWidth()/2 - 2*pad, 'left') + love.graphics.printf(self.state.values[2], love.graphics.getWidth()/2 + 2*pad, 350, love.graphics.getWidth()/2 - 2*pad, 'left') + else + love.graphics.setColor( 255, 255, 255, 80 ) + + love.graphics.setFont(smallFont) + love.graphics.printf("Loading currency...", 0, love.graphics.getHeight() - 200, love.graphics.getWidth(), 'center') + end +end + +function node:onUpdate(v) + for n in ipairs(v.values) do + if self.state.values[n] then + if self.state.values[n] ~= v.values[n] then + self.state.changes[n] = self.state.values[n] > v.values[n] + end + else + self.state.changes[n] = true + end + end + + self.state.values = v.values +end + +return node diff --git a/nodes/currency_thread.lua b/nodes/currency_thread.lua new file mode 100644 index 0000000..77b98dc --- /dev/null +++ b/nodes/currency_thread.lua @@ -0,0 +1,22 @@ +local socket = require("socket") +local http = require("socket.http") +local json = require("vendor.json") +local lume = require("vendor.lume") + +local btcURL = 'http://www.bitmarket.pl/json/BTCPLN/ticker.json' +local tryURL = 'http://api.fixer.io/latest?base=TRY' + +local r, c, h = http.request(btcURL) +if c == 200 then + btcpln = json.decode(r)['last'] + local r, c, h = http.request(tryURL) + if c == 200 then + tryidr = json.decode(r)['rates']['IDR'] + end + love.thread.getChannel('currency'):push({ + values = {math.floor(btcpln), math.floor(tryidr)}, + }) + print("Update finished") +else + print("Update failed") +end -- cgit v1.2.3