|
|
@@ -25,7 +25,6 @@ func NewDownStreamService() {
|
|
|
logger.Infof("新设备连接:%s", conn.GetClientHost())
|
|
|
})
|
|
|
server.OnNewMessage(func(conn *tcpserver.Client, msg []byte) {
|
|
|
- //logger.Debugf("[%s]收到新消息:%s", conn.GetClientHost(), string(msg))
|
|
|
logger.Debugf("[%s]收到新消息[byte]:%v", conn.GetClientHost(), msg)
|
|
|
if dealDtuUpMsg(conn, msg) {
|
|
|
conn.PutRawRes(msg)
|
|
|
@@ -57,32 +56,35 @@ func dealDtuUpMsg(conn *tcpserver.Client, msg []byte) bool {
|
|
|
// 处理DTU注册包及心跳包
|
|
|
func dealDtuRegisterHeartBeat(conn *tcpserver.Client, dtuMsg *iotStruct.DTUManageCmd) {
|
|
|
if dtuMsg.Cmd == iotStruct.CmdRegister || dtuMsg.Cmd == iotStruct.CmdHeartBeat {
|
|
|
- //clientState, _ := data.DtuMapState.Get(dtuMsg.SN)
|
|
|
- clientState, ok := iotMap.MapDtuClient.Get(dtuMsg.SN)
|
|
|
+ dtuClient, ok := iotMap.MapDtuClient.Get(dtuMsg.SN)
|
|
|
+ if !ok {
|
|
|
+ dtu, ok2 := iotMap.MapDtuDevice.Get(dtuMsg.SN)
|
|
|
+ if !ok2 {
|
|
|
+ logger.Errorf("设备未注册: %s", dtu.SN)
|
|
|
+ }
|
|
|
+ dtuClient = &iotStruct.DtuClient{
|
|
|
+ Logger: dtu.Logger,
|
|
|
+ Config: dtu.Config,
|
|
|
+ }
|
|
|
+ }
|
|
|
conn.SetClientRegisterID(dtuMsg.SN)
|
|
|
- clientState.FD = conn
|
|
|
- clientState.Online = true
|
|
|
- clientState.UpdateTime()
|
|
|
-
|
|
|
- //data.IpSN.Add(conn.GetClientHost(), dtuMsg.SN)
|
|
|
- //onlineSn, ok := data.OnlineSN.Get(dtuMsg.SN)
|
|
|
- if !ok || clientState.FD.GetClientHost() != clientState.FD.GetClientHost() {
|
|
|
+ dtuClient.FD = conn
|
|
|
+ dtuClient.Online = true
|
|
|
+ dtuClient.UpdateTime()
|
|
|
+ if !ok || dtuClient.FD.GetClientHost() != dtuClient.FD.GetClientHost() {
|
|
|
msg := &iotStruct.DtuRegisterChanMsg{
|
|
|
- Sn: dtuMsg.SN,
|
|
|
- Value: clientState,
|
|
|
+ SN: dtuMsg.SN,
|
|
|
+ Value: dtuClient,
|
|
|
}
|
|
|
DtuRegisterChan <- msg
|
|
|
} else {
|
|
|
- logger.Debugf("[%s]设备上线:%s", clientState.FD.GetClientHost(), dtuMsg.SN)
|
|
|
- iotMap.MapDtuClient.Add(dtuMsg.SN, clientState)
|
|
|
+ dtuClient.Logger.Infof("== HeartBeat == %s", conn.GetClientHost())
|
|
|
+ iotMap.MapDtuClient.Add(dtuMsg.SN, dtuClient)
|
|
|
}
|
|
|
if dtuMsg.Cmd == iotStruct.CmdRegister {
|
|
|
- // 设备上线
|
|
|
- UpdateDtuStatus(dtuMsg.SN, constant.IotDeviceOnline)
|
|
|
- logger.Infof("======【%s】====== Register %s", dtuMsg.SN, conn.GetClientHost())
|
|
|
- }
|
|
|
- if dtuMsg.Cmd == iotStruct.CmdHeartBeat {
|
|
|
- logger.Infof("======【%s】====== HeartBeat %s", dtuMsg.SN, conn.GetClientHost())
|
|
|
+ logger.Debugf("== Register == %s %s", dtuMsg.SN, conn.GetClientHost())
|
|
|
+ } else if dtuMsg.Cmd == iotStruct.CmdHeartBeat {
|
|
|
+ logger.Debugf("== HeartBeat == %s %s", dtuMsg.SN, conn.GetClientHost())
|
|
|
}
|
|
|
} else {
|
|
|
logger.Errorf("Register || HeartBeat 失败")
|
|
|
@@ -98,7 +100,7 @@ func dealDTUDisconnect(conn *tcpserver.Client) {
|
|
|
UpdateDtuStatus(sn, constant.IotDeviceOffline)
|
|
|
if client, ok := iotMap.MapDtuClient.Get(sn); ok && client.FD.GetClientHost() == addr {
|
|
|
iotMap.MapDtuClient.Remove(sn)
|
|
|
- logger.Debugf("[%s]设备离线:%s", addr, sn)
|
|
|
+ client.Logger.Debugf("设备离线:%s", addr)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -110,8 +112,7 @@ func runTran(msg <-chan *iotStruct.DtuRegisterChanMsg) {
|
|
|
for {
|
|
|
select {
|
|
|
case msg := <-msg:
|
|
|
- logger.Infof("【更新设备配置】 SN:%s", msg.Sn)
|
|
|
- updateOnlineConf(msg)
|
|
|
+ dtuClientOnline(msg)
|
|
|
case nowTime := <-ticker.C:
|
|
|
lastMinute = collectAndReport(nowTime, lastMinute)
|
|
|
}
|
|
|
@@ -119,13 +120,16 @@ func runTran(msg <-chan *iotStruct.DtuRegisterChanMsg) {
|
|
|
}
|
|
|
|
|
|
// 设备上线更新配置
|
|
|
-func updateOnlineConf(msg *iotStruct.DtuRegisterChanMsg) {
|
|
|
- if dtu, ok := iotMap.MapDtuDevice.Get(msg.Sn); ok {
|
|
|
+func dtuClientOnline(msg *iotStruct.DtuRegisterChanMsg) {
|
|
|
+ if dtu, ok := iotMap.MapDtuDevice.Get(msg.SN); ok {
|
|
|
msg.Value.Config = dtu.Config
|
|
|
- iotMap.MapDtuClient.Add(msg.Sn, msg.Value)
|
|
|
- logger.Debugf("[%s]设备上线:%s ;配置: %v", msg.Value.FD.GetClientHost(), msg.Sn, dtu.Config)
|
|
|
+ msg.Value.Logger = dtu.Logger
|
|
|
+ iotMap.MapDtuClient.Add(msg.SN, msg.Value)
|
|
|
+ // 设备上线
|
|
|
+ UpdateDtuStatus(msg.SN, constant.IotDeviceOnline)
|
|
|
+ dtu.Logger.Infof("设备上线:%s ;配置: %v 上报配置:%v", msg.Value.FD.GetClientHost(), dtu.Config, dtu.Config.SlaveConfig)
|
|
|
} else {
|
|
|
- logger.Debugf("【设备上线失败】[%s]设备不存在。", msg.Sn)
|
|
|
+ logger.Errorf("【设备上线失败】[%s]设备不存在。", msg.SN)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -136,20 +140,16 @@ func collectAndReport(nowTime time.Time, lastMinute int) int {
|
|
|
count := 0
|
|
|
onlineDtu := make(map[string]*iotStruct.DtuClient)
|
|
|
|
|
|
- //for _, v := range data.DtuMap.Map.Map {
|
|
|
- // vv := v.(*iotStruct.DTUDeviceState)
|
|
|
- // log.Println("===============", vv.Info.Name)
|
|
|
- //}
|
|
|
iotMap.MapDtuClient.Map.Range(func(key, value interface{}) bool {
|
|
|
v := value.(*iotStruct.DtuClient)
|
|
|
dtuSn := key.(string)
|
|
|
dtu, ok := iotMap.MapDtuDevice.Get(dtuSn)
|
|
|
if !ok || dtu.Config == nil {
|
|
|
- logger.Errorf("【在线设备更新配置】设备不存在 SN:%s", dtuSn)
|
|
|
+ dtu.Logger.Errorf("【在线设备更新配置】设备不存在 SN:%s", dtuSn)
|
|
|
return true
|
|
|
}
|
|
|
-
|
|
|
v.Config = dtu.Config
|
|
|
+ v.Logger = dtu.Logger
|
|
|
onlineDtu[dtuSn] = v
|
|
|
count++
|
|
|
return true
|
|
|
@@ -163,7 +163,7 @@ func collectAndReport(nowTime time.Time, lastMinute int) int {
|
|
|
if minute%client.Config.Cycle == 0 {
|
|
|
// 采集上报数据
|
|
|
if dataArray := collectData(dtuSn, client.Config); len(*dataArray) > 0 {
|
|
|
- logger.Debugf("开始上报平台:%s", dtuSn)
|
|
|
+ client.Logger.Debugf("开始上报平台:%s", dtuSn)
|
|
|
reportData(dataArray)
|
|
|
}
|
|
|
}
|