X10 mochad com error detection

Moderator: leecollings

Post Reply
franzelare
Posts: 141
Joined: Thursday 19 February 2015 21:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

X10 mochad com error detection

Post by franzelare »

I see that my old CM15Pro about once a month dies on me.. and domoticz cannot access the controller anymore and since i still have all kind of X10 devices in use i loose a lot of functionality.

I wrote a dz event script that checks the domoticz error log and if 5 communication errors occure in a row, resets my pi to get communication again.
I have made a uservariable MochadError to count, since I did not get the dz even data working properly

Code: Select all

myHttpResponse = "getDomoticzErrorLog"
PRINT_MODE = false
DEBUG_MODE = false


return {
    on      =   {   httpResponses   =   { myHttpResponse },
                    timer           =   { "every 2 minutes"}
                },

    logging =   {   level           =   domoticz.LOG_DEBUG,
                    marker          =   "getStr from log"    
                },

    data    =   {   lastlogtime     =   { initial = 0 },
                    counter = { initial = 0 }
                }, 

    execute = function(dz, trigger)
        MochadCounter = dz.variables('MochadError').value
        if (PRINT_MODE==true) then
            dz.log("MochadCounter " .. MochadCounter)
        end
        
        local searchStr = "Error: Mochad: Can not connect to: 127.0.0.1:1099"          -- Change to reflect the messages you want to catch

        local function askDomoticzForLogLines()
            local lastLogTime = dz.data.lastlogtime            -- get time last logrequest
            local logLevel    = 4                              -- loglevel (1=normal,2=Status,4=error,268435455=all
            local jsonString  = "/json.htm?type=command" .. 
                                "&param=getlog" ..
                                "&lastlogtime=" .. tostring(lastLogTime) ..
                                "&loglevel=" .. tostring(logLevel)
            
            local logURL      =  dz.settings["Domoticz url"] .. jsonString
            
            dz.openURL  ({ url      = logURL,
                           method   = "GET", 
                           callback = myHttpResponse 
                        })

            dz.data.lastlogtime = dz.time.dDate        -- store current Time as seconds from epoch  
        end

        local function findString()
           if trigger.json.result ~= nil then               -- Only when there are errormessages in the log
               local resultTable = trigger.json.result
               for i = #resultTable,1,-1 do                 -- traverse backwards to get latest violation first
                    local s = resultTable[i].message
                    local found = s:find(searchStr,1,true) -- searcg plain string starting at pos 1 (no pattern) 
                    local dzVents = s:find("dzVents")
                    if found and not(dzVents) then
                       return true
                    end
                end
            end
        end

        if trigger.isDevice or trigger.isTimer then
            askDomoticzForLogLines()                     -- get the relevant loglines
        elseif trigger.ok then
            if (DEBUGT_MODE==true) then
                dz.log("Show ALL: \n" .. trigger.data,dz.LOG_DEBUG)
            end
            if findString() then
                if (PRINT_MODE==true) then
                    dz.log("string " .. searchStr .. " found! ",dz.LOG_DEBUG)
                end
                dz.notify('No signal from Mochad, increasing error counter',dz.PRIORITY_LOW)

                MochadCounter = MochadCounter + 1
                dz.log("MochadCounter " .. MochadCounter)
                dz.variables('MochadError').set(MochadCounter)
            if (MochadCounter == 5) then
                 MochadCounter = 0
                 dz.variables('MochadError').set(MochadCounter)
                 sleep(2)
                 dz.log('5 errors from mochad, rebooting system')
                 dz.devices('Reboot').switchOn()
                 os.execute('sudo reboot') 
            end
            else    
                if (PRINT_MODE==true) then
                    dz.log("No (new) occurrences of string " .. searchStr .. " in the domoticz log ",dz.LOG_DEBUG)
                end
            MochadCounter = 0
                dz.variables('MochadError').set(MochadCounter)
            end    
        else
            dz.log("Access problem to log. Data found: " .. trigger.data,dz.LOG_ERROR)
        end

end


}
hope this might help others out!
i still need to figure out how to just reintialize the USB communication instead of a full reboot
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest