node.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. gl.setup(1920, 1080)
  2. util.resource_loader{
  3. "font.ttf";
  4. "background.png";
  5. "logo.png";
  6. "spaceapi_logo.png";
  7. }
  8. local json = require('json')
  9. local white = resource.create_colored_texture(1,1,1,1)
  10. --
  11. -- Global variables and default values
  12. --
  13. local bus = "in N/A minutes"
  14. local power = 1337
  15. local network_in = '1337 MB'
  16. local network_out = '1337 MB'
  17. local spaceapi = {
  18. ["Status"] = "Unknown",
  19. ["Device present"] = "N/A",
  20. --["Member count"] = "N/A",
  21. ["URL"] = "Unknown",
  22. }
  23. local events = {
  24. {
  25. ["s_day"] = '23',
  26. ["s_month"] = 'Feb',
  27. ["s_time"] = '19:99',
  28. ["name"] = 'No event',
  29. }
  30. }
  31. --
  32. -- Communication
  33. --
  34. util.file_watch("events.json", function(data)
  35. events = {}
  36. evt = json.decode(data)
  37. week = evt.week_events
  38. for i, f in ipairs(week) do
  39. table.insert(events, f)
  40. end
  41. future = evt.future_events
  42. for i, f in ipairs(future) do
  43. table.insert(events, f)
  44. end
  45. end)
  46. util.file_watch("spaceapi.json", function(data)
  47. api = json.decode(data)
  48. spaceapi = {
  49. ["Status"] = api['state']['message'],
  50. ["Device present"] = api['sensors']['people_now_present'][1]['value'],
  51. ["Member count"] = api['sensors']['total_member_count'][3]['value'],
  52. }
  53. end)
  54. util.data_mapper{
  55. ["power/set"] = function(content)
  56. power = content
  57. end;
  58. ["network/in/set"] = function(content)
  59. network_in = content
  60. end;
  61. ["network/out/set"] = function(content)
  62. network_out = content
  63. end;
  64. }
  65. util.file_watch("bus.txt", function(content)
  66. bus = content
  67. end)
  68. --
  69. -- Fragments
  70. --
  71. function fixme()
  72. function draw()
  73. background:draw(0,0,WIDTH,HEIGHT)
  74. --logo:draw(WIDTH-250,5,WIDTH-35,185)
  75. font:write(150, 50, "FIXME", 200, 1,1,1,1)
  76. end
  77. return {
  78. draw = draw;
  79. }
  80. end
  81. function scroller()
  82. function texts()
  83. return {
  84. "https://git.fixme.ch/FIXME/screen",
  85. --"Next bus (Renens-Village) " .. bus,
  86. }
  87. end
  88. local text = util.running_text{
  89. font = font;
  90. size = 80;
  91. speed = 150;
  92. color = {1,1,1,1};
  93. generator = util.generator(texts)
  94. }
  95. function draw()
  96. text:draw(HEIGHT-95)
  97. end
  98. return {
  99. draw = draw;
  100. }
  101. end
  102. --
  103. -- Main screen cycler
  104. --
  105. function cycler(panels, interval)
  106. local panel
  107. local next_cycle = 0
  108. function next_panel()
  109. panel = panels.next()
  110. next_cycle = sys.now() + interval
  111. end;
  112. function draw()
  113. if sys.now() > next_cycle then
  114. next_panel()
  115. end;
  116. panel:draw()
  117. local remaining = next_cycle - sys.now()
  118. --if remaining < 0.2 or remaining > 4.8 then
  119. -- print(remaining)
  120. --end
  121. local size = 5
  122. local xpos = 270
  123. white:draw(remaining / interval * WIDTH, xpos, 0, xpos + size)
  124. end;
  125. return {
  126. draw = draw
  127. }
  128. end;
  129. function panel_events()
  130. function draw()
  131. pos = 360
  132. for i, e in ipairs(events) do
  133. date = string.format("%s %s %s", e.s_day, e.s_month, e.s_time)
  134. font:write(150, pos, date, 60, 0.5,0.5,0.5,1)
  135. font:write(150, pos+60, e.name, 80, 1,1,1,1)
  136. pos = pos + 135
  137. if i > 3 then
  138. break
  139. end
  140. end
  141. end;
  142. return {
  143. draw = draw;
  144. }
  145. end;
  146. function panel_spaceapi()
  147. -- TODO: Maybe instead pick a random space from the api ?
  148. function draw()
  149. -- Logo
  150. util.draw_correct(spaceapi_logo, 50, 400, WIDTH-1200, HEIGHT-250)
  151. -- Fields
  152. local xpos = 700
  153. local ypos = 450
  154. for k, v in pairs(spaceapi) do
  155. local name = string.format("%-16s", k)
  156. font:write(xpos, ypos, name, 60, 1,1,1,1)
  157. font:write(xpos+500, ypos+10, v, 40, 0.5, 0.5, 0.5,1)
  158. ypos = ypos + 90
  159. end
  160. end;
  161. return {
  162. draw = draw;
  163. }
  164. end;
  165. function panel_clock()
  166. function draw()
  167. --TODO: Add date
  168. util.draw_correct(resource.render_child("analogclock"), 550, 400, WIDTH-550, HEIGHT-200)
  169. end;
  170. return {
  171. draw = draw;
  172. }
  173. end;
  174. --
  175. -- MAIN
  176. --
  177. local fix = fixme()
  178. local scroll = scroller()
  179. local cycling = cycler(util.generator(function()
  180. return {
  181. panel_spaceapi(),
  182. panel_events(),
  183. panel_clock(),
  184. }
  185. end), 5)
  186. function node.render()
  187. fix:draw()
  188. cycling:draw()
  189. scroll:draw()
  190. local pos = 30
  191. font:write(WIDTH-650, pos, "DL: " .. network_in .. "/s", 80, 1,1,1,1)
  192. font:write(WIDTH-650, pos+70, "UP: " .. network_out .. "/s", 80, 1,1,1,1)
  193. font:write(WIDTH-650, pos+140, "Power: " .. power .. " W", 80, 1,1,1,1)
  194. end