Monday, March 2, 2009

Sample EventGhost Python Script for Parsing xPL Messages

To make my EventGhost xPL plugin really useful, you really need to be able to pick apart an xPL message to extract data from it. Luckily, EG allows you to associate a script with a macro. This makes it pretty simple to create complex interactions based on xPL messages. Just create an event to trigger on 'xPL.*' and add an action to it that's a Python script. Here's a sample script that sends an acknowledgment message when Blabber receives the word 'test' from a user. This script can easily be extended to handle all your xPL interactions.

import re

# split up the event components
xPLSplit = eg.event.suffix.split(':')

# make the pieces easy to remember
xPLType = xPLSplit[0]
xPLSchema = xPLSplit[1]
xPLSource = xPLSplit[2]
xPLTarget = xPLSplit[3]
xPLBody = xPLSplit[4]

# we only want xpl-trig messages
if xPLType == "xpl-trig":
# only want messages from blabber
if re.search('doghouse-blabber',xPLSource):
bodySplit = xPLBody.split(',')
# extract the components of the message body
for i in range(3):
if re.search('device',bodySplit[i]):
senderSplit = bodySplit[i].split('=')
elif re.search('type',bodySplit[i]):
typeSplit = bodySplit[i].split('=')
elif re.search('current',bodySplit[i]):
currentSplit = bodySplit[i].split('=')
# we only want the message type and messages that
# contain the word 'test'
if typeSplit[1] == "message" and currentSplit[1]=="test":
# if we get 'test' we'll reply back to the sender
# that we got his message

returnMsg = "device="+senderSplit[1]+"\n"+"current=EG got your test"
eg.plugins.xPL.sendxPL(u'xpl-cmnd', u'control.basic', xPLSource, \
returnMsg)


No comments:

Post a Comment