Jump to content

Laptop Battery Status


Recommended Posts

#NoTrayIcon
Opt('TrayMenuMode', 1)
Opt('TrayOnEventMode', 1)
TraySetOnEvent (-14, 'ExitAll')
#Include <Process.au3>
ProcessSetPriority(@AutoItPID, 1)
#Include <Misc.au3>
_SingleTon ('BatteryStatus')
_RunDOS('powercfg /batteryalarm critical >' & @TempDir & '\battery')
Local $batterylevel = FileRead(@TempDir & '\battery')
FileDelete(@TempDir & '\battery')
$batterylevel = StringSplit($batterylevel, 'Level', 1)
$batterylevel = StringSplit($batterylevel[2], 'percent', 1)
$batterylevel = StringReplace($batterylevel[1], ' ', '') / 1
Local $fullcapacity = '', $generalstatus = ''
Local $objwmiservice = ObjGet ('winmgmts:\\localhost\root\WMI')
Local $objwmiservice_ = ObjGet ('winmgmts:\\localhost\root\CIMV2')
While 1
 Local $batterystatus = $objWMIService.ExecQuery ('SELECT * FROM BatteryFullChargedCapacity', 'WQL', 0x10 + 0x20)
 If IsObj ($batterystatus) Then
  For $status in $batterystatus
   $fullcapacity = $status.fullchargedcapacity
  Next
 EndIf
 If $fullcapacity <> '' Then ExitLoop
 If $generalstatus <> 'Battery Not Detected' Then
  $generalstatus = 'Battery Not Detected'
  FileInstall('0', @TempDir & '\0', 1)
  Opt('TrayIconHide', 0)
  TraySetIcon (@TempDir & '\0')
  FileDelete(@TempDir & '\0')
  TraySetToolTip ('Battery Not Detected')
 EndIf
 Sleep(2000)
WEnd
Local $sleeptime = 1500, $cache = '', $sample = 0, $diff = 0, $last = 0, $first = 0, $time = ''
Local $h = '', $m = '', $s = ''
While 1
 Local $message = 'Power Online'
 Local $batterystatus = $objWMIService.ExecQuery ('SELECT * FROM BatteryStatus', 'WQL', 0x10 + 0x20)
 If IsObj ($batterystatus) Then
  For $status in $batterystatus
   Local $online = Not (Not ($status.poweronline))
   Local $charging = Not (Not ($status.charging))
   Local $discharging = Not (Not ($status.discharging))
  Next
 EndIf
 Local $batterystatus = $objWMIService_.ExecQuery ('SELECT * FROM Win32_Battery', 'WQL', 0x10 + 0x20)
 If IsObj ($batterystatus) Then
  For $status in $batterystatus
   Local $remainingcapacity = $status.estimatedchargeremaining
  Next
 EndIf
 If $cache <> $remainingcapacity Then
  $last = $first
  $first = TimerDiff(0)
  If $sample > 1 And $sample < 4 Then $diff = $first - $last
  If $sample > 3 Then $diff= ($diff+ ($first - $last)) / 2
  $sample = $sample + 1
  $cache = $remainingcapacity
 EndIf
 If $charging Then
  $message = 'Charging: ' & $remainingcapacity & '%'
  If $diff > 0 Then
   $time = Round(((100 - $remainingcapacity) * $diff) / 1000, 0)
   $h = Int($time / 3600)
   $m = Int(($time - $h * 3600) / 60)
   $s = $time - $h * 3600 - $m * 60
   $message = $message & @CRLF
   $message &= 'Remaining time: ' & $h & 'h:' & $m & 'm:' & $s & 's'
  EndIf
  $sleeptime = 750
 EndIf
 If $discharging Then
  $message = 'Discharging: ' & $remainingcapacity & '%'
  If $diff > 0 Then
   $time = Round((($remainingcapacity - $batterylevel) * $diff) / 1000, 0)
   $h = Int($time / 3600)
   $m = Int(($time - $h * 3600) / 60)
   $s = $time - $h * 3600 - $m * 60
   $message = $message & @CRLF
   $message &= 'Remaining time: ' & $h & 'h:' & $m & 'm:' & $s & 's'
  EndIf
  $sleeptime = 750
 EndIf
 If Not $charging And Not $discharging Then $sleeptime = 1500
 If $online And $generalstatus <> 'Power Online' Then
  FileInstall('1', @TempDir & '\1', 1)
  Opt('TrayIconHide', 0)
  TraySetIcon (@TempDir & '\1')
  FileDelete(@TempDir & '\1')
  $generalstatus = 'Power Online'
  $diff = 0
  $sample = 0
 EndIf
 If Not $online And $generalstatus <> 'Offline' Then
  FileInstall('2', @TempDir & '\2', 1)
  Opt('TrayIconHide', 0)
  TraySetIcon (@TempDir & '\2')
  FileDelete(@TempDir & '\2')
  $generalstatus = 'Offline'
  $diff = 0
  $sample = 0
 EndIf
 TraySetToolTip ($message)
 Sleep($sleeptime)
WEnd
Func ExitAll()
 TrayTip('', 'Closing...', 1)
 Sleep(99)
 Exit 0
EndFunc   ;==>ExitAll

Edited by psandu.ro
Link to comment
Share on other sites

  • 2 years later...

Try Now!

I've tried this script and it runs. However, the only thing I can see from the tray icon is Power Online.

I've messed with the program and can see the data it pulls from WMI is mostly good but I don't see it display anything different in the tray.

Is it also supposed to change the look of the icon?

Thanks

Be open minded but not gullible.A hammer sees everything as a nail ... so don't be A tool ... be many tools.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...