Skip to content

Commit

Permalink
build on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
korovindenis committed Jun 11, 2023
1 parent 102728c commit 1dd79ae
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
File renamed without changes.
84 changes: 84 additions & 0 deletions internal/service/service_windows.go
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,
}
}

0 comments on commit 1dd79ae

Please sign in to comment.