Power Status (was Re: Because LC can't do two things at once.)

Peter M. Brigham pmbrig at gmail.com
Tue Mar 3 17:32:28 EST 2015


on 3/3/15, I wrote:

> OK, as usual, I find some tweaks *after* I hit the send button. I changed caseSwitch() so it handles the case of an empty tCheckValue, allowing getPowerSource() to  report "no battery" if getting the URL returns empty. The expansion of caseSwitch() is useful in and of itself….

Sigh. Some typos in the handler I sent -- I was changing variable names and didn't change everything. Here is the (hopefully) correct and testable handler. It never fails -- I am learning that if I want to debug something, all I have to do is send it off to this list and I'll immediately find the errors, *after* I hit send.

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig

----------

function getPowerSource
   -- returns the current power source for a laptop
   --    "AC" or "Battery"
   --    or "no battery" if there is no battery (Unix)
   -- requires caseSwitch()
   switch the platform
      case "MacOS"
         -- thanks to Martin Koob, use-LC list
         put shell ("pmset -g batt") into tStatus
         -- returns something like:
         --    Currently drawing from 'AC Power'
         --     -InternalBattery-0	99%; finishing charge; 0:00 remaining
         return char 2 to -1 of word -2 of line 1 of tStatus
         break
      case "Win32"
         -- thanks to Bob Sneidar, use-LC list
         put shell("WMIC Path Win32_Battery GetAvailability") into tStatus
         -- Line 3 will contain 2 if the battery is charging, 3 if running on battery
         put line 3 of tStatus into tStatus
         return caseSwitch(tStatus,"3=Battery","*=AC")
         break
      default
         -- Unix, thanks to Richard Gaskin, use-LC list
         put url "/sys/class/power_supply/BAT0/" into tStatus
         if tStatus = empty then put url "/sys/class/power_supply/BAT1/" into tStatus
         put word 1 of tStatus into tStatus
         return caseSwitch(tStatus,"discharging=Battery","charging,full=AC",\
         "=no battery","*=*")
         -- if tStatus = empty, returns "no battery", else if tStatus is non-standard,
         --    just returns whatever "/sys/class/power_supply/BATx/" reports
   end switch
end getPowerSource





More information about the use-livecode mailing list