lib.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. package lib
  2. import (
  3. "Yunlot/lib/TcpServerLib"
  4. "Yunlot/logs"
  5. "bytes"
  6. "crypto/sha256"
  7. "encoding/hex"
  8. "encoding/json"
  9. "fmt"
  10. "github.com/nats-io/nats.go"
  11. "github.com/thinkeridea/go-extend/exunicode/exutf8"
  12. "github.com/yosssi/gmq/mqtt"
  13. "github.com/yosssi/gmq/mqtt/client"
  14. "golang.org/x/text/encoding/simplifiedchinese"
  15. "golang.org/x/text/transform"
  16. "io/ioutil"
  17. "math/rand"
  18. "plugin"
  19. "reflect"
  20. "runtime"
  21. "strconv"
  22. "strings"
  23. "time"
  24. "unicode/utf8"
  25. )
  26. var Nats *nats.Conn
  27. var MqttClient *client.Client
  28. var TopicMap map[string]bool
  29. var PluginMap map[string]*plugin.Plugin
  30. var TcpMap map[string]*TcpServerLib.Session
  31. func init() {
  32. TopicMap = make(map[string]bool)
  33. PluginMap = make(map[string]*plugin.Plugin)
  34. TcpMap = make(map[string]*TcpServerLib.Session)
  35. }
  36. const Success = 200
  37. const Error = 201
  38. type JSONR struct {
  39. //必须的大写开头
  40. Code int16
  41. Msg string
  42. Data interface{} // 泛型
  43. }
  44. type JSONS struct {
  45. //必须的大写开头
  46. List interface{}
  47. Total int16
  48. PageIndex int
  49. PageSize int
  50. }
  51. func C_Page(list interface{}, PageIndex, PageSize int, Total int64) (Jsons JSONS) {
  52. Jsons.List = list
  53. Jsons.Total = int16(Total)
  54. Jsons.PageIndex = PageIndex
  55. Jsons.PageSize = PageSize
  56. //Jsons.PageSize = int16(math.Ceil(float64(Total) / float64(PageSize)))
  57. return Jsons
  58. }
  59. func Strval(value interface{}) string {
  60. var key string
  61. if value == nil {
  62. return key
  63. }
  64. switch value.(type) {
  65. case float64:
  66. ft := value.(float64)
  67. key = strconv.FormatFloat(ft, 'f', -1, 64)
  68. case float32:
  69. ft := value.(float32)
  70. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  71. case int:
  72. it := value.(int)
  73. key = strconv.Itoa(it)
  74. case uint:
  75. it := value.(uint)
  76. key = strconv.Itoa(int(it))
  77. case int8:
  78. it := value.(int8)
  79. key = strconv.Itoa(int(it))
  80. case uint8:
  81. it := value.(uint8)
  82. key = strconv.Itoa(int(it))
  83. case int16:
  84. it := value.(int16)
  85. key = strconv.Itoa(int(it))
  86. case uint16:
  87. it := value.(uint16)
  88. key = strconv.Itoa(int(it))
  89. case int32:
  90. it := value.(int32)
  91. key = strconv.Itoa(int(it))
  92. case uint32:
  93. it := value.(uint32)
  94. key = strconv.Itoa(int(it))
  95. case int64:
  96. it := value.(int64)
  97. key = strconv.FormatInt(it, 10)
  98. case uint64:
  99. it := value.(uint64)
  100. key = strconv.FormatUint(it, 10)
  101. case string:
  102. key = value.(string)
  103. case []byte:
  104. key = string(value.([]byte))
  105. default:
  106. newValue, _ := json.Marshal(value)
  107. key = string(newValue)
  108. }
  109. return key
  110. }
  111. func To_int(value interface{}) int {
  112. var key int
  113. if value == nil {
  114. return key
  115. }
  116. switch value.(type) {
  117. case float64:
  118. key = int(value.(float64))
  119. case float32:
  120. key = int(value.(float32))
  121. case int:
  122. key = int(value.(int))
  123. case uint:
  124. key = int(value.(uint))
  125. case int8:
  126. key = int(value.(int8))
  127. case uint8:
  128. key = int(value.(uint8))
  129. case int16:
  130. key = int(value.(int16))
  131. case uint16:
  132. key = int(value.(uint16))
  133. case int32:
  134. key = int(value.(int32))
  135. case uint32:
  136. key = int(value.(uint32))
  137. case int64:
  138. key = int(value.(int64))
  139. case uint64:
  140. key = int(value.(uint64))
  141. case string:
  142. key, _ = strconv.Atoi(value.(string))
  143. case []byte:
  144. key, _ = strconv.Atoi(string(value.([]byte)))
  145. default:
  146. newValue, _ := json.Marshal(value)
  147. key, _ = strconv.Atoi(string(newValue))
  148. }
  149. return key
  150. }
  151. func To_int64(value interface{}) int64 {
  152. var key int64
  153. if value == nil {
  154. return key
  155. }
  156. switch value.(type) {
  157. case float64:
  158. key = int64(value.(float64))
  159. case float32:
  160. key = int64(value.(float32))
  161. case int:
  162. key = int64(value.(int))
  163. case uint:
  164. key = int64(value.(uint))
  165. case int8:
  166. key = int64(value.(int8))
  167. case uint8:
  168. key = int64(value.(uint8))
  169. case int16:
  170. key = int64(value.(int16))
  171. case uint16:
  172. key = int64(value.(uint16))
  173. case int32:
  174. key = int64(value.(int32))
  175. case uint32:
  176. key = int64(value.(uint32))
  177. case int64:
  178. key = int64(value.(int64))
  179. case uint64:
  180. key = int64(value.(uint64))
  181. case string:
  182. key, _ = strconv.ParseInt(value.(string), 10, 64)
  183. case []byte:
  184. key, _ = strconv.ParseInt(string(value.([]byte)), 10, 64)
  185. default:
  186. newValue, _ := json.Marshal(value)
  187. key, _ = strconv.ParseInt(string(newValue), 10, 64)
  188. }
  189. return key
  190. }
  191. func To_float32(value interface{}) float32 {
  192. var key float32
  193. if value == nil {
  194. return key
  195. }
  196. switch value.(type) {
  197. case float64:
  198. key = float32(value.(float64))
  199. case float32:
  200. key = float32(value.(float32))
  201. case int:
  202. key = float32(value.(int))
  203. case uint:
  204. key = float32(value.(uint))
  205. case int8:
  206. key = float32(value.(int8))
  207. case uint8:
  208. key = float32(value.(uint8))
  209. case int16:
  210. key = float32(value.(int16))
  211. case uint16:
  212. key = float32(value.(uint16))
  213. case int32:
  214. key = float32(value.(int32))
  215. case uint32:
  216. key = float32(value.(uint32))
  217. case int64:
  218. key = float32(value.(int64))
  219. case uint64:
  220. key = float32(value.(uint64))
  221. case string:
  222. key_float64, _ := strconv.ParseFloat(value.(string), 32/64)
  223. key = float32(key_float64)
  224. case []byte:
  225. key_float64, _ := strconv.ParseFloat(string(value.([]byte)), 32/64)
  226. key = float32(key_float64)
  227. default:
  228. newValue, _ := json.Marshal(value)
  229. key_float64, _ := strconv.ParseFloat(string(newValue), 32/64)
  230. key = float32(key_float64)
  231. }
  232. key_float64, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", key), 32/64)
  233. key = float32(key_float64)
  234. return key
  235. }
  236. func To_string(value interface{}) string {
  237. var key string
  238. if value == nil {
  239. return key
  240. }
  241. switch value.(type) {
  242. case float64:
  243. ft := value.(float64)
  244. key = strconv.FormatFloat(ft, 'f', -1, 64)
  245. case float32:
  246. ft := value.(float32)
  247. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  248. case int:
  249. it := value.(int)
  250. key = strconv.Itoa(it)
  251. case uint:
  252. it := value.(uint)
  253. key = strconv.Itoa(int(it))
  254. case int8:
  255. it := value.(int8)
  256. key = strconv.Itoa(int(it))
  257. case uint8:
  258. it := value.(uint8)
  259. key = strconv.Itoa(int(it))
  260. case int16:
  261. it := value.(int16)
  262. key = strconv.Itoa(int(it))
  263. case uint16:
  264. it := value.(uint16)
  265. key = strconv.Itoa(int(it))
  266. case int32:
  267. it := value.(int32)
  268. key = strconv.Itoa(int(it))
  269. case uint32:
  270. it := value.(uint32)
  271. key = strconv.Itoa(int(it))
  272. case int64:
  273. it := value.(int64)
  274. key = strconv.FormatInt(it, 10)
  275. case uint64:
  276. it := value.(uint64)
  277. key = strconv.FormatUint(it, 10)
  278. case string:
  279. key = value.(string)
  280. case []byte:
  281. key = string(value.([]byte))
  282. default:
  283. newValue, _ := json.Marshal(value)
  284. key = string(newValue)
  285. }
  286. return key
  287. }
  288. func Random(min, max int) int {
  289. rand.Seed(time.Now().Unix()) //Seed生成的随机数
  290. return rand.Intn(max-min) + min
  291. }
  292. // 取文本(字符串)中间
  293. func GetBetweenStr(str, start, end string) string {
  294. n := strings.Index(str, start)
  295. if n == -1 {
  296. n = 0
  297. } else {
  298. n = n + len(start) // 增加了else,不加的会把start带上
  299. }
  300. str = string([]byte(str)[n:])
  301. m := strings.Index(str, end)
  302. if m == -1 {
  303. m = len(str)
  304. }
  305. str = string([]byte(str)[:m])
  306. return str
  307. }
  308. // getYearMonthToDay 查询指定年份指定月份有多少天
  309. // @params year int 指定年份
  310. // @params month int 指定月份
  311. func GetYearMonthToDay(year int, month int) int {
  312. // 有31天的月份
  313. day31 := map[int]bool{
  314. 1: true,
  315. 3: true,
  316. 5: true,
  317. 7: true,
  318. 8: true,
  319. 10: true,
  320. 12: true,
  321. }
  322. if day31[month] == true {
  323. return 31
  324. }
  325. // 有30天的月份
  326. day30 := map[int]bool{
  327. 4: true,
  328. 6: true,
  329. 9: true,
  330. 11: true,
  331. }
  332. if day30[month] == true {
  333. return 30
  334. }
  335. // 计算是平年还是闰年
  336. if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
  337. // 得出2月的天数
  338. return 29
  339. }
  340. // 得出2月的天数
  341. return 28
  342. }
  343. func Decimal(value float64) float64 {
  344. value, _ = strconv.ParseFloat(fmt.Sprintf("%.1f", value), 64)
  345. return value
  346. }
  347. func Strconv_Atoi(string string) int {
  348. int, _ := strconv.Atoi(string)
  349. return int
  350. }
  351. // 获取正在运行的函数名
  352. func FuncName() string {
  353. pc := make([]uintptr, 1)
  354. runtime.Callers(2, pc)
  355. f := runtime.FuncForPC(pc[0])
  356. return f.Name()
  357. }
  358. func Limit_len(str string, lenx int) string {
  359. if utf8.RuneCountInString(str) > lenx {
  360. return exutf8.RuneSubString(str, 0, lenx-3) + "..."
  361. }
  362. return exutf8.RuneSubString(str, 0, lenx)
  363. }
  364. func Utf8ToGbk(s []byte) ([]byte, error) {
  365. reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder())
  366. d, e := ioutil.ReadAll(reader)
  367. if e != nil {
  368. return nil, e
  369. }
  370. return d, nil
  371. }
  372. // #取得随机字符串:通过打乱slice来操作
  373. func GetRandstring(length int, char string, rand_x int64) string {
  374. if length < 1 {
  375. return ""
  376. }
  377. if len(char) <= 6 || len(char) <= length {
  378. char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  379. }
  380. charArr := strings.Split(char, "")
  381. ran := rand.New(rand.NewSource(time.Now().Unix() + rand_x))
  382. l := len(charArr)
  383. for i := l - 1; i > 0; i-- {
  384. r := ran.Intn(i)
  385. charArr[r], charArr[i] = charArr[i], charArr[r]
  386. }
  387. rchar := charArr[:length]
  388. return strings.Join(rchar, "")
  389. }
  390. // Sha256加密
  391. func Sha256(src string) string {
  392. m := sha256.New()
  393. m.Write([]byte(src))
  394. res := hex.EncodeToString(m.Sum(nil))
  395. return res
  396. }
  397. // 判断时间是当年的第几周
  398. func WeekByDate() string {
  399. t := time.Now()
  400. yearDay := t.YearDay()
  401. yearFirstDay := t.AddDate(0, 0, -yearDay+1)
  402. firstDayInWeek := int(yearFirstDay.Weekday())
  403. //今年第一周有几天
  404. firstWeekDays := 1
  405. if firstDayInWeek != 0 {
  406. firstWeekDays = 7 - firstDayInWeek + 1
  407. }
  408. var week int
  409. if yearDay <= firstWeekDays {
  410. week = 1
  411. } else {
  412. week = (yearDay-firstWeekDays)/7 + 2
  413. }
  414. return fmt.Sprintf("%d%02d", t.Year(), week) // 202253
  415. }
  416. // 发送数据
  417. func Mqtt_publish(topic string, b []byte) {
  418. TopicMap[topic] = true
  419. // Publish a message.
  420. err := MqttClient.Publish(&client.PublishOptions{
  421. QoS: mqtt.QoS0,
  422. TopicName: []byte(topic),
  423. Message: b,
  424. })
  425. logs.PrintlnMqtt("Mqtt-> " + topic + ":" + string(b))
  426. if err != nil {
  427. logs.PrintlnError("MqttServer", "发送消息失败 [Mqtt_publish]", "-> "+topic+" "+string(b))
  428. }
  429. }
  430. // 获取json指定参数 [AAAA BBBB],0,JSON !!!如果失败,返回已找到的全部json
  431. func Json_key(topicNameList []string, topicNameI int, articleSlide map[string]interface{}) interface{} {
  432. if len(topicNameList) > topicNameI {
  433. a, is := articleSlide[topicNameList[topicNameI]]
  434. if !is {
  435. return articleSlide
  436. }
  437. if reflect.TypeOf(a).String() == "map[string]interface {}" {
  438. return Json_key(topicNameList, topicNameI+1, a.(map[string]interface{}))
  439. }
  440. }
  441. if len(topicNameList) == topicNameI {
  442. return articleSlide
  443. }
  444. return articleSlide[topicNameList[topicNameI]]
  445. }
  446. // 发送数据
  447. func TCP_publish(T_sn string, byte_r []byte) {
  448. logs.PrintlnMqtt("TCP-> " + T_sn + ":" + string(byte_r))
  449. if v, is := TcpMap[T_sn]; is {
  450. err := v.Send(string(byte_r))
  451. if err != nil {
  452. logs.PrintlnError("TCP", "发送消息失败 [TCP_publish]", "-> "+T_sn+" "+string(byte_r))
  453. }
  454. } else {
  455. logs.PrintlnError("TCP", "TcpMap[T_sn] 不存在,发送消息失败 [TCP_publish]", "-> "+T_sn+" "+string(byte_r))
  456. }
  457. }