fishermedders

telemetry server cc

Jun 20th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.30 KB | None | 0 0
  1.  
  2. --APIS
  3.  
  4. --[[ Computercraft UI API by ThePH]]
  5.  
  6. -- functions comonly used, suing targets (being a monitor or term)
  7. function cur(target, x, y) target.setCursorPos(x, y) end
  8. function color(target,fg,bg) if fg > -1 then target.setTextColor(fg) end if bg > -1 then target.setBackgroundColor(bg) end end
  9. function drawPixel(target, x, y, nColour) if nColour then term.setBackgroundColor( nColour ) end cur(target, x, y) v.write(' ') end
  10.  
  11. --Function to check if item is contained in tbl and return en key or 0 if not in tbl
  12. function inTable(tbl, item)
  13. if type(item) == 'table' then
  14. for i, v in pairs(item) do
  15. for key,value in pairs(tbl) do
  16. if value == v then return key end
  17. end
  18. end
  19. elseif type(item) == 'string' then
  20. for key,value in pairs(tbl) do
  21. if value == item then return key end
  22. end
  23. end
  24. return 0
  25. end
  26.  
  27. -- Reset the the UI list to make it fit the 'format'
  28. function reset(list)
  29. list.ui = {}
  30. list.shape = {}
  31. return list
  32. end
  33.  
  34.  
  35. -- Function to create and edit the lists
  36.  
  37. -- Adding a UI element
  38. function addUI(uiList, keyName, x, y, text, fg, bg, action, pages, output, enabled, visible)
  39. if enabled == nil then enabled = true end
  40. if visible == nil then visible = true end
  41. uiList.ui[keyName] = {x, y, text, fg, bg, action, pages, output, enabled, visible}
  42. return uiList
  43. end
  44.  
  45. -- Adding a shape (p3 is not used yet, but its here just in case)
  46. function addShape(uiList, keyName, shape, x, y, p1, p2, p3, color, pages, output, enabled)
  47. if enabled == nil then enabled = true end
  48. uiList.shape[keyName] = {shape, x, y, p1, p2, p3, color, pages, output, enabled}
  49. return uiList
  50. end
  51.  
  52. -- Editing a UI element
  53. function editUI(uiList, key, id, value)
  54. local ids = {'x','y','text','fg','bg','action','pages','output', 'enabled', 'visible'}
  55. local index = inTable(ids, id)
  56. uiList.ui[key][index] = value
  57. return uiList
  58. end
  59.  
  60. -- Draws the UI and shapes (shapes before)
  61. function draw(uiList, pageList, dFG, dBG)
  62. for index, value in pairs(uiList.shape) do
  63. local shape, x, y, p1, p2, p3, color, pagesIn, output, enabled = unpack(value)
  64. local pageOk = false
  65. if inTable(pagesIn, pageList) > 0 then
  66. pageOk = true
  67. end
  68. if pageOk and enabled then
  69. if shape == 'line' then
  70. drawLine(x, y, p1, p2, output, color)
  71. elseif shape == 'outline' then
  72. drawOutline(x, y, p1, p2, output, color)
  73. elseif shape == 'box' then
  74. drawBox(x, y, p1, p2, output, color)
  75. end
  76. end
  77. end
  78. for index, value in pairs(uiList.ui) do
  79. local x, y, text, fg, bg, action, pagesIn, output, enabled, visible = unpack(value)
  80. local pageOk = false
  81. if inTable(pagesIn, pageList) > 0 then
  82. pageOk = true
  83. end
  84. if pageOk and visible and enabled then
  85. for i,v in pairs(output) do
  86. color(v, dFG, dBG)
  87. cur(v, x, y)
  88. color(v, fg, bg)
  89. v.write(text)
  90. end
  91. end
  92. end
  93. end
  94.  
  95. -- Function to draw something not used for interaction (captions etc)
  96. function drawSingle(x, y, text, fg, bg, output)
  97. for i, v in pairs(output) do
  98. cur(v, x, y)
  99. color(v, fg, bg)
  100. v.write(text)
  101. end
  102. end
  103.  
  104. -- Checks mouse function (using the ui list, x, y pages and (optional, the targets))
  105. -- returns the action, the key in the ui list and the position on the string
  106. function mouse(uiList, mx, my, pageList, target)
  107. for index, value in pairs(uiList.ui) do
  108. local x, y, text, fg, bg, action, pagesIn, output, enabled, visible = unpack(value)
  109. pageOk = false
  110. if inTable(pagesIn, pageList) > 0 then pageOk = true end
  111. if (not(target) or inTable(output, target) > 0) and not(action == 'nil') and pageOk and enabled then
  112. for i, v in pairs(pagesIn) do
  113. if inTable(pageList, v) then
  114. if my == y then
  115. if mx >= x and mx <= (x + text:len() - 1) then
  116. return action, index, (mx - x + 1)
  117. end
  118. end
  119. break
  120. end
  121. end
  122. end
  123. end
  124. return nil, nil, nil
  125. end
  126.  
  127. -- function the draw an empty box on target
  128. function drawOutline(x1,y1, x2, y2, target, c)
  129. drawLine(x1,y1, x1, y2, target, c)
  130. drawLine(x1,y2, x2, y2, target, c)
  131. drawLine(x2,y1, x2, y2, target, c)
  132. drawLine(x1,y1, x2, y1, target, c)
  133. end
  134.  
  135. -- function from paintutils ported for using targets
  136. local function drawPixelInternal( xPos, yPos, target )
  137. target.setCursorPos(xPos, yPos)
  138. target.write(" ")
  139. end
  140.  
  141. -- function to draw line using targets
  142. function drawLine(startX, startY, endX, endY, target, nColour)
  143. for i, v in pairs(target) do
  144. line( startX, startY, endX, endY, v, nColour )
  145. end
  146. end
  147.  
  148. -- function to draw a filled box on target
  149. function drawBox(startX, startY, endX, endY, target, nColour)
  150. if startY <= endY then s = 1 else s= -1 end
  151. for index=startY, endY, s do
  152. drawLine( startX, index, endX, index, target, nColour )
  153. end
  154. end
  155.  
  156. -- function from paintutils ported for using targets
  157. function line( startX, startY, endX, endY, v, nColour )
  158. if nColour then
  159. v.setBackgroundColor( nColour )
  160. end
  161.  
  162. startX = math.floor(startX)
  163. startY = math.floor(startY)
  164. endX = math.floor(endX)
  165. endY = math.floor(endY)
  166.  
  167. if startX == endX and startY == endY then
  168. drawPixelInternal( startX, startY, v )
  169. return
  170. end
  171.  
  172. local minX = math.min( startX, endX )
  173. if minX == startX then
  174. minY = startY
  175. maxX = endX
  176. maxY = endY
  177. else
  178. minY = endY
  179. maxX = startX
  180. maxY = startY
  181. end
  182.  
  183. local xDiff = maxX - minX
  184. local yDiff = maxY - minY
  185.  
  186. if xDiff > math.abs(yDiff) then
  187. local y = minY
  188. local dy = yDiff / xDiff
  189. for x=minX,maxX do
  190. drawPixelInternal( x, math.floor( y + 0.5 ), v )
  191. y = y + dy
  192. end
  193. else
  194. local x = minX
  195. local dx = xDiff / yDiff
  196. if maxY >= minY then
  197. for y=minY,maxY do
  198. drawPixelInternal( math.floor( x + 0.5 ), y, v )
  199. x = x + dx
  200. end
  201. else
  202. for y=minY,maxY,-1 do
  203. drawPixelInternal( math.floor( x + 0.5 ), y, v )
  204. x = x - dx
  205. end
  206. end
  207. end
  208. end
  209.  
  210.  
  211. --START PROGRAM HERE
  212. tConfig = {}
  213. pMonitor = {}
  214.  
  215.  
  216. function fGrabPheripherals()
  217.  
  218. if tConfig["is-receiver"] then
  219. local p = peripheral.find("monitor", function(name, object) object.side = name return true end)
  220.  
  221. if p == nil then
  222.  
  223. error("Could not find a monitor!")
  224.  
  225. return false
  226. end
  227.  
  228. pMonitor = peripheral.wrap(p.side)
  229. end
  230.  
  231. local p = peripheral.find("modem", function(name, object) object.side = name return true end)
  232.  
  233. if p == nil then
  234.  
  235. error("Could not find a modem")
  236.  
  237. return false
  238. end
  239.  
  240. rednet.open(p.side)
  241.  
  242.  
  243. return true
  244. end
  245.  
  246. --Data types (ae2, steam-turbines, reactor, power-storage[GENERIC THIS WILL CHANGE TO A SPECIFIC TYPE])
  247.  
  248. --Receiver Side (Monitoring)
  249.  
  250. function fListenLoop()
  251. while true do
  252. fListenLoopUpdate()
  253. end
  254. end
  255.  
  256. function fListenLoopUpdate()
  257. tEvent = {rednet.receive(tConfig["protocol-id"])}
  258.  
  259. tPacket = textutils.unserialize(tEvent[2])
  260.  
  261. if tPacket == nil then
  262. print("Received invalid packet data")
  263. print("Invalid message from (" .. tEvent[1] .. "): " .. tPacket[2])
  264. return
  265. end
  266.  
  267. local tData = tPacket[tConfig["data-type"]]
  268.  
  269. if tData == nil then
  270. print("Received invalid data type")
  271. print("Data typed received from (" .. tEvent[1] .. "): " .. tEvent[2])
  272. return
  273. end
  274.  
  275. print("Data received: " .. tEvent[2])
  276. --Render data
  277. end
  278.  
  279.  
  280.  
  281.  
  282. --Sender Side (Data sending)
  283. function fSendPacket(dataType, statusContent)
  284. local tToBeSent = {[dataType]=statusContent}
  285. rednet.broadcast(textutils.serialize(tToBeSent),tConfig["protocol-id"])
  286. end
  287.  
  288. function fSendLoop()
  289. while true do
  290. if tConfig["data-type"] == "ae2" then
  291. ae = peripheral.wrap("right")
  292. tSend = {["power"]=ae.getDemandedEnergy()}
  293. fSendPacket("ae2",tSend)
  294. end
  295. sleep(tConfig["update-time"])
  296. end
  297. end
  298.  
  299.  
  300.  
  301. --
  302.  
  303. function fGetConfig()
  304. local tFile = fs.open("config","r")
  305. local sReturn = textutils.unserialize(tFile.readAll())
  306. tFile.close()
  307. return sReturn
  308. end
  309.  
  310. if not fs.exists("config") then
  311.  
  312. print("Starting setup routine!")
  313. local sReceiver = ""
  314.  
  315. repeat
  316. print("Is this a sender or a reciever? [s/r]")
  317. sReceiver = read()
  318. until(sReceiver == "s" or sReceiver == "r")
  319.  
  320. print("What is the data type?")
  321. local sType = read()
  322.  
  323. print("What is the network ID?")
  324. local sID = read()
  325. local sDelay = ""
  326.  
  327. repeat
  328. print("What should the time be between updates? (inseconds)")
  329. sDelay = read()
  330. until(tonumber(sDelay) ~= nil)
  331.  
  332. local tWriteConfig = {
  333. ["is-receiver"] = (sReceiver == "r"), --receiver (Monitoring), node (Sends data)
  334. ["data-type"] = sType,
  335. ["protocol-id"] = sID,
  336. ["update-time"] = tonumber(sDelay)
  337. }
  338. local tFile = fs.open("config","w")
  339. tFile.write(textutils.serialize(tWriteConfig))
  340. tFile.close()
  341. end
  342.  
  343. tConfig = fGetConfig()
  344.  
  345. --Starts respective loop
  346.  
  347. function onStart()
  348. if not fGrabPheripherals() then
  349. error("Invalid peripheral. Terminating")
  350. return
  351. end
  352.  
  353.  
  354. if tConfig["is-receiver"] then
  355. print("Starting loop as receiver")
  356. print("Data type: " .. tConfig["data-type"])
  357. print("Protocol id: " .. tConfig["protocol-id"])
  358. --print("Update interval: " .. tConfig["update-time"])
  359.  
  360. fListenLoop()
  361.  
  362. else
  363. print("Starting loop as sender")
  364. print("Data type: " .. tConfig["data-type"])
  365. print("Protocol id: " .. tConfig["protocol-id"])
  366. print("Update interval: " .. tConfig["update-time"])
  367.  
  368. fSendLoop()
  369. end
  370. end
  371.  
  372.  
  373. onStart()
  374.  
  375. if fGrabPheripherals() then
  376.  
  377.  
  378. else
  379.  
  380.  
  381. end
Add Comment
Please, Sign In to add comment