main.go 3.0 KB

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