Page 1 of 1
[ADCH++] Pyton scripts
Posted: 21 Jan 2009, 20:43
by Sulan
Basic script:
Start and run hub.
Here is a basic script to just start and run ADCH++ from a python script. I would like to add that im not that skilled with programming so there might be some stuff that i have got mixed up.
First of all i would like to mention that i had issues when importing pyadchpp.py, i had to edit the line where it imports _pyadchpp.dll
From:
To:
Code: Select all
try:
import _pyadchpp
except:
import imp, os
_pyadchpp=imp.load_dynamic('_pyadchpp', '_pyadchpp.dll')
del imp, os
You need the .dll files from ADCH++ and pyadchpp.py in the same directory as the basic script.
Basic script:
Code: Select all
#!/usr/bin/python
#import python adchpp module
import pyadchpp
#import os module
import os
import time
def run():
#initialize the hubsoft with the complete path to your config directory containing adchpp.xml
pyadchpp.initialize(os.getcwd()+'\config\\')
pyadchpp.startup()
#create a loop so the program doesnt exit
while 1:
time.sleep(0.1)
if __name__== '__main__':
run()
Re: [ADCH++] Pyton scripts
Posted: 21 Jan 2009, 20:56
by Sulan
Signal examples:
Used basic script and added some signal handeling as examples.
Code: Select all
#!/usr/bin/python
#import python adchpp module
import pyadchpp
#import os module
import os
import time
def onReceive(client, cmd, n):
print 'Client object:',client
print 'AdcCommandObject:',cmd
print 'Command:',cmd.toString()
def onState(client, state):
print 'Nick:',client.getField('NI') #Nick not know untill after first INF
print 'State:', state
def run():
#initialize the hubsoft with the complete path to your config directory containing adchpp.xml
pyadchpp.initialize(os.getcwd()+'\config\\')
pyadchpp.startup()
#create a loop so the program doesnt exit
#get clientmanager instance
clientmanager = pyadchpp.getCM()
#register signals
x = clientmanager.signalReceive().connect(onReceive)
y = clientmanager.signalState().connect(onState)
while 1:
time.sleep(0.1)
if __name__== '__main__':
run()
Re: [ADCH++] Pyton scripts
Posted: 25 Jan 2009, 18:23
by Sulan
I made a small script that can load other python modules so they can interact with the hub.
The script will import any pythonscript in a 'pyscripts' directory and execute 'register' function.
AdchCore:
Code: Select all
import threading
import pyadchpp as hub
import os
import sys
import traceback
import time
class ADCH( threading.Thread ):
def __init__ ( self):
threading.Thread.__init__ ( self )
self.path = os.getcwd()
self.scriptDir = 'pyscripts'
hub.initialize(self.path+'\config\\')
self.keeprunning = True
self.CM = hub.getCM()
self.PM = hub.getPM()
self.SM = hub.getSM()
self.newSignals = []
self.signals = []
self.start()
def importModules(self):
try:
if not os.path.exists(self.scriptDir):
os.mkdir(self.scriptDir)
if not self.scriptDir in sys.path:
sys.path.insert(0, self.scriptDir)
for script in os.listdir(os.getcwd()+'\\'+self.scriptDir):
if script.endswith('.py'):
try:
p = __import__(script[:-3], None, None, [''])
if hasattr(p, 'register'):
impfunc = getattr(p, 'register')
impfunc(self)
print 'Loaded module:',p.__name__
except Exception, err:
traceback.print_exc(file=sys.stdout)
except Exception, err:
traceback.print_exc(file=sys.stdout)
def startHub(self):
try:
hub.startup()
except Exception, err:
traceback.print_exc(file=sys.stdout)
def shutdown(self):
self.newSignals = []
self.signals = []
hub.shutdown()
def run (self):
self.importModules()
while self.keeprunning:
while len(self.newSignals) >0:
signal, callback= self.newSignals.pop()
x=signal(callback)
self.signals.append(x)
time.sleep(0.1)
if __name__ == '__main__':
hc = ADCH()
Testscript to put in to pyscripts:
Code: Select all
import pyadchpp as hub
def testFunc(client, cmd, n):
CMD = hub.AdcCommand.toString(cmd)
print CMD
def register(core):
core.newSignals.append( (core.CM.signalReceive().connect, testFunc) )
Re: [ADCH++] Pyton scripts
Posted: 01 Feb 2009, 19:58
by poy
Sulan wrote:Basic script:
First of all i would like to mention that i had issues when importing pyadchpp.py, i had to edit the line where it imports _pyadchpp.dll
From:
To:
Code: Select all
try:
import _pyadchpp
except:
import imp, os
_pyadchpp=imp.load_dynamic('_pyadchpp', '_pyadchpp.dll')
del imp, os
instead of modifying the script generated by SWIG, you can create a different file called "_pyadchpp.py" in which you put your import code:
Code: Select all
import imp
_pyadchpp = imp.load_dynamic('_pyadchpp', '_pyadchpp.dll')
Re: [ADCH++] Pyton scripts
Posted: 27 Mar 2009, 18:10
by arnetheduck
fix in bzr (python modules should be named .pyd on win)