main.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package main
  2. import (
  3. _ "Cold_Api/Nats"
  4. "Cold_Api/conf"
  5. "Cold_Api/controllers/lib"
  6. "Cold_Api/models/Device"
  7. _ "Cold_Api/routers"
  8. "fmt"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. beego "github.com/beego/beego/v2/server/web"
  11. "github.com/beego/beego/v2/server/web/filter/cors"
  12. _ "github.com/go-sql-driver/mysql"
  13. "runtime"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. func init() {
  19. lib.WritePid()
  20. fmt.Println(runtime.GOOS)
  21. orm.RegisterDriver("mysql", orm.DRMySQL)
  22. //orm.RegisterDataBase("default", "mysql", "zdxq:7e5853d9178edfcc@tcp(47.108.133.234:3306)/zdxq?charset=utf8",100,200)
  23. orm.RegisterDataBase("default", "mysql",
  24. conf.MysqlServer_Username+":"+conf.MysqlServer_Password+"@tcp("+conf.MysqlServer_UrlPort+")/"+conf.MysqlServer_Database+"?charset=utf8mb4&loc=Local&parseTime=True",
  25. conf.MysqlServer_MaxIdleConnections, conf.MysqlServer_MaxOpenConnections)
  26. orm.RunSyncdb("default", false, false) // 创建数据库
  27. println(conf.MysqlServer_Username + ":" + conf.MysqlServer_Password + "@tcp(" + conf.MysqlServer_UrlPort + ")/" + conf.MysqlServer_Database + "?charset=utf8mb4&loc=Local&parseTime=True")
  28. }
  29. func main() {
  30. HTTPPort_int, _ := strconv.Atoi(conf.HTTPPort)
  31. beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
  32. AllowAllOrigins: true,
  33. AllowMethods: []string{"*"},
  34. AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin"},
  35. ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin"},
  36. AllowCredentials: true,
  37. }))
  38. beego.BConfig.AppName = conf.AppName // 项目名
  39. beego.BConfig.ServerName = conf.AppName + conf.HTTPPort //server 名称
  40. beego.BConfig.RunMode = "dev" // 应用的运行模式
  41. beego.BConfig.Listen.HTTPPort = HTTPPort_int //监听端口 本地:8518 线上:8528
  42. beego.AddFuncMap("convertm", convertM)
  43. beego.AddFuncMap("IsYD", IsYD)
  44. fmt.Println("======= beego.Run ======")
  45. beego.Run()
  46. }
  47. // 转十进制
  48. func convertM(Devicesensor Device.DeviceSensor) (out string) {
  49. var TouTime float64
  50. TouTime = 2 * 60
  51. now := time.Now()
  52. //DeviceData_, is := Device.Read_DeviceSensorData_ById_New(Devicesensor.T_sn, Devicesensor.T_id)
  53. //if is {
  54. // // 谁 是最新的
  55. // fmt.Println(DeviceData_.T_time, DeviceData_.T_time.Format("2006-01-02 15:04:05"))
  56. // fmt.Println(Devicesensor.T_time, Devicesensor.T_time.Format("2006-01-02 15:04:05"))
  57. // if DeviceData_.T_time.Unix() > Devicesensor.T_time.Unix() {
  58. // subM := now.Sub(DeviceData_.T_time)
  59. // fmt.Println(subM.Minutes(), "分钟")
  60. // if subM.Minutes() > TouTime {
  61. // return "温湿度传感器-2.png"
  62. // } else {
  63. // return "温湿度传感器-1.png"
  64. // }
  65. //
  66. // }
  67. //}
  68. //fmt.Println(Devicesensor.T_time, Devicesensor.T_time.Format("2006-01-02 15:04:05"))
  69. subM := now.Sub(Devicesensor.T_time)
  70. //fmt.Println(int(subM.Minutes()), "分钟")
  71. if subM.Minutes() > TouTime {
  72. // 传感器超时
  73. return "温湿度传感器-2.png"
  74. }
  75. if (0 == Devicesensor.T_Tlower && 0 == Devicesensor.T_RHlower) && (0 == Devicesensor.T_Tupper && 0 == Devicesensor.T_RHupper) {
  76. return "温湿度传感器-1.png"
  77. }
  78. if Devicesensor.T_t < Devicesensor.T_Tlower || Devicesensor.T_rh < Devicesensor.T_RHlower || Devicesensor.T_t > Devicesensor.T_Tupper || Devicesensor.T_rh > Devicesensor.T_RHupper {
  79. // 传感器 超标
  80. return "温湿度传感器-3.png"
  81. }
  82. // 传感器正常
  83. return "温湿度传感器-1.png"
  84. }
  85. // 转十进制
  86. func IsYD(sn string) (out int) {
  87. if strings.Contains(sn, "YD") {
  88. return 1
  89. }
  90. // 传感器正常
  91. return 0
  92. }