View difference between Paste ID: RqWDpVqD and KgYMzgL0
SHOW: | | - or go back to the newest paste.
1
2
--[[ Setup Vars ]]--
3
4
timeUrl = "http://www.8bittrip.comli.com/time.php"
5
splitOn = ":"
6
7
--[[ Sides for hour/minute connected to terminal ]]--
8
hourSide = "left"
9
minSide  = "back"
10
11
--[[ Set your colours here ]]--
12
tl  = colors.lime
13
t   = colors.lightBlue
14
tr  = colors.black
15
m   = colors.orange
16
bl  = colors.red
17
b   = colors.white
18
br  = colors.pink
19
20
tl2 = colors.lightGray
21
t2  = colors.gray
22
tr2 = colors.green
23
m2  = colors.blue
24
bl2 = colors.brown
25
b2  = colors.cyan
26
br2 = colors.yellow
27
28
--[[ End Vars Setup ]]--
29
30
--[[ Code below here should not be altered ]]--
31
wordHE = tl + tr + m + bl + br + tl2 + t2 + m2 + bl2 + b2
32
wordLO = tl + bl + b + tl2 + t2 + tr2 + bl2 + b2 + br2
33
wordSY = tl + t + m + br + b + tl2 + m2 + tr2 + br2 + b2
34
wordNC = tl + bl + t + tr + br + bl2 + t2 + tl2 + b2
35
36
37
--[[ Standard function to split strings ]]--
38
39
function split(str, pat)
40
   local t = {}  -- NOTE: use {n = 0} in Lua-5.0
41
   local fpat = "(.-)" .. pat
42
   local last_end = 1
43
   local s, e, cap = str:find(fpat, 1)
44
   while s do
45
      if s ~= 1 or cap ~= "" then
46
	 table.insert(t,cap)
47
      end
48
      last_end = e+1
49
      s, e, cap = str:find(fpat, last_end)
50
   end
51
   if last_end <= #str then
52
      cap = str:sub(last_end)
53
      table.insert(t, cap)
54
   end
55
   return t
56
end
57
58
--[[ Standard function (slightly modified) to get URL by Espen ]]--
59
60
function httpPullCheck(urlArg, fileArg)
61
62
	
63
	--[[
64
	HttpTest by Espen v1.3
65
	An example code to show how to use the ComputerCraft HTTP functions.
66
67
	Thanks to Advert for making me aware of the redundant call to http.get after http.request!
68
	--]]
69
70-
	--[[ Setting up variables ]] --
70+
	--[[ Setting up variables ]]
71
	local fileWriteOK
72
	local url = false
73
	local filename = false
74
75
	local function getHttpBody( url )
76-
			get( url )
76+
			http.request( url )
77
			
78
			while true do
79
					local event, urle, hBody = os.pullEvent()
80
					if event == "http_success" then
81
							print( "HTTP SUCCESS\nURL " )
82
							return hBody
83
					else
84
							
85
							print( "HTTP FAILURE\nURL , retry in 1min" )    -- If the error is not catched, this will exit the program.
86
							return nil      -- In case this function is called via pcall.
87
					end
88
			end
89
	end
90
91
	local function processHttpBody( hBody, filename )
92
			local hFile
93
			
94
			if hBody then
95
							local body = hBody.readAll()    -- Read the whole body.
96
							hFile = io.open( filename, "w" )        -- Open the provided filename for writing.
97
							hFile:write( body )      -- Write the body to the file.
98
							hFile:close()   -- Save the changes and close the file.
99
							hBody.close()   -- Do not know for sure if this is really necessary, but just in case.
100
			else
101
					print( "Sorry, no body to process." )
102
			end
103
			
104
			
105
	end
106
107
	local function checkOverwrite( filename )
108
			term.setCursorBlink( false )
109
			print("\nWarning: File already exists. Overwrite? (Y/N)")
110
			
111
			while true do
112
							event, choice = os.pullEvent( "char" )  -- Only listen for "char" events.
113
							if string.lower( choice ) == "y" then return true end
114
							if string.lower( choice ) == "n" then return false end
115
			end
116
	end
117
118
	local function printUsage()
119
			print("Usage:")
120
			print("httptest <URL> <OUTPUT-FILENAME>")
121
			print("Example:")
122
			print("httptest http://www.google.com/ webout")
123
			print("\nThe response body will then be output into the file 'webout', line for line.")
124
	end
125
126
127
	--[[ ===== Execution Entry ===== ]]
128
129
130
	--[[ Processing arguments ]]
131
	if urlArg then url = urlArg end
132
	if fileArg then filename = fileArg end	
133
	
134
	--[[ Making sure all necessary arguments were provided by the user ]]
135
	if not url or not filename then
136
			printUsage()
137
	--elseif not fs.exists( filename ) or checkOverwrite( filename ) then
138
	else
139
			processHttpBody( getHttpBody( url ), filename ) --If getHttpBody successfully returns a body, we continue with processing the body and saving it to a file.
140
			
141
	end
142
143
end
144
145
function setDoubleDigits(outputSide, digitsTotal)
146
		
147
		
148
		if digitsTotal < 10 then
149
			leftDigit = 0
150
		else
151
			leftDigit = math.floor(digitsTotal/10)
152
		end
153
		
154
				
155
		if digitsTotal == 0 then
156
			rightDigit = 0
157
		else
158
			rightDigit = digitsTotal%10
159
		end
160
		
161
		
162
		--setDigits	
163
		rs.setBundledOutput(outputSide, leftMixes[leftDigit] + rightMixes[rightDigit])
164
end
165
166
--[[ Mixes of colours to get digits ]]--
167
168
rightMixes = { }
169
rightMixes[0] = tl2 + t2 + tr2 + bl2 + b2 + br2 
170
rightMixes[1] = tr2 + br2 
171
rightMixes[2] = t2 + tr2 + m2 + bl2 + b2 
172
rightMixes[3] = t2 + tr2 + m2 + br2 + b2 
173
rightMixes[4] = tl2 + tr2 + m2 + br2 
174
rightMixes[5] = t2 + tl2 + m2 + br2 + b2 
175
rightMixes[6] = t2 + tl2 + m2 + br2 + bl2 + b2 
176
rightMixes[7] = t2 + tr2 + br2 
177
rightMixes[8] = t2 + tl2 + tr2 + m2 + br2 + bl2 + b2 
178
rightMixes[9] = t2 + tl2 + tr2 + m2 + br2 + b2 
179
180
leftMixes = { }
181
leftMixes[0] = tl + t + tr + bl + b + br 
182
leftMixes[1] = tr + br 
183
leftMixes[2] = t + tr + m + bl + b 
184
leftMixes[3] = t + tr + m + br + b 
185
leftMixes[4] = tl + tr + m + br 
186
leftMixes[5] = t + tl + m + br + b 
187
leftMixes[6] = t + tl + m + br + bl + b 
188
leftMixes[7] = t + tr + br 
189
leftMixes[8] = t + tl + tr + m + br + bl + b 
190
leftMixes[9] = t + tl + tr + m + br + b 
191
192
--[[ Program starts here ]]--
193
194
firstRun = true
195
lastSync = 0
196
splitTime = { }
197
 
198
 --[[ Display "HELO" and delay to ready HTTP API ]]--
199
 
200
print("System sync prep...")
201
rs.setBundledOutput(hourSide, wordHE)
202
rs.setBundledOutput(minSide, wordLO)
203
sleep(10.0)
204
 
205
 --[[ Loop runs once every 60 secs, time updates once every hour ]]--
206
 
207
while true do
208
		
209
		--[[ Sync time every 60min/first run ]]--
210
		
211
		if firstRun or 	lastSync >= 60 then
212
			
213
			rs.setBundledOutput(hourSide, wordSY)
214
			rs.setBundledOutput(minSide, wordNC)
215
			
216
			sleep(10.0)
217
			httpPullCheck(timeUrl,"time")
218
			
219
			if fs.exists( "time" ) then 
220
		
221
				h = fs.open("time","r") 
222
			
223
				newTime = h.readLine()
224
				
225
				h:close()
226
				
227
				lastSync = 0
228
				firstRun = false
229
				
230
			end
231
			
232
		end
233
		
234
		--[[ Check that newTime exists, else print 00:00 ]]--
235
		
236
		print(newTime)
237
		if lastSync == 0 and newTime then
238
			splitTime = split(newTime, splitOn)	
239
			splitTime[1] = tonumber(splitTime[1])
240
			splitTime[2] = tonumber(splitTime[2]) -1 
241
		elseif not newTime then
242
			splitTime[1] = 0
243
			splitTime[2] = -1
244
		end
245
		
246
		--[[ Increase minute by 1 ]]--
247
		
248
		splitTime[2] = splitTime[2] + 1
249
250
		--[ Increase hour if needed ]--
251
		
252
		if splitTime[2] >= 60 then
253
			splitTime[2] = 0
254
			if splitTime[1] == 23 then
255
				splitTime[1] = 0
256
			else 
257
				splitTime[1] = splitTime[1] + 1
258
			end
259
		end
260
					
261
			
262
		--[[ Print time check and run digit set functions ]]--
263
		
264
		print("Updating time to: "..splitTime[1]..":"..splitTime[2])		
265
		
266
		--Begin set hour
267
		setDoubleDigits(hourSide, splitTime[1])
268
		
269
		--Begin set minute
270
		setDoubleDigits(minSide, splitTime[2])
271
			
272
		print("Time set, last sync "..lastSync.."min ago.")
273
		
274
		--[[ Sleep for 1min before running again ]]--
275
		os.sleep(60.0)
276
		lastSync = lastSync + 1
277
		   
278
end