node.lua 5.0 KB

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