コード例 #1
0
ファイル: dsc_gateway.py プロジェクト: metamatt/stargate
	def __init__(self, house, gateway_instance_name, config):
		super(DscGateway, self).__init__(house, gateway_instance_name)
		
		# create devices
		self.panel_device = DscPanel(self)
		# parse layout from config file
		# areas
		areas_by_zone = {}
		for area_name in config.area_mapping: # map name: list of zone ids
			sg_area = house.get_area_by_name(area_name)
			for zone_num in config.area_mapping[area_name]:
				areas_by_zone[zone_num] = sg_area
		# zones
		self.zones_by_id = {}
		for zone_num in config.zones:
			if zone_num not in areas_by_zone:
				logger.warn('DSC zone %d not mapped to any area; using "unknown" as default' % zone_num)
				areas_by_zone[zone_num] = house.get_area_by_name('(Unknown)')
			self.zones_by_id[zone_num] = create_device_for_zone(self, areas_by_zone[zone_num], zone_num, config.zones[zone_num])
		# partitions
		self.partitions_by_id = {}
		for partition_num in config.partition_names:
			self.partitions_by_id[partition_num] = DscPartition(self, partition_num, config.partition_names[partition_num])

		# set up network connections
		self.panel_server = DscPanelServer(self, self.house.watchdog, config.gateway.hostname, 4025, config.gateway.password)
		if config.gateway.has_key('reflector_port'):
			self.reflector = Reflector(self, config.gateway.reflector_port, config.gateway.password)
		else:
			self.reflector = None

		# and start everything in motion
		self.panel_server.connect()
コード例 #2
0
ファイル: dsc_gateway.py プロジェクト: metamatt/stargate
class DscGateway(sg_house.StargateGateway):
	def __init__(self, house, gateway_instance_name, config):
		super(DscGateway, self).__init__(house, gateway_instance_name)
		
		# create devices
		self.panel_device = DscPanel(self)
		# parse layout from config file
		# areas
		areas_by_zone = {}
		for area_name in config.area_mapping: # map name: list of zone ids
			sg_area = house.get_area_by_name(area_name)
			for zone_num in config.area_mapping[area_name]:
				areas_by_zone[zone_num] = sg_area
		# zones
		self.zones_by_id = {}
		for zone_num in config.zones:
			if zone_num not in areas_by_zone:
				logger.warn('DSC zone %d not mapped to any area; using "unknown" as default' % zone_num)
				areas_by_zone[zone_num] = house.get_area_by_name('(Unknown)')
			self.zones_by_id[zone_num] = create_device_for_zone(self, areas_by_zone[zone_num], zone_num, config.zones[zone_num])
		# partitions
		self.partitions_by_id = {}
		for partition_num in config.partition_names:
			self.partitions_by_id[partition_num] = DscPartition(self, partition_num, config.partition_names[partition_num])

		# set up network connections
		self.panel_server = DscPanelServer(self, self.house.watchdog, config.gateway.hostname, 4025, config.gateway.password)
		if config.gateway.has_key('reflector_port'):
			self.reflector = Reflector(self, config.gateway.reflector_port, config.gateway.password)
		else:
			self.reflector = None

		# and start everything in motion
		self.panel_server.connect()

	# public interface to StargateHouse
	def get_device_by_gateway_id(self, gateway_devid):
		# our devid format is "scope,num" where scope is one of: [ zone, partition, command ].
		(scope, num) = self.crack_dsc_devid(gateway_devid)
		if scope == 'zone':
			return self.zones_by_id[num]
		elif scope == 'partition':
			return self.partitions_by_id[num]
		else:
			raise Error('whoops')

	def crack_dsc_devid(self, gateway_devid):
		cracked = gateway_devid.split(':')
		assert len(cracked) == 2
		assert int(cracked[1]) > 0
		return (cracked[0], int(cracked[1]))
		
	# child-device interface for device status
	def get_zone_status(self, zone_num):
		return self.panel_server.cache.get_zone_status(zone_num)

	def get_partition_status(self, partition_num):
		return self.panel_server.cache.get_partition_status(partition_num)

	def send_user_command(self, partition_num, user_cmd_num):
		# Envisalink UI calls this "PGM", but it's really the user-command which you often map PGM outputs to listen to, but it's not actually that direct.
		# partition_num is 1..8
		# user_cmd_num is 1..4
		command = 20 # 020 in DSC-speak, but Python interprets that as octal, which is not what we want
		data = [ str(partition_num), str(user_cmd_num) ]
		assert len(data) == 2
		self.panel_server.send_dsc_command(command, data)

	# panel action callback
	def on_user_action(self, dev_type, dev_id, state, refresh):
		if dev_type == 'zone':
			logger.debug('panel action zone %d' % dev_id)
			if self.zones_by_id.has_key(dev_id):
				device = self.zones_by_id[dev_id]
				device.on_user_action(state, refresh)
		elif dev_type == 'partition':
			if self.partitions_by_id.has_key(dev_id):
				device = self.partitions_by_id[dev_id]
				device.on_user_action(state, refresh)
		else:
			logger.warn('ignoring action for %s:%s' % (dev_type, zone_id))