Author Topic: Computer "WakeOnTime" class  (Read 1061 times)

SteveDee

  • Newbie
  • *
  • Posts: 33
Computer "WakeOnTime" class
« on: October 21, 2009, 01:57:33 AM »
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:-

Code: [Select]
PUBLIC myAlarm AS NEW clsAlarmClock
In button1 Click:-

Code: [Select]
myAlarm.Clear()
Button2:-

Code: [Select]
DIM dteDate AS Date

  dteDate = DateAdd(Now(), gb.Minute, 10)
  myAlarm.Set(dteDate)

Button3:-

Code: [Select]
 myAlarm.Read()
  Label1.Text = myAlarm.strStartDate
  Label2.Text = myAlarm.strStartTime

...and here is the clsAlarmClock code:-
Code: [Select]
' 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

« Last Edit: October 22, 2009, 01:16:04 AM by SteveDee »

Linux Basic

Computer "WakeOnTime" class
« on: October 21, 2009, 01:57:33 AM »

Folkwang

  • Newbie
  • *
  • Posts: 1
Re: Computer "WakeOnTime" class
« Reply #1 on: February 19, 2012, 10:59:11 AM »
Hello SteveDee,

I put all of these lines in Gambas2.
 When I run the prog then Gambas2 says:
"Unknown identifier: clsAlarmClock in line 3 in FMain.class".

What is wrong ??

SteveDee

  • Newbie
  • *
  • Posts: 33
Re: Computer "WakeOnTime" class
« Reply #2 on: February 21, 2012, 04:50:14 PM »
My guess would be that you have not created the clsAlarmClock class properly. I don't know your level of ability so forgive me starting with the basics.

When you create a new project to try this code example, right click on the "Classes" node in the left hand pane and select new > Class...

In the New File dialog enter the new class Name as: clsAlarmClock

After clicking "OK" you should have a new file with " 'Gambas class file" at the top. Now just paste the class code into this file.

If you still have a problem, you may need to post all your code (for clsAlarmClock.class & FMain.class) here.

If your project does run, start by clicking Button3 (the "Read" button) which should display the current contents of your BIOS (probably 1970-01-01 and 00:00:00).

Good luck.