Skip to content

Instantly share code, notes, and snippets.

@youz
Created January 30, 2012 07:52
Show Gist options
  • Save youz/1703114 to your computer and use it in GitHub Desktop.
Save youz/1703114 to your computer and use it in GitHub Desktop.
#xyzzy でバッテリーの状態を確認
;;; -*- mode:lisp; package:battery -*-
(provide "battery")
(defpackage :battery
(:use :lisp :editor))
(in-package :battery)
(export '(get-status))
;;; ref. http://msdn.microsoft.com/en-us/library/windows/desktop/aa394074(v=vs.85).aspx
(defvar *win32-battery-properties*
'(Availability ; Availability and status of the device
BatteryStatus ; Status of the battery
Chemistry ; Battery's chemistry
Description ; Description of the object
DesignCapacity ; Design capacity of the battery in milliwatt-hours
DesignVoltage ; Design voltage of the battery in millivolts
DeviceID ; Identifies the battery
EstimatedChargeRemaining ; Estimate of the percentage of full charge remaining
EstimatedRunTime ; Estimate in minutes of the time to battery charge depletion
ExpectedLife ; Battery's expected lifetime in minutes, assuming that the battery is fully charged
FullChargeCapacity ; Full charge capacity of the battery in milliwatt-hours
MaxRechargeTime ; Maximum time, in minutes, to fully charge the battery
SmartBatteryVersion ; Data Specification version number supported by the battery
Status ; Current status of the object
TimeOnBattery ; Elapsed time in seconds since the computer system's UPS last switched to battery power,
; or the time since the system or UPS was last restarted, whichever is less
TimeToFullCharge ; Remaining time to charge the battery fully in minutes at the current charging rate and usage
))
(defmacro whenlet (var test &body body)
`(let ((,var ,test)) (when ,var ,@body)))
(defun get-win32-battery ()
(let* ((locator (ole-create-object "WbemScripting.SWbemLocator"))
(service (ole-method locator 'ConnectServer "."))
(result (ole-method service 'ExecQuery "SELECT * FROM Win32_Battery")))
(ole-for-each (r result)
(return-from get-win32-battery
(mapcan (lambda (s) (list s (ole-getprop r s)))
*win32-battery-properties*)))))
(defun availability-string (a)
(when (< 0 a 18)
(nth (1- a)
'("Other"
"Unknown"
"Running or Full Power"
"Warning"
"In Test"
"Not Applicable"
"Power Off"
"Off Line"
"Off Duty"
"Degraded"
"Not Installed"
"Install Error"
"Power Save - Unknown"
"Power Save - Low Power Mode"
"Power Save - Standby"
"Power Cycle"
"Power Save - Warning"))))
(defun battery-status-string (bs)
(when (< 0 bs 12)
(nth (1- bs)
'("Discharging"
"Online"
"Fully Charged"
"Low"
"Critical"
"Charging"
"Charging (High)"
"Charging (Low)"
"Charging (Critical)"
"Undefined"
"Partially Charged"))))
(defun chemistry-string (c)
(when (< 0 c 9)
(nth (1- c)
'("Other" "Unknown" "Lead Acid" "Nickel Cadmium" "Nickel Metal Hydride"
"Lithium-ion" "Zinc air" "Lithium Polymer"))))
(defun get-status (&optional key)
(let ((wb (get-win32-battery)))
(whenlet a #0=(getf wb 'Availability)
(setf #0# (availability-string a)))
(whenlet bs #1=(getf wb 'BatteryStatus)
(setf #1# (battery-status-string bs)))
(whenlet c #2=(getf wb 'Chemistry)
(setf #2# (chemistry-string c)))
(if key (getf wb key)
wb)))
(defun user::battery ()
(interactive)
(let ((bs (get-status)))
(message "~A (~D%)" (getf bs 'BatteryStatus) (getf bs 'EstimatedChargeRemaining))))
battery> (format t "~{~A~30T~A~%~}" (get-status))
Availability Unknown
BatteryStatus Online
Chemistry Lithium-ion
Description 内部バッテリ
DesignCapacity nil
DesignVoltage 11919
DeviceID 00646PanasonicCF-VZSU69
EstimatedChargeRemaining 78
EstimatedRunTime 71582788
ExpectedLife nil
FullChargeCapacity nil
MaxRechargeTime nil
SmartBatteryVersion nil
Status OK
TimeOnBattery nil
TimeToFullCharge nil
nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment