csmit195

Namar Reactor: Code

Jun 11th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.51 KB | None | 0 0
  1. --[[
  2.     Script: PDA Reactor Controller (Big Reactors) (RECEIVER PC)
  3.     Author: csmit195
  4.     Server: Toasty Networks - Tekkit Legends
  5.    
  6.     For usage by: namar4777
  7.     Description: Script remotely controls Reactor via Pocket Computer regardless of distance using HTTP Post API. The code relies on regularily checking for status updates.
  8.    
  9.     Technologies used:
  10.     * Lua / ComputerCraft
  11.     * NodeJS DataStore server(offering as a free service)
  12.    
  13.     Potential Future Upgrades:
  14.     Remote mobile / web app controller as long as reactor is chunk loaded.
  15. ]]
  16.  
  17. -- Configuration Settings
  18. local cfg = {
  19.     ['redstoneSide'] = 'right',
  20.     ['autoUpdate'] = true
  21. }
  22.  
  23. -- Cache Variables
  24. local currentReactorState = rs.getOutput(cfg.redstoneSide)
  25.  
  26. function initiate()
  27.     while true do
  28.         print('loop start')
  29.         local reactorRequest = http.get('http://45.76.117.50:8080/datastore/get/namar_reactor/state')
  30.         local requestContent = reactorRequest.readAll()
  31.         local reactorState = requestContent == 'error:nostore' or requestContent == 'true' or false
  32.         print('state:'..tostring(reactorState))
  33.         print('raw:'..requestContent)
  34.         if ( currentReactorState ~= reactorState ) then
  35.             print('passed IF')
  36.             setReactorState(reactorState)
  37.         end
  38.         print('loop end')
  39.         reactorRequest.close()
  40.         sleep(10)
  41.     end
  42. end
  43.  
  44. function setReactorState(bool)
  45.     rs.setOutput(cfg.redstoneSide, bool)
  46.     currentReactorState = bool
  47.     print('state changed to:'..tostring(bool))
  48. end
  49.  
  50. function toggleReactorState()
  51.     setReactorState(not rs.getOutput(cfg.redstoneSide))
  52. end
  53.  
  54. initiate()
Advertisement
Add Comment
Please, Sign In to add comment