main.go 3.2 KB

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