--[[ Setup Vars ]]-- timeUrl = "http://www.8bittrip.comli.com/time.php" splitOn = ":" --[[ Sides for hour/minute connected to terminal ]]-- hourSide = "left" minSide = "back" --[[ Set your colours here ]]-- tl = colors.lime t = colors.lightBlue tr = colors.black m = colors.orange bl = colors.red b = colors.white br = colors.pink tl2 = colors.lightGray t2 = colors.gray tr2 = colors.green m2 = colors.blue bl2 = colors.brown b2 = colors.cyan br2 = colors.yellow --[[ End Vars Setup ]]-- --[[ Code below here should not be altered ]]-- wordHE = tl + tr + m + bl + br + tl2 + t2 + m2 + bl2 + b2 wordLO = tl + bl + b + tl2 + t2 + tr2 + bl2 + b2 + br2 wordSY = tl + t + m + br + b + tl2 + m2 + tr2 + br2 + b2 wordNC = tl + bl + t + tr + br + bl2 + t2 + tl2 + b2 --[[ Standard function to split strings ]]-- function split(str, pat) local t = {} -- NOTE: use {n = 0} in Lua-5.0 local fpat = "(.-)" .. pat local last_end = 1 local s, e, cap = str:find(fpat, 1) while s do if s ~= 1 or cap ~= "" then table.insert(t,cap) end last_end = e+1 s, e, cap = str:find(fpat, last_end) end if last_end <= #str then cap = str:sub(last_end) table.insert(t, cap) end return t end --[[ Standard function (slightly modified) to get URL by Espen ]]-- function httpPullCheck(urlArg, fileArg) --[[ HttpTest by Espen v1.3 An example code to show how to use the ComputerCraft HTTP functions. Thanks to Advert for making me aware of the redundant call to http.get after http.request! --]] --[[ Setting up variables ]] -- local fileWriteOK local url = false local filename = false local function getHttpBody( url ) get( url ) while true do local event, urle, hBody = os.pullEvent() if event == "http_success" then print( "HTTP SUCCESS\nURL " ) return hBody else print( "HTTP FAILURE\nURL , retry in 1min" ) -- If the error is not catched, this will exit the program. return nil -- In case this function is called via pcall. end end end local function processHttpBody( hBody, filename ) local hFile if hBody then local body = hBody.readAll() -- Read the whole body. hFile = io.open( filename, "w" ) -- Open the provided filename for writing. hFile:write( body ) -- Write the body to the file. hFile:close() -- Save the changes and close the file. hBody.close() -- Do not know for sure if this is really necessary, but just in case. else print( "Sorry, no body to process." ) end end local function checkOverwrite( filename ) term.setCursorBlink( false ) print("\nWarning: File already exists. Overwrite? (Y/N)") while true do event, choice = os.pullEvent( "char" ) -- Only listen for "char" events. if string.lower( choice ) == "y" then return true end if string.lower( choice ) == "n" then return false end end end local function printUsage() print("Usage:") print("httptest ") print("Example:") print("httptest http://www.google.com/ webout") print("\nThe response body will then be output into the file 'webout', line for line.") end --[[ ===== Execution Entry ===== ]] --[[ Processing arguments ]] if urlArg then url = urlArg end if fileArg then filename = fileArg end --[[ Making sure all necessary arguments were provided by the user ]] if not url or not filename then printUsage() --elseif not fs.exists( filename ) or checkOverwrite( filename ) then else processHttpBody( getHttpBody( url ), filename ) --If getHttpBody successfully returns a body, we continue with processing the body and saving it to a file. end end function setDoubleDigits(outputSide, digitsTotal) if digitsTotal < 10 then leftDigit = 0 else leftDigit = math.floor(digitsTotal/10) end if digitsTotal == 0 then rightDigit = 0 else rightDigit = digitsTotal%10 end --setDigits rs.setBundledOutput(outputSide, leftMixes[leftDigit] + rightMixes[rightDigit]) end --[[ Mixes of colours to get digits ]]-- rightMixes = { } rightMixes[0] = tl2 + t2 + tr2 + bl2 + b2 + br2 rightMixes[1] = tr2 + br2 rightMixes[2] = t2 + tr2 + m2 + bl2 + b2 rightMixes[3] = t2 + tr2 + m2 + br2 + b2 rightMixes[4] = tl2 + tr2 + m2 + br2 rightMixes[5] = t2 + tl2 + m2 + br2 + b2 rightMixes[6] = t2 + tl2 + m2 + br2 + bl2 + b2 rightMixes[7] = t2 + tr2 + br2 rightMixes[8] = t2 + tl2 + tr2 + m2 + br2 + bl2 + b2 rightMixes[9] = t2 + tl2 + tr2 + m2 + br2 + b2 leftMixes = { } leftMixes[0] = tl + t + tr + bl + b + br leftMixes[1] = tr + br leftMixes[2] = t + tr + m + bl + b leftMixes[3] = t + tr + m + br + b leftMixes[4] = tl + tr + m + br leftMixes[5] = t + tl + m + br + b leftMixes[6] = t + tl + m + br + bl + b leftMixes[7] = t + tr + br leftMixes[8] = t + tl + tr + m + br + bl + b leftMixes[9] = t + tl + tr + m + br + b --[[ Program starts here ]]-- firstRun = true lastSync = 0 splitTime = { } --[[ Display "HELO" and delay to ready HTTP API ]]-- print("System sync prep...") rs.setBundledOutput(hourSide, wordHE) rs.setBundledOutput(minSide, wordLO) sleep(10.0) --[[ Loop runs once every 60 secs, time updates once every hour ]]-- while true do --[[ Sync time every 60min/first run ]]-- if firstRun or lastSync >= 60 then rs.setBundledOutput(hourSide, wordSY) rs.setBundledOutput(minSide, wordNC) sleep(10.0) httpPullCheck(timeUrl,"time") if fs.exists( "time" ) then h = fs.open("time","r") newTime = h.readLine() h:close() lastSync = 0 firstRun = false end end --[[ Check that newTime exists, else print 00:00 ]]-- print(newTime) if lastSync == 0 and newTime then splitTime = split(newTime, splitOn) splitTime[1] = tonumber(splitTime[1]) splitTime[2] = tonumber(splitTime[2]) -1 elseif not newTime then splitTime[1] = 0 splitTime[2] = -1 end --[[ Increase minute by 1 ]]-- splitTime[2] = splitTime[2] + 1 --[ Increase hour if needed ]-- if splitTime[2] >= 60 then splitTime[2] = 0 if splitTime[1] == 23 then splitTime[1] = 0 else splitTime[1] = splitTime[1] + 1 end end --[[ Print time check and run digit set functions ]]-- print("Updating time to: "..splitTime[1]..":"..splitTime[2]) --Begin set hour setDoubleDigits(hourSide, splitTime[1]) --Begin set minute setDoubleDigits(minSide, splitTime[2]) print("Time set, last sync "..lastSync.."min ago.") --[[ Sleep for 1min before running again ]]-- os.sleep(60.0) lastSync = lastSync + 1 end