Skip to content

Instantly share code, notes, and snippets.

@mhulse
Created January 24, 2012 01:34
Show Gist options
  • Select an option

  • Save mhulse/1667177 to your computer and use it in GitHub Desktop.

Select an option

Save mhulse/1667177 to your computer and use it in GitHub Desktop.
Caché DTI ContentPublisher: Weather widget example code.
<script language="cache" runat="server">
set id = 1
set exists = ##class(dt.z.guide.weather.hourly).%ExistsId(id)
if (exists) {
set cond = ##class(dt.z.guide.weather.hourly).conditionsGetStored(id)
set temp = ##class(dt.z.guide.weather.hourly).temperatureGetStored(id)
set img = ##class(dt.z.guide.weather.hourly).imageGetStored(id)
if ((cond = "") || (temp = "")) {
set weather = 0
} else {
set weather = 1
}
} else {
set weather = 0
}
if (pageBodyClass = "offsite") {
set weather = 0
}
</script>
<csp:if condition='weather=1'>
<div style="background: url('http://blahblahblah.com/pathto/assets/img/weather/#(img)#') no-repeat 15px 0;">
<div>City, <abbr>ST</abbr></div>
<div>#(cond)# &#151;#(temp)#&#176;</div>
<a href="#">Full forecast</a>
</div>
<csp:else>
<div class="col-3 omega">
<div>City, <abbr>ST</abbr></div>
<div><a href="#">Full forecast</a></div>
</div>
</csp:if>
<DOCTYPE html>
<html>
<title>Weather retrieval/display methods</title>
</html>
<body>
#[ do $$$cspListClassMethods ]#
<script language="cache" method="ShowWeather" arguments="id:%String" returntype="%String">
/// This is a utility to show weather
; Check to see if "guide" exists:
set exists = ##class(dt.z.guide.weather.hourly).%ExistsId(id)
if (exists) {
; Get weather "conditions" and "temperature":
write ##class(dt.z.guide.weather.hourly).conditionsGetStored(id),!
write ##class(dt.z.guide.weather.hourly).temperatureGetStored(id),!
} else {
w "No weather today!",!
}
quit ""
</script>
<script language="cache" method="ReadWeather" arguments="id:%String" returntype="%String">
/// Reads weather data from weather.gov.
set sc = $$$OK
try {
set (sc, reader) = ""
set filename = "http://www.weather.gov/xml/current_obs/KBTR.xml"
set sc = ##class(%XML.TextReader).ParseURL(filename, .reader)
if ($$$ISOK(sc)) {
set (temp, cond, time) = ""
while (reader.Read()) {
if ((reader.Path = "/current_observation/temp_f") && (reader.NodeType = "chars")) {
set temp = reader.Value
}
if ((reader.Path = "/current_observation/weather") && (reader.NodeType = "chars")) {
set cond = reader.Value
}
if ((reader.Path = "/current_observation/observation_time_rfc822") && (reader.NodeType = "chars")) {
set time = reader.Value
}
}
set weather = ##class(dt.z.guide.weather.hourly).%OpenId(1,,.sc)
set weather.temperature = temp
set weather.conditions = cond
set weather.timedate = time
set sc = weather.%Save()
}
} catch e {
set sc = e.AsStatus()
}
quit sc
</script>
<script language="cache" method="DoSomething" arguments='something: %String=""' sqlproc sqlname="Task" returntype="%String">
// log the task to the populate log
do ##class(dt.cms.populate.utility.Utility).logMinor(something)
do ..ReadWeather("")
quit ""
</script>
</body>
</html>
@mhulse

mhulse commented Jan 24, 2012

Copy link
Copy Markdown
Author

The need was to implement a "weather widget" that wouldn't be dependent on the speed/connection of a third-party host. So the idea was to use some custom code, Guides tables, and the Auto-Slot Populate feature in Lightning to retrieve XML data behind the scenes (and less frequently than every page hit) and store that data in a "Guides" table which the “weather widget” could use to populate the current weather.

Full discussion found here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment