36 lines
1.2 KiB
Go
36 lines
1.2 KiB
Go
package device_status
|
||
|
||
import (
|
||
"context"
|
||
"github.com/go-redis/redis/v8"
|
||
)
|
||
|
||
// DeviceStatus структура устройства его статуса в Redis
|
||
type DeviceStatus struct {
|
||
Key string
|
||
Client *redis.Client
|
||
}
|
||
|
||
// NewDeviceStatus конструктор, принимающий клиент Redis
|
||
func NewDeviceStatus(redisClient *redis.Client) *DeviceStatus {
|
||
return &DeviceStatus{
|
||
Key: "device_status",
|
||
Client: redisClient,
|
||
}
|
||
}
|
||
|
||
// GetOnline получает статус онлайн для указанного устройства из Redis.
|
||
func (ds *DeviceStatus) GetOnline(ctx context.Context, devID string) (string, error) {
|
||
return ds.Client.HGet(ctx, ds.Key, devID).Result()
|
||
}
|
||
|
||
// SetOnline устанавливает статус онлайн для указанного устройства в Redis.
|
||
func (ds *DeviceStatus) SetOnline(ctx context.Context, devID string) error {
|
||
return ds.Client.HSet(ctx, ds.Key, devID, 0).Err()
|
||
}
|
||
|
||
// SetOffline устанавливает статус оффлайн для указанного устройства в Redis.
|
||
func (ds *DeviceStatus) SetOffline(ctx context.Context, devID string) error {
|
||
return ds.Client.HDel(ctx, ds.Key, devID).Err()
|
||
}
|