<?sphp $this->text('pagetitle') ?>
 
Home of the Squeezebox™ & Transporter® network music players.

Christchurch Weather Live source code

From SqueezeboxWiki

Revision as of 11:53, 17 March 2011 by Soulkeeper (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This page is a discussion on the source code to an applet I created called Christchurch Weather Live

You can download the source code (same as below) from http://squeezebox.crowe.co.nz/applets/WeatherLive/V1.00/WeatherLive.zip and I have an applet respository at http://squeezebox.crowe.co.nz/appletrepository.xml

Contents

File #1 of 3 - The Configuration File (WeatherLiveMeta.lua)

local oo            = require("loop.simple")
local AppletMeta    = require("jive.AppletMeta")
local appletManager = appletManager
local jiveMain      = jiveMain

module(...)
oo.class(_M, AppletMeta)
 
function jiveVersion(meta)
     return 1, 1
end

function defaultSettings(meta)
     return {
             currentSetting = 0,
     }
end

function registerApplet(meta)
     jiveMain:addItem(meta:menuItem('weatherliveApplet', 'home', "Weather Live", function(applet, ...) applet:menu(...) end, 20))
end

The WeatherLiveMeta.lua file is a configuration file that is used to register the applet. It defines what versions the applet will run on and it also allows us to create menu items when we install the applet.

File #2 of 3 - The Applet File (WeatherLiveApplet.lua)

--[[
 =head1 NAME
 
 applets.WeatherLive - Weather Applet
 
 =head1 DESCRIPTION
 
 This applet was created to show my weather data from http://weather.crowe.co.nz on the SqueeseBox controller
 
 =head1 FUNCTIONS
 
 Applet related methods are described in <jive.Applet>. 
 
 =cut
 --]]
 
local tostring = tostring

local io                    = require("io")
local oo                    = require("loop.simple")
local math                  = require("math")
local string                = require("string")
local jul             		= require("jive.utils.log")
local RequestHttp     		= require("jive.net.RequestHttp")
local SocketHttp      		= require("jive.net.SocketHttp")
local Applet                = require("jive.Applet")
local Checkbox              = require("jive.ui.Checkbox")
local Choice                = require("jive.ui.Choice")
local Framework             = require("jive.ui.Framework")
local Icon                  = require("jive.ui.Icon")
local Label                 = require("jive.ui.Label")
local Popup                 = require("jive.ui.Popup")
local Group                 = require("jive.ui.Group")
local RadioButton           = require("jive.ui.RadioButton")
local RadioGroup            = require("jive.ui.RadioGroup")
local SimpleMenu            = require("jive.ui.SimpleMenu")
local Slider                = require("jive.ui.Slider")
local Surface               = require("jive.ui.Surface")
local Textarea              = require("jive.ui.Textarea")
local Textinput             = require("jive.ui.Textinput")
local Window                = require("jive.ui.Window")
local Timer                 = require("jive.ui.Timer")
local jnt              		= jnt
 
local EVENT_KEY_PRESS  		= jive.ui.EVENT_KEY_PRESS
local EVENT_MOUSE_PRESS     = jive.ui.EVENT_MOUSE_PRESS

local log             		= jul.logger("applets.setup")

 
module(...)
oo.class(_M, Applet)
 
log:info("WeatherLive Loaded")
 
function menu(self, menuItem)
 
       log:info("menu")
       local group = RadioGroup()
       local currentSetting = self:getSettings().currentSetting
 
       -- create a SimpleMenu object with selections to be created
       local menu = SimpleMenu("menu", {	
		{ 	text = self:string("WEATHERLIVE_SUMMARY"),
			sound = "WINDOWSHOW",
			callback = function(event, menuItem)					
				self:DownloadAndShowLiveSummaryImage(menuItem)
			end },
		{ 	text = self:string("WEATHERLIVE_WINDSUMMARY"),
			sound = "WINDOWSHOW",
			callback = function(event, menuItem)					
				self:DownloadAndShowWindSummaryImage(menuItem)
			end },
		{	text = self:string("WEATHERLIVE_NORTHISLAND"),
			sound = "WINDOWSHOW",
			callback = function(event, menuItem)					
				self:DownloadAndShowNorthIsland(menuItem)
			end },	
		{	text = self:string("WEATHERLIVE_SOUTHISLAND"),
			sound = "WINDOWSHOW",
			callback = function(event, menuItem)					
				self:DownloadAndShowSouthIsland(menuItem)
			end },
		{	text = self:string("WEATHERLIVE_FORECAST7DAYS"),
			sound = "WINDOWSHOW",
			callback = function(event, menuItem)					
				self:DownloadAndShow7DayForecast(menuItem)
			end },
		{	text = self:string("WEATHERLIVE_SUNRISE"),
			sound = "WINDOWSHOW",
			callback = function(event, menuItem)					
				self:DownloadAndShowSunRiseSunSet(menuItem)
			end	}
		})
 
       -- create a window object
       local window = Window("window", self:string("WEATHERLIVE")) 
 
       -- add the SimpleMenu to the window
       window:addWidget(menu)
 
       -- show the window
       window:show()
end
 
function DownloadAndShow7DayForecast(self, menuItem)
	DownloadAndShowText(self, menuItem, "/SqueezeBox/7DayForecast.aspx")
end

function DownloadAndShowSunRiseSunSet(self, menuItem)
	DownloadAndShowText(self, menuItem, "/SqueezeBox/SunriseSunset.aspx")
end
 
function DownloadAndShowLiveSummaryImage(self, menuItem)
	DownloadAndShowSummaryImage(self, menuItem, "/SqueezeBox/LiveSummary.aspx")
end
 
function DownloadAndShowWindSummaryImage(self, menuItem)
	DownloadAndShowSummaryImage(self, menuItem, "/SqueezeBox/WindSummary.aspx")
end

function DownloadAndShowNorthIsland(self, menuItem)
	DownloadAndShowSummaryImage(self, menuItem, "/SqueezeBox/NorthIsland.aspx")
end

function DownloadAndShowSouthIsland(self, menuItem)
	DownloadAndShowSummaryImage(self, menuItem, "/SqueezeBox/SouthIsland.aspx")
end
 
function DownloadAndShowSummaryImage(self, menuItem, path)
	
	local window = Window("window")
	
	local host = "weather.crowe.co.nz"
	local port = 80
	log:info("photo URL: ", host, ":", port, path)

	-- request photo
	local http = SocketHttp(jnt, host, port, "Weather SBC")
	local req = RequestHttp(function(chunk, err)
			if chunk then
				local srf = Surface:loadImageData(chunk, #chunk)
				window:addWidget(Icon("image", srf))
			end
		end,
		'GET',
		path)
	http:fetch(req)

	window:addListener(EVENT_KEY_PRESS | EVENT_MOUSE_PRESS,
		 function(event)
			  window:hide()
			  return EVENT_CONSUME
		end
	)
	
	 self:tieAndShowWindow(window)
	return window
end

function DownloadAndShowText(self, menuItem, path)
	
	local window = Window("window")
	
	local host = "weather.crowe.co.nz"
	local port = 80
	log:info("photo URL: ", host, ":", port, path)

	-- request photo
	local http = SocketHttp(jnt, host, port, "Weather SBC")
	local req = RequestHttp(function(chunk, err)
			if chunk then				
				window:addWidget(Textarea("textarea", chunk))
			end
		end,
		'GET',
		path)
	http:fetch(req)

	window:addListener(EVENT_KEY_PRESS | EVENT_MOUSE_PRESS,
		 function(event)
			  window:hide()
			  return EVENT_CONSUME
		end
	)
	
	 self:tieAndShowWindow(window)
	return window
end

File #3 of 3 - The Language File (strings.txt)

WEATHERLIVE
	EN	Christchurch Weather Live

WEATHERLIVE_SUMMARY
	EN	Live Summary
	
WEATHERLIVE_WINDSUMMARY
	EN	Wind Summary

WEATHERLIVE_NORTHISLAND
	EN	North Island

WEATHERLIVE_SOUTHISLAND
	EN	South Island
	
WEATHERLIVE_FORECAST7DAYS
	EN	7 Day Forecast
	
WEATHERLIVE_SUNRISE
	EN	Sunrise and Sunset

See The strings.txt File for an explanation of the file above.

See also