Advertisement
Exumed

Untitled

Jan 19th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. local modem = peripheral.wrap("back")
  2. local monitor = peripheral.wrap("top")
  3. DISPLAYMODE = 1 -- 1 = EnergyUsage Persentage Vertical Bar
  4.  
  5. local MFSU = {StartCHANNEL = 1200, StopCHANNEL = 1200}
  6. local EnergyUsage = {StartCHANNEL = 2200, StopCHANNEL = 2200}
  7. local EnergyProduction = {StartCHANNEL = 3200, StopCHANNEL = 3200}
  8.  
  9.  
  10.  
  11.  
  12.  
  13. local StoredValues= {} --Stores the recent Values
  14.  
  15. local MFSUData = {}
  16. local EnergyUsageData = {}
  17. local EnergyProductionData = {}
  18.  
  19.  
  20.  
  21. function openChannels(startChannel, stopChannel)
  22. if modem == null then
  23. print("Place a Modem at the Bottom")
  24. else
  25. for i=startChannel, startChannel + stopChannel - startChannel, 1 do
  26. modem.open(i)
  27. end
  28. end
  29. end
  30.  
  31. function resetMonitor()
  32. width, height = monitor.getSize()
  33. monitor.clear()
  34. monitor.setBackgroundColor(colors.black)
  35. monitor.setTextScale(0.5)
  36. for i = 1, height,1 do
  37. monitor.setCursorPos(1, i)
  38. monitor.clearLine()
  39. end
  40. end
  41.  
  42. local function splitData(message)
  43. sD = {}
  44. for token in string.gmatch(message, "([^,]+),%s*") do
  45. if token ~= null then
  46. table.insert(sD,1, token)
  47. end
  48. end
  49. return sD
  50. end
  51.  
  52. function addValuesMFSU(senderChannel, message)
  53. if senderChannel >= MFSU.StartCHANNEL then
  54. if senderChannel <= MFSU.StopCHANNEL then
  55. sD = splitData(message)
  56. exist = false
  57.  
  58. for k,v in pairs(MFSUData) do
  59. if v ~= 0 then
  60. if v.channel == senderChannel then
  61. v.count = sD[1]
  62. v.capacity = sD[2]
  63. v.stored = sD[3]
  64. exist = true
  65. end
  66. end
  67. end
  68. if exist == false then
  69. item = { channel = senderChannel, count = sD[1], capacity = sD[2], stored = sD[3] }
  70. table.insert(MFSUData,1, item)
  71. end
  72. end
  73. end
  74. end
  75.  
  76. function addValuesEnergyUsage(senderChannel, message)
  77. if senderChannel >= EnergyUsage.StartCHANNEL then
  78. if senderChannel <= EnergyUsage.StopCHANNEL then
  79. sD = splitData(message)
  80. exist = false
  81.  
  82. for k,v in pairs(EnergyUsageData) do
  83. if v ~= 0 then
  84. if v.channel == senderChannel then
  85. v.count = sD[1]
  86. v.maxEnergy = sD[2]
  87. v.energy = sD[3]
  88. exist = true
  89. end
  90. end
  91. end
  92. if exist == false then
  93. item = { channel = senderChannel, count = sD[1], maxEnergy = sD[2], energy = sD[3] }
  94. table.insert(EnergyUsageData,1, item)
  95. end
  96. end
  97. end
  98. end
  99.  
  100.  
  101. function addValuesEnergyProduction(senderChannel, message)
  102. if senderChannel >= EnergyProduction.StartCHANNEL then
  103. if senderChannel <= EnergyProduction.StopCHANNEL then
  104. sD = splitData(message)
  105. exist = false
  106.  
  107. for k,v in pairs(EnergyProductionData) do
  108. if v ~= 0 then
  109. if v.channel == senderChannel then
  110. v.countLV = sD[1]
  111. v.countMV = sD[2]
  112. v.countHV = sD[3]
  113. v.energyEmitted = sD[4]
  114. exist = true
  115. end
  116. end
  117. end
  118. if exist == false then
  119. item = { channel = senderChannel, countLV = sD[1], countMV = sD[2], countHV = sD[3], energyEmitted = sD[4] }
  120. table.insert(EnergyProductionData,1, item)
  121. end
  122. end
  123. end
  124. end
  125. local function calculateMFSU()
  126. MFSUCalc = { count = 0, capacity = 0, stored = 0 }
  127. for k,v in pairs(MFSUData) do
  128. if v ~= 0 then
  129. MFSUCalc.count = MFSUCalc.count + v.count
  130. MFSUCalc.capacity = MFSUCalc.capacity + v.capacity
  131. MFSUCalc.stored = MFSUCalc.stored + v.stored
  132. end
  133. end
  134.  
  135. return MFSUCalc
  136. end
  137. local function calculateEnergyUsage()
  138. EnergyUsageCalc = { count = 0, maxEnergy = 0, energy = 0 }
  139. for k,v in pairs(EnergyUsageData) do
  140. if v ~= 0 then
  141. EnergyUsageCalc.count = EnergyUsageCalc.count + v.count
  142. EnergyUsageCalc.maxEnergy = EnergyUsageCalc.maxEnergy + v.maxEnergy
  143. EnergyUsageCalc.energy = EnergyUsageCalc.energy + v.energy
  144. end
  145. end
  146. return EnergyUsageCalc
  147. end
  148. local function calculatEnergyProduction()
  149. EnergyProductionCalc = { countLV = 0, countMV = 0, countHV = 0, energyEmitted = 0 }
  150. for k,v in pairs(EnergyProductionData) do
  151. if v ~= 0 then
  152. EnergyProductionCalc.countLV = EnergyProductionCalc.countLV + v.countLV
  153. EnergyProductionCalc.countMV = EnergyProductionCalc.countMV + v.countMV
  154. EnergyProductionCalc.countHV = EnergyProductionCalc.countHV + v.countHV
  155. EnergyProductionCalc.energyEmitted = EnergyProductionCalc.energyEmitted + v.energyEmitted
  156. end
  157. end
  158. return EnergyProductionCalc
  159. end
  160.  
  161.  
  162. function displayMode1(StoredMFSU, StoredEnergyUsage, StoredEnergyProduction)
  163. local width, height = monitor.getSize()
  164. bXStart = width / 2
  165. bXStop = bXStart
  166. bYStart = 0
  167. bYStop = height
  168.  
  169.  
  170.  
  171.  
  172.  
  173. usagePerc = StoredEnergyUsage.energy / ( StoredEnergyProduction.energyEmitted /100 )
  174. if usagePerc == null then
  175. usagePerc = 100
  176. end
  177.  
  178. term.redirect(monitor)
  179. if usagePerc < 100 then
  180. bXStop = bXStart + (width / 2 ) * usagePerc
  181. if bXStop > width then
  182. bXStop = width
  183. -- Notice more than 100%
  184. end
  185.  
  186. print("xStart " ..bXStart)
  187. print("yStart " ..bXStop)
  188. print("xStop " ..bYStart)
  189. print("yStop " ..bYStop)
  190.  
  191. paintutils.drawFilledBox(bXStart, bYStart, bXStop, bYStop, colors.green)
  192. end
  193. if usagePerc > 100 then
  194. bXStop = bXStart - (width / 2 ) * usagePerc
  195. if bXStop > width then
  196. bXStop = 0
  197. -- Notice more than 100%
  198. end
  199. paintutils.drawFilledBox(bXStart, bYStart, bXStop, bYStop, colors.red)
  200. end
  201. paintutils.drawLine(width /2 , 0, width /2 , height, colors.white)
  202. term.restore()
  203.  
  204. end
  205.  
  206.  
  207.  
  208. local function countTableItems(table)
  209. maxCount = 0
  210. for k,v in pairs(table) do
  211. if v ~= 0 then
  212. maxCount = maxCount +1
  213. end
  214. end
  215. return maxCount
  216. end
  217.  
  218.  
  219.  
  220. -- START
  221.  
  222.  
  223.  
  224. openChannels(EnergyUsage.StartCHANNEL, EnergyUsage.StopCHANNEL)
  225. openChannels(EnergyProduction.StartCHANNEL, EnergyProduction.StopCHANNEL)
  226. openChannels(MFSU.StartCHANNEL, MFSU.StopCHANNEL)
  227.  
  228. resetMonitor()
  229.  
  230. while (true) do
  231. local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  232.  
  233. if message ~= null then
  234. if message ~= "" then
  235. addValuesMFSU(senderChannel, message)
  236. addValuesEnergyUsage(senderChannel, message)
  237. addValuesEnergyProduction(senderChannel, message)
  238. end
  239. end
  240.  
  241. MFSUCalc = calculateMFSU()
  242. EnergyUsageCalc = calculateEnergyUsage()
  243. EnergyProductionCalc = calculatEnergyProduction()
  244.  
  245. resetMonitor()
  246.  
  247. -- storedValuesCount = countTableItems(tableStoredValues)
  248.  
  249.  
  250. if DISPLAYMODE == 1 then
  251. displayMode1(MFSUCalc, EnergyUsageCalc, EnergyProductionCalc)
  252. end
  253.  
  254.  
  255.  
  256.  
  257.  
  258. sleep(0.15)
  259. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement