Advertisement
1lann

weather

Jun 11th, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.51 KB | None | 0 0
  1. local tempMeasure = "c"
  2. local use12hour = true
  3.  
  4. local isPlastic = false
  5.  
  6. for k,v in pairs(rs.getSides()) do
  7.     if peripheral.getType(v) == "terminal_glasses_bridge" then
  8.         isPlastic = true
  9.         break
  10.     end
  11. end
  12.  
  13. if not isPlastic then print("Could not find Glass Bridge!") error() end
  14.  
  15.  
  16. local plasticInstallation = shell.getRunningProgram()
  17.  
  18. -- Variables & Stuff
  19. local corruption = false
  20. local firstTime = true
  21. local gColors = {}
  22. gColors.red = 0xff3333
  23. gColors.blue = 0x7dd2e4
  24. gColors.yellow = 0xffff4d
  25. gColors.green = 0x4dff4d
  26. gColors.gray = 0xe0e0e0
  27. gColors.textGray = 0x676767
  28. gColors.text = 0x5a5a5a
  29. gColors.rain = 0x2e679f
  30.  
  31. -- Fancy functions
  32. local gWidth = 24
  33. local extraSupport = 0
  34.  
  35. local function reliableSleep(time)
  36.     os.startTimer(time)
  37.     local startTime = os.clock()
  38.     while true do
  39.         os.pullEvent()
  40.         if os.clock() >= startTime+time then return true end
  41.     end
  42. end
  43.  
  44. local function plasticGet(url)
  45.     http.request(url)
  46.     while true do
  47.         local e, rUrl, rmsg = os.pullEvent()
  48.         if (e == "http_success") and (rUrl == url) then
  49.             if rmsg then
  50.                 local data = rmsg.readAll()
  51.                 rmsg.close()
  52.                 if data then
  53.                     return "success", data
  54.                 else
  55.                     http.request(url)
  56.                 end
  57.             else
  58.                 http.request(url)
  59.             end
  60.         elseif (e == "http_failure") and (rUrl == url) then
  61.             return "failure"
  62.         elseif (e == "chat_command") and rUrl == "cancel" then
  63.             return "cancel"
  64.         end
  65.     end
  66. end
  67.  
  68. local function slowText(text, object)
  69.     if not(searchArgs("lite")) then
  70.         object.setText("")
  71.         for i = 1, #text do
  72.             object.setText(string.sub(text, 1, i))
  73.             reliableSleep(0.05)
  74.         end
  75.     else
  76.         object.setText(text)
  77.     end
  78. end
  79.  
  80. local function getCenter(text)
  81.     if #text > 23 then
  82.         extraSupport = 10
  83.     else
  84.         gWidth = 24
  85.         extraSupport = 0
  86.     end
  87.     return (((gWidth/2)-(#text/2))*6)+20+extraSupport
  88. end
  89.  
  90. local function centerText(text, object)
  91.     object.setText("")
  92.     object.setX(getCenter(text))
  93.     slowText(text, object)
  94. end
  95.  
  96. local function trimText(s)
  97.   return s:match("^%s*(.-)%s*$")
  98. end
  99.  
  100. -- Find glass bridge
  101. local plastic = false
  102. local worldSensor = false
  103.  
  104. if fs.exists("/ocs/apis/sensor") then
  105.     os.loadAPI("ocs/apis/sensor")
  106. end
  107.  
  108. for k,v in pairs(rs.getSides()) do
  109.     if peripheral.getType(v) == "terminal_glasses_bridge" then
  110.         plastic = peripheral.wrap(v)
  111.         break
  112.     elseif (peripheral.getType(v) == "sensor") and (not worldSensor) then
  113.         worldSensor = sensor.wrap(v)
  114.         if not (worldSensor.getSensorName() == "worldCard") then
  115.             worldSensor = false
  116.         end
  117.     end
  118. end
  119.  
  120. if not plastic then error("Could not find Glass Bridge!") end
  121.  
  122. -- Setup the actual "plastic"
  123. plastic.clear()
  124. local mainBox = plastic.addBox(20, 20, 1, 48, gColors.gray, 0.7)
  125. local outlineT = plastic.addBox(18,18,2,2,gColors.blue,0.7)
  126. local outlineB = plastic.addBox(18,68,2,2,gColors.blue,0.7)
  127. --Startup Animation
  128. mainBox.setWidth(136)
  129. outlineT.setWidth(140)
  130. outlineB.setWidth(140)
  131. mainBox.setHeight(48)
  132. local header = plastic.addText(70, 25, "", gColors.textGray)
  133. local secondText = plastic.addText(50, 40, "", gColors.blue)
  134. local thirdText = plastic.addText(40, 55, "", gColors.blue)
  135. local mainText = plastic.addText(40, 27, "", gColors.blue)
  136. local tempText = plastic.addText(40, 70, "", gColors.text)
  137. header.setZIndex(5)
  138. mainText.setZIndex(5)
  139. secondText.setZIndex(5)
  140. thirdText.setZIndex(5)
  141.  
  142. --------------------------------------------------------
  143. ---------- Place your app code below here --------------
  144. --------------------------------------------------------
  145.  
  146. --[[
  147. Notes:
  148. Needs additional texts:
  149. One for Temperature - Temp text
  150. One for Degree Measure - Measure Text
  151. One for Min/Max Temperatures - Thirs Text
  152. One for Humidity. - Second Text
  153. One for Location - Main text
  154. One for Current Weather/Today's Weather - Header
  155. One for High: BLAH
  156. Probable Layout:
  157.  ---------------------
  158. H  LOCATION HERE
  159. M WEATHER STATE HERE
  160. S Hu: 86%
  161. T Lo: Hi:
  162.  ---------------------
  163.  
  164. Also requires additional setup. Replace timezone with city.
  165. On startup, load weather data to get time data, and store the time URL (Lat/long)
  166. Replace get online time to use lat and long parameters
  167. ]]
  168. local settings = {}
  169. settings["temperature"] = "c"
  170. settings["use12hour"] = true
  171. local function getRawWT(city) --f or c for unit
  172.     local unit = settings["temperature"]
  173.     local use12hour = settings["use12hour"]
  174.     local months = {"Jan", "Feb", "March", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"}
  175.     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")
  176.     if resp ~= "success" or webData:find("<error>") then
  177.         return "invalid"
  178.     end
  179.     local city, country = webData:match([[<areaName><!%[CDATA%[([^>]+)%]%]></areaName><country><!%[CDATA%[([^>]+)%]%]></country>]])
  180.     if trimText(country) == "United States of America" then
  181.         country = "USA"
  182.     end
  183.     city = trimText(city):gsub(" City", "")
  184.     local resolvedLocation = city .. ", " .. country
  185.  
  186.     local currentTemp = nil
  187.     if unit == "c" then
  188.         currentTemp = webData:match([[<current_condition>.+<temp_C>([^<]+)</temp_C>.+</current_condition>]])
  189.     elseif unit == "f" then
  190.         currentTemp = webData:match([[<current_condititeon>.+<temp_F>([^<]+)</temp_F>.+</current_condition>]])
  191.     else
  192.         return "corrupt"
  193.     end
  194.     local currentHumidity = webData:match([[<current_condition>.+<humidity>([^<]+)</humidity>.+</current_condition>]])
  195.     local currentWeather = trimText(webData:match([[<current_condition>.+<weatherDesc><!%[CDATA%[([^>]+)%]%]></weatherDesc>.+</current_condition>]]))
  196.     local lowTemp, highTemp = nil
  197.     if unit == "c" then
  198.         highTemp = webData:match([[<weather>.+<tempMaxC>([^<]+)</tempMaxC>.+</weather>]])
  199.         lowTemp = webData:match([[<weather>.+<tempMinC>([^<]+)</tempMinC>.+</weather>]])
  200.     elseif unit == "f" then
  201.         highTemp = webData:match([[<weather>.+<tempMaxF>([^<]+)</tempMaxF>.+</weather>]])
  202.         lowTemp = webData:match([[<weather>.+<tempMinF>([^<]+)</tempMinF>.+</weather>]])
  203.     end
  204.     dayWeather = trimText(webData:match([[<weather>.+<weatherDesc><!%[CDATA%[([^<]+)%]%]></weatherDesc>.+</weather>]]))
  205.  
  206.     local time = nil
  207.     local year, month, day, rawTime, timeFormat = webData:match("<localObsDateTime>(%d+)%-(%d+)%-(%d+) (%d+:%d+) (%u+)</localObsDateTime>")
  208.     local resolvedDate = day .. " " .. months[tonumber(month)] .. " " .. year
  209.     if not use12hour then
  210.         if timeFormat == "AM" then
  211.             time = rawTime
  212.         elseif timeFormat == "PM" then
  213.             time = tostring(tonumber(rawTime:sub(1,2))+12) .. rawTime:sub(3,-1)
  214.         else
  215.             return getRawWT(city)
  216.         end
  217.     else
  218.         time = rawTime .. " " .. timeFormat
  219.     end
  220.     if time:sub(1,1) == "0" then time = time:sub(2,-1) end
  221.  
  222.     print("Forecast for: ", resolvedLocation)
  223.     print(" -- CURRENT -- ")
  224.     print("Temperatrue: ", currentTemp, tempMeasure)
  225.     print("Weather: ", currentWeather)
  226.     print("Humidity: ", currentHumidity,"%")
  227.     print("Time: ",  time)
  228.     print("Date: ", resolvedDate)
  229.     print(" -- DAILY --")
  230.     print("Max: ", highTemp, tempMeasure)
  231.     print("Low: ", lowTemp, tempMeasure)
  232.     print("Weather: ", dayWeather)
  233.  
  234.     local current = {}
  235.     current["temp"] = currentTemp
  236.     current["weather"] = currentWeather
  237.     current["humidity"] = currentHumidity
  238.     current["time"] = time
  239.     current["date"] = resolvedDate
  240.     current["url"] = timeURL
  241.  
  242.     local day = {}
  243.     day["high"] = highTemp
  244.     day["low"] = lowTemp
  245.     day["weather"] = dayWeather
  246.  
  247.     return "success", current, day
  248. end
  249.  
  250. -- local tempMeasure = "c"
  251. -- local location = "Perth, Australia"
  252.  
  253. -- getWeather(city, use12hour, unit)
  254. print("Enter location:")
  255. getRawWT(read())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement