標籤:ruby os x 電源狀態
最近本貓所在的小區時常停電,往往半夜或是淩晨才來電啊!早上起來本貓在想如何知道確切的來電時間,但又不費事的方法呢。
方法一是用手機錄音器錄音,因為來電後門禁會發出“滴”的一聲,所以只要知道開始錄音的時間就可以知道來電的時間了!
方法二是停電就會停水,找一個容器放在開啟得水龍頭下面,保持超小的出水口,然後第二天看容量就可以知道來電的時間。不過這種方法略微腦殘點。
方法三是直接打電話問門衛大叔,因為他們24小時不睡覺啊。或者打電話問供電局說:請相信本貓有一個很重要的原因,請告訴我昨晚來電的時間吧!
方法四是如果筆記本電池還夠的話,寫代碼來獲得準確的時間吧!本貓查了一下如何在OS X系統下擷取AC狀態,就是電來源配接器的狀態。如果是Conneted則表示有市電否則表示電池在供電。度娘搜尋一下,很簡單的方法:
system_profiler SPSoftwareDataType SPHardwareDataType SPPowerDataType | grep -E "Serial Number \(system\)|Computer Name|Cycle Count|Charge Remaining|Charge Capacity"
返回如下:
Computer Name: kissAir
Serial Number (system): XXXXXXXXXXXXXX :)
Charge Remaining (mAh): 4296
Full Charge Capacity (mAh): 4340
Cycle Count: 67
雖然有電池的資訊,不過沒有AC的資訊,自己稍微修改了一下,刪除那些不用的輸出:
system_profiler SPPowerDataType
返回:
Power:
Battery Information:
Model Information:
Serial Number: XXXXXXXXXXXXXXX :)
Manufacturer: DP
Device Name: bq20z451
Pack Lot Code: 0
PCB Lot Code: 0
Firmware Version: 406
Hardware Revision: 1
Cell Revision: 173
Charge Information:
Charge Remaining (mAh): 4321
Fully Charged: No
Charging: No
Full Charge Capacity (mAh): 4321
Health Information:
Cycle Count: 67
Condition: Normal
Battery Installed: Yes
Amperage (mA): -9
Voltage (mV): 8268
System Power Settings:
AC Power:
System Sleep Timer (Minutes): 60
Disk Sleep Timer (Minutes): 10
Display Sleep Timer (Minutes): 10
Wake on AC Change: No
Wake on Clamshell Open: Yes
Wake on LAN: Yes
AutoPowerOff Delay: 14400
AutoPowerOff Enabled: 1
DarkWakeBackgroundTasks: 1
Display Sleep Uses Dim: Yes
PrioritizeNetworkReachabilityOverSleep: 0
Standby Delay: 4200
Standby Enabled: 1
Battery Power:
System Sleep Timer (Minutes): 10
Disk Sleep Timer (Minutes): 10
Display Sleep Timer (Minutes): 2
Wake on AC Change: No
Wake on Clamshell Open: Yes
AutoPowerOff Delay: 14400
AutoPowerOff Enabled: 1
Current Power Source: Yes
DarkWakeBackgroundTasks: 0
Display Sleep Uses Dim: Yes
Reduce Brightness: Yes
Standby Delay: 4200
Standby Enabled: 1
Hardware Configuration:
UPS Installed: No
AC Charger Information:
Connected: No
Charging: No
注意最後幾行,那個就是AC的串連狀態啊!不過還要進一步精簡命令:
[email protected]: ~$system_profiler SPPowerDataType|grep Connected:
Connected: Yes
[email protected]: ~$system_profiler SPPowerDataType|grep Connected:
Connected: No
這次可以了,接下來就很簡單了,用代碼搞定吧:
#!/usr/bin/rubywhile trueret = `system_profiler SPPowerDataType|grep Connected:`if ret =~ /Yes/#表示有電源接入puts "power come when #{Time.now}";breakend;sleep 1end
各位童鞋腦洞開啟,幫我想想其他好方法吧!
Mac OS X 簡單的方法知道何時來電了