示例#1
0
	def collection_timer_handler(self):
		Log.debug("collection_timer_handler enter")
		# setup the list of deferred
		deferreds = []
		# collect for each pack
		for pack in self.packs:
			# schedule the collection in a thread
			deferreds.append(threads.deferToThread(self.pack_collection_handler, pack))
		# wait for all of the collection to finish
		d = defer.DeferredList(deferreds)
		results = yield d

		Log.info("measurements collected") 

		# create list of measurements
		measurements = []

		# go through the results
		for result in results:
			# get the pack
			pack = result[1]['pack']
			for circuit in range(pack.get_num_circuits()):
				# create the measurement 
				circuit_id = str(pack.get_index()) + "-" + str(circuit)
				value = result[1][circuit]
				measurement = Measurement(circuit=circuit_id, amperage=value, voltage=pack.get_voltage())
				# store it in the record
				measurements.append(measurement)
		# create the record to be written to the database
		record = Record(uuid=str(uuid.uuid1()), duration=10.0, timestamp=datetime.utcnow().replace(microsecond=0).isoformat() + "Z")
       
		# write the record
		yield Database.write_record(record, measurements)
		# poke the uploader to tell it records are ready
		self.uploader.upload()
		# done 
		Log.debug("collection_timer_handler exit")