feat: init pc-monitor project
- Client: Go-based Windows hardware monitoring (CPU, GPU, memory, disk, network, power) - Server: Go + Gin + SQLite backend with REST API - Frontend: Vue 3 + Element Plus dashboard - Docker deployment support - Windows service installation script
This commit is contained in:
73
server/service/device.go
Normal file
73
server/service/device.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"pc-monitor-server/model"
|
||||
"pc-monitor-server/repository"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DeviceService struct {
|
||||
repo *repository.DeviceRepository
|
||||
}
|
||||
|
||||
func NewDeviceService(repo *repository.DeviceRepository) *DeviceService {
|
||||
return &DeviceService{repo: repo}
|
||||
}
|
||||
|
||||
func (s *DeviceService) Register(hostname, osName, ip string) (*model.Device, error) {
|
||||
id := generateDeviceID(hostname, ip)
|
||||
device, err := s.repo.GetByID(id)
|
||||
if err == nil {
|
||||
device.LastReportAt = time.Now()
|
||||
device.Status = "online"
|
||||
s.repo.Update(device)
|
||||
return device, nil
|
||||
}
|
||||
|
||||
device = &model.Device{
|
||||
ID: id,
|
||||
Hostname: hostname,
|
||||
OS: osName,
|
||||
IP: ip,
|
||||
RegisteredAt: time.Now(),
|
||||
LastReportAt: time.Now(),
|
||||
Status: "online",
|
||||
}
|
||||
if err := s.repo.Create(device); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return device, nil
|
||||
}
|
||||
|
||||
func (s *DeviceService) GetAll() ([]model.Device, error) {
|
||||
return s.repo.GetAll()
|
||||
}
|
||||
|
||||
func (s *DeviceService) GetByID(id string) (*model.Device, error) {
|
||||
return s.repo.GetByID(id)
|
||||
}
|
||||
|
||||
func (s *DeviceService) Delete(id string) error {
|
||||
return s.repo.Delete(id)
|
||||
}
|
||||
|
||||
func (s *DeviceService) Heartbeat(id string) error {
|
||||
device, err := s.repo.GetByID(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
device.LastReportAt = time.Now()
|
||||
device.Status = "online"
|
||||
return s.repo.Update(device)
|
||||
}
|
||||
|
||||
func (s *DeviceService) CheckOffline() {
|
||||
s.repo.MarkOffline(2 * time.Minute)
|
||||
}
|
||||
|
||||
func generateDeviceID(hostname, ip string) string {
|
||||
hash := sha256.Sum256([]byte(fmt.Sprintf("%s-%s", hostname, ip)))
|
||||
return fmt.Sprintf("%x", hash[:8])
|
||||
}
|
||||
Reference in New Issue
Block a user