videoplayer/service/toolService.go

167 lines
4.7 KiB
Go
Raw Normal View History

package service
import (
2025-06-06 14:36:59 +08:00
"errors"
2025-01-02 15:39:05 +08:00
"fmt"
"regexp"
"time"
"videoplayer/proto"
"videoplayer/worker"
)
func SetToolRedisList(key string, value string, expire int) (code int, message string) {
if expire == 0 {
if worker.PushRedisList(key, value) {
return proto.SuccessCode, "success"
} else {
return proto.OperationFailed, "push redis list failed"
}
} else if expire > 0 {
if worker.PushRedisListWithExpire(key, value, time.Duration(expire)) {
return proto.SuccessCode, "success"
} else {
return proto.OperationFailed, "push redis list with expire failed"
}
} else {
return proto.ParameterError, "expire time can not be negative"
}
}
func SetToolRedisSet(key string, value string, expire int) (code int, message string) {
if expire == 0 {
if worker.SetRedis(key, value) {
return proto.SuccessCode, "success"
} else {
return proto.OperationFailed, "set redis failed"
}
} else if expire > 0 {
if worker.SetRedisWithExpire(key, value, time.Duration(expire)) {
return proto.SuccessCode, "success"
} else {
return proto.OperationFailed, "set redis with expire failed"
}
} else {
return proto.ParameterError, "expire time can not be negative"
}
}
func SetToolRedisKV(key string, value string, expire int) (code int, message string) {
if expire == 0 {
if worker.SetRedis(key, value) {
return proto.SuccessCode, "success"
} else {
return proto.OperationFailed, "set redis failed"
}
} else if expire > 0 {
if worker.SetRedisWithExpire(key, value, time.Duration(expire)) {
return proto.SuccessCode, "success"
} else {
return proto.OperationFailed, "set redis with expire failed"
}
} else {
return proto.ParameterError, "expire time can not be negative"
}
}
func GetToolRedis(key string) (code int, message string) {
val := worker.GetRedis(key)
if val == "" {
return proto.OperationFailed, "get redis failed"
} else {
return proto.SuccessCode, val
}
}
2024-12-21 17:16:38 +08:00
func GetAllRedis() (code int, msg string, data []worker.RedisInfo) {
data, err := worker.GetAllRedisInfo()
if err != nil {
return proto.OperationFailed, err.Error(), nil
}
return proto.SuccessCode, "success", data
}
2025-01-02 15:39:05 +08:00
func SendEmail(email, subject, body string) {
//捕获异常
defer func() {
if err := recover(); err != nil {
fmt.Errorf("tool send mail error: %s", err)
}
}()
// TODO
// 发送邮件
// 邮件内容
// 邮件标题
// 收件人
// 发送邮件
// 发送邮件通知
// 发送邮件通知
var em worker.MyEmail
em.SmtpPassword = "nihzazdkmucnbhid"
em.SmtpHost = "pop.qq.com:587"
em.SmtpUserName = "354425203@qq.com"
em.SmtpPort = 587
em.ImapPort = 993
err := em.Send(subject, body, []string{email})
if err != nil {
fmt.Println("send mail error:", err)
}
}
// 地址校验
func CheckEmail(email string) bool {
//正则表达式判断是否是邮箱
pattern := `^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+$`
reg := regexp.MustCompile(pattern)
return reg.MatchString(email)
}
2025-06-06 14:36:59 +08:00
// 获取监控设备及状态
func GetMonitorDeviceListWithStatus(userId int) ([]proto.GetMonitorDeviceStatus, error) {
var deviceStatus []proto.GetMonitorDeviceStatus
user := GetUserByIDFromUserCenter(userId)
if user.Role != "admin" {
return deviceStatus, errors.New("user is not admin, can not get monitor device list")
} else {
devices := worker.GetRedisSetMembers(proto.Config.MONITOR_SERVER_TOKEN)
for _, device := range devices {
status, expireIn := worker.GetRedisWithExpire("monitor_" + device)
2025-06-06 14:36:59 +08:00
var deviceInfo proto.GetMonitorDeviceStatus
deviceInfo.ID = device
deviceInfo.Expire = expireIn
2025-06-06 14:36:59 +08:00
if status == "" {
deviceInfo.Status = "offline"
} else {
deviceInfo.Status = status
}
deviceStatus = append(deviceStatus, deviceInfo)
}
}
return deviceStatus, nil
}
func UpdateMonitorDeviceListWithStatus(userId int, deviceReq []proto.GetMonitorDeviceStatus) error {
user := GetUserByIDFromUserCenter(userId)
var err error
if user.Role != "admin" {
err = errors.New("user is not admin, can not update monitor device list")
return err
} else {
// 更新监控设备状态.如果在集合中则添加,不在则添加到集合中
devices := worker.GetRedisSetMembers(proto.Config.MONITOR_SERVER_TOKEN)
deviceMap := make(map[string]bool, len(devices))
for _, device := range devices {
deviceMap[device] = true
}
for _, device := range deviceReq {
if _, ok := deviceMap[device.ID]; ok {
// 如果设备在集合中,则更新状态
worker.SetRedisWithExpire("monitor_"+device.ID, device.Status, time.Duration(device.Expire)*time.Second)
} else {
worker.SetRedisSetAdd(proto.Config.MONITOR_SERVER_TOKEN, device.ID)
worker.SetRedisWithExpire("monitor_"+device.ID, device.Status, time.Duration(device.Expire)*time.Second)
}
}
}
return err
}