2020-12-20 15:08:43 +08:00
|
|
|
|
package iot
|
2020-12-18 07:34:31 +08:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/hmac"
|
|
|
|
|
"crypto/sha256"
|
|
|
|
|
"encoding/hex"
|
2020-12-18 23:59:25 +08:00
|
|
|
|
"encoding/json"
|
2020-12-18 07:34:31 +08:00
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2020-12-20 15:08:43 +08:00
|
|
|
|
// 时间戳:为设备连接平台时的UTC时间,格式为YYYYMMDDHH,如UTC 时间2018/7/24 17:56:20 则应表示为2018072417。
|
2020-12-18 07:34:31 +08:00
|
|
|
|
func TimeStamp() string {
|
2020-12-18 23:59:25 +08:00
|
|
|
|
strFormatTime := time.Now().Format("2006-01-02 15:04:05")
|
|
|
|
|
strFormatTime = strings.ReplaceAll(strFormatTime, "-", "")
|
|
|
|
|
strFormatTime = strings.ReplaceAll(strFormatTime, " ", "")
|
2020-12-18 07:34:31 +08:00
|
|
|
|
strFormatTime = strFormatTime[0:10]
|
|
|
|
|
return strFormatTime
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 15:08:43 +08:00
|
|
|
|
// 设备采集数据UTC时间(格式:yyyyMMdd'T'HHmmss'Z'),如:20161219T114920Z。
|
|
|
|
|
//设备上报数据不带该参数或参数格式错误时,则数据上报时间以平台时间为准。
|
2020-12-22 23:35:42 +08:00
|
|
|
|
func GetEventTimeStamp() string {
|
2020-12-20 15:08:43 +08:00
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
return now.Format("20060102T150405Z")
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-18 07:34:31 +08:00
|
|
|
|
func HmacSha256(data string, secret string) string {
|
|
|
|
|
h := hmac.New(sha256.New, []byte(secret))
|
|
|
|
|
h.Write([]byte(data))
|
|
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
|
}
|
2020-12-18 23:59:25 +08:00
|
|
|
|
|
2020-12-20 15:08:43 +08:00
|
|
|
|
func Interface2JsonString(v interface{}) string {
|
2020-12-22 23:35:42 +08:00
|
|
|
|
if v == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2020-12-18 23:59:25 +08:00
|
|
|
|
byteData, err := json.Marshal(v)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return string(byteData)
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 15:08:43 +08:00
|
|
|
|
func GetTopicRequestId(topic string) string {
|
2020-12-19 00:15:32 +08:00
|
|
|
|
return strings.Split(topic, "=")[1]
|
2020-12-18 23:59:25 +08:00
|
|
|
|
}
|
2020-12-19 00:51:55 +08:00
|
|
|
|
|
2020-12-20 15:08:43 +08:00
|
|
|
|
func FormatTopic(topic, deviceId string) string {
|
2020-12-19 00:51:55 +08:00
|
|
|
|
return strings.ReplaceAll(topic, "{device_id}", deviceId)
|
|
|
|
|
}
|
2020-12-22 23:35:42 +08:00
|
|
|
|
|
|
|
|
|
// 根据当前运行的操作系统重新修改文件路径以适配操作系统
|
|
|
|
|
func SmartFileName(filename string) string {
|
|
|
|
|
// Windows操作系统适配
|
|
|
|
|
if strings.Contains(OsName(), "windows") {
|
|
|
|
|
pathParts := strings.Split(filename, "/")
|
|
|
|
|
pathParts[0] = pathParts[0] + ":"
|
2020-12-23 23:08:35 +08:00
|
|
|
|
return strings.Join(pathParts, "\\")
|
2020-12-22 23:35:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return filename
|
|
|
|
|
}
|