Changeset 11


Ignore:
Timestamp:
2011-05-23 19:30:08 (14 years ago)
Author:
mcvflorin
Message:
  • Updated the code to use 'luup.io.intercept' in the startup initialization functions.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified I_ElkAlarmPanel1.lua

    r10 r11  
    3030local log = luup.log 
    3131 
    32 ---------- Generic Utilities ---------- 
     32------------------------------ Generic Utilities ------------------------------ 
    3333 
    3434local function debug (text) 
     
    149149end 
    150150 
    151 ---------- Action implementations ---------- 
     151---------------------------- Action implementations --------------------------- 
    152152 
    153153local function requestArmMode (device, state, pinCode) 
     
    190190end 
    191191 
    192 ---------- Message Handling ---------- 
     192------------------------- Message Handling: Constants ------------------------- 
    193193 
    194194--[[ Here only for reference: 
     
    388388    ['T'] = "Intercom Key" 
    389389} 
     390 
     391----------------- Message Processing: Function Implementations ---------------- 
    390392 
    391393local function processZoneChangeUpdate (data) 
     
    579581end 
    580582 
    581 ---------- Incoming Message Processing ---------- 
     583------------------------- Incoming Message Processing ------------------------- 
    582584 
    583585local PANEL_RESPONSES = { 
     
    587589            debug ("Message type description: Zone Definition Data") 
    588590            processZoneDefinitionData (data) 
     591            return true 
    589592        end 
    590593    }, 
     
    594597            debug ("Message type description: Zone Partion Report") 
    595598            processZonePartitionReport (data) 
     599            return true 
    596600        end 
    597601    }, 
     
    601605            debug ("Message type description: Zone and Partition Labels") 
    602606            processLabels (data) 
     607            return true 
    603608        end 
    604609    }, 
     
    608613            debug ("Message type description: Zone Status Report") 
    609614            processZoneStatusReport (data) 
     615            return true 
    610616        end 
    611617    }, 
     
    615621            debug ("Message type description: Arming Status Report") 
    616622            processArmingStatusReport (data) 
     623            return true 
    617624        end 
    618625    }, 
     
    622629            debug ("Message type description: Keypad Function Key Press") 
    623630            processKeypadKeyPress (data) 
     631            return true 
    624632        end 
    625633    }, 
     
    629637            debug ("Message type description: System Trouble Status") 
    630638            processSystemTroubleStatus (data) 
     639            return true 
    631640        end 
    632641    }, 
     
    636645            debug ("Message type description: Log Data") 
    637646            processLogData (data) 
     647            return true 
    638648        end 
    639649    }, 
     
    643653            debug ("Message type description: Zone Change Update") 
    644654            processZoneChangeUpdate (data) 
     655            return true 
     656        end 
     657    }, 
     658    ["VN"] = { 
     659        "M1 Version Number", 
     660        function (data) 
     661            debug ("Message type description: M1 Version Number") 
     662            if (data == "") then 
     663                log ("ERROR: Missing version data.") 
     664                return 
     665            end 
     666            local uver, mver, lver = data:match ("^(%w%w)(%w%w)(%w%w)") 
     667            return string.format ("%d.%d.%d", tonumber (uver, 16), tonumber (mver, 16), tonumber (lver, 16)) 
    645668        end 
    646669    } 
     
    676699end 
    677700 
    678 ---------- Startup Functions ---------- 
     701------------------------------ Startup Functions ------------------------------ 
    679702 
    680703local function sendCommand (command, data) 
     
    686709    debug ("(sendCommand) Command to send: " .. cmd) 
    687710    if (not luup.io.write (cmd)) then 
    688         local message = string.format ("ERROR (sendCommand): Cannot send command: '%s'", cmd) 
    689         log (message) 
    690         luup.set_failure (true) 
    691  
    692         return false, message 
    693     end 
    694  
    695     return true, "Ok" 
     711        log (string.format ("(sendCommand) ERROR: Failed to send command: '%s'", cmd)) 
     712        return false 
     713    end 
     714 
     715    return true 
    696716end 
    697717 
     
    756776 
    757777 
     778local function getZonesStatus() 
     779    luup.io.intercept() 
     780    local status = sendCommand ("zs") 
     781    if (not status) then 
     782        log ("(getZonesStatus) ERROR: Failed to get zones status.") 
     783        return false, "Failed to get zones status." 
     784    end 
     785    local msg = luup.io.read() 
     786    status = processMessage (msg) 
     787    if (not status) then 
     788        log ("(getZonesStatus) ERROR: Failed to get zones status.") 
     789        return false, "Failed to get zones status." 
     790    end 
     791 
     792end 
     793 
     794 
    758795local function getZoneNames() 
    759796    for i in ipairs (g_childDevices.zones) do 
    760         sendCommand ("sd", string.format ("00%03d", i)) 
     797        luup.io.intercept() 
     798        local status = sendCommand ("sd", string.format ("00%03d", i)) 
     799        if (not status) then 
     800            log ("(getZoneNames) ERROR: Failed to get zones names.") 
     801        end 
     802        local msg = luup.io.read() 
     803        status = processMessage (msg) 
     804        if (not status) then 
     805            log ("(getZoneNames) ERROR: Failed to get zones names.") 
     806        end 
    761807    end 
    762808end 
     
    792838 
    793839 
    794 local function getPartitionNames() 
     840 local function getPartitionsStatus() 
     841    luup.io.intercept() 
     842    local status = sendCommand ("as") 
     843    if (not status) then 
     844        log ("(getPartitionsStatus) ERROR: Failed to get partitions status.") 
     845        return false, "Failed to get partitions status." 
     846    end 
     847    local msg = luup.io.read() 
     848    status = processMessage (msg) 
     849    if (not status) then 
     850        log ("(getPartitionsStatus) ERROR: Failed to get partitions status.") 
     851        return false, "Failed to get partitions status." 
     852    end 
     853 end 
     854 
     855 
     856local function getPartitionsName() 
    795857    for i in ipairs (g_childDevices.partitions) do 
    796         sendCommand ("sd", string.format ("01%03d", i)) 
     858        luup.io.intercept() 
     859        local status = sendCommand ("sd", string.format ("01%03d", i)) 
     860        if (not status) then 
     861            log ("(getPartitionNames) ERROR: Failed to get partition names.") 
     862            return 
     863        end 
     864        local msg = luup.io.read() 
     865        status = processMessage (msg) 
     866        if (not status) then 
     867            log ("(getPartitionNames) ERROR: Failed to get partition names.") 
     868            return 
     869        end 
    797870    end 
    798871end 
     
    801874local function getZonesAndPartitions() 
    802875    -- Get zone information. 
    803     local status, message = sendCommand ("zd") 
    804     if (status) then 
    805         -- Get partition information. 
    806         status, message = sendCommand ("zp") 
    807     end 
    808  
    809     return status, message 
     876    luup.io.intercept() 
     877    local status = sendCommand ("zd") 
     878    if (not status) then 
     879        log ("(getZonesAndPartitions) ERROR: Failed to get zones and partitions.") 
     880        return false, "Failed to get zones and partitions." 
     881    end 
     882    local msg = luup.io.read() 
     883    status = processMessage (msg) 
     884    if (not status) then 
     885        log ("(getZonesAndPartitions) ERROR: Failed to get zones and partitions.") 
     886        return false, "Failed to get zones and partitions." 
     887    end 
     888 
     889    -- Get partition information. 
     890    luup.io.intercept() 
     891    status = sendCommand ("zp") 
     892    if (not status) then 
     893        log ("(getZonesAndPartitions) ERROR: Failed to get zones and partitions.") 
     894        return false, "Failed to get zones and partitions." 
     895    end 
     896    msg = luup.io.read() 
     897    status = processMessage (msg) 
     898    if (not status) then 
     899        log ("(getZonesAndPartitions) ERROR: Failed to get zones and partitions.") 
     900        return false, "Failed to get zones and partitions." 
     901    end 
     902 
     903    return true 
    810904end 
    811905 
     
    813907local function getVersion() 
    814908    luup.io.intercept() 
    815     sendCommand ("vn") 
    816     local msg = luup.io.read() 
    817     local msgType, data = checkMessage (message) 
    818     if (not msgType || data == "") then 
    819         debug ("(getVersion) ERROR: Missing version data.") 
    820         return "N/A" 
    821     end 
    822      
    823     local uver, mver, lver = data:match ("^(%w%w)(%w%w)(%w%w)") 
    824      
    825     return string.format ("%d.%d.%d", tonumber (uver, 16), tonumber (mver, 16), tonumber (lver, 16)) 
    826 end 
    827  
    828 -------------------------------------------------------------------------------------------------- 
     909    local status = sendCommand ("vn") 
     910    if (status) then 
     911        local msg = luup.io.read() 
     912        return processMessage (msg) 
     913    end 
     914end 
     915 
     916------------------------------------------------------------------------------- 
    829917 
    830918function start (lul_device) 
     
    847935        end 
    848936 
    849         getPartitionNames() 
     937        getPartitionsName() 
     938 
    850939        -- Get each Partition's status. 
    851         status, message = sendCommand ("as") 
     940        status, message = getPartitionsStatus() 
    852941        if (not status) then 
    853942            return status, message, "Elk Alarm Panel" 
    854943        end 
     944 
    855945        -- Get the Chime Mode for each partition. 
    856946        sendCommand ("kf", "010") 
     947 
    857948        -- Create a device for each active partition. 
    858949        appendPartitions (rootPtr) 
    859950 
    860951        getZoneNames() 
     952 
    861953        -- Get each Zone's status. 
    862         status, message = sendCommand ("zs") 
     954        status, message = getZonesStatus() 
    863955        if (not status) then 
    864956            return status, message, "Elk Alarm Panel" 
    865957        end 
     958 
    866959        -- Create a device for each zone. 
    867960        appendZones (rootPtr) 
Note: See TracChangeset for help on using the changeset viewer.