Пример #1
0
	def startup_dyio(self, use_lock=True) :
		if use_lock :
			self.dyio_lock.acquire()
		try :
			self.dyio = DyIO(SerialConnection(self.serial))
			self.dyio.connect()
			self.channels = [None] * 24
			self.setup = False
		finally :
			if use_lock :
				self.dyio_lock.release()
Пример #2
0
class IOD(SocketServer.TCPServer) :
	def __init__(self, serial, port) :
		self.serial = serial
		self.port = port
		self.dyio_lock = threading.Lock()
		self.startup_dyio()
		SocketServer.TCPServer.__init__(self, ('0.0.0.0', self.port), IODHandler)

	def startup_dyio(self, use_lock=True) :
		if use_lock :
			self.dyio_lock.acquire()
		try :
			self.dyio = DyIO(SerialConnection(self.serial))
			self.dyio.connect()
			self.channels = [None] * 24
			self.setup = False
		finally :
			if use_lock :
				self.dyio_lock.release()

	def shutdown_dyio(self, use_lock=True) :
		if use_lock :
			self.dyio_lock.acquire()
		try :
			# TODO determine what subset of these calls is really required
			self.dyio.removeAllDyIOEventListeners()
			self.dyio.stopHeartBeat()
			self.dyio.disconnect()
		finally :
			if use_lock :
				self.dyio_lock.release()

	def op_reset(self) :
		self.dyio_lock.acquire()
		try :
			try :
				self.shutdown_dyio(use_lock=False)
			except :
				pass
			self.startup_dyio(use_lock=False)
			return {iod_proto.SLOT_STATUS : iod_proto.STATUS_OK}
		finally :
			self.dyio_lock.release()

	def op_setup(self, arg) :
		self.dyio_lock.acquire()
		try :
			setup = set()
			for channel, channeltype in arg :
				if channel in setup :
					return {iod_proto.SLOT_STATUS : iod_proto.STATUS_FAIL}
				
				if self.channels[channel] is not None :
					continue

				if channeltype == iod_proto.CHANNELTYPE_DIGITAL :
					self.channels[channel] = DigitalInputChannel(self.dyio.getChannel(channel))
				elif channeltype == iod_proto.CHANNELTYPE_ANALOG :
					self.channels[channel] = AnalogInputChannel(self.dyio.getChannel(channel))
				elif channeltype == iod_proto.CHANNELTYPE_DIGITALOUT :
					self.channels[channel] = DigitalOutputChannel(self.dyio.getChannel(channel))
			
			self.setup = True

			return {iod_proto.SLOT_STATUS : iod_proto.STATUS_OK}
		finally :
			self.dyio_lock.release()

	def op_set(self, arg) :
		self.dyio_lock.acquire()
		try :
			if not self.setup :
				return {iod_proto.SLOT_STATUS : iod_proto.STATUS_FAIL}
			for channel, value in arg :
				if not isinstance(self.channels[channel], DigitalOutputChannel) :
					return {iod_proto.SLOT_STATUS : iod_proto.STATUS_FAIL}

			for channel, value in arg :
				value = bool(value)
				self.channels[channel].setHigh(value)

			return {iod_proto.SLOT_STATUS : iod_proto.STATUS_OK}
		finally :
			self.dyio_lock.release()

	def op_sample(self, arg) :
		self.dyio_lock.acquire()
		try :
			if not self.setup :
				return {iod_proto.SLOT_STATUS : iod_proto.STATUS_FAIL}

			for channelid in arg :
				if isinstance(self.channels[channelid], DigitalInputChannel) :
					continue
				if isinstance(self.channels[channelid], AnalogInputChannel) :
					continue

				return {iod_proto.SLOT_STATUS : iod_proto.STATUS_FAIL}

			samples = []

			for channelid in arg :
				if isinstance(self.channels[channelid], DigitalInputChannel) :
					v = not self.channels[channelid].isHigh()
				if isinstance(self.channels[channelid], AnalogInputChannel) :
					v = self.channels[channelid].getVoltage()

				samples.append((channelid, v))

			return {iod_proto.SLOT_STATUS : iod_proto.STATUS_OK, iod_proto.SLOT_DATA : samples}
		finally :
			self.dyio_lock.release()