main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package main
  2. import (
  3. "Yunlot/Handle/HttpServer"
  4. "Yunlot/Handle/MqttServer"
  5. "Yunlot/Handle/TcpServer"
  6. _ "Yunlot/Nats"
  7. "Yunlot/RunCode"
  8. "Yunlot/TimeTask"
  9. "Yunlot/conf"
  10. "Yunlot/logs"
  11. "Yunlot/models/Product"
  12. _ "Yunlot/routers"
  13. "fmt"
  14. "github.com/beego/beego/v2/adapter/orm"
  15. orm2 "github.com/beego/beego/v2/client/orm"
  16. beego "github.com/beego/beego/v2/server/web"
  17. _ "github.com/go-sql-driver/mysql"
  18. "strconv"
  19. )
  20. func init() {
  21. logs.Println("MysqlServer:" + conf.MysqlServer_Username + ":" + conf.MysqlServer_Password + "@tcp(" + conf.MysqlServer_UrlPort + ")/" + conf.MysqlServer_Database + "?charset=utf8mb4&loc=Local&parseTime=True")
  22. orm.RegisterDriver("mysql", orm.DRMySQL)
  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, true) // 创建数据库
  27. }
  28. func main() {
  29. beego.BConfig.AppName = conf.AppName + "_" + strconv.Itoa(conf.HTTPPort) // 项目名
  30. beego.BConfig.ServerName = conf.AppName + "_" + strconv.Itoa(conf.HTTPPort) //server 名称
  31. beego.BConfig.RunMode = "dev" // 应用的运行模式
  32. beego.BConfig.Listen.HTTPPort = conf.HTTPPort //监听端口 本地:8518 线上:8528
  33. orm2.Debug = true
  34. logs.Println("MqttServer", " ======= 项目启动 ========")
  35. //go MqttServer2.Run_MqttServer() // MQTT 通讯
  36. //go MqttServer.MqttPolling() // MQTT 设备轮询
  37. //go MqttServer.DeviceMqttMap_go() //// 缓存数据发送-确保设备在休眠后 能收到数据
  38. // MQTT 通讯
  39. if conf.MqttServer_Open == 1 {
  40. mode := Product.ProductMode{
  41. Id: 1,
  42. T_name: "MQTT",
  43. T_address: fmt.Sprintf("mqtt://%s", conf.MqttServer_Url),
  44. T_describe: "MQTT 接入方式为设备和云平台提供双向连接,设备既可上报属性数据,也可接收云端的消息下发。",
  45. T_state: 1,
  46. }
  47. mode.Add()
  48. go MqttServer.Run_MqttServer()
  49. }
  50. // TCP 通讯
  51. if conf.TCPServer_Open == 1 {
  52. mode := Product.ProductMode{
  53. Id: 2,
  54. T_name: "TCP",
  55. T_address: fmt.Sprintf("TCP:%d", conf.TCPServer_Port),
  56. T_describe: "TCP 接入方式为设备和云平台提供双向连接,设备既可上报属性数据,也可接收云端的消息下发。",
  57. T_state: 1,
  58. }
  59. mode.Add()
  60. go TcpServer.TcpServer()
  61. }
  62. // HTTP 通讯
  63. if conf.HTTPServer_Open == 1 {
  64. mode := Product.ProductMode{
  65. Id: 6,
  66. T_name: "HTTP",
  67. T_address: fmt.Sprintf("http://:%d", conf.HTTPServer_Port),
  68. T_describe: "HTTP 接入方式为设备和云平台提供双向连接,设备既可上报属性数据,也可接收云端的消息下发。",
  69. T_state: 1,
  70. }
  71. mode.Add()
  72. go HttpServer.Run()
  73. }
  74. //go Nats.NatsInit() // Nats 通讯
  75. go TimeTask.Init() // 时间任务
  76. go RunCode.RunCode_Inte() // 代码编译
  77. beego.Run()
  78. }