Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tempMeasure = "c"
- local use12hour = true
- local isPlastic = false
- for k,v in pairs(rs.getSides()) do
- if peripheral.getType(v) == "terminal_glasses_bridge" then
- isPlastic = true
- break
- end
- end
- if not isPlastic then print("Could not find Glass Bridge!") error() end
- local plasticInstallation = shell.getRunningProgram()
- -- Variables & Stuff
- local corruption = false
- local firstTime = true
- local gColors = {}
- gColors.red = 0xff3333
- gColors.blue = 0x7dd2e4
- gColors.yellow = 0xffff4d
- gColors.green = 0x4dff4d
- gColors.gray = 0xe0e0e0
- gColors.textGray = 0x676767
- gColors.text = 0x5a5a5a
- gColors.rain = 0x2e679f
- -- Fancy functions
- local gWidth = 24
- local extraSupport = 0
- local function reliableSleep(time)
- os.startTimer(time)
- local startTime = os.clock()
- while true do
- os.pullEvent()
- if os.clock() >= startTime+time then return true end
- end
- end
- local function plasticGet(url)
- http.request(url)
- while true do
- local e, rUrl, rmsg = os.pullEvent()
- if (e == "http_success") and (rUrl == url) then
- if rmsg then
- local data = rmsg.readAll()
- rmsg.close()
- if data then
- return "success", data
- else
- http.request(url)
- end
- else
- http.request(url)
- end
- elseif (e == "http_failure") and (rUrl == url) then
- return "failure"
- elseif (e == "chat_command") and rUrl == "cancel" then
- return "cancel"
- end
- end
- end
- local function slowText(text, object)
- if not(searchArgs("lite")) then
- object.setText("")
- for i = 1, #text do
- object.setText(string.sub(text, 1, i))
- reliableSleep(0.05)
- end
- else
- object.setText(text)
- end
- end
- local function getCenter(text)
- if #text > 23 then
- extraSupport = 10
- else
- gWidth = 24
- extraSupport = 0
- end
- return (((gWidth/2)-(#text/2))*6)+20+extraSupport
- end
- local function centerText(text, object)
- object.setText("")
- object.setX(getCenter(text))
- slowText(text, object)
- end
- local function trimText(s)
- return s:match("^%s*(.-)%s*$")
- end
- -- Find glass bridge
- local plastic = false
- local worldSensor = false
- if fs.exists("/ocs/apis/sensor") then
- os.loadAPI("ocs/apis/sensor")
- end
- for k,v in pairs(rs.getSides()) do
- if peripheral.getType(v) == "terminal_glasses_bridge" then
- plastic = peripheral.wrap(v)
- break
- elseif (peripheral.getType(v) == "sensor") and (not worldSensor) then
- worldSensor = sensor.wrap(v)
- if not (worldSensor.getSensorName() == "worldCard") then
- worldSensor = false
- end
- end
- end
- if not plastic then error("Could not find Glass Bridge!") end
- -- Setup the actual "plastic"
- plastic.clear()
- local mainBox = plastic.addBox(20, 20, 1, 48, gColors.gray, 0.7)
- local outlineT = plastic.addBox(18,18,2,2,gColors.blue,0.7)
- local outlineB = plastic.addBox(18,68,2,2,gColors.blue,0.7)
- --Startup Animation
- mainBox.setWidth(136)
- outlineT.setWidth(140)
- outlineB.setWidth(140)
- mainBox.setHeight(48)
- local header = plastic.addText(70, 25, "", gColors.textGray)
- local secondText = plastic.addText(50, 40, "", gColors.blue)
- local thirdText = plastic.addText(40, 55, "", gColors.blue)
- local mainText = plastic.addText(40, 27, "", gColors.blue)
- local tempText = plastic.addText(40, 70, "", gColors.text)
- header.setZIndex(5)
- mainText.setZIndex(5)
- secondText.setZIndex(5)
- thirdText.setZIndex(5)
- --------------------------------------------------------
- ---------- Place your app code below here --------------
- --------------------------------------------------------
- --[[
- Notes:
- Needs additional texts:
- One for Temperature - Temp text
- One for Degree Measure - Measure Text
- One for Min/Max Temperatures - Thirs Text
- One for Humidity. - Second Text
- One for Location - Main text
- One for Current Weather/Today's Weather - Header
- One for High: BLAH
- Probable Layout:
- ---------------------
- H LOCATION HERE
- M WEATHER STATE HERE
- S Hu: 86%
- T Lo: Hi:
- ---------------------
- Also requires additional setup. Replace timezone with city.
- On startup, load weather data to get time data, and store the time URL (Lat/long)
- Replace get online time to use lat and long parameters
- ]]
- local settings = {}
- settings["temperature"] = "c"
- settings["use12hour"] = true
- local function getRawWT(city) --f or c for unit
- local unit = settings["temperature"]
- local use12hour = settings["use12hour"]
- local months = {"Jan", "Feb", "March", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"}
- local resp, webData = plasticGet("http://api.worldweatheronline.com/free/v1/weather.ashx?q="..textutils.urlEncode(city).."&format=xml&extra=localObsTime&num_of_days=1&includelocation=yes&key=8gfsty6jk2qp5rw2jd4yaeee")
- if resp ~= "success" or webData:find("<error>") then
- return "invalid"
- end
- local city, country = webData:match([[<areaName><!%[CDATA%[([^>]+)%]%]></areaName><country><!%[CDATA%[([^>]+)%]%]></country>]])
- if trimText(country) == "United States of America" then
- country = "USA"
- end
- city = trimText(city):gsub(" City", "")
- local resolvedLocation = city .. ", " .. country
- local currentTemp = nil
- if unit == "c" then
- currentTemp = webData:match([[<current_condition>.+<temp_C>([^<]+)</temp_C>.+</current_condition>]])
- elseif unit == "f" then
- currentTemp = webData:match([[<current_condititeon>.+<temp_F>([^<]+)</temp_F>.+</current_condition>]])
- else
- return "corrupt"
- end
- local currentHumidity = webData:match([[<current_condition>.+<humidity>([^<]+)</humidity>.+</current_condition>]])
- local currentWeather = trimText(webData:match([[<current_condition>.+<weatherDesc><!%[CDATA%[([^>]+)%]%]></weatherDesc>.+</current_condition>]]))
- local lowTemp, highTemp = nil
- if unit == "c" then
- highTemp = webData:match([[<weather>.+<tempMaxC>([^<]+)</tempMaxC>.+</weather>]])
- lowTemp = webData:match([[<weather>.+<tempMinC>([^<]+)</tempMinC>.+</weather>]])
- elseif unit == "f" then
- highTemp = webData:match([[<weather>.+<tempMaxF>([^<]+)</tempMaxF>.+</weather>]])
- lowTemp = webData:match([[<weather>.+<tempMinF>([^<]+)</tempMinF>.+</weather>]])
- end
- dayWeather = trimText(webData:match([[<weather>.+<weatherDesc><!%[CDATA%[([^<]+)%]%]></weatherDesc>.+</weather>]]))
- local time = nil
- local year, month, day, rawTime, timeFormat = webData:match("<localObsDateTime>(%d+)%-(%d+)%-(%d+) (%d+:%d+) (%u+)</localObsDateTime>")
- local resolvedDate = day .. " " .. months[tonumber(month)] .. " " .. year
- if not use12hour then
- if timeFormat == "AM" then
- time = rawTime
- elseif timeFormat == "PM" then
- time = tostring(tonumber(rawTime:sub(1,2))+12) .. rawTime:sub(3,-1)
- else
- return getRawWT(city)
- end
- else
- time = rawTime .. " " .. timeFormat
- end
- if time:sub(1,1) == "0" then time = time:sub(2,-1) end
- print("Forecast for: ", resolvedLocation)
- print(" -- CURRENT -- ")
- print("Temperatrue: ", currentTemp, tempMeasure)
- print("Weather: ", currentWeather)
- print("Humidity: ", currentHumidity,"%")
- print("Time: ", time)
- print("Date: ", resolvedDate)
- print(" -- DAILY --")
- print("Max: ", highTemp, tempMeasure)
- print("Low: ", lowTemp, tempMeasure)
- print("Weather: ", dayWeather)
- local current = {}
- current["temp"] = currentTemp
- current["weather"] = currentWeather
- current["humidity"] = currentHumidity
- current["time"] = time
- current["date"] = resolvedDate
- current["url"] = timeURL
- local day = {}
- day["high"] = highTemp
- day["low"] = lowTemp
- day["weather"] = dayWeather
- return "success", current, day
- end
- -- local tempMeasure = "c"
- -- local location = "Perth, Australia"
- -- getWeather(city, use12hour, unit)
- print("Enter location:")
- getRawWT(read())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement