Commits

Scott Bennett-McLeish committed ecef6c2

Added temperature collector script for the "Simple Data Collectors" post.

Comments (0)

Files changed (2)

temperature_collectors/ExtractTemperatures.groovy

+import groovy.json.*
+
+class ExtractTemperatures {
+	
+	def processBOM(Expando site) {
+		//create a URL and get the text from it
+		def content = new URL(site.source).text
+
+		//create a new jsonSluper and parse the json data
+		def jsonSlurper = new JsonSlurper()
+		def jsonData = jsonSlurper.parseText(content)
+		
+		//extract just the entry we want
+		def latestEntry = jsonData.observations.data[0]
+		
+		println "\n==== BOM ${site.name} ===="
+		println "airTemp = ${latestEntry.air_temp}"
+		println "windSpeed = ${latestEntry.wind_spd_kmh}"
+		println "windDirection = ${latestEntry.wind_dir}"
+		
+		return latestEntry.air_temp
+	}
+	
+	def processAgDeptWithRegex(Expando site) {
+		//generic URL values
+		String agDeptPrefix = "http://agspsrv34.agric.wa.gov.au/climate/livedata/"
+		String agDeptsuffix = "webpag.htm"
+		
+		//retrieve web page text for the particular site
+		String sourceText = new URL(agDeptPrefix + site.source + agDeptsuffix).text
+		
+		//extract all lines with the "Yellow" formatting, then just get the first one (which we know is the correct one) for Temperature.
+		String firstYellowRow = sourceText.findAll(/<td width="68"><font face="Courier New"[\s]?color="yellow">[\s]?(?:[0-9]+\.[0-9]?)[<\/font>]?<\/td>/)[0]
+		String currentTemp = firstYellowRow.findAll(/[0-9]+\.[0-9]/)[0] //just get the "decimal" element
+		
+		//extract all lines with the "Yellow" formatting, then just get the sixth one (which we know is the correct one) for Wind Speed.
+		String windSpeedElement = sourceText.findAll(/<td width="68"><font face="Courier New"[\s]?color="yellow">[\s]?(?:[0-9]+\.[0-9]?)[<\/font>]?<\/td>/)[5]
+		String currentWindSpeed = windSpeedElement.findAll(/[0-9]+\.[0-9]/)[0] //just get the "decimal" element
+		
+		//extract all lines with the "Yellow" formatting, then just get the one with ENSW variations in it for Wind Direction.
+		String windDirectionElement = sourceText.findAll(/<td width="68"><font face="Courier New"[\s]?color="yellow">[\s]?(?:[ENSW]+)[\s]?[<\/font>]?<\/td>/)[0]
+		String currentWindDirection = windDirectionElement.findAll(/(?!=ow">[\s]?)[ENSW]+[\s]?(?=<\/)/)[0] //just get the element value
+		
+		println "\n==== Ag. Dept. ${site.name} ===="
+		println "airTemp = ${currentTemp}"
+		println "windSpeed = ${currentWindSpeed}"
+		println "windDirection = ${currentWindDirection}"
+	
+		return currentTemp
+	}
+}

temperature_collectors/RunWeatherCollectors.groovy

+g = new ExtractTemperatures()
+
+def th = Thread.start {
+	while (true) {
+		println "\n\n ${new Date().toString()}"
+		
+		def bomPerth = new Expando(source: "http://www.bom.gov.au/fwo/IDW60901/IDW60901.94608.json", name: "Perth")
+		g.processBOM(bomPerth)
+	
+		def agWanneroo = new Expando(source: "wn", name: "Wanneroo")
+		g.processAgDeptWithRegex(agWanneroo)
+		
+		sleep 60000
+	}
+}