helper

package
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2024 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// VideoBucketName ImageBucketName MinioRegion minio 相关
	VideoBucketName     = "alarm.video"
	ImageBucketName     = "alarm.image"
	MinioRegion         = "cn-north-1"
	GlobalBaseConfigKey = "global_base_config"

	// Default RPCCodeSuc 状态码
	RPCCodeSuc    = 10200
	RPCCodeFailed = 10201

	// Default RPCInfo rpc信息
	RPCMsgSuc               = "消息接收成功"
	RPCMsgFailed            = "告警消息内容不全"
	RPCMsgPredictInfoFailed = "推理机信息收集不全"
	RPCMsgTaskCloseFailed   = "任务关闭失败"
	RPCMsgTaskCloseOpen     = "任务开启失败"
	RpcServicePredict       = "predict"
	RpcServiceSchedule      = "schedule"
	RpcServiceGateway       = "gateway"
	RpcEtcdPredictErr       = "发现etcd的predict服务失败"
	RpcPredictNewClientErr  = "创建推理机RPC客户端失败"
	RpcPredictTaskOpenErr   = "分配任务给推理机失败"
	RpcRegisterErr          = "注册服务到etcd失败"
	RpcConnEtcdErr          = "rpc kitex 连接etcd失败"
	RpcGateNewClientErr     = "创建gateRPC客户端失败"

	// for gateway and video
	VideoPreviewUrlSplitStr = "ISSTECHVIDEOSPLIT"

	// AlarmInfoErr 告警消息处理
	AlarmInfoErr = "推理机发送的告警信息不全"
	AlarmSubject = "alarm"
	AlarmQueue   = "queue01"

	HttpClientTimeOut   = 5
	NoticeStatusDefault = 0
	NoticeStatusSuc     = 1
	NoticeStatusFailed  = 2

	MinioContentImg   = "image/jpeg"
	MinioContentVideo = "video/mp4"
	MinioDefaultDay   = time.Hour * 24 * 7 // 七天

	TaskStatusClose = 0
	TaskStatusOpen  = 1

	//推理机状态
	PredictStatusOpen  = 1001
	PredictStatusClose = 1002
)
View Source
const (
	DistrictIdBits = uint(4) //区域 所占用位置
	NodeIdBits     = uint(9) //节点 所占位置 2的n次幂,6就是2的6次幂64个节点

	DistrictIdShift = sequenceBits + NodeIdBits
)

Variables

View Source
var (
	TokenExpired     = errors.New("token is expired")
	TokenNotValidYet = errors.New("token not active yet")
	TokenMalformed   = errors.New("that's not even a token")
	TokenInvalid     = errors.New("couldn't handle this token")
	SignKey          = "niChatTest"
)

Functions

func CheckError

func CheckError(err error, errDesc string, errInfo interface{})

func CheckErrorWithPanic

func CheckErrorWithPanic(err error, errDesc string, errInfo interface{})

func CreateShutdownScript

func CreateShutdownScript(srvName string) error

func DeleteOldLogFiles

func DeleteOldLogFiles(logsDir string, numDaysAgo, hour, minute int)

用于删除指定天数的指定目录下的日志文件 logsDir:日志所在的目录 numDaysAgo:删除几天前的文件 hour minute:每天的几点几分执行

func FormatStreamUrl

func FormatStreamUrl(streamUrl string) (newStreamUrl string)

检查视频流的url,将复杂的字符替换掉,此处以大华为案例

func GenerateRandomString

func GenerateRandomString(length int) string

生成随机字符串函数

func GetCurTime10bit

func GetCurTime10bit() (now time.Time, ts int64)

获取当前时间和对应的10位int64类型的时间戳

func GetCurTime13bit

func GetCurTime13bit() (now time.Time, ts int64)

GetCurTime 获取当前时间毫秒级和13位的时间戳,UTC ,数组截取时为了避免尾数四舍五入的情况

func GetCurTimeStr

func GetCurTimeStr() (nowStr string)

获取字符串类型的当前时间

func GetDelayCloseTask

func GetDelayCloseTask(recordVideoDuration uint8) int

获取延迟关闭任务时间 延迟关闭任务的目的:避免关闭视频流时发生告警引起视频无法录制到事件的后N秒

func GetDeleteOldRecord

func GetDeleteOldRecord(recordVideoDuration uint8) int

获取删除历史录制视频文件的时间

func GetDirAllFiles

func GetDirAllFiles(dirPath string) (err error, fileNames []string)

获取指定目录下所有文件名,带后缀

func GetDirAllFilesWithoutExt

func GetDirAllFilesWithoutExt(dirPath string) (err error, fileNames []string)

获取指定目录下所有文件名,将文件后缀去掉

func GetMergeVideoTimeWait

func GetMergeVideoTimeWait(recordVideoDuration uint8) int

获取合并视频的延迟时间

func GetTaskIntervalTime

func GetTaskIntervalTime(recordVideoDuration uint8) int

获取关闭任务后间隔开启任务的时间 开启任务时要检查上次关闭视频流的时间,目的是:避免因为延迟关闭而导致刚开启的任务视频流被关闭 例如:视频A的任务1 在10:01:00关闭了,但实际它将在N(5)秒后关闭视频流,如果马上开启任务, 与上次关闭视频流时间小于10:01:00+5 那在05秒的时候就会关闭05秒之前开启的任务,导致任务失败

func GetUrlPrefix

func GetUrlPrefix(rawURL string) (protocol, host, streamPath string)

根据url获取protocol host:ip:port steamPath(已做唯一值处理格式为:ip+port/md5(url))

func GetYamlConf

func GetYamlConf(cnfPath string, configStruct interface{})

func HostIpReplace

func HostIpReplace(oldHost, sourceIp string) (newHost string)

替换host为指定ip

func InitGatewayConn

func InitGatewayConn(confPath string) (mnc conn.MinioClient, sqlc conn.SqlClient, edc conn.EtcdClient, err error)

func InitPredictConn

func InitPredictConn(confPath string) (edc conn.EtcdClient, err error)

func InitScheduleConn

func InitScheduleConn(confPath string) (mnc conn.MinioClient, ntc conn.NatsClient, edc conn.EtcdClient, err error)

func InitVideoConn

func InitVideoConn(confPath string) (mnc conn.MinioClient, edc conn.EtcdClient, err error)

func TimeStampToTime

func TimeStampToTime(ts int64) time.Time

将时间戳转为时间类型

func TimeToTimeStampInt10bit

func TimeToTimeStampInt10bit(t time.Time) int32

将传入的时间转为10位的int类型,多用于时间比较

func TimeToTimeStampInt6413bit

func TimeToTimeStampInt6413bit(t time.Time) int64

将传入的时间转为13位的int64类型,多用于时间比较

func TimeToTimeStampStr13bit

func TimeToTimeStampStr13bit(t time.Time) string

将时间转为13位的字符串类型,多用于日志或数据入库

Types

type CustomClaims

type CustomClaims struct {
	UserId    string `json:"user_id"`
	NetId     string `json:"net_id"`
	TimeStamp int64  `json:"time_stamp"`
	jwt.StandardClaims
}

type GatewayConfSt

type GatewayConfSt struct {
	EtcdConf model.EtcdConf  `json:"etcd_conf" yaml:"etcd_conf"`
	SqlCnf   model.SQLConf   `json:"sql_cnf" yaml:"sql_cnf"`
	MinioCnf model.MinioConf `json:"minio_cnf" yaml:"minio_cnf"`
	LogCnf   model.LogConf   `json:"log_cnf" yaml:"log_cnf"`
}

func GetGatewayConfByYml

func GetGatewayConfByYml(cnfPath string) (env GatewayConfSt)

GetGatewayConfByYml 按配置文件获取配置信息

type GlobalBaseConfig

type GlobalBaseConfig struct {
	/* 这里是详情描述
		for video rtsp和rtmp 每个视频服务都会做视频转码,转码后的地址供推理端使用,多个视频服务时需要配置多个,
		video服务在本地配置文件中有video_id的编号,从0开始,按此id获取数组中的暴漏地址
		 例如:["1.1.1.1:1935","1.1.1.1:1935","1.1.1.2:1935"] 配置文件中video_id=0将匹配到1.1.1.1:1935

		H5LiveAddress: 给h5页面访问的视频地址,逻辑同上,此地址是客户端所在机器能访问到的,如果是代理后
		的ip:port 则注意代理配置内容关系是 video真实ip:port代理到nginx外网ip ,我们配置的是nginx外网ip:port

		MinioPreviewAddress: 默认写的是minio服务所在ip:port 如果是代理后的ip:port 则注意代理配置内容关系是
		minio真实ip:port代理到nginx外网ip,我们配置的是nginx外网ip:port

		RecordVideoDuration: 录制的视频时长,单位秒,默认10s,取值范围10-60s
	    FfmpegBinPath: 引用的ffmpeg命令路径 "ffmpeg_bin_path": "ffmpeg"
		LogFileKeepDays: 服务产生的日志文件保留天数-不包含审计日志,审计日志已入库不受影响
	*/
	RtspExportAddress         []string `json:"rtsp_export_address" yaml:"rtsp_export_address"`                     // 给推理端内部的rtsp地址
	RtmpExportAddress         []string `json:"rtmp_export_address" yaml:"rtmp_export_address"`                     // 给推理端内部的rtmp地址
	H5LiveAddress             []string `json:"h5_live_address" yaml:"h5_live_address"`                             // 前端页面播放直播流的地址
	MinioPreviewAddress       string   `json:"minio_preview_address" yaml:"minio_preview_address"`                 // 告警图片和视频浏览地址
	FfmpegBinPath             string   `json:"ffmpeg_bin_path" yaml:"ffmpeg_bin_path"`                             // 引用的ffmpeg命令路径
	RecordPath                string   `json:"record_path" yaml:"record_path"`                                     // 设置录制视频的路径:record/hls
	RecordVideoDuration       uint8    `json:"record_video_duration" yaml:"record_video_duration"`                 // 录制的视频时长,单位秒,默认10s
	LogFileExpirationDays     uint8    `json:"log_file_expiration_days" yaml:"log_file_expiration_days"`           // 日志文件过期时间
	MinioDataExpirationDays   uint16   `json:"minio_data_expiration_days" yaml:"minio_data_expiration_days"`       // minio中数据过期时间
	TaskAlgCountLimit         uint8    `json:"task_alg_count_limit" yaml:"task_alg_count_limit"`                   // 每个任务允许的算法个数,取值范围1-10,默认3
	ThirdPartyVideoSourceType uint8    `json:"third_party_video_source_type" yaml:"third_party_video_source_type"` // 第三方视频源同步类型,0不开启,1华为,2海康
	ThirdPartyAddress         []string `json:"third_party_address" yaml:"third_party_address"`                     // 格式:"ip:port,ip:port"支持视频集群地址
	ThirdPartyUserName        string   `json:"third_party_user_name" yaml:"third_party_user_name"`                 // 用户名,用于获取第三方验证token或session的
	ThirdPartyUserPwd         string   `json:"third_party_user_pwd" yaml:"third_party_user_pwd"`                   // 用户密码,用于获取第三方验证token或session的
}

全局基础配置-放在etcd用于后台配置动态更新

type IdWorker

type IdWorker struct {
	// contains filtered or unexported fields
}

func NewIdWorker

func NewIdWorker(districtId, NodeId int64) (*IdWorker, error)

NewIdWorker 创建雪花算法对象,当前设置为17位id,支持的districtId区域(可理解为IDC机房或云区域)为2的2次幂0-3,共4个区域,NodeId范围是2的7次幂0-127共128个节点数量,8毫秒内自增id

func (*IdWorker) GetId

func (id *IdWorker) GetId() (id17 int64)

GetId 获取id,当前设置为17位id,支持的区域为2的2次幂4个区域,nodeId范围是2的7次幂0-127共128个节点数量,8毫秒内自增id

func (*IdWorker) GetIdStr

func (id *IdWorker) GetIdStr() (idStr string)

GetIdStr 获取id,当前设置为19位id,支持的区域为2的2次幂4个区域,nodeId范围是2的7次幂0-127共128个节点数量,8毫秒内自增id

func (*IdWorker) NextId

func (id *IdWorker) NextId() (int64, error)

NextId get a snowflake id.

func (*IdWorker) NextIds

func (id *IdWorker) NextIds(num int) ([]int64, error)

NextIds get snowflake ids.

type JWTInfo

type JWTInfo struct {
	SignKey []byte
}

func NewJWT

func NewJWT() *JWTInfo

func (*JWTInfo) RefreshToken

func (j *JWTInfo) RefreshToken(tokenString string) (string, error)

RefreshToken 更新token

func (*JWTInfo) TokenCreate

func (j *JWTInfo) TokenCreate(tokenInfo CustomClaims) (string, error)

TokenCreate 创建token

func (*JWTInfo) TokenParse

func (j *JWTInfo) TokenParse(token string) (*CustomClaims, error)

TokenParse 解析token

type PredictConfSt

type PredictConfSt struct {
	EtcdConf model.EtcdConf `json:"etcd_conf" yaml:"etcd_conf"`
	LogCnf   model.LogConf  `json:"log_cnf" yaml:"log_cnf"`
}

func GetPredictConfByYml

func GetPredictConfByYml(cnfPath string) (env PredictConfSt)

GetPredictConfByYml 按配置文件获取配置信息

type ScheduleConfSt

type ScheduleConfSt struct {
	EtcdConf model.EtcdConf  `json:"etcd_conf" yaml:"etcd_conf"`
	MinioCnf model.MinioConf `json:"minio_cnf" yaml:"minio_cnf"`
	LogCnf   model.LogConf   `json:"log_cnf" yaml:"log_cnf"`
}

func GetScheduleConfByYml

func GetScheduleConfByYml(cnfPath string) (env ScheduleConfSt)

GetScheduleConfByYml 按配置文件获取配置信息

type VideoConfSt

type VideoConfSt struct {
	EtcdConf model.EtcdConf  `json:"etcd_conf" yaml:"etcd_conf"`
	MinioCnf model.MinioConf `json:"minio_cnf" yaml:"minio_cnf"`
	LogCnf   model.LogConf   `json:"log_cnf" yaml:"log_cnf"`
}

func GetVideoConfByYml

func GetVideoConfByYml(cnfPath string) (env VideoConfSt)

GetVideoConfByYml 按配置文件获取配置信息

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL