Advertisement
infiniteblock

Untitled

Jan 1st, 2020
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.73 KB | None | 0 0
  1. if warpdriveCommons then os.unloadAPI("warpdriveCommons") end
  2. if not os.loadAPI("warpdrive/warpdriveCommons") then error("missing warpdriveCommons") end
  3. local w = warpdriveCommons.w
  4. local uri = ""
  5. ----------- Accelerator support
  6.  
  7. local lhc_controlPoints = {}
  8. local lhc_parameters = {}
  9. local accelerator = nil
  10.  
  11. function lhc_boot(isDetailed)
  12. if accelerator == nil or accelerator.isInterfaced() == nil then
  13. return
  14. end
  15.  
  16. if isDetailed == nil then
  17. isDetailed = true
  18. end
  19.  
  20. if isDetailed then
  21. w.write("Booting Accelerator controller")
  22.  
  23. w.writeLn("...")
  24. w.sleep(0.1)
  25.  
  26. w.setColorNormal()
  27. w.write("- checking assembly : ")
  28. end
  29.  
  30. local timeout = 10
  31. local isValid, message
  32. local count
  33. repeat
  34. isValid, message = accelerator.getAssemblyStatus()
  35. count = accelerator.getControlPointsCount()
  36. w.sleep(0.05)
  37. timeout = timeout - 1
  38. until (isValid == true and count >= 0) or timeout < 0
  39. if timeout < 0 then
  40. w.setColorWarning()
  41. w.writeLn("failed")
  42. w.writeLn(message)
  43. w.setColorNormal()
  44. w.sleep(6)
  45. w.reboot()
  46. elseif isDetailed then
  47. w.setColorSuccess()
  48. w.writeLn("passed")
  49. end
  50. w.sleep(0.2)
  51.  
  52. -- getting control points
  53. lhc_controlPoints = {}
  54. local lhc_controlChannels = { }
  55. local countParameters = 0
  56.  
  57. if count ~= nil and count > 0 then
  58. for i = 0, count - 1 do
  59. local success, x, y, z, tier, type, isEnabled, controlChannel = accelerator.getControlPoint(i)
  60. if success then
  61. if isDetailed then
  62. w.write(type .. " " .. tier .. " #" .. controlChannel .. " @ (" .. w.format_integer(x, 6) .. " " .. w.format_integer(y, 3) .. " " .. w.format_integer(z, 6) .. ") ")
  63. if isEnabled then
  64. w.writeLn("Enabled")
  65. else
  66. w.writeLn("Disabled")
  67. end
  68. end
  69. lhc_controlPoints[i + 1] = { x, y, z, tier, type, isEnabled, controlChannel }
  70. lhc_controlChannels[controlChannel] = "true"
  71. countParameters = countParameters + 1
  72. elseif isDetailed then
  73. w.setColorWarning()
  74. w.writeLn("Error " .. x)
  75. w.setColorNormal()
  76. w.sleep(0.5)
  77. end
  78. end
  79. elseif isDetailed then
  80. w.setColorWarning()
  81. w.writeLn("No control point detected!")
  82. w.sleep(0.5)
  83. end
  84.  
  85. -- getting parameters
  86. local controlChannels = { accelerator.getParametersControlChannels() }
  87. if controlChannels ~= nil and #controlChannels > 0 then
  88. for key, value in pairs(controlChannels) do lhc_controlChannels[value] = { } end
  89. countParameters = countParameters + 1
  90. end
  91. lhc_parameters = {}
  92. if isDetailed then w.sleep(0.1) end
  93.  
  94. if countParameters ~= 0 then
  95. local index = 1
  96. for controlChannel, _ in pairs(lhc_controlChannels) do
  97. local success, controlChannelCheck, isEnabled, threshold, description = accelerator.parameter(controlChannel)
  98. if success ~= nil and success then
  99. if isDetailed then
  100. w.write("#" .. w.format_integer(controlChannelCheck, 9) .. " ")
  101. if isEnabled then
  102. w.write("Enabled")
  103. else
  104. w.write("Disabled")
  105. end
  106. w.writeLn(" " .. w.format_integer(threshold * 100, 3) .. "% '" .. description .. "'")
  107. end
  108. lhc_parameters[index] = { controlChannel, isEnabled, threshold, description }
  109. index = index + 1
  110. elseif isDetailed then
  111. w.setColorWarning()
  112. if controlChannelCheck ~= nil then
  113. w.writeLn("Error " .. controlChannelCheck)
  114. else
  115. w.writeLn("Error nil?")
  116. end
  117. w.sleep(0.5)
  118. w.setColorNormal()
  119. end
  120. if controlChannel ~= controlChannelCheck and isDetailed then
  121. w.setColorWarning()
  122. w.writeLn("Error: requested " .. controlChannel .. ", received " .. controlChannelCheck)
  123. w.sleep(0.5)
  124. w.setColorNormal()
  125. end
  126. end
  127. elseif isDetailed then
  128. w.setColorWarning()
  129. w.writeLn("No control channel detected!")
  130. w.sleep(0.5)
  131. end
  132. end
  133.  
  134. function lhc_page_parameter()
  135. w.page_begin("<==== Change accelerator parameter ====>")
  136. local _, indexSelected = lhc_parameter_getIndexes()
  137. local controlChannel, isEnabled, threshold, description = lhc_parameter_get(indexSelected)
  138. w.writeLn("Control channel #" .. w.format_integer(controlChannel, 9) .. " applies to:")
  139. for key, controlPoint in ipairs(lhc_controlPoints) do
  140. local cp_x, cp_y, cp_z, cp_tier, cp_type, cp_isEnabled, cp_controlChannel = table.unpack(controlPoint)
  141. if cp_controlChannel == controlChannel then
  142. w.write(string.format("%s %s @%s %s %s ",
  143. w.format_string(cp_type, 10),
  144. w.format_string(cp_tier, 1),
  145. w.format_integer(cp_x, 7), w.format_integer(cp_y, 3), w.format_integer(cp_z, 7) ))
  146. if isEnabled then
  147. w.writeLn("Enabled")
  148. else
  149. w.writeLn("Disabled")
  150. end
  151. end
  152. end
  153.  
  154. w.writeLn("")
  155.  
  156. local _, y = w.getCursorPos()
  157. -- description
  158. w.setCursorPos(1, y + 3)
  159. w.setColorHelp()
  160. w.writeFullLine(" Press enter to validate.")
  161.  
  162. w.setCursorPos(1, y)
  163. w.setColorNormal()
  164. w.writeLn("Current description is '" .. description .. "'")
  165. w.write("Enter a description: ")
  166. description = w.input_readText(description)
  167.  
  168. w.setCursorPos(1, y + 3)
  169. w.setColorNormal()
  170. w.writeFullLine(" ")
  171.  
  172. -- threshold
  173. w.setCursorPos(1, y + 7)
  174. w.setColorHelp()
  175. w.writeFullLine(" Press enter to validate.")
  176.  
  177. w.setCursorPos(1, y + 4)
  178. w.setColorNormal()
  179. w.writeLn("Current threshold is " .. w.format_integer(threshold * 100) .. "%")
  180. w.write("Enter parameter threshold: ")
  181. local new_threshold = w.input_readInteger(threshold * 100) / 100
  182. threshold = math.min(2.0, math.max(0.0, new_threshold))
  183. lhc_parameters[indexSelected] = { controlChannel, isEnabled, threshold, description }
  184. accelerator.parameter(controlChannel, isEnabled, threshold, description)
  185.  
  186. w.setCursorPos(1, y + 7)
  187. w.setColorNormal()
  188. w.writeFullLine(" ")
  189. end
  190.  
  191. lhc_parameter_indexSelected = 1
  192. lhc_parameter_indexFirstLine = 1
  193. lhc_parameter_lines = 7
  194. function lhc_parameter_getIndexes()
  195. if lhc_parameters ~= nil then
  196. if lhc_parameter_indexSelected > #lhc_parameters then
  197. lhc_parameter_indexSelected = 1
  198. elseif lhc_parameter_indexSelected < 1 then
  199. lhc_parameter_indexSelected = #lhc_parameters
  200. end
  201. if lhc_parameter_indexFirstLine > lhc_parameter_indexSelected then
  202. lhc_parameter_indexFirstLine = lhc_parameter_indexSelected
  203. elseif lhc_parameter_indexFirstLine + lhc_parameter_lines < lhc_parameter_indexSelected then
  204. lhc_parameter_indexFirstLine = lhc_parameter_indexSelected - lhc_parameter_lines
  205. end
  206. return lhc_parameter_indexFirstLine, lhc_parameter_indexSelected
  207. else
  208. return 1, 1
  209. end
  210. end
  211.  
  212. function lhc_parameter_get(index)
  213. local parameter = lhc_parameters[index]
  214. local controlChannel, isEnabled, threshold, description = -1, false, 0, "-"
  215. if parameter == nil or #parameter ~= 4 then
  216. lhc_boot(false)
  217. w.status_showWarning("Invalid parameter at index " .. index)
  218. else
  219. controlChannel, isEnabled, threshold, description = table.unpack(parameter)
  220. end
  221. return controlChannel, isEnabled, threshold, description
  222. end
  223.  
  224. function lhc_parameter_updateThreshold(offset)
  225. if lhc_parameters ~= nil and offset ~= nil then
  226. local _, indexSelected = lhc_parameter_getIndexes()
  227. local controlChannel, isEnabled, threshold, description = lhc_parameter_get(indexSelected)
  228. threshold = math.min(2.0, math.max(0.0, threshold + offset / 100))
  229. lhc_parameters[indexSelected] = { controlChannel, isEnabled, threshold, description }
  230. accelerator.parameter(controlChannel, isEnabled, threshold, description)
  231. end
  232. end
  233.  
  234. function lhc_parameter_toggleEnable(forced)
  235. if lhc_parameters ~= nil then
  236. local _, index = lhc_parameter_getIndexes()
  237. local controlChannel, isEnabled, threshold, description = lhc_parameter_get(index)
  238. if forced == nil then
  239. isEnabled = not isEnabled
  240. else
  241. isEnabled = forced
  242. end
  243. lhc_parameters[index] = { controlChannel, isEnabled, threshold, description }
  244. accelerator.parameter(controlChannel, isEnabled, threshold, description)
  245. end
  246. end
  247.  
  248. function lhc_page()
  249. w.page_begin(w.data_getName() .. " - Accelerator controller")
  250. if accelerator ~= nil then
  251. -- w.writeLn("")
  252. local status, isEnabled, isPowered, energy, temperatureCurrent_K, temperatureTarget_K = accelerator.state()
  253. if status == nil then
  254. lhc_boot(false)
  255. w.status_showWarning("Invalid accelerator status, rebooting...")
  256. status, isEnabled, isPowered, energy, temperatureCurrent_K, temperatureTarget_K = "-", false, false, 0, 0, 0
  257. end
  258. w.writeLn("Accelerator status: " .. status)
  259.  
  260. w.write(" Controller is ")
  261. if isEnabled then
  262. w.setColorGood()
  263. w.writeLn("Enabled")
  264. else
  265. w.setColorBad()
  266. w.writeLn("Disabled")
  267. end
  268.  
  269. local energyStored, energyMax, energyUnits = accelerator.getEnergyStatus()
  270. if energyStored == nil then energyStored = 0 end
  271. if energyMax == nil or energyMax == 0 then energyMax = 1 end
  272. w.setColorNormal()
  273. w.write(" Energy level is ")
  274. if isPowered then
  275. w.setColorGood()
  276. elseif isEnabled then
  277. w.setColorBad()
  278. else
  279. w.setColorDisabled()
  280. end
  281. w.writeLn(math.floor(energyStored / energyMax * 100) .. " % (" .. energyStored .. " " .. energyUnits .. ")")
  282. -- w.writeLn("")
  283.  
  284. w.setColorNormal()
  285. w.write(" Magnets temperature is ")
  286. if temperatureCurrent_K <= temperatureTarget_K then
  287. w.setColorGood()
  288. elseif isEnabled then
  289. w.setColorBad()
  290. else
  291. w.setColorDisabled()
  292. end
  293. w.write(string.format("%.1f K", math.floor(temperatureCurrent_K * 10) / 10))
  294. w.setColorNormal()
  295. w.writeLn(". Target is " .. temperatureTarget_K .. " K")
  296. -- w.writeLn("")
  297.  
  298. w.write("Parameters: ")
  299. if #lhc_parameters == 0 then
  300. w.setColorDisabled()
  301. w.writeLn("")
  302. w.writeCentered(" -no valid node detected-")
  303. else
  304. local indexFirstLine, indexSelected = lhc_parameter_getIndexes()
  305. w.writeLn(indexSelected .. "/" .. #lhc_parameters)
  306. local indexLastLine = math.min(indexFirstLine + lhc_parameter_lines, #lhc_parameters)
  307. for indexCurrent = indexFirstLine, indexLastLine do
  308. if indexCurrent == indexSelected then
  309. w.setColorSelected()
  310. w.clearLine()
  311. w.write(">")
  312. else
  313. w.write(" ")
  314. end
  315. local controlChannel, isEnabled, threshold, description = lhc_parameter_get(indexCurrent)
  316. if description == "-" then
  317. description = "#" .. w.format_integer(controlChannel)
  318. end
  319. w.write(string.format("%s is ",
  320. w.format_string(description, 25) ))
  321. if isEnabled then
  322. w.write("enabled ")
  323. else
  324. w.write("disabled")
  325. end
  326. w.writeLn(", set to " .. w.format_integer(threshold * 100, 3) .. "%")
  327. w.setColorNormal()
  328. end
  329. end
  330. else
  331. w.status_showWarning("No accelerator controller detected")
  332. end
  333.  
  334. w.setCursorPos(1, 15)
  335. w.setColorControl()
  336. w.writeFullLine(" Start accelerator (S), Stop accelerator (P)")
  337. w.writeFullLine(" Select parameter (Up, Down), Enable parameter (E)")
  338. w.writeFullLine(" Change parameter (Enter), Adjust threshold (+, -)")
  339. end
  340.  
  341. function lhc_key(character, keycode)
  342. if character == 's' or character == 'S' then
  343. if accelerator ~= nil then
  344. accelerator.enable(true)
  345. else
  346. w.status_showWarning("No accelerator detected")
  347. end
  348. return true
  349. elseif character == 'p' or character == 'P' then
  350. if accelerator ~= nil then
  351. accelerator.enable(false)
  352. else
  353. w.status_showWarning("No accelerator detected")
  354. end
  355. return true
  356. elseif keycode == 200 or keycode == 203 then -- Up or Left arrow
  357. lhc_parameter_indexSelected = lhc_parameter_indexSelected - 1
  358. return true
  359. elseif keycode == 208 or keycode == 205 then -- Down or Right arrow
  360. lhc_parameter_indexSelected = lhc_parameter_indexSelected + 1
  361. return true
  362. elseif keycode == 28 or character == 'c' or character == 'C' then -- Return or Enter
  363. lhc_page_parameter()
  364. return true
  365. elseif character == '-' then
  366. lhc_parameter_updateThreshold(-1)
  367. return true
  368. elseif character == '+' then
  369. lhc_parameter_updateThreshold(1)
  370. return true
  371. elseif character == 'e' or character == 'E' or character == ' ' then
  372. lhc_parameter_toggleEnable()
  373. lhc_parameter_indexSelected = lhc_parameter_indexSelected + 1
  374. return true
  375. elseif character == 'y' or character == 'Y' then
  376. lhc_parameter_toggleEnable(true)
  377. lhc_parameter_indexSelected = lhc_parameter_indexSelected + 1
  378. return true
  379. elseif character == 'n' or character == 'N' then
  380. lhc_parameter_toggleEnable(false)
  381. lhc_parameter_indexSelected = lhc_parameter_indexSelected + 1
  382. return true
  383. end
  384. return false
  385. end
  386.  
  387. function lhc_register()
  388. w.device_register("warpdriveAccelerator",
  389. function(deviceType, address, wrap) accelerator = wrap end,
  390. function() end)
  391. on = redstone.getAnalogInput("right")
  392. if on > 6 then
  393. w.event_register("particleBunchCollided" , function() w.status_showSuccess("Particle bunch have collided") http.post(uri,"{\"content\":\":radioactive: Particle's Successfully Collided.\"}",{['content-type']="application/json"}) return false end )
  394. w.event_register("particleBunchInjected" , function() w.status_showSuccess("Particle bunch injection done") http.post(uri,"{\"content\":\":syringe: Bunch Injection Done.\"}",{['content-type']="application/json"}) return false end )
  395. w.event_register("acceleratorCoolingReset", function() w.status_showWarning("Accelerator coolant has leaked! restarting...") http.post(uri,"{\"content\":\":hot_face: Accelerator Coolant Leak! Restarting...\"}",{['content-type']="application/json"}) return true end )
  396. w.event_register("acceleratorCoolingDone" , function() w.status_showSuccess("Accelerator cooling completed") http.post(uri,"{\"content\":\":cold_face: Accelerator Cooling Completed.\"}",{['content-type']="application/json"}) return true end )
  397. w.event_register("acceleratorUpdated" , function() w.status_showSuccess("Accelerator updated") http.post(uri,"{\"content\":\":boom: Accelerator Parameters Updated.\"}",{['content-type']="application/json"}) lhc_boot(false) return true end )
  398. end
  399. elseif on < 5 then
  400. w.event_register("particleBunchCollided" , function() w.status_showSuccess("Particle bunch have collided") return false end )
  401. w.event_register("particleBunchInjected" , function() w.status_showSuccess("Particle bunch injection done") return false end )
  402. w.event_register("acceleratorCoolingReset", function() w.status_showWarning("Accelerator coolant has leaked! restarting...") return true end )
  403. w.event_register("acceleratorCoolingDone" , function() w.status_showSuccess("Accelerator cooling completed") return true end )
  404. w.event_register("acceleratorUpdated" , function() w.status_showSuccess("Accelerator updated") lhc_boot(false) return true end )
  405. end
  406. end
  407.  
  408. ----------- connections status
  409.  
  410. function connections_page(isBooting)
  411. w.page_begin(w.data_getName() .. " - Connections")
  412.  
  413. w.writeLn("")
  414.  
  415. local monitors = w.device_getMonitors()
  416. if #monitors == 0 then
  417. w.setColorDisabled()
  418. w.writeLn("No Monitor detected")
  419. elseif #monitors == 1 then
  420. w.setColorSuccess()
  421. w.writeLn("1 monitor detected")
  422. else
  423. w.setColorSuccess()
  424. w.writeLn(#monitors .. " Monitors detected")
  425. end
  426.  
  427. if accelerator == nil or accelerator.isInterfaced() == nil then
  428. w.setColorDisabled()
  429. w.writeLn("No accelerator controller detected")
  430. else
  431. w.setColorSuccess()
  432. w.writeLn("Accelerator controller detected")
  433. lhc_boot(isBooting)
  434. end
  435.  
  436. w.writeLn("")
  437. w.setColorNormal()
  438. w.writeLn("This is a keyboard controlled user interface.")
  439. w.write("Key controls are written like so: ")
  440. w.setColorControl()
  441. w.write("Action (key)")
  442. w.setColorNormal()
  443. w.writeLn(".")
  444. w.write("For example, typing ")
  445. w.setColorControl()
  446. w.write(" 1 ")
  447. w.setColorNormal()
  448. w.writeLn(" will open Accelerator")
  449. w.writeLn("controls.")
  450. end
  451.  
  452. ----------- Boot sequence
  453.  
  454. w.page_setEndText(" Home (0), Accelerator controls (1)")
  455. w.page_register('0', connections_page, nil)
  456. w.page_register('1', lhc_page, lhc_key)
  457. lhc_register()
  458.  
  459. w.boot()
  460. local success, message = pcall(w.run)
  461. if not success then
  462. print("failed with message")
  463. print(message)
  464. w.sleep(3.0)
  465. print("rebooting...")
  466. w.reboot()
  467. else
  468. w.close()
  469. end
  470. w.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement