View difference between Paste ID: c1mP0CK6 and DWaLuQra
SHOW: | | - or go back to the newest paste.
1
local aeStorage = peripheral.wrap("bottom")
2
local bigDisplay = peripheral.wrap("top")
3
local firstTopTen = peripheral.wrap("left")
4
local secondTopTen = peripheral.wrap("right")
5
local leftColor = 8192 -- green
6
local rightColor = 2048 -- blue
7
local overlapColor = 16 -- yellow
8
9
local graphWindow = {}
10
local x
11
local y
12
local JSON = (loadfile "JSON.lua")()
13
JSON.strictTypes = true
14
function printDebug(textString)
15
   print(textString)
16
end
17
18
function initDisplays()
19
   bigDisplay.clear()
20
   bigDisplay.setTextScale(.5)
21
   firstTopTen.clear()
22
   secondTopTen.clear()
23
end
24
25
function num2Text( inputNumber, textLength ) 
26
   inputNumber = tostring( inputNumber )
27
   if string.len(inputNumber ) < textLength then
28
      inputNumber = string.rep( " " , textLength - string.len( inputNumber ) )..inputNumber
29
   elseif string.len(inputNumber ) > textLength then
30
      inputNumber = "ERR: String too long"
31
   end
32
   return inputNumber
33
end
34
35-
function valueToScale( maxValue, inputValue )
35+
function valueToScale( minValue, maxValue, inputValue )
36-
   return graphWindow.ylen - math.ceil( inputValue / ( maxValue / ( graphWindow.ylen - 1 ) ) )
36+
   return math.ceil( ( graphWindow.ylen - 1 ) - ( inputValue - minValue ) / ( ( maxValue - minValue ) / ( graphWindow.ylen - 2 ) ) )
37
end
38
39-
function scaleToValue( maxValue, inputScale )
39+
function scaleToValue( minValue, maxValue, inputScale )
40-
   return maxValue - math.floor( ( maxValue / ( graphWindow.ylen - 1 ) ) * ( inputScale - 1 ) )
40+
   return math.floor( maxValue - ( ( maxValue - minValue) / ( graphWindow.ylen - 1 ) ) * ( inputScale - 1 ) )
41
end
42
43-
function drawScale( maxYLeft, maxYRight )
43+
local maxYLeft 
44
local maxYRight
45
local minYLeft
46-
   printDebug(textutils.serialize(graphWindow))
46+
local minYRight
47
48
function drawScale( leftArray, rightArray )
49
   local leftLen = string.len( tostring( maxYLeft ) )
50-
      bigDisplay.write( num2Text( scaleToValue(maxYLeft, i), leftLen ).."-|" )
50+
51
   for i = graphWindow.ylen, graphWindow.y, - 1 do
52
      bigDisplay.setCursorPos( 1 , i )
53-
      bigDisplay.write( "|-"..num2Text( scaleToValue(maxYRight, i), rightLen ) )
53+
54
      bigDisplay.write( num2Text( scaleToValue(minYLeft, maxYLeft, i), leftLen ).."-|" )
55
      bigDisplay.setCursorPos( graphWindow.xlen + graphWindow.x , i )
56
      bigDisplay.setTextColor(rightColor)
57
      bigDisplay.write( "|-"..num2Text( scaleToValue(minYRight, maxYRight, i), rightLen ) )
58
   end
59
   bigDisplay.setCursorPos( graphWindow.x , y )
60
   bigDisplay.setTextColor(1)
61
   bigDisplay.write( string.rep( "-", graphWindow.xlen ) )
62
end
63
64
local leftGraphTable
65
local rightGraphTable
66
67
function buildGraph( leftArray, rightArray )
68
   maxYLeft = math.max(unpack(leftArray))
69
   maxYRight = math.max(unpack(rightArray))
70-
      leftGraphTable[i] = valueToScale(math.max(unpack(leftArray)), leftArray[lefti] )
70+
   minYLeft = math.min(unpack(leftArray))
71-
      rightGraphTable[i] = valueToScale(math.max(unpack(rightArray)), rightArray[righti] )
71+
   minYRight = math.min(unpack(rightArray))
72
   local extendLeft = ( maxYLeft - minYLeft ) / 10
73
   local extendRight = ( maxYRight - minYRight ) / 10
74
   maxYLeft = math.ceil( maxYLeft + extendLeft )
75
   maxYRight = math.ceil( maxYRight + extendRight )
76
   minYLeft = math.max(math.floor( minYLeft - extendLeft ), 0 )
77
   minYRight = math.max(math.floor( minYRight - extendRight ), 0 )
78
   leftGraphTable = {}
79
   rightGraphTable = {}
80
   local lefti = #leftArray
81
   local righti = #rightArray
82
   for i = graphWindow.xlen - 1 , 1, - 1 do
83
      if lefti == 0 or righti == 0 then break end
84
      leftGraphTable[i] = valueToScale(minYLeft, maxYLeft, leftArray[lefti] )
85
      rightGraphTable[i] = valueToScale(minYRight, maxYRight, rightArray[righti] )
86
      lefti = lefti - 1
87
      righti = righti - 1
88
   end
89
   printDebug(textutils.serialize(leftGraphTable))
90
   printDebug(textutils.serialize(leftArray))
91
end
92
   
93
function drawGraph()
94
   for i = graphWindow.xlen - 1 , 1, - 1 do
95
      if not leftGraphTable[i] or not rightGraphTable[i] then break end
96
      for l = 1, graphWindow.ylen do
97
         bigDisplay.setCursorPos( i + graphWindow.x, l)
98
         bigDisplay.write(" ")
99
      end
100
      bigDisplay.setCursorPos( i + graphWindow.x, leftGraphTable[i])
101
      if leftGraphTable[i] == rightGraphTable[i] then
102
         -- If the table values are the same, draw them as an overlap
103
         bigDisplay.setTextColor( overlapColor )
104
         bigDisplay.write("=")
105
      else
106
         -- Otherwise, draw one
107
         bigDisplay.setTextColor( leftColor )
108
         bigDisplay.write("-")
109
         -- Then the other
110
         bigDisplay.setCursorPos( i + graphWindow.x, rightGraphTable[i])
111
         bigDisplay.setTextColor( rightColor )
112
         bigDisplay.write("-")
113
      end
114
   end
115
end
116
   
117
function rotateHistory( inputArray )
118
   local outputArray
119
   if not inputArray or #inputArray < ( graphWindow.xlen + 3 ) then
120
      outputArray = inputArray
121
   else
122
      for i = 2, #inputArray do
123
         outputArray[i-1] = inputArray[i]
124
      end
125
      inputArray[#inputArray] = nil
126
   end
127
   return outputArray
128
end
129
130
local historyType = {}
131
local historyCount = {}
132
if fs.exists("AE_History") then
133
   local recoveredHistory = fs.open("AE_History", "r")
134
   local historyData = recoveredHistory.readLine()
135
   printDebug(historyData)
136
   if historyData then
137
      historyData = JSON:decode(historyData)
138
      printDebug(textutils.serialize(historyData))
139
      historyType = historyData["Type"]
140
      historyCount = historyData["Count"]
141
      printDebug("Recovered old data.")
142
   end
143
   recoveredHistory.close()
144
end   
145
initDisplays()
146
while true do
147
   -- Initialize displays, draw the graph axis on bigDisplay and grab AE data
148
   -- local allItems = aeStorage.getAvailableItems()
149
   table.insert( historyType, aeStorage.getRemainingItemTypes() + aeStorage.getStoredItemTypes() )
150
   table.insert( historyCount, aeStorage.getRemainingItemCount() + aeStorage.getStoredItemCount() )
151-
   drawScale( math.max(unpack(historyCount)), math.max(unpack(historyType)) )
151+
152
   recoveredHistory = fs.open("AE_History", "w")
153
   local tempTable = { 
154-
   printDebug(textutils.serialize(historyType).." - "..textutils.serialize(historyCount))
154+
155-
   sleep(60)
155+
156
   }
157
   recoveredHistory.write( JSON:encode( tempTable ) )
158
   recoveredHistory.close()
159
   local leftLen = string.len( tostring( math.max(unpack(historyCount)) ) )
160
   local rightLen = string.len( tostring( math.max(unpack(historyType)) ) )
161
   x, y = bigDisplay.getSize()
162
   graphWindow.x = leftLen + 3
163
   graphWindow.y = 1
164
   graphWindow.xlen = ( x - ( leftLen + 2 ) ) - ( rightLen + 2 )
165
   graphWindow.ylen = y - 1 
166
   buildGraph( historyCount, historyType )
167
   drawScale( historyCount, historyType )
168
   -- Draw graphs
169
   drawGraph()
170
   -- printDebug(textutils.serialize(historyType).." - "..textutils.serialize(historyCount))
171
   sleep(600)
172
   -- historyCount = rotateHistory( historyCount )
173
   -- historyType = rotateHistory( historyType )
174
end