123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package SystemInfo
- import (
- pb "AudioPlayer/gRPC_Model"
- "context"
- "os/exec"
- "strings"
- "time"
- )
- type TimeGRpcServer struct {
- pb.UnimplementedTimeServer
- }
- func (s *TimeGRpcServer) GetTime(ctx context.Context, in *pb.Empty) (*pb.Timestamp, error) {
- now := time.Now()
- return &pb.Timestamp{
- Seconds: now.Unix(),
- }, nil
- }
- func (s *TimeGRpcServer) SetTime(ctx context.Context, ts *pb.Timestamp) (*pb.Empty, error) {
- t := time.Unix(ts.Seconds, 0).Format(time.DateTime)
- _, err := exec.Command("timedatectl", "set-time", t).Output()
- if err != nil {
- return &pb.Empty{}, err
- }
- return &pb.Empty{}, nil
- }
- func Bool2String(b bool) string {
- if b {
- return "true"
- }
- return "false"
- }
- func (s *TimeGRpcServer) AutoSet(ctx context.Context, ts *pb.StateSwitch) (*pb.Empty, error) {
- _, err := exec.CommandContext(ctx, "timedatectl", "set-ntp", Bool2String(ts.State)).Output()
- if err != nil {
- return nil, err
- }
- return &pb.Empty{}, nil
- }
- func (s *TimeGRpcServer) AutoSetState(ctx context.Context, ts *pb.Empty) (*pb.StateSwitch, error) {
- out, err := exec.CommandContext(ctx, "timedatectl", "status").Output()
- return &pb.StateSwitch{State: strings.Contains(string(out), "NTP service: active")}, err
- }
|