Changeset 18


Ignore:
Timestamp:
2011-06-08 19:40:28 (14 years ago)
Author:
mcvflorin
Message:

+ Implemented Arm/Bypass? for sensors.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified I_ElkAlarmPanel1.lua

    r17 r18  
    2828local lug_device = nil 
    2929local g_errorMessage = nil 
    30 local g_isStartupMode = true 
     30local g_pinCode = nil 
    3131local g_taskHandle = -1 
    3232 
     
    366366 
    367367 
    368 ---------------------------- Action implementations --------------------------- 
     368---------------------------- Action Implementations --------------------------- 
     369 
     370 
     371function storePinCode (pinCode) 
     372    debug ("(storePinCode) pinCode=" .. tostring (pinCode)) 
     373 
     374    if (not pinCode or pinCode == "") then 
     375        log ("(storePinCode) ERROR: Invalid PIN code.") 
     376        task ("ERROR: Invalid PIN code.", TASK_ERROR) 
     377        return false 
     378    end 
     379 
     380    pinCode = padLeft (pinCode, 6, '0') 
     381    if (not pinCode:match ("^%d%d%d%d%d%d$")) then 
     382        log ("(storePinCode) ERROR: Invalid PIN code.") 
     383        task ("ERROR: Invalid PIN code.", TASK_ERROR) 
     384        return false 
     385    end 
     386 
     387    g_pinCode = pinCode 
     388end 
    369389 
    370390 
    371391function requestArmMode (device, state, pinCode) 
    372392    debug (string.format ("(requestArmMode) device=%d, state=%s, pinCode=%s", device, state, tostring (pinCode))) 
     393 
     394    if ((not pinCode or pinCode == "") and (not g_pinCode)) then 
     395        log ("(requestArmMode) ERROR: PIN code required.") 
     396        task ("ERROR: PIN code required.", TASK_ERROR) 
     397        return false 
     398    end 
    373399 
    374400    local partition = extractPartition (luup.devices[device].id) 
    375401    if (not partition) then 
    376402        log ("(requestArmMode) ERROR: Found unexpected child.") 
    377         task ("Unknown error encountered.", TASK_ERROR) 
     403        task ("ERROR: Unknown error encountered.", TASK_ERROR) 
    378404        return false 
    379405    end 
     
    404430    end 
    405431 
    406     local data = partition .. padLeft (pinCode, 6, '0') 
     432    local data 
     433    if (pinCode and pinCode ~= "") then 
     434        data = partition .. padLeft (pinCode, 6, '0') 
     435    else 
     436        data = partition .. g_pinCode 
     437    end 
    407438 
    408439    local status = sendCommand (command, data) 
     
    412443        return false 
    413444    end 
    414      
     445 
     446    g_pinCode = pinCode 
    415447    return true 
    416448end 
     
    424456 
    425457 
    426 local function bypassZone (zoneNo, pinCode) 
     458local function unbypassZone (zoneNo, tryCount) 
     459    local functionName = "unbypassZone" 
     460    local errorMessage = string.format ("Failed to unbypass zone %d.", zoneNo) 
     461 
     462    local status = sendIntercepted ("zb", functionName, errorMessage, string.format ("%s1%s", padLeft (zoneNo, 3, '0'), padLeft (g_pinCode, 6, '0'))) 
     463    if (not status) then 
     464        return false 
     465    end 
     466 
     467    local zone, status = readResponse ("ZB", functionName, errorMessage) 
     468    if (zone ~= zoneNo) 
     469        log (string.format ("(%s) ERROR: %s", functionName, errorMessage)) 
     470        task ("ERROR: " .. errorMessage, TASK_ERROR) 
     471        return false 
     472    end 
     473 
     474    if (zone == "unbypassed") then 
     475        debug (string.format ("(%s) SUCCESS: Unbypass successful.", functionName)) 
     476        return true 
     477    end 
     478         
     479    tryCount = (tryCount or 0) + 1 
     480    if (tryCount >= 4) then 
     481        log (string.format ("(%s) ERROR: %s", functionName, errorMessage)) 
     482        task ("ERROR: " .. errorMessage, TASK_ERROR) 
     483        return false 
     484    end 
     485     
     486    return unbypassZone (zoneNo, tryCount) 
     487end 
     488 
     489 
     490local function bypassZone (zoneNo, tryCount) 
    427491    local functionName = "bypassZone" 
    428492    local errorMessage = string.format ("Failed to bypass zone %d.", zoneNo) 
    429493 
    430     local status = sendIntercepted ("zb", functionName, errorMessage, string.format ("%s1%s", padLeft (zoneNo, 3, '0'), padLeft (pinCode, 6, '0'))) 
     494    local status = sendIntercepted ("zb", functionName, errorMessage, string.format ("%s1%s", padLeft (zoneNo, 3, '0'), padLeft (g_pinCode, 6, '0'))) 
    431495    if (not status) then 
    432496        return false 
    433497    end 
    434      
     498 
    435499    local zone, status = readResponse ("ZB", functionName, errorMessage) 
    436      
    437     if (zone ~= zoneNo or status ~= "bypassed") 
     500    if (zone ~= zoneNo) 
    438501        log (string.format ("(%s) ERROR: %s", functionName, errorMessage)) 
    439502        task ("ERROR: " .. errorMessage, TASK_ERROR) 
     
    441504    end 
    442505 
    443     debug (string.format ("(%s) SUCCESS: Bypass successful.", functionName)) 
    444     return true 
    445 end 
    446  
    447  
    448 -- TODO: get PIN code 
     506    if (zone == "bypassed") then 
     507        debug (string.format ("(%s) SUCCESS: Bypass successful.", functionName)) 
     508        return true 
     509    end 
     510         
     511    tryCount = (tryCount or 0) + 1 
     512    if (tryCount >= 4) then 
     513        log (string.format ("(%s) ERROR: %s", functionName, errorMessage)) 
     514        task ("ERROR: " .. errorMessage, TASK_ERROR) 
     515        return false 
     516    end 
     517     
     518    return bypassZone (zoneNo, tryCount) 
     519end 
     520 
     521 
    449522function setArmed (device, newArmedValue) 
    450     debug (string.format ("(setArmed) device=%d, newArmedValue=%s", device, newArmedValue)) 
    451      
     523    debug (string.format ("(setArmed) device=%d, newArmedValue=%s, g_pinCode=%s", device, newArmedValue, tostring (g_pinCode))) 
     524 
     525    if (not g_pinCode) then 
     526        log (string.format ("(%s) ERROR: Pin code required.", functionName)) 
     527        task ("ERROR: Pin code required.", TASK_ERROR) 
     528        return false 
     529    end 
     530 
    452531    local zone = extractZone (luup.devices[device].id) 
    453532    if (not zone) then 
     
    456535        return false 
    457536    end 
    458      
     537 
    459538    local status; 
    460539    if (newArmedValue == "0") then 
    461540        status = bypassZone (zone) 
    462541    else 
    463     end 
    464      
     542        status = unbypassZone (zone) 
     543    end 
     544 
    465545    if (status) then 
    466546        luup.variable_set (SECURITY_SID, "Armed", newArmedValue, device) 
     
    789869local function readResponse (expectedMsgType, functionName, errorMsg) 
    790870    local retryCount = 0 
    791      
     871 
    792872    local msg = luup.io.read() 
    793873    local pos = msg:find (expectedMsgType) 
     
    798878        retryCount = retryCount + 1 
    799879    end 
    800      
     880 
    801881    if (not pos) then 
    802882        log (string.format ("(%s) ERROR: %s", functionName, errorMsg)) 
     
    811891        return status 
    812892    end 
    813      
     893 
    814894    return status 
    815895end 
     
    841921        return false 
    842922    end 
    843      
     923 
    844924    return true 
    845925end 
     
    9431023        return readResponse ("ZS", functionName, errorMessage) 
    9441024    end 
    945      
     1025 
    9461026    return false 
    9471027end 
     
    9611041        end 
    9621042    end 
    963      
     1043 
    9641044    return true 
    9651045end 
     
    10031083        return readResponse ("KF", functionName, errorMessage) 
    10041084    end 
    1005      
     1085 
    10061086    return false 
    10071087end 
     
    10161096        return readResponse ("AS", functionName, errorMessage) 
    10171097    end 
    1018      
     1098 
    10191099    return false 
    10201100 end 
     
    10341114        end 
    10351115    end 
    1036      
     1116 
    10371117    return true 
    10381118end 
     
    10411121local function getZonesAndPartitions() 
    10421122    local functionName = "getZonesAndPartitions" 
    1043      
     1123 
    10441124    -- Get zone information. 
    10451125    local errorMessage = "Failed to get zones." 
     
    10581138        return readResponse ("ZP", functionName, errorMessage) 
    10591139    end 
    1060      
     1140 
    10611141    return false 
    10621142end 
     
    10711151        return readResponse ("VN", functionName, errorMessage) 
    10721152    end 
    1073          
     1153 
    10741154    return nil 
    10751155end 
     
    10911171 
    10921172    -- Get all the zones and active partitions. 
    1093     local status, message = getZonesAndPartitions() 
     1173    local status = getZonesAndPartitions() 
    10941174    if (not status) then 
    1095         return false, tostring (message), "Elk Alarm Panel" 
     1175        return false, g_errorMessage, "Elk Alarm Panel" 
    10961176    end 
    10971177 
     
    10991179 
    11001180    -- Get each Partition's status. 
    1101     status, message = getPartitionStatuses() 
     1181    status = getPartitionStatuses() 
    11021182    if (not status) then 
    1103         return false, tostring (message), "Elk Alarm Panel" 
     1183        return false, g_errorMessage, "Elk Alarm Panel" 
    11041184    end 
    11051185 
     
    11191199    -- Get a list with the created child devices. 
    11201200    getChildDevices() 
    1121      
     1201 
    11221202    -- Get each Zone's status. 
    1123     status, message = getZoneStatuses() 
     1203    status = getZoneStatuses() 
    11241204    if (not status) then 
    1125         return false, tostring (message), "Elk Alarm Panel" 
     1205        return false, g_errorMessage, "Elk Alarm Panel" 
    11261206    end 
    11271207 
Note: See TracChangeset for help on using the changeset viewer.