Promixis
April 2nd, 2005, 12:25 PM
This is a quick example of a weather object that you can use with threads. Its also shows how you need to use the gir.coinitialize functions to use luacom with threads..
--[[
Weather Object
Object/class for retreiving the weather from weather.com
Also shows how to use luacom from a seperate thread
--]]
WeatherRetrieval = {
Location = 55410,
RefreshInterval = 30, -- minutes refreseh for current conditions,
RefreshTime = 0,
Weather = nil,-- contains current weather
ThreadID = nil, -- contains thread for async retreival
New = function (self,o)
o = o or {}
setmetatable (o,self)
self.__index = self
return o
end,
Update = function (self)
gir.CoInitialize()
if win.GetElapsedSeconds (self.RefreshTime) < self.RefreshInterval then
-- return nil
end
self.RefreshTime = win.GetElapsedSeconds ()
local WeatherHTTPReq = luacom.CreateObject("WinHttp.WinHttpRequest.5.1")
if not WeatherHTTPReq then
return
end
WeatherHTTPReq:Open("GET", "http://www.msnbc.com/m/chnk/d/weather_d_src.asp?acid=" .. self.Location, 0)
WeatherHTTPReq:Send();
if WeatherHTTPReq.Status ~= 200 then
return
end
local s = WeatherHTTPReq.ResponseText
Math = {}
Math.round = function (x)
return math.floor (x)
end
s= string.gsub (s,"}","return this end")
s =(string.gsub (s,"{","this = {}"))
s= string.gsub (s,"sw","")
local sf = loadstring (s)
sf ()
weather = makeWeatherObj ()
Math = nil
WeatherHTTPReq = nil
collectgarbage ()
self.Weather = weather
gir.CoUninitialize();
return true -- success
end,
UpdateAsync = function (self)
self.ThreadID = thread.newthread (function (...) return self:Update (unpack (arg)) end,{self})
end,
Print = function (self)
while self.ThreadID:isthreadrunning () do -- wait if async retrieval
print ("Waiting")
win.Sleep (100)
end
for x,y in pairs (self.Weather) do
print (x,": ",y)
end
end
}
MplsWeather = WeatherRetrieval:New ({Location = 55410})
MplsWeather:UpdateAsync ()
--MplsWeather:Update ()
MplsWeather:Print ()
--[[
Weather Object
Object/class for retreiving the weather from weather.com
Also shows how to use luacom from a seperate thread
--]]
WeatherRetrieval = {
Location = 55410,
RefreshInterval = 30, -- minutes refreseh for current conditions,
RefreshTime = 0,
Weather = nil,-- contains current weather
ThreadID = nil, -- contains thread for async retreival
New = function (self,o)
o = o or {}
setmetatable (o,self)
self.__index = self
return o
end,
Update = function (self)
gir.CoInitialize()
if win.GetElapsedSeconds (self.RefreshTime) < self.RefreshInterval then
-- return nil
end
self.RefreshTime = win.GetElapsedSeconds ()
local WeatherHTTPReq = luacom.CreateObject("WinHttp.WinHttpRequest.5.1")
if not WeatherHTTPReq then
return
end
WeatherHTTPReq:Open("GET", "http://www.msnbc.com/m/chnk/d/weather_d_src.asp?acid=" .. self.Location, 0)
WeatherHTTPReq:Send();
if WeatherHTTPReq.Status ~= 200 then
return
end
local s = WeatherHTTPReq.ResponseText
Math = {}
Math.round = function (x)
return math.floor (x)
end
s= string.gsub (s,"}","return this end")
s =(string.gsub (s,"{","this = {}"))
s= string.gsub (s,"sw","")
local sf = loadstring (s)
sf ()
weather = makeWeatherObj ()
Math = nil
WeatherHTTPReq = nil
collectgarbage ()
self.Weather = weather
gir.CoUninitialize();
return true -- success
end,
UpdateAsync = function (self)
self.ThreadID = thread.newthread (function (...) return self:Update (unpack (arg)) end,{self})
end,
Print = function (self)
while self.ThreadID:isthreadrunning () do -- wait if async retrieval
print ("Waiting")
win.Sleep (100)
end
for x,y in pairs (self.Weather) do
print (x,": ",y)
end
end
}
MplsWeather = WeatherRetrieval:New ({Location = 55410})
MplsWeather:UpdateAsync ()
--MplsWeather:Update ()
MplsWeather:Print ()