package service import ( "errors" "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 } } 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 } 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) } // 获取监控设备及状态 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) var deviceInfo proto.GetMonitorDeviceStatus deviceInfo.ID = device deviceInfo.Expire = expireIn 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 }