Changes in / [1:3]


Ignore:
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • /I_WeMo1.xml

    r1 r3  
    88    this_plugin = require ("L_WeMo1") 
    99    reentry = this_plugin.reentry 
     10    cancelProxySubscription = this_plugin.cancelProxySubscription 
    1011    return this_plugin.initialize (lul_device) 
    1112end 
  • /L_WeMo1.lua

    r1 r3  
    375375        local duration = headers["timeout"]:match("Second%-(%d+)") 
    376376        debug("Subscription confirmed, SID = " .. headers["sid"] .. " with timeout " .. duration, 2) 
    377         return headers["sid"], duration 
     377        return headers["sid"], tonumber(duration) 
    378378    end 
    379379end 
    380380 
    381381-- getProxyApiVersion() 
     382-- Calls the proxy with GET /version. 
     383-- Sets the ProxyApiVersion Luup variable to the value received 
     384-- (or the empty string). 
     385-- Return value: 
     386--   nil if the proxy is not running. 
     387--   The proxy API version (as a string) otherwise. 
    382388function getProxyApiVersion() 
    383389    local sock = function() 
     
    411417end 
    412418 
     419-- proxyVersionAtLeast(n) 
     420-- Returns true if the proxy is running and is at least version n. 
    413421function proxyVersionAtLeast(n) 
    414422    if (ProxyApiVersion and tonumber(ProxyApiVersion:match("^(%d+)")) >= n) then 
     
    418426end 
    419427 
     428-- informProxyOfSubscription(deviceId) 
     429-- Sends a PUT /upnp/event/[sid] message to the proxy, 
     430-- asking it to inform this plugin if the BinaryState 
     431-- UPnP variable changes. 
     432-- Return value: 
     433--   nil if the proxy timed out (should try again). 
     434--   false if the proxy refused our request (permanently). 
     435--   true if the proxy agreed to our request. 
    420436function informProxyOfSubscription(deviceId) 
    421437    debug("Informing proxy of subscription for device " .. deviceId, 2) 
     
    440456        method = "PUT", 
    441457        headers = { 
    442             ["Content-Type"] = "text/html", 
     458            ["Content-Type"] = "text/xml", 
    443459            ["Content-Length"] = proxyRequestBody:len(), 
    444460        }, 
     
    458474end 
    459475 
     476-- cancelProxySubscription(sid) 
     477-- Sends a DELETE /upnp/event/[sid] message to the proxy, 
     478-- Return value: 
     479--   nil if the proxy timed out (should try again). 
     480--   false if the proxy refused our request (permanently). 
     481--   true if the proxy agreed to our request. 
     482function cancelProxySubscription(sid) 
     483    debug("Cancelling unwelcome subscription for sid " .. sid, 2) 
     484    local sock = function() 
     485        local s = socket.tcp() 
     486        s:settimeout(2) 
     487        return s 
     488    end 
     489 
     490    local request, code = http.request({ 
     491        url = "http://localhost:2529/upnp/event/" .. url.escape(sid), 
     492        create = sock, 
     493        method = "DELETE", 
     494        source = ltn12.source.empty(), 
     495        sink = ltn12.sink.null(), 
     496    }) 
     497    if (request == nil and code ~= "closed") then 
     498        debug("Failed to cancel subscription: " .. code, 2) 
     499        return nil 
     500    elseif (code ~= 200) then 
     501        debug("Failed to cancel subscription: " .. code, 2) 
     502        return false 
     503    else 
     504        debug("Successfully cancelled subscription", 2) 
     505        return true 
     506    end 
     507end 
     508 
     509-- queueAction(delay, retries, action) 
     510-- Remember to run the function in action (with no parameters) 
     511-- in delay seconds.  Allow only the specified number of retries. 
    460512function queueAction(delay, retries, action) 
    461513    table.insert(FutureActionQueue, { 
     
    466518end 
    467519 
     520-- renewSubscription(deviceId) 
     521-- Try to renew the UPnP subscription for the child device deviceId. 
     522-- Return value: 
     523--   nil if the renewal request timed out (and we should retry). 
     524--   false if the renewal was refused (permanently). 
     525--   true if the renewal was accepted (a later renewal will be 
     526--     queued and the proxy will be informed). 
    468527function renewSubscription(deviceId) 
    469528    debug("Renewing subscription for device " .. deviceId, 2) 
     
    512571        end 
    513572    end 
    514  
    515 end 
    516  
     573end 
     574 
     575-- schedule() 
     576-- Ask the plugin to sleep for as many seconds as 
     577-- the next event (or five minutes, if there are no events). 
    517578function schedule() 
    518579    -- How long to sleep? 
    519     local delay = 3600 
     580    local delay = 300 
    520581    for i = 1, #FutureActionQueue do 
    521582        if (FutureActionQueue[i].time <= os.time()) then 
     
    529590end 
    530591 
     592-- reentry() 
     593-- This function will be called when a sleep from 
     594-- schedule() completes.  In theory, one of the queued actions 
     595-- is now ready to perform. 
    531596function reentry() 
    532597    local action = nil 
     
    538603    end 
    539604 
     605    -- Clock skew might mean there is no action. 
    540606    if (action) then 
    541607        local result = action.action() 
     
    745811    end 
    746812    debug("SID does not match: expected " .. ChildDevices[lul_device].sid .. ", got " .. sid) 
     813  -- Try to shut the proxy up, we don't care about this SID. 
     814    luup.call_delay("cancelProxySubscription", 1, sid) 
    747815    return false 
    748816end 
     
    833901 
    834902-- handleSetTarget(lul_device, newTargetValue) 
     903-- Called when the user asks to turn on or off a switch. 
    835904function handleSetTarget(lul_device, newTargetValue) 
    836905    debug("Setting Target = " .. newTargetValue .. " for device " .. lul_device) 
     
    861930    return false 
    862931end 
    863  
    864 function processTimerQueue() 
    865 end 
Note: See TracChangeset for help on using the changeset viewer.