- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
/I_WeMo1.xml
r1 r3 8 8 this_plugin = require ("L_WeMo1") 9 9 reentry = this_plugin.reentry 10 cancelProxySubscription = this_plugin.cancelProxySubscription 10 11 return this_plugin.initialize (lul_device) 11 12 end -
/L_WeMo1.lua
r1 r3 375 375 local duration = headers["timeout"]:match("Second%-(%d+)") 376 376 debug("Subscription confirmed, SID = " .. headers["sid"] .. " with timeout " .. duration, 2) 377 return headers["sid"], duration377 return headers["sid"], tonumber(duration) 378 378 end 379 379 end 380 380 381 381 -- 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. 382 388 function getProxyApiVersion() 383 389 local sock = function() … … 411 417 end 412 418 419 -- proxyVersionAtLeast(n) 420 -- Returns true if the proxy is running and is at least version n. 413 421 function proxyVersionAtLeast(n) 414 422 if (ProxyApiVersion and tonumber(ProxyApiVersion:match("^(%d+)")) >= n) then … … 418 426 end 419 427 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. 420 436 function informProxyOfSubscription(deviceId) 421 437 debug("Informing proxy of subscription for device " .. deviceId, 2) … … 440 456 method = "PUT", 441 457 headers = { 442 ["Content-Type"] = "text/ html",458 ["Content-Type"] = "text/xml", 443 459 ["Content-Length"] = proxyRequestBody:len(), 444 460 }, … … 458 474 end 459 475 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. 482 function 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 507 end 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. 460 512 function queueAction(delay, retries, action) 461 513 table.insert(FutureActionQueue, { … … 466 518 end 467 519 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). 468 527 function renewSubscription(deviceId) 469 528 debug("Renewing subscription for device " .. deviceId, 2) … … 512 571 end 513 572 end 514 515 end 516 573 end 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). 517 578 function schedule() 518 579 -- How long to sleep? 519 local delay = 3 600580 local delay = 300 520 581 for i = 1, #FutureActionQueue do 521 582 if (FutureActionQueue[i].time <= os.time()) then … … 529 590 end 530 591 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. 531 596 function reentry() 532 597 local action = nil … … 538 603 end 539 604 605 -- Clock skew might mean there is no action. 540 606 if (action) then 541 607 local result = action.action() … … 745 811 end 746 812 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) 747 815 return false 748 816 end … … 833 901 834 902 -- handleSetTarget(lul_device, newTargetValue) 903 -- Called when the user asks to turn on or off a switch. 835 904 function handleSetTarget(lul_device, newTargetValue) 836 905 debug("Setting Target = " .. newTargetValue .. " for device " .. lul_device) … … 861 930 return false 862 931 end 863 864 function processTimerQueue()865 end
Note: See TracChangeset
for help on using the changeset viewer.