-
Notifications
You must be signed in to change notification settings - Fork 0
/
covidindoX.lua
357 lines (319 loc) · 8.75 KB
/
covidindoX.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
--Data provided by Kawal Corona API
local utils = require('lib/utils')
local json = require('lib/json')
local tableExt = require('lib/tableExt')
local WinX = require('lib/WinX')
--catch argument
local logMode = false
local redGradientMode = false
local ops = utils.getOptions(arg)
--print(tableExt.dump(ops))
if #ops > 0 then
for k,v in ipairs(ops) do
if v == "log" or v=="l" then
logMode = true
elseif v == "red" or v=="r" then
redGradientMode = true
end
end
end
--code to color
local function getColor(n)
local ans = colors.white
if n > 5/6 then
ans = colors.purple
elseif n > 4/6 then
ans = colors.red
elseif n > 3/6 then
ans = colors.orange
elseif n > 2/6 then
ans = colors.green
elseif n > 1/6 then
ans = colors.lime
else
ans = colors.yellow
end
return ans
end
--get and clear monitor
local m = peripheral.wrap('left')
m.setTextScale(0.5)
m.clear()
--get current terminal
local t = term.current()
--enable red gradient mode
local function setColor(mode)
if mode then
m.setPaletteColor(colors.yellow, 0xebcdc6)
m.setPaletteColor(colors.lime, 0xe3ab9e)
m.setPaletteColor(colors.green, 0xd88877)
m.setPaletteColor(colors.orange, 0xc96651)
m.setPaletteColor(colors.red, 0xb8412e)
m.setPaletteColor(colors.purple, 0xa50a0a)
else
m.setPaletteColor(colors.yellow, 0xDEDE6C)
m.setPaletteColor(colors.lime, 0x7FCC19)
m.setPaletteColor(colors.green, 0x57A64E)
m.setPaletteColor(colors.orange, 0xF2B233)
m.setPaletteColor(colors.red, 0xCC4C4C)
m.setPaletteColor(colors.purple, 0x191919)
end
end
setColor(redGradientMode)
--read mapdata
local data = fs.open("mapindo.json","r")
local cvdata = data.readAll()
data.close()
local mapData = json.decode(cvdata)
--read code to region name
data = fs.open("c2nindo.json","r")
cvdata = data.readAll()
data.close()
local c2name = json.decode(cvdata)
--build windows
local titleWindow = window.create(m,2,2,97,6)
local dataWindow = window.create(m,1,8,33,1)
local creatorWindow = window.create(m,100,2,20,3)
local timeWindow = window.create(m,92,8,30,1)
local mapWindow = WinX("mapWindow",m,1,9,121,44)
local legendWindow = WinX("legendWindow",mapWindow,6,32,14,10)
local totalWindow = WinX("totalWindow",mapWindow,102,6,19,6)
local popupWindow = WinX("popupWindow",mapWindow,1,1,27,6,false)
--read timestamp
local tstampfile = fs.open("tsindo","r")
local tstamp = tonumber(tstampfile.readAll())
tstampfile.close()
local tstampnow = os.time(os.date("*t"))
local totalData
local function updateData()
print("Updating data...")
local getdata = http.get("https://api.kawalcorona.com/indonesia/provinsi")
cvdata = getdata.readAll()
local savedata = fs.open("dataindo.json","w")
savedata.write(cvdata)
savedata.close()
getdata = http.get("https://api.kawalcorona.com/indonesia")
totalData = getdata.readAll()
savedata = fs.open("indototal.json","w")
savedata.write(totalData)
savedata.close()
tstampnow = os.time(os.date("*t"))
tstamp = tstampnow
savedata = fs.open("tsindo","w")
savedata.write(tstampnow)
savedata.close()
end
if tstampnow-tstamp >= 600 then
--update saved data
updateData()
else
--read saved data
print("Opening data...")
data = fs.open("dataindo.json","r")
cvdata = data.readAll()
data.close()
data = fs.open("indototal.json","r")
totalData = data.readAll()
data.close()
end
local function drawLegend()
legendWindow:drawWindow(colors.lightBlue, colors.lightGray)
totalWindow:drawWindow(colors.lightBlue, colors.lightGray)
legendWindow:paintPixel(4,3,colors.white)
legendWindow:paintPixel(4,4,colors.yellow)
legendWindow:paintPixel(4,5,colors.lime)
legendWindow:paintPixel(4,6,colors.green)
legendWindow:paintPixel(4,7,colors.orange)
legendWindow:paintPixel(4,8,colors.red)
legendWindow:paintPixel(4,9,colors.purple)
legendWindow:setTextColor(colors.black)
legendWindow:setBackgroundColor(colors.lightGray)
legendWindow:setCursorPos(2,2)
legendWindow:write("Color Scale:")
legendWindow:setCursorPos(6,3)
legendWindow:write("= 0.00")
legendWindow:setCursorPos(6,4)
legendWindow:write("> 0.00")
legendWindow:setCursorPos(6,5)
legendWindow:write("> 0.17")
legendWindow:setCursorPos(6,6)
legendWindow:write("> 0.33")
legendWindow:setCursorPos(6,7)
legendWindow:write("> 0.50")
legendWindow:setCursorPos(6,8)
legendWindow:write("> 0.67")
legendWindow:setCursorPos(6,9)
legendWindow:write("> 0.83")
totalWindow:setTextColor(colors.black)
totalWindow:setBackgroundColor(colors.lightGray)
totalWindow:setCursorPos(2,2)
totalWindow:write(" Total Cases")
totalWindow:setCursorPos(2,3)
totalWindow:write("Confirmed: "..totalData.positif)
totalWindow:setCursorPos(2,4)
totalWindow:write("Deaths : "..totalData.meninggal)
totalWindow:setCursorPos(2,5)
totalWindow:write("Recovered: "..totalData.sembuh)
end
local function drawMap()
--paint map
mapWindow:drawImage('indo',1,1,true)
end
drawMap()
local function drawHeader()
--paint title
utils.drawImage(titleWindow,'titleindo',1,1,true)
creatorWindow.write("by Purplefin Neptuna")
creatorWindow.setCursorPos(1,3)
creatorWindow.write("WinX Mode")
dataWindow.write("Data provided by Kawal Corona API")
timeWindow.write(os.date("Last Update: %x %X",tstamp))
end
drawHeader()
local cv = {}
local codeToColor = {}
local maxCase = 0
local function parseData()
--process json
local cvdata = json.decode(cvdata)
print("Got "..tableExt.length(cvdata).." confirmed regions")
-- CV is region based covid cv[region_code]
local cvlen = tableExt.length(cvdata)
for i = 1, cvlen, 1 do
local region = cvdata[i].attributes
cv[region.Kode_Provi] = {
["death"] = region.Kasus_Meni,
["recov"] = region.Kasus_Semb,
["confi"] = region.Kasus_Posi
}
end
--get max case
for k,v in pairs(cv) do
maxCase = math.max(v.confi,maxCase)
end
--process total json
totalData = json.decode(totalData)
totalData = totalData[1]
totalData.positif = string.gsub(totalData.positif, ',', '')
totalData.sembuh = string.gsub(totalData.sembuh, ',', '')
totalData.meninggal = string.gsub(totalData.meninggal, ',', '')
end
parseData()
local function calculateData()
--calculate heat level per region
for k,v in pairs(cv) do
if logMode then
codeToColor[k] = getColor(math.log(v.confi)/math.log(maxCase))
else
codeToColor[k] = getColor(v.confi/maxCase)
end
end
end
calculateData()
--draw popup window for region data
local popupDrawn = false
local function drawPopup(cid, x, y)
local rx = x
local ry = y
local nx = x
local ny = y
local popName = c2name[tostring(cid)]
local numPos
local numDed
local numRec
if cv[cid] ~= nil then
numPos = cv[cid].confi
numDed = cv[cid].death
numRec = cv[cid].recov
else
numPos = 0
numDed = 0
numRec = 0
end
local popPos = "Confirmed: "..numPos
local popDed = "Deaths : "..numDed
local popRec = "Recovered: "..numRec
local maxL = math.max(#popName, #popPos, #popDed, #popRec)
local maxD = math.max(#popPos, #popDed, #popRec)
popPos = popPos..string.rep(" ",maxD-#popPos)
popDed = popDed..string.rep(" ",maxD-#popDed)
popRec = popRec..string.rep(" ",maxD-#popRec)
maxL = maxL
if rx > 60 then
nx = nx - maxL - 2
else
nx = nx + 1
end
if ry > 22 then
ny = ny - 6
else
ny = ny + 1
end
popupWindow:reposition(nx,ny,maxL+2,6)
popupWindow:drawWindow(colors.lightBlue, colors.lightGray)
popupWindow:setTextColor(colors.black)
popupWindow:setBackgroundColor(colors.lightGray)
popupWindow:setCursorPos(2,2)
popupWindow:write(utils.stringMiddle(popName,maxL))
popupWindow:setCursorPos(2,3)
popupWindow:write(utils.stringMiddle(popPos,maxL))
popupWindow:setCursorPos(2,4)
popupWindow:write(utils.stringMiddle(popDed,maxL))
popupWindow:setCursorPos(2,5)
popupWindow:write(utils.stringMiddle(popRec,maxL))
end
--draw map color based on case
local function drawHeat()
for i = 1, 121 do
for j = 1, 44 do
local pData = mapData[i][j]
if pData ~= 0 then
local cData = codeToColor[pData]
if cData ~= nil then
mapWindow:paintPixel(i,j,cData)
end
end
end
end
end
drawHeat()
drawLegend()
--for force recalculate
local function recalculate()
calculateData()
drawHeat()
end
--for force update data
local function forceUpdate()
updateData()
parseData()
drawHeader()
recalculate()
end
local running = true
while running do
local p = {}
p[1], p[2], p[3], p[4], p[5] = os.pullEventRaw()
if p[1] == 'monitor_touch' then
local nx = p[3]
local ny = p[4] - 8
if ny > 0 and nx > 0 then
local reg = mapData[nx][ny]
if reg == 0 and popupDrawn then
popupDrawn = false
popupWindow:setVisible(false)
elseif reg ~= 0 then
if not popupDrawn then
popupWindow:setVisible(true)
end
drawPopup(reg, p[3], p[4]-8)
popupDrawn = true
end
end
elseif p[1] == "terminate" then
printError("App Terminated.")
setColor(false)
m.setPaletteColor(colors.purple, 0xb266e5)
running = false
end
end