package common
import (
"bufio"
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"log"
"os"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"github.com/xx/config"
"github.com/xx/config/e"
)
var (
BASE_IP = config.Config.Services.Web.Baseip
PEOPLEIMGPATH = config.Config.Services.Image.Peopleimgpath
IMGBASEPATH = config.Config.Services.Image.Imgbasepath
ACCESSPATH = config.Config.Services.Image.Accessbasepath
//报警参数配置//map
ALARMEVENT_CONFIG = "alarm_event_configure"
//记录上次报警时间前缀 map
LASTALARM_PRE = "alarm_lastesttime_"
BLACKLIST_LASTALARM_PRE = LASTALARM_PRE + "blacklist_"
PROTECT_LASTALARM_PRE = LASTALARM_PRE + "protectarea_"
HOTSPOT_LASTALARM_PRE = LASTALARM_PRE + "hotspot_"
//摄像机列表 set
UUID_PRE = "alarm_camerauuid_"
PROTECT_UUID = UUID_PRE + "protectarea"
BLACKLIST_UUID = UUID_PRE + "blacklist"
HOTSPOT_UUID = UUID_PRE + "hotspot"
DOUBT_UUID = UUID_PRE + "doubt"
//可疑人员临时数据 map
DOUBT_PRE = "alarm_doubt_"
DOUBT_SHORTTRACK = DOUBT_PRE + "shorttrack"
DOUBT_LONGREPEAT = DOUBT_PRE + "longrepeat"
DOUBT_LONGSTAY = DOUBT_PRE + "longstay"
ALARM_CHANNEL = "AlarmWebsocket" //报警websocket使用
)
/*获取某年的某月一共多少天*/
func GetDaysByYearMonth(year int, month int) (days int) {
if month != 2 {
if month == 4 || month == 6 || month == 9 || month == 11 {
days = 30
} else {
days = 31
}
} else {
if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
days = 29
} else {
days = 28
}
}
return
}
func TimeCost(str string, start time.Time) {
terminal := time.Since(start)
fmt.Println(str, terminal)
}
func CreateFile(path string, content []byte) (errCode int64) {
//创建文件
file, err := os.Create(path)
if err != nil {
log.Println("CreateFile1 file err:", err)
return e.ERROR_ALARM_CREATE_FILE
}
defer file.Close()
w := bufio.NewWriter(file)
_, err = w.Write(content)
if err != nil {
log.Println("CreateFile2 file err:", err)
return e.ERROR_ALARM_CREATE_FILE
}
return 0
}
func SplitImageBase64Content(str string) (suff, content string) {
splitstr := strings.Split(str, ",")
if len(splitstr) != 2 {
return "", ""
}
suff = splitstr[0][11 : len(splitstr[0])-7]
content = splitstr[1]
return
}
func GetImageSuffixes(str string) string {
reg := regexp.MustCompile(`^data:image/.*;base64`)
allstr := reg.FindAllString(str, -1)
if len(allstr) == 0 {
return ""
}
strPre := allstr[0]
return strPre[11 : len(strPre)-7]
}
func HashSHA256File(filePath string) string {
var hashValue string
file, err := os.Open(filePath)
if err != nil {
log.Println("HashSHA256File file err:", err)
return hashValue
}
defer file.Close()
hash := sha256.New()
if _, err := io.Copy(hash, file); err != nil {
return hashValue
}
hashInBytes := hash.Sum(nil)
hashValue = hex.EncodeToString(hashInBytes)
return hashValue
}
func HashSHA256Byte(str []byte) (sha256str string) {
h := sha256.New()
h.Write(str)
sha256str = hex.EncodeToString(h.Sum(nil))
return
}
func SplitNameAndSuf(imagename string) (name, suf string) {
nameArr := strings.Split(imagename, ".")
suf = nameArr[len(nameArr)-1]
l := len(imagename) - (len(suf) + 1)
name = string([]rune(imagename)[:l])
return
}
//yadd需要加几年 madd 需要加几个月
func SpliceYearMonth(t, yadd, madd int) string {
y := strconv.Itoa(time.Unix(int64(t), 0).Year() + yadd)
n := strconv.Itoa(int(time.Unix(int64(t), 0).Month()) + madd)
if len(n) == 1 {
n = "0" + n
}
return y + n
}
func SpliceYearMonthToStr(y, n int) string {
ystr := strconv.Itoa(y)
nstr := strconv.Itoa(n)
if len(nstr) == 1 {
nstr = "0" + nstr
}
return ystr + nstr
}
//判断目录是否存在,不存在则创建,创建失败则返回错误信息
func PathExists(path string) (exist bool) {
_, err := os.Stat(path)
if err == nil {
log.Println("PathExists file err:", err)
return false
}
if os.IsNotExist(err) {
err = os.MkdirAll(path, 0777)
}
return true
}
func FileExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
//获取操作系统的目录分隔符
func GetSeparator() (s string) {
var ostype = runtime.GOOS
if ostype == "windows" {
s = "\\"
} else if ostype == "linux" {
s = "/"
}
return
}
//字符串拼接
func StrSub(strs ...string) string {
var buffers bytes.Buffer
for _, str := range strs {
buffers.WriteString(str)
}
return buffers.String()
}