Changeset 2


Ignore:
Timestamp:
2012-10-22 10:57:40 (12 years ago)
Author:
mcvflorin
Message:

Implemented the packet handling mechanism.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/I_EnOceanESP3.xml

    r1 r2  
    1010 
    1111            -- Callbacks 
    12             clearTask = enoceanPlugin.clearTask 
    13             sendMessage = enoceanPlugin.sendMessage 
     12            handleRadioTelegram = enoceanPlugin.handleRadioTelegram 
     13            handleEventMessage = enoceanPlugin.handleEventMessage 
    1414 
    1515            return enoceanPlugin.init(lul_device) 
  • TabularUnified trunk/L_EnOceanESP3.lua

    r1 r2  
    3636 
    3737local SYNC_BYTE = string.char(0x55) 
     38local DELAY_PROCESS = 1 
    3839 
    3940local CRC8_TABLE = { 
     
    9697local getinfo  = debug.getinfo -- Get the 'getinfo' function from the 'debug' module before overwriting the 'debug' keyword. 
    9798 
    98 local function debug (text) 
    99     if (DEBUG_MODE == true) then 
    100         log(text) 
    101     end 
     99local function debug() 
     100    return 
    102101end 
    103102 
     
    259258 
    260259------------------------------------------------------------------------------- 
    261 -- Message handler functions 
    262 ------------------------------------------------------------------------------- 
     260-- Radio telegram handlers 
     261------------------------------------------------------------------------------- 
     262 
     263local RADIO_TELEGRAM_HANDLERS = { 
     264    -- RPS 
     265    [0xF6] = function (data) 
     266        log("(RADIO_TELEGRAM_HANDLERS::RPS) RPS telegram handler") 
     267    end, 
     268     
     269    -- 1BS 
     270    [0xD5] = function (data) 
     271        log("(RADIO_TELEGRAM_HANDLERS::1BS) 1BS telegram handler") 
     272    end, 
     273     
     274    -- 4BS 
     275    [0xA5] = function (data) 
     276        log("(RADIO_TELEGRAM_HANDLERS::4BS) 4BS telegram handler") 
     277    end 
     278} 
     279 
     280------------------------------------------------------------------------------- 
     281-- Packet handler functions 
     282------------------------------------------------------------------------------- 
     283 
     284local PACKET_HANDLERS = { 
     285    [0x1] = "handleRadioTelegram", 
     286    [0x4] = "handleEventMessage" 
     287} 
     288 
     289 
     290function handleRadioTelegram (data) 
     291    log("(EnOceanPlugin::handleRadioTelegram) Handle radio telegram") 
     292    RADIO_TELEGRAM_HANDLERS[string.byte(data:sub(1, 1))](data:sub(2)) 
     293end 
     294 
     295 
     296function handleEventMessage (data) 
     297    log("(EnOceanPlugin::handleEventMessage) Handle event message") 
     298end 
    263299 
    264300 
     
    267303------------------------------------------------------------------------------- 
    268304 
    269 -- 0x55 0x0 0xa 0x7 0x1 0xeb 0xa5 0xce 0x79 0x0 0xf 0x0 0x81 0x1 0x75 0x0 0x1 0xff 0xff 0xff 0xff 0x44 0x0 0x6e 
     305-- 4BS 
     306-- 0x55 | 0x0 0xa 0x7 0x1 | 0xeb | 0xa5 - 0xce 0x79 0x0 0xf - 0x0 0x81 0x1 0x75 - 0x0 | 0x1 0xff 0xff 0xff 0xff 0x44 0x0 | 0x6e 
     307-- 0x55 | 0x0 0xa 0x7 0x1 | 0xeb | 0xa5 - 0xc6 0x79 0x0 0xf - 0x0 0x81 0x1 0x75 - 0x0 | 0x1 0xff 0xff 0xff 0xff 0x43 0x0 | 0x15 
     308-- 0x55 | 0x0 0xa 0x7 0x1 | 0xeb | 0xa5 - 0xc6 0x79 0x0 0xf - 0x0 0x81 0x1 0x75 - 0x0 | 0x1 0xff 0xff 0xff 0xff 0x43 0x0 | 0x15 
     309 
     310-- 1BS 
     311-- 0x55 | 0x0 0x7 0x7 0x1 | 0x7a | 0xd5 - 0x8 - 0x0 0x1 0x8a 0xbb - 0x0 | 0x1 0xff 0xff 0xff 0xff 0x3a 0x0 | 0xbc 
     312-- 0x55 | 0x0 0x7 0x7 0x1 | 0x7a | 0xd5 - 0x8 - 0x0 0x0 0x85 0xe - 0x0 | 0x1 0xff 0xff 0xff 0xff 0x37 0x0 | 0x3a 
     313 
    270314 
    271315local state        = STATE.GET_SYNC_STATE -- The current state 
     
    274318local rxDataLen    = 0  -- The length of the data field 
    275319local rxOptDataLen = 0  -- The length of the optional data field 
     320local rxPacketType = 0  -- The packet type 
    276321 
    277322-- State machine to load incoming packet bytes 
     
    375420        state = STATE.GET_DATA_STATE 
    376421        rxCount = 0 
     422        rxPacketType = string.byte(rxBuf[4]) 
    377423    end, 
    378424 
     
    396442        if (computeCrc(rxBuf, rxCount) == string.byte(byte)) then 
    397443            -- Correct packet received 
    398             log("(EnOceanPlugin::CHECK_CRC8D_STATE) Correct packet received, go to GET_SYNC_STATE") 
    399             -- TODO: DO SOMETHING WITH THE DATA (use luup.call_delay) 
     444            log("(EnOceanPlugin::CHECK_CRC8D_STATE) Correct packet received, schedule the packet handler function and go to GET_SYNC_STATE") 
     445            if (PACKET_HANDLERS[rxPacketType] ~= nil) then 
     446                luup.call_delay(PACKET_HANDLERS[rxPacketType], DELAY_PROCESS, table.concat(rxBuf, "", 1, rxCount)) 
     447            else 
     448                debug("(EnOceanPlugin::CHECK_CRC8D_STATE) Unhandled packet type ".. rxPacketType) 
     449            end 
    400450            return 
    401451        end 
     
    450500        DEBUG_MODE = (debugMode == "1") and true or false 
    451501    end 
     502     
     503    if (DEBUG_MODE) then 
     504        debug = log 
     505    end 
    452506 
    453507    -- Check if the panel is connected through the serial port, or via Ethernet. 
Note: See TracChangeset for help on using the changeset viewer.