<?xml version="1.0" encoding="UTF-8" ?>
<EventGhost Name="Configuration Tree" Expanded="True" Version="1462" Guid="{738B97C4-E734-4B9C-83A6-A377C4191167}" Time="1271452882.24">
    <Autostart Name="Autostart" Expanded="True">
        <Plugin File="DirectoryWatcher" Identifier="DirectoryWatcher">
            gAJYCwAAAEQ6XFRlbXBcTE9HcQCIhnEBLg==
        </Plugin>
        <Plugin File="FileOperations" Identifier="FileOperations">
            gAIpLg==
        </Plugin>
        <Plugin File="Timer" Identifier="Timer">
            gAIpLg==
        </Plugin>
    </Autostart>
    <Folder Name="Example config" Expanded="True">
        <Macro Name="After init of EG find the latest log file and starts monitoring" Expanded="True">
            <Event Name="Main.OnInit" />
            <Action>
                EventGhost.PythonScript(u'pattern = "BTV-XXXXXXXXXX.log"\npth = r"c:\\Temp\\LOG"\n\n#Declaration of variables: \neg.globals.monitoredFile = None\neg.globals.BTV_log_last_line = "DUMMY"\neg.globals.lastEvtTime = 0\n\nfrom os import path, listdir\n#find all the log files that match the pattern:\nfiles = [(path.getmtime(path.join(pth,f)), path.join(pth,f)) for f in listdir(pth) if (f[:4] == pattern[:4] and f[-4:] == pattern[-4:])]\n#sort log sort files by time changes:\nfiles.sort()\n#last changed file:\nlastLogFile = files[-1][1]\n#if file exist, trigger event\nif lastLogFile:\n    eg.TriggerEvent("lastLogFile",prefix = "BTV",payload = (lastLogFile,))')
            </Action>
        </Macro>
        <Macro Name="Start last log file monitoring" Expanded="True">
            <Event Name="DirectoryWatcher.Created" />
            <Event Name="BTV.lastLogFile" />
            <Action>
                EventGhost.PythonScript(u'pattern = "BTV-XXXXXXXXXX.log"\n\nfrom os import path\nfp = eg.event.payload[0]\nf = path.split(fp)[1]\nif f[:4] == pattern[:4] and f[-4:] == pattern[-4:]:\n    eg.globals.monitoredFile = fp #Now we observe only the file fp\n    eg.globals.BTV_log_last_line = "DUMMY" #Reset variable\n\n')
            </Action>
        </Macro>
        <Macro Name="Check log file" Expanded="True">
            <Event Name="DirectoryWatcher.Updated" />
            <Action>
                EventGhost.PythonScript(u'#Filter:\n# 1) Check whether this is just the file that we are watching\n# 2) If it is (rapid sequence), more than one event, only the first process and throw away the other\nfp = eg.event.payload[0]\nif eg.globals.monitoredFile == fp:\n    evtTime = eg.event.time\n    if evtTime - eg.globals.lastEvtTime &gt; 0.2:\n        eg.result = fp                   # File for further processing\n        eg.globals.lastEvtTime = evtTime # Set variabe\n    else:\n        eg.programCounter = None         # Throw away (rapid sequence)\nelse:\n    eg.programCounter = None             # Throw away (other file)\n\n\n')
            </Action>
            <Action>
                FileOperations.Read(0, u'{eg.result}', 0, 0, 'cp1252', 10, True, 10)
            </Action>
            <Action>
                EventGhost.PythonScript(u'pattern = "Adding error: \'12\'"\ndata = eg.result\n\n#exist eg.globals.BTV_log_last_line in data ?\nfor bgn in range(len(data)):\n    if data[bgn] == eg.globals.BTV_log_last_line:\n        break\nbgn += 1\nif bgn &gt;= len(data):\n    bgn = 0\n\n#exists pattern in line ?\nfor i in range(bgn, len(data)):\n    if pattern in data[i]:\n        eg.TriggerEvent("error_occured",prefix ="BTV",payload = data[i])\n        break #Simply trigger only one event, even if multiple occurrences of error\n\n#Update variable eg.globals.BTV_log_last_line\nfor i in range(len(data)-1,-1,-1):\n    if data[i].strip()!="":\n        eg.globals.BTV_log_last_line = data[i]\n        break')
            </Action>
        </Macro>
        <Macro Name="Transmit IR off" Expanded="True">
            <Event Name="BTV.error_occured" />
            <Action Name="Transmit IR off">
                EventGhost.Comment()
            </Action>
            <Action>
                Timer.TimerAction(u'BTVon', 0, 1, 10.0, u'BTVon', False, True, 1, u'00:00:00')
            </Action>
        </Macro>
        <Macro Name="Transmit IR on" Expanded="True">
            <Event Name="Timer.BTVon" />
            <Action Name="Transmit IR on">
                EventGhost.Comment()
            </Action>
        </Macro>
    </Folder>
</EventGhost>
