2020-06-28 18:15:50 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2020-08-12 17:20:49 +08:00
|
|
|
|
"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"
|
2020-08-19 09:14:25 +08:00
|
|
|
|
|
|
|
|
|
"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
|
|
|
|
|
)
|
|
|
|
|
|
2020-08-19 22:49:31 +08:00
|
|
|
|
|
2020-08-19 09:14:25 +08:00
|
|
|
|
func main() {
|
|
|
|
|
|
2020-08-19 22:49:31 +08:00
|
|
|
|
/**************获取配置文件***********************/
|
|
|
|
|
setting.GetConf()
|
|
|
|
|
|
|
|
|
|
setting.LogerInit(setting.LogLevel,setting.LogSaveToFile,setting.LogFileMaxCnt)
|
2020-08-12 17:20:49 +08:00
|
|
|
|
|
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
|
|
|
|
|
2020-08-12 17:20:49 +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
|
|
|
|
|
|
|
|
|
/**************网口初始化***********************/
|
2020-08-01 17:22:06 +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
|
|
|
|
|
2020-08-12 17:20:49 +08:00
|
|
|
|
// 定时
|
2020-07-31 21:43:41 +08:00
|
|
|
|
for _,v := range device.CollectInterfaceMap{
|
2020-09-05 13:01:03 +08:00
|
|
|
|
CommunicationManage := device.NewCommunicationManageTemplate(v)
|
|
|
|
|
//CommunicationManage.CollInterfaceName = v.CollInterfaceName
|
2020-08-18 14:54:06 +08:00
|
|
|
|
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
|
|
|
|
|
2020-08-18 14:54:06 +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
|
|
|
|
|
2020-08-12 17:20:49 +08:00
|
|
|
|
go CommunicationManage.CommunicationManageDel()
|
|
|
|
|
}
|
2020-06-28 18:15:50 +08:00
|
|
|
|
|
2020-08-12 17:20:49 +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
|
|
|
|
|
2020-08-15 10:21:39 +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
|
|
|
|
|
2020-08-18 21:36:15 +08:00
|
|
|
|
//mqttClient.MQTTClient_Init()
|
2020-06-28 18:15:50 +08:00
|
|
|
|
|
|
|
|
|
/**************httpserver初始化****************/
|
|
|
|
|
// 默认启动方式,包含 Logger、Recovery 中间件
|
|
|
|
|
serverWeb := &http.Server{
|
2020-08-12 11:30:57 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|