Skip to content

Commit af45061

Browse files
feat : wakeup and sleep function added
1 parent d6b0917 commit af45061

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

orangetool/orangetool.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import string
99
import random
10+
import time
1011
ip_pattern=r"(?:[0-9]{1,3}\.){3}[0-9]{1,3}"
1112
api_1="http://ipinfo.io/ip"
1213
VERSION="orangetool-v0.2"
@@ -99,7 +100,7 @@ def set_ip(ip,DEVICE="eth0",DEBUG=False):
99100
'''
100101
try:
101102
if bool(re.match(ip_pattern,ip))==False or ip.find("192.168.")==-1 or DEVICE not in mac().keys():
102-
raise Exception
103+
raise Exception("IP Formation Error")
103104
static_string=static_string.replace("ip",ip)
104105
static_string=static_string.replace("device",DEVICE)
105106
file=open("/etc/network/interfaces","w")
@@ -265,7 +266,7 @@ def ping(ip,packet_number=3,DEBUG=False):
265266
'''
266267
try:
267268
if re.match(ip_pattern,ip)==False:
268-
raise Exception
269+
raise Exception("IP Formation Error")
269270
output=str(list(sub.Popen(["ping",ip,"-c",str(packet_number)],stdout=sub.PIPE,stderr=sub.PIPE).communicate())[0])
270271
if output.find("Unreachable")==-1:
271272
return True
@@ -405,7 +406,7 @@ def mount(device_name,mount_address=None,DEBUG=False):
405406
'''
406407
try:
407408
if mount_status(device_name)!="u":
408-
raise Exception
409+
raise Exception("Device already mount")
409410
if mount_address==None:
410411
mount_address="/mnt/"+random_generator(5)
411412
command=sub.Popen(["mkdir",mount_address], stdout=sub.PIPE, stderr=sub.PIPE)
@@ -504,6 +505,53 @@ def hdmi_size(v=None,h=None,DEBUG=False):
504505
if DEBUG==True:
505506
print(str(e))
506507
return "Error"
508+
def wakeup(day=0,hour=0,minute=0,DEBUG=False):
509+
'''
510+
This function set wakeup time for kernel RTC (need sudo)
511+
:param day: days for wakeup
512+
:param hour: hout for wakeup
513+
:param minute: minute for wakeup
514+
:param DEBUG: Flag for using Debug mode
515+
:type day:int
516+
:type hour:int
517+
:type minute:int
518+
:type DEBUG:bool
519+
:return: bool
520+
'''
521+
try:
522+
total_time=day*24*60+hour*60+minute
523+
epoch=time.time()+total_time*60
524+
file=open("/sys/class/rtc/rtc0/wakealarm","w")
525+
file.write("0")
526+
file.close()
527+
file = open("/sys/class/rtc/rtc0/wakealarm", "w")
528+
file.write(str(epoch))
529+
file.close()
530+
return True
531+
except Exception as e:
532+
if DEBUG==True:
533+
print(str(e))
534+
return "Error"
535+
def sleep(DEBUG=False):
536+
'''
537+
This function is a shortcut for sleep (need sudo)
538+
:param DEBUG: Flag for using Debug mode
539+
:type DEBUG:bool
540+
:return: None
541+
'''
542+
try:
543+
command=sub.Popen("pm-suspend",stderr=sub.PIPE,stdout=sub.PIPE,stdin=sub.PIPE)
544+
response=list(command.communicate())
545+
if len(response[1])>0:
546+
raise Exception('Root Error')
547+
except Exception as e:
548+
if DEBUG==True:
549+
print(str(e))
550+
return "Error"
551+
552+
553+
554+
507555

508556

509557

0 commit comments

Comments
 (0)