node.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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'][0]['value'],
  51. -- ["Member count"] = api['sensors']['total_member_count'][2]['value'],
  52. ["URL"] = api['url'],
  53. }
  54. end)
  55. util.data_mapper{
  56. ["power/set"] = function(content)
  57. power = content
  58. end;
  59. ["network/in/set"] = function(content)
  60. network_in = content
  61. end;
  62. ["network/out/set"] = function(content)
  63. network_out = content
  64. end;
  65. }
  66. util.file_watch("bus.txt", function(content)
  67. bus = content
  68. end)
  69. --
  70. -- Fragments
  71. --
  72. function fixme()
  73. function draw()
  74. background:draw(0,0,WIDTH,HEIGHT)
  75. --logo:draw(WIDTH-250,5,WIDTH-35,185)
  76. font:write(150, 50, "FIXME", 200, 1,1,1,1)
  77. end
  78. return {
  79. draw = draw;
  80. }
  81. end
  82. function scroller()
  83. function texts()
  84. return {
  85. "https://git.fixme.ch/FIXME/screen",
  86. --"Next bus (Renens-Village) " .. bus,
  87. }
  88. end
  89. local text = util.running_text{
  90. font = font;
  91. size = 80;
  92. speed = 150;
  93. color = {1,1,1,1};
  94. generator = util.generator(texts)
  95. }
  96. function draw()
  97. text:draw(HEIGHT-95)
  98. end
  99. return {
  100. draw = draw;
  101. }
  102. end
  103. --
  104. -- Main screen cycler
  105. --
  106. function cycler(panels, interval)
  107. local panel
  108. local next_cycle = 0
  109. function next_panel()
  110. panel = panels.next()
  111. next_cycle = sys.now() + interval
  112. end;
  113. function draw()
  114. if sys.now() > next_cycle then
  115. next_panel()
  116. end;
  117. panel:draw()
  118. local remaining = next_cycle - sys.now()
  119. --if remaining < 0.2 or remaining > 4.8 then
  120. -- print(remaining)
  121. --end
  122. local size = 5
  123. local xpos = 270
  124. white:draw(remaining / interval * WIDTH, xpos, 0, xpos + size)
  125. end;
  126. return {
  127. draw = draw
  128. }
  129. end;
  130. function panel_events()
  131. function draw()
  132. pos = 360
  133. for i, e in ipairs(events) do
  134. date = string.format("%s %s %s", e.s_day, e.s_month, e.s_time)
  135. font:write(150, pos, date, 60, 0.5,0.5,0.5,1)
  136. font:write(150, pos+60, e.name, 80, 1,1,1,1)
  137. pos = pos + 135
  138. if i > 3 then
  139. break
  140. end
  141. end
  142. end;
  143. return {
  144. draw = draw;
  145. }
  146. end;
  147. function panel_spaceapi()
  148. -- TODO: Maybe instead pick a random space from the api ?
  149. function draw()
  150. -- Logo
  151. util.draw_correct(spaceapi_logo, 50, 400, WIDTH-1200, HEIGHT-250)
  152. -- Fields
  153. local xpos = 700
  154. local ypos = 450
  155. for k, v in pairs(spaceapi) do
  156. local name = string.format("%-16s", k)
  157. font:write(xpos, ypos, name, 60, 1,1,1,1)
  158. font:write(xpos+500, ypos+10, v, 40, 0.5, 0.5, 0.5,1)
  159. ypos = ypos + 90
  160. end
  161. end;
  162. return {
  163. draw = draw;
  164. }
  165. end;
  166. function panel_clock()
  167. function draw()
  168. --TODO: Add date
  169. util.draw_correct(resource.render_child("analogclock"), 550, 400, WIDTH-550, HEIGHT-200)
  170. end;
  171. return {
  172. draw = draw;
  173. }
  174. end;
  175. --
  176. -- MAIN
  177. --
  178. local fix = fixme()
  179. local scroll = scroller()
  180. local cycling = cycler(util.generator(function()
  181. return {
  182. panel_spaceapi(),
  183. panel_events(),
  184. panel_clock(),
  185. }
  186. end), 5)
  187. function node.render()
  188. fix:draw()
  189. cycling:draw()
  190. scroll:draw()
  191. local pos = 30
  192. font:write(WIDTH-650, pos, "DL: " .. network_in .. "/s", 80, 1,1,1,1)
  193. font:write(WIDTH-650, pos+70, "UP: " .. network_out .. "/s", 80, 1,1,1,1)
  194. font:write(WIDTH-650, pos+140, "Power: " .. power .. " W", 80, 1,1,1,1)
  195. end