Example #1
0
def enumerate_batteries():
	"""
	This function enumerates the batteries on the system and creates
	an entry in the DataBase table for each.

	This is deprecated by the update loop itself.
	"""

	batts = acpi.batteries()

	for batt in batts:
		b = None
		try:
			b = Batt.objects.get(name=batt[0])
		except ObjectDoesNotExist:
			log.debug("creating new entry for '%s'"%batt[0])
		if b is None:
			b=Batt(name=batt[0],
					status_text=batt[1],
					percent_full=batt[2],
					remaining_text=batt[4],
					)
		else:
			b.status_text=batt[1]
			b.percent_full=batt[2]
			b.remaining_text=batt[4]
		b.save()
Example #2
0
def enumerate_batteries():
    """
	This function enumerates the batteries on the system and creates
	an entry in the DataBase table for each.

	This is deprecated by the update loop itself.
	"""

    batts = acpi.batteries()

    for batt in batts:
        b = None
        try:
            b = Batt.objects.get(name=batt[0])
        except ObjectDoesNotExist:
            log.debug("creating new entry for '%s'" % batt[0])
        if b is None:
            b = Batt(
                name=batt[0],
                status_text=batt[1],
                percent_full=batt[2],
                remaining_text=batt[4],
            )
        else:
            b.status_text = batt[1]
            b.percent_full = batt[2]
            b.remaining_text = batt[4]
        b.save()
Example #3
0
	def start(self, args):
		"""
		This function starts a loop, intended to be run in parallel to the main server process,
		to asynchronously maintain the database with up-to-date 

		The loop will iterate the entries in the maintained databases (e.g. batteries) and action them in the following way:
		 - if the entry is a known meta-command, this will be executed. These can be used to stop the looping, for example.
		 - if the entry matches a currently installed battery, this entry will be updated with the status
		 - if the entry is not a command and does not match installed batteries, it will be flagged as absent (NOT YET IMPLEMENTED)
		If any batteries are found to be present in the system and not in the database, they will be added.
		"""
		stop = False
		#enumerate_batteries()
		while not stop:
			log.debug('hwupdate running (period=%.2fsec)...'%(UPDATE_PERIOD))
			sleep(UPDATE_PERIOD)
			log.debug("Reading ACPI battery information...")
			batts = acpi.batteries()
			log.debug("...done reading ACPI (%d found)." % (len(batts)))
			battnames = [b[0] for b in batts]
			known_batts = []
			try:
				log.debug("Scanning database...")
				with transaction.commit_on_success():
					dbbatteries = Batt.objects.all()
					for dbbatt in dbbatteries:
						if dbbatt.name == 'STOP_UPDATING':
							log.info("hwupdate async task signaled to stop. Stopping.")
							stop = True
							dbbatt.delete()
						elif dbbatt.name in battnames:
							log.debug("Battery '%s' matches in ACPI and DB." % (dbbatt.name))
							hwbatt=batts[battnames.index(dbbatt.name)]
							dbbatt.status=hwbatt.status
							dbbatt.level=hwbatt.level
							dbbatt.uptime=hwbatt.uptime
							dbbatt.str_uptime=hwbatt.str_uptime
							dbbatt.save()
							log.debug(" - updating with %s/%d/%s" % (hwbatt.status, hwbatt.level, hwbatt.str_uptime))
							known_batts += [dbbatt.name]

						else:
							log.info("Battery '%s' in DB is absent in ACPI." % dbbatt.name)

				log.debug("Checking all HW batteries")
				for hwbatt in batts:
					if hwbatt[0] not in known_batts:
						log.info("Battery '%s' in ACPI is absent in DB. Adding." % hwbatt.name)
						dbbatt=Batt(name=hwbatt.name,
							level=hwbatt.level,
							status=hwbatt.status,
							uptime=hwbatt.uptime,
							str_uptime=hwbatt.str_uptime,
							)
						dbbatt.save()

				log.debug("...done scanning DataBase.")
			except Exception as ex:
				log.exception('HWUpdate got exception: %s'%str(ex))
Example #4
0
def batteries():
	"""
	Returns batteries as retrieved from ACPI or system class path.

	Will return a named tuple if requested, and this is false only for legacy reasons.

	TODO: add non acpi_binary version
	"""
	if use_acpi_binary:
		batts = acpi.batteries()
	else:
		raise NotImplementedError
		batt_status_path = os.path.join(ps_path, 'BAT%d'%(batt_num), 'status')
		with open(batt_status_path, 'r') as f:
			batt_status = f.read()
			status_string = batt_status

	return batts
Example #5
0
def batteries():
    """
	Returns batteries as retrieved from ACPI or system class path.

	Will return a named tuple if requested, and this is false only for legacy reasons.

	TODO: add non acpi_binary version
	"""
    if use_acpi_binary:
        batts = acpi.batteries()
    else:
        raise NotImplementedError
        batt_status_path = os.path.join(ps_path, 'BAT%d' % (batt_num),
                                        'status')
        with open(batt_status_path, 'r') as f:
            batt_status = f.read()
            status_string = batt_status

    return batts
Example #6
0
    def start(self, args):
        """
		This function starts a loop, intended to be run in parallel to the main server process,
		to asynchronously maintain the database with up-to-date 

		The loop will iterate the entries in the maintained databases (e.g. batteries) and action them in the following way:
		 - if the entry is a known meta-command, this will be executed. These can be used to stop the looping, for example.
		 - if the entry matches a currently installed battery, this entry will be updated with the status
		 - if the entry is not a command and does not match installed batteries, it will be flagged as absent (NOT YET IMPLEMENTED)
		If any batteries are found to be present in the system and not in the database, they will be added.
		"""
        stop = False
        #enumerate_batteries()
        while not stop:
            log.debug('hwupdate running (period=%.2fsec)...' % (UPDATE_PERIOD))
            sleep(UPDATE_PERIOD)
            log.debug("Reading ACPI battery information...")
            batts = acpi.batteries()
            log.debug("...done reading ACPI (%d found)." % (len(batts)))
            battnames = [b[0] for b in batts]
            known_batts = []
            try:
                log.debug("Scanning database...")
                with transaction.commit_on_success():
                    dbbatteries = Batt.objects.all()
                    for dbbatt in dbbatteries:
                        if dbbatt.name == 'STOP_UPDATING':
                            log.info(
                                "hwupdate async task signaled to stop. Stopping."
                            )
                            stop = True
                            dbbatt.delete()
                        elif dbbatt.name in battnames:
                            log.debug("Battery '%s' matches in ACPI and DB." %
                                      (dbbatt.name))
                            hwbatt = batts[battnames.index(dbbatt.name)]
                            dbbatt.status = hwbatt.status
                            dbbatt.level = hwbatt.level
                            dbbatt.uptime = hwbatt.uptime
                            dbbatt.str_uptime = hwbatt.str_uptime
                            dbbatt.save()
                            log.debug(" - updating with %s/%d/%s" %
                                      (hwbatt.status, hwbatt.level,
                                       hwbatt.str_uptime))
                            known_batts += [dbbatt.name]

                        else:
                            log.info("Battery '%s' in DB is absent in ACPI." %
                                     dbbatt.name)

                log.debug("Checking all HW batteries")
                for hwbatt in batts:
                    if hwbatt[0] not in known_batts:
                        log.info(
                            "Battery '%s' in ACPI is absent in DB. Adding." %
                            hwbatt.name)
                        dbbatt = Batt(
                            name=hwbatt.name,
                            level=hwbatt.level,
                            status=hwbatt.status,
                            uptime=hwbatt.uptime,
                            str_uptime=hwbatt.str_uptime,
                        )
                        dbbatt.save()

                log.debug("...done scanning DataBase.")
            except Exception as ex:
                log.exception('HWUpdate got exception: %s' % str(ex))