最近本猫所在的小区时常停电,往往半夜或是凌晨才来电啊!早上起来本猫在想如何知道确切的来电时间,但又不费事的方法呢。
方法一是用手机录音器录音,因为来电后门禁会发出“滴”的一声,所以只要知道开始录音的时间就可以知道来电的时间了!
方法二是停电就会停水,找一个容器放在打开得水龙头下面,保持超小的出水口,然后第二天看容量就可以知道来电的时间。不过这种方法略微脑残点。
方法三是直接打电话问门卫大叔,因为他们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的连接状态啊!不过还要进一步精简命令:
apple@kissAir: ~$system_profiler SPPowerDataType|grep Connected:
Connected: Yes
apple@kissAir: ~$system_profiler SPPowerDataType|grep Connected:
Connected: No
这次可以了,接下来就很简单了,用代码搞定吧:
#!/usr/bin/ruby while true ret = `system_profiler SPPowerDataType|grep Connected:` if ret =~ /Yes/ #表示有电源接入 puts "power come when #{Time.now}";break end;sleep 1 end
各位童鞋脑洞打开,帮我想想其他好方法吧!