I produced this class as part of an app to control computer startup based upon me-tv recording schedules, but I thought it may also be useful for other applications where you want your computer to wakeup at a certain time.
Comments would be very welcome.
To Play with this class, just create a new project and add 3 buttons and 2 labels. Then add these lines:-
PUBLIC myAlarm AS NEW clsAlarmClock
In button1 Click:-
myAlarm.Clear()
Button2:-
DIM dteDate AS Date
dteDate = DateAdd(Now(), gb.Minute, 10)
myAlarm.Set(dteDate)
Button3:-
myAlarm.Read()
Label1.Text = myAlarm.strStartDate
Label2.Text = myAlarm.strStartTime
...and here is the clsAlarmClock code:-
' Gambas class file
' clsAlarmClock
'********************************************************************************
'This class allows the ACPI WakeOnTime BIOS feature to be Set, Cleared and Read
'
'Use the method "Clear" to disable WakeOnTime (must do this before using "Set")
'Use method "Set" to set computer WakeOnTime
'Use the method "Read" to read the WakeOnTime settings into a temp file, and
' then read individual properties; blnClockStatus, strStartTime, strStartDate
'
'Steve Davis
'September 2009
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
PROPERTY READ blnClockStatus AS Boolean 'Is WakeOnTime enabled?
PRIVATE blnStatus AS Boolean
PROPERTY READ strStartTime AS String 'The current WakeOnTime time
PRIVATE strTime AS String
PROPERTY READ strStartDate AS String 'The current WakeOnTime date
PRIVATE strDate AS String
'=====================================================================
PUBLIC FUNCTION Clear() AS Boolean
'Clear (disable) the BIOS WakeOnTime feature
'---------------------------------------------------------------------
DIM strClear AS String
strClear = "sudo sh -c 'echo 0 > /sys/class/rtc/rtc0/wakealarm'"
TRY SHELL strClear
RETURN TRUE
CATCH
RETURN FALSE
END
'=====================================================================
PUBLIC FUNCTION Set(dteDate AS Date) AS Boolean
'Convert a regular date into "seconds since epoch"
'Set the WakeOnTime using "seconds since epoch"
'---------------------------------------------------------------------
DIM strDate AS String
DIM strTimeWakeSecs AS String
DIM strSet AS String
strDate = CStr(dteDate)
strDate = "date -d '" & strDate & "' +%s"
TRY SHELL strDate TO strTimeWakeSecs
strTimeWakeSecs = Replace(strTimeWakeSecs, Chr(10), "")
strSet = "sudo sh -c \'echo " & strTimeWakeSecs & " > /sys/class/rtc/rtc0/wakealarm\'"
TRY SHELL strSet
RETURN TRUE
CATCH
RETURN FALSE
END
'=====================================================================
PUBLIC FUNCTION Read() AS Boolean
'This method copies the RTC settings into a hidden file in the
'users Home directory.
'Then writes the WakeOnTime settings to class properties.
'---------------------------------------------------------------------
DIM strFilePath AS String 'where to save file
DIM strFile AS String 'contents of file
DIM strCommand AS String
DIM hFile AS File
strFilePath = User.Home & "/.rtc.tmp"
IF Exist(strFilePath) THEN
TRY KILL strFilePath
ENDIF
strCommand = "cat /proc/driver/rtc > " & strFilePath
TRY SHELL strCommand WAIT
IF Exist(strFilePath) THEN
hFile = OPEN strFilePath FOR READ
READ #hFile, strFile, -255
CLOSE #hFile
IF ReadValue(strFile, "alarm_IRQ") = "yes" THEN
blnStatus=TRUE
ELSE
blnStatus=FALSE
ENDIF
strDate=ReadValue(strFile, "alrm_date")
strTime=ReadValue(strFile, "alrm_time")
RETURN FALSE
ENDIF
CATCH
RETURN FALSE
END
'=====================================================================
PRIVATE FUNCTION ReadValue(strFile AS String, strParam AS String) AS String
'Read a specified parameter setting from the specified file.
'---------------------------------------------------------------------
DIM strText AS String
strText = Mid(strfile, InStr(strfile, strParam))
strText = Mid(strText, InStr(strText, ":") + 1)
strText = Mid(strText, 1, InStr(strText, Chr(10)) - 1)
RETURN Trim(strText)
END
'CLASS PROPERTIES---------------------------------------------------------------
PRIVATE FUNCTION blnClockStatus_Read() AS Boolean
RETURN blnStatus
END
PRIVATE FUNCTION strStartTime_Read() AS String
RETURN strTime
END
PRIVATE FUNCTION strStartDate_Read() AS String
RETURN strDate
END