修改:1、优化tcpClient通信接口,链接断掉时可以重连

This commit is contained in:
pengwang 2021-05-18 22:34:56 +08:00
parent e9bfbddadc
commit bbf8e25307
7 changed files with 62 additions and 32 deletions

3
.gitignore vendored
View File

@ -4,4 +4,5 @@ plugin/
config/
selfpara/
log/
.DS_Store
.DS_Store
openGW

View File

@ -192,7 +192,6 @@ func DeviceNodeManageInit() {
//采集接口
if ReadCollectInterfaceManageFromJson() == true {
setting.Logger.Debugf("read collectInterface json ok")
//log.Printf("collectMInterfaceMap %+v\n",CollectInterfaceMap)
} else {
}

View File

@ -1,5 +1,7 @@
package device
import "goAdapter/setting"
type CommunicationInterface interface {
Open() bool
Close() bool
@ -10,6 +12,12 @@ type CommunicationInterface interface {
GetInterval() string
}
type CommunicationInterfaceTemplate struct {
Name string `json:"Name"` // 接口名称
Type string `json:"Type"` // 接口类型,比如serial,tcp,udp,http
Param interface{} `json:"Param"` // 接口参数
}
type CommunicationTemplate struct {
Name string `json:"Name"` //接口名称
Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http
@ -34,8 +42,8 @@ func CommInterfaceInit() {
CommunicationInterfaceMap = append(CommunicationInterfaceMap, v)
}
}
for _, v := range CommunicationInterfaceMap {
setting.Logger.Debugf("commName %v,", v.GetName())
v.Open()
}
}

View File

@ -95,8 +95,8 @@ func (c *CommunicationManageTemplate) AnalysisRx() {
//阻塞读
rxBufCnt = c.CollInterface.CommInterface.ReadData(rxBuf)
if rxBufCnt > 0 {
//setting.Logger.Debugf("curRxBufCnt %v,", rxBufCnt)
//setting.Logger.Debugf("CurRxBuf %X\n", rxBuf[:rxBufCnt])
setting.Logger.Debugf("curRxBufCnt %v,", rxBufCnt)
setting.Logger.Debugf("CurRxBuf %X\n", rxBuf[:rxBufCnt])
//rxTotalBufCnt += rxBufCnt
//追加接收的数据到接收缓冲区
@ -128,13 +128,13 @@ func (c *CommunicationManageTemplate) CommunicationStateMachine(cmd Communicatio
if cmd.FunName == "GetDeviceRealVariables" {
txBuf, ok, con = v.GenerateGetRealVariables(v.Addr, step)
if ok == false {
setting.Logger.Errorf("%v:DeviceCustomCmd false", c.CollInterface.CollInterfaceName)
setting.Logger.Errorf("%v:GetRealVariables complete", c.CollInterface.CollInterfaceName)
goto LoopCommon
}
} else {
txBuf, ok, con = v.DeviceCustomCmd(v.Addr, cmd.FunName, cmd.FunPara, step)
if ok == false {
setting.Logger.Errorf("%v:DeviceCustomCmd false", c.CollInterface.CollInterfaceName)
setting.Logger.Errorf("%v:DeviceCustomCmd complete", c.CollInterface.CollInterfaceName)
goto LoopCommon
}
}

View File

@ -7,6 +7,7 @@ import (
"net"
"os"
"path/filepath"
"time"
)
type TcpInterfaceParam struct {
@ -26,10 +27,12 @@ type CommunicationTcpTemplate struct {
var CommunicationTcpMap = make([]*CommunicationTcpTemplate, 0)
func (c *CommunicationTcpTemplate) Open() bool {
conn, err := net.Dial("tcp", c.Param.IP+":"+c.Param.Port)
conn, err := net.DialTimeout("tcp", c.Param.IP+":"+c.Param.Port, 2*time.Second)
if err != nil {
setting.Logger.Errorf("%s,tcp open err,%v", c.Name, err)
//setting.Logger.Errorf("%s,tcp open err,%v", c.Name, err)
return false
} else {
setting.Logger.Debugf("%s,tcp open ok", c.Name)
}
c.Conn = conn
return true
@ -51,6 +54,11 @@ func (c *CommunicationTcpTemplate) WriteData(data []byte) int {
cnt, err := c.Conn.Write(data)
if err != nil {
setting.Logger.Errorf("%s,tcp write err,%v", c.Name, err)
err = c.Conn.Close()
if err != nil {
setting.Logger.Errorf("%s,tcp close err,%v", c.Name, err)
}
c.Open()
return 0
}
return cnt
@ -62,8 +70,9 @@ func (c *CommunicationTcpTemplate) ReadData(data []byte) int {
if c.Conn != nil {
cnt, err := c.Conn.Read(data)
//setting.Logger.Debugf("%s,tcp read data cnt %v", c.Name, cnt)
if err != nil {
setting.Logger.Errorf("%s,tcp read err,%v", c.Name, err)
//setting.Logger.Errorf("%s,tcp read err,%v", c.Name, err)
return 0
}
return cnt
@ -131,5 +140,5 @@ func WriteCommTcpInterfaceListToJson() {
if err != nil {
log.Println("write commTcpInterface.json err", err)
}
log.Println("write commTcpInterface.json sucess")
setting.Logger.Infof("write commTcpInterface.json sucess")
}

View File

@ -766,11 +766,14 @@ func apiAddCommInterface(context *gin.Context) {
n, _ := context.Request.Body.Read(bodyBuf)
// fmt.Println(string(bodyBuf[:n]))
var Param json.RawMessage
interfaceInfo := struct {
Name string `json:"Name"` // 接口名称
Type string `json:"Type"` // 接口类型,比如serial,tcp,udp,http
Param *json.RawMessage `json:"Param"`
}{}
}{
Param: &Param,
}
err := json.Unmarshal(bodyBuf[:n], &interfaceInfo)
if err != nil {
@ -783,35 +786,35 @@ func apiAddCommInterface(context *gin.Context) {
return
}
// log.Printf("info %+v\n",interfaceInfo)
// log.Printf("type %+v\n",reflect.TypeOf(interfaceInfo.Param))
// switch t:= interfaceInfo.Param.(type){
// case device.SerialInterfaceParam:
// log.Printf("param %+v\n",t)
// device.CommInterfaceList.AddCommInterface(t.Name,t.Type,t.Param)
// default:
// aParam.Code = "1"
// aParam.Message = "param is noexist"
// aParam.Data = ""
// sJson,_ := json.Marshal(aParam)
// context.String(http.StatusOK,string(sJson))
// }
var msg json.RawMessage
switch interfaceInfo.Type {
case "serial":
serial := &device.SerialInterfaceParam{}
err := json.Unmarshal(msg, serial)
err := json.Unmarshal(Param, serial)
if err != nil {
setting.Logger.Errorf("CommunicationSerialInterface json unMarshall err,", err)
break
}
setting.Logger.Debugf("type %+v\n", serial)
// device.CommInterfaceList.AddCommInterface(serial.Name,serial.Type,serial.Param)
case "tcp":
}
case "tcpClient":
tcp := device.TcpInterfaceParam{}
err := json.Unmarshal(Param, &tcp)
if err != nil {
setting.Logger.Errorf("CommunicationTcpInterface json unMarshall err,%v", err)
break
}
setting.Logger.Debugf("type %+v\n", tcp)
TcpInterface := &device.CommunicationTcpTemplate{
Param: tcp,
CommunicationTemplate: device.CommunicationTemplate{
Name: interfaceInfo.Name,
Type: interfaceInfo.Type,
},
}
// device.WriteCommInterfaceListToJson()
device.CommunicationTcpMap = append(device.CommunicationTcpMap, TcpInterface)
device.WriteCommTcpInterfaceListToJson()
}
aParam.Code = "0"
aParam.Message = ""
@ -848,7 +851,17 @@ func apiGetCommInterface(context *gin.Context) {
aParam.Code = "0"
aParam.Message = ""
for _, v := range device.CommunicationSerialMap {
CommunicationInterface := CommunicationInterfaceTemplate{
Name: v.Name,
Type: v.Type,
Param: v.Param,
}
CommunicationInterfaceManage.InterfaceCnt++
CommunicationInterfaceManage.InterfaceMap = append(CommunicationInterfaceManage.InterfaceMap,
CommunicationInterface)
}
for _, v := range device.CommunicationTcpMap {
CommunicationInterface := CommunicationInterfaceTemplate{
Name: v.Name,
Type: v.Type,

View File

@ -6,6 +6,6 @@
"SURVEY_ADDR": "http://47.103.45.84:9004/diaowen/survey!answerSurveryMobile.action?surveyId=",
"REPORT_ADDR": "http://139.196.207.46:443/",
"BASE_TITLE": "开源版",
"HOME_TITLE": "通用采集器系统登录(QQ:1028704210)",
"HOME_TITLE": "openGW系统登录(QQ:1028704210)",
"OPEN_SOURCE": false
}