#!/usr/bin/python from twisted.internet import reactor, protocol from twisted.protocols.basic import LineReceiver # camState = {'temp': "301.5", 'shspd': "21", 'ffmt': "TIFF", 'binres': "4", 'imsz': "2048", 'xstart': "0", 'ystart': "0", 'floc': "/experiments/dingo/camdata", 'fname': "image003.tiff", 'fcntr': "3", 'status': "Idle", 'shtime': "6"} class CamServ(LineReceiver): def __init__(self): self.camState = {'camera': "clock=1mhz,bin=1x,size=2048,gain=1xhs,flip=normal,xpos=0,ypos=0,exposure=0.777,temperature=-50,threshold=800,shutteropentime=100,shutterclosetime=200", 'file': "path=/experiments/dingo/data,basename=DingoTst,startnumber=1,imageformat=tiff,experimentdetail=this is an experiment", 'meta': "sx=100,sy=200,experiment=testing3", 'status': "camera status", 'state': "Idle", 'info': "camera info"} print "Camera get/set commands: ", self.camState.keys() def getCmd(self, par): if par in self.camState: self.sendLine(self.camState[par]) else: self.sendLine("ERROR: Unknown parameter, " + par) def setCmd(self, s): par,val = s.split(',',1) if par in self.camState: self.camState[par] = val self.sendLine("OK") else: self.sendLine("ERROR: Unknown parameter, " + par) def takeCmd(self, cmd): if cmd == 'shot': self.sendLine("OK") elif cmd == 'multishot': self.sendLine("OK") else: self.sendLine("ERROR:1") def clearCmd(self, par): if par == 'meta': self.sendLine("OK") else: self.sendLine("ERROR:1") def lineReceived(self, line): print "RECEIVED: " + line cmd = line.split() if len(cmd) == 0: self.sendLine(line) return if cmd[0] == "get": self.getCmd(cmd[1]) elif cmd[0] =="set": if len(cmd) == 1: self.sendLine("ERROR:2") return self.setCmd(cmd[1]) elif cmd[0] == "take": if len(cmd) == 1: self.sendLine("ERROR:2") return self.takeCmd(cmd[1]) elif cmd[0] == "clear": if len(cmd) == 1: self.sendLine("ERROR:2") return self.clearCmd(cmd[1]) else: self.sendLine("ERROR: Unknown command, " + line) def main(): factory = protocol.ServerFactory() factory.protocol = CamServ reactor.listenTCP(63300,factory) reactor.run() if __name__ == '__main__': main()