-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
102728c
commit 1dd79ae
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package service | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
) | ||
|
||
var syscall Syscall | ||
|
||
func Init() { | ||
syscall = Syscall{} | ||
} | ||
|
||
type Syscall struct { | ||
LINUX_REBOOT_CMD_POWER_OFF byte | ||
LINUX_REBOOT_CMD_RESTART byte | ||
} | ||
|
||
func (s Syscall) Reboot(mode byte) error { | ||
fmt.Println("A ", mode, " would have occurred") | ||
|
||
return nil | ||
} | ||
|
||
type Status struct { | ||
Mode string | ||
TimeShutDown string | ||
} | ||
|
||
type countdown struct { | ||
t int | ||
h int | ||
m int | ||
s int | ||
} | ||
|
||
func New(s *Status, logslevel uint) { | ||
for { | ||
if s.Mode == "" { | ||
time.Sleep(time.Second * 5) | ||
} else { | ||
time.Sleep(1 * time.Second) | ||
v, _ := time.Parse(time.RFC3339, s.TimeShutDown) | ||
timeRemaining := getTimeRemaining(v) | ||
|
||
if s.Mode != "" { | ||
if timeRemaining.t <= 0 { | ||
// bye | ||
if logslevel > 0 { | ||
log.Println("Run:", s.Mode) | ||
} | ||
callMode := syscall.LINUX_REBOOT_CMD_POWER_OFF | ||
if s.Mode == "reboot" { | ||
callMode = syscall.LINUX_REBOOT_CMD_RESTART | ||
} | ||
err := syscall.Reboot(callMode) | ||
if err != nil { | ||
log.Fatalf("%s", err) | ||
} | ||
} else if logslevel > 1 { | ||
log.Printf("Time for %s - %d:%d:%d\n", s.Mode, timeRemaining.h, timeRemaining.m, timeRemaining.s) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func getTimeRemaining(t time.Time) countdown { | ||
currentTime := time.Now().UTC() | ||
difference := t.Sub(currentTime) | ||
|
||
total := int(difference.Seconds()) | ||
hours := int(total / (60 * 60) % 24) | ||
minutes := int(total/60) % 60 | ||
seconds := int(total % 60) | ||
|
||
return countdown{ | ||
t: total, | ||
h: hours, | ||
m: minutes, | ||
s: seconds, | ||
} | ||
} |