goAdapter/main.go

116 lines
3.1 KiB
Go
Raw Normal View History

2020-06-28 18:15:50 +08:00
package main
import (
"fmt"
2020-09-17 10:55:33 +08:00
"github.com/sirupsen/logrus"
2020-06-28 18:15:50 +08:00
"net/http"
"time"
2020-07-06 09:51:05 +08:00
"github.com/robfig/cron"
"golang.org/x/sync/errgroup"
"goAdapter/device"
"goAdapter/httpServer"
"goAdapter/setting"
2020-07-06 09:51:05 +08:00
)
2020-06-28 18:15:50 +08:00
var (
g errgroup.Group
)
func main() {
/**************获取配置文件***********************/
setting.GetConf()
setting.LogerInit(setting.LogLevel,setting.LogSaveToFile,setting.LogFileMaxCnt)
2020-06-28 18:15:50 +08:00
//记录起始时间
2020-07-06 09:51:05 +08:00
setting.GetTimeStart()
2020-06-28 18:15:50 +08:00
2020-09-17 10:55:33 +08:00
setting.Logger.Info("goteway V0.0.1")
2020-07-07 18:08:01 +08:00
setting.MemoryDataStream = setting.NewDataStreamTemplate("内存使用率")
setting.DiskDataStream = setting.NewDataStreamTemplate("硬盘使用率")
setting.DeviceOnlineDataStream = setting.NewDataStreamTemplate("设备在线率")
setting.DevicePacketLossDataStream = setting.NewDataStreamTemplate("通信丢包率")
2020-06-28 18:15:50 +08:00
/**************网口初始化***********************/
setting.NetworkParaRead()
2020-07-06 09:51:05 +08:00
for _, v := range setting.NetworkParamList.NetworkParam {
2020-09-17 10:55:33 +08:00
if setting.SetNetworkParam(v.ID, v) == true{
setting.Logger.WithFields(logrus.Fields{
"networkName": v.Name,
"status": "true",
}).Info("set network")
}else {
setting.Logger.WithFields(logrus.Fields{
"networkName": v.Name,
"status": "false",
}).Info("set network")
}
2020-06-28 18:15:50 +08:00
}
2020-07-06 09:51:05 +08:00
setting.NetworkParamList = setting.GetNetworkParam()
2020-06-28 18:15:50 +08:00
/**************变量模板初始化****************/
2020-07-06 09:51:05 +08:00
device.DeviceNodeManageInit()
2020-06-28 18:15:50 +08:00
/**************目标平台初始化****************/
2020-07-06 09:51:05 +08:00
setting.RemotePlatformInit()
2020-09-17 10:55:33 +08:00
/**************NTP校时初始化****************/
setting.NTPInit()
2020-06-28 18:15:50 +08:00
/**************创建定时获取网络状态的任务***********************/
// 定义一个cron运行器
2020-09-17 10:55:33 +08:00
cronProcess := cron.New()
2020-06-28 18:15:50 +08:00
// 定时5秒每5秒执行print5
2020-09-17 10:55:33 +08:00
cronProcess.AddFunc("*/5 * * * * *", setting.GetNetworkStatus)
2020-06-28 18:15:50 +08:00
// 定时
for _,v := range device.CollectInterfaceMap{
CommunicationManage := device.NewCommunicationManageTemplate(v)
//CommunicationManage.CollInterfaceName = v.CollInterfaceName
str := fmt.Sprintf("@every %dm%ds",v.PollPeriod/60,v.PollPeriod%60)
2020-09-17 10:55:33 +08:00
setting.Logger.Infof("str %+v",str)
2020-06-28 18:15:50 +08:00
//cronGetNetStatus.AddFunc("10 */1 * * * *", CommunicationManage.CommunicationManagePoll)
2020-09-17 10:55:33 +08:00
cronProcess.AddFunc(str, CommunicationManage.CommunicationManagePoll)
2020-06-28 18:15:50 +08:00
go CommunicationManage.CommunicationManageDel()
}
2020-06-28 18:15:50 +08:00
// 定时60秒,定时获取系统信息
2020-09-17 10:55:33 +08:00
cronProcess.AddFunc("*/60 * * * * *", setting.CollectSystemParam)
// 每天0点,定时获取NTP服务器的时间并校时
cronProcess.AddFunc("0 0 0 * * ?", func(){
setting.NTPGetTime()
})
2020-06-28 18:15:50 +08:00
// 定时60秒,mqtt发布消息
//cronGetNetStatus.AddFunc("*/30 * * * * *", mqttClient.MqttAppPublish)
2020-09-17 10:55:33 +08:00
cronProcess.Start()
defer cronProcess.Stop()
2020-06-28 18:15:50 +08:00
//mqttClient.MQTTClient_Init()
2020-06-28 18:15:50 +08:00
/**************httpserver初始化****************/
// 默认启动方式,包含 Logger、Recovery 中间件
serverWeb := &http.Server{
Addr: setting.HttpPort,
2020-07-06 09:51:05 +08:00
Handler: httpServer.RouterWeb(),
2020-06-28 18:15:50 +08:00
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
g.Go(func() error {
return serverWeb.ListenAndServe()
})
if err := g.Wait(); err != nil {
2020-09-17 10:55:33 +08:00
setting.Logger.Fatal(err)
2020-06-28 18:15:50 +08:00
}
}