修改:1、完善通信接口模版的命令
This commit is contained in:
parent
e6cf5f7cd4
commit
601b00938a
|
@ -131,6 +131,7 @@ func ReadCollectInterfaceManageFromJson() bool {
|
|||
return false
|
||||
}
|
||||
|
||||
log.Printf("CollectInterfaceParamMap %+v\n",CollectInterfaceParamMap)
|
||||
for k, v := range CollectInterfaceParamMap.CollectInterfaceParam {
|
||||
|
||||
//创建接口实例
|
||||
|
@ -162,8 +163,8 @@ func DeviceNodeManageInit() {
|
|||
CommInterfaceInit()
|
||||
//采集接口
|
||||
if ReadCollectInterfaceManageFromJson() == true {
|
||||
log.Println("read interface json ok")
|
||||
|
||||
log.Println("read collectInterface json ok")
|
||||
//log.Printf("collectMInterfaceMap %+v\n",CollectInterfaceMap)
|
||||
} else {
|
||||
|
||||
for i := 0; i < MaxCollectInterfaceManage; i++ {
|
||||
|
|
|
@ -15,14 +15,12 @@ type CommunicationTemplate struct{
|
|||
Status bool `json:"-"` //接口状态
|
||||
}
|
||||
|
||||
type CommunicationInterfaceListTemplate struct{
|
||||
CommunicationInterfaceMap []CommunicationInterface
|
||||
}
|
||||
|
||||
var CommunicationInterfaceList CommunicationInterfaceListTemplate
|
||||
//通信接口Map
|
||||
var CommunicationInterfaceMap = make([]CommunicationInterface,0)
|
||||
|
||||
func CommInterfaceInit() {
|
||||
|
||||
//获取串口通信接口参数
|
||||
if ReadCommSerialInterfaceListFromJson() == false{
|
||||
|
||||
}else{
|
||||
|
@ -30,11 +28,12 @@ func CommInterfaceInit() {
|
|||
|
||||
for _,v := range CommunicationSerialMap{
|
||||
|
||||
CommunicationInterfaceList.CommunicationInterfaceMap = append(CommunicationInterfaceList.CommunicationInterfaceMap,&v)
|
||||
CommunicationInterfaceMap = append(CommunicationInterfaceMap,&v)
|
||||
}
|
||||
}
|
||||
|
||||
for _,v := range CommunicationInterfaceList.CommunicationInterfaceMap{
|
||||
//打开串口通信
|
||||
for _,v := range CommunicationInterfaceMap{
|
||||
|
||||
v.Open()
|
||||
}
|
||||
|
|
|
@ -1,5 +1,12 @@
|
|||
package device
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type TcpInterfaceParam struct{
|
||||
Name string `json:"Name"`
|
||||
IP string `json:"IP"`
|
||||
|
@ -8,27 +15,80 @@ type TcpInterfaceParam struct{
|
|||
Interval string `json:"Interval"` //通信间隔
|
||||
}
|
||||
|
||||
type CommunicationTcpInterface struct{
|
||||
type CommunicationTcpTemplate struct{
|
||||
CommunicationTemplate
|
||||
Param TcpInterfaceParam `json:"Param"` //接口参数
|
||||
}
|
||||
|
||||
func (c *CommunicationTcpInterface)Open() bool{
|
||||
var CommunicationTcpMap = make([]CommunicationTcpTemplate,0)
|
||||
|
||||
func (c *CommunicationTcpTemplate)Open() bool{
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *CommunicationTcpInterface)Close() bool{
|
||||
func (c *CommunicationTcpTemplate)Close() bool{
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *CommunicationTcpInterface)WriteData(data []byte) int{
|
||||
func (c *CommunicationTcpTemplate)WriteData(data []byte) int{
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *CommunicationTcpInterface)ReadData(data []byte) int{
|
||||
func (c *CommunicationTcpTemplate)ReadData(data []byte) int{
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func ReadCommTcpInterfaceListFromJson() bool {
|
||||
|
||||
exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||
fileDir := exeCurDir + "/selfpara/commTcpInterface.json"
|
||||
|
||||
if fileExist(fileDir) == true {
|
||||
fp, err := os.OpenFile(fileDir, os.O_RDONLY, 0777)
|
||||
if err != nil {
|
||||
log.Println("open commTcpInterface.json err", err)
|
||||
return false
|
||||
}
|
||||
defer fp.Close()
|
||||
|
||||
data := make([]byte, 20480)
|
||||
dataCnt, err := fp.Read(data)
|
||||
|
||||
err = json.Unmarshal(data[:dataCnt], &CommunicationTcpMap)
|
||||
if err != nil {
|
||||
log.Println("commTcpInterface unmarshal err", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
log.Println("commTcpInterface.json is not exist")
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func WriteCommTcpInterfaceListToJson() {
|
||||
|
||||
exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||
|
||||
fileDir := exeCurDir + "/selfpara/commTcpInterface.json"
|
||||
|
||||
fp, err := os.OpenFile(fileDir, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
|
||||
if err != nil {
|
||||
log.Println("open commTcpInterface.json err", err)
|
||||
return
|
||||
}
|
||||
defer fp.Close()
|
||||
|
||||
sJson, _ := json.Marshal(CommunicationTcpMap)
|
||||
|
||||
_, err = fp.Write(sJson)
|
||||
if err != nil {
|
||||
log.Println("write commTcpInterface.json err", err)
|
||||
}
|
||||
log.Println("write commTcpInterface.json sucess")
|
||||
}
|
11
main.go
11
main.go
|
@ -81,13 +81,6 @@ func main() {
|
|||
/**************httpserver初始化****************/
|
||||
// 默认启动方式,包含 Logger、Recovery 中间件
|
||||
|
||||
//serverCommon := &http.Server{
|
||||
// Addr: ":8091",
|
||||
// Handler: routerCommon(),
|
||||
// ReadTimeout: 5 * time.Second,
|
||||
// WriteTimeout: 10 * time.Second,
|
||||
//}
|
||||
|
||||
serverWeb := &http.Server{
|
||||
Addr: ":80",
|
||||
Handler: httpServer.RouterWeb(),
|
||||
|
@ -95,10 +88,6 @@ func main() {
|
|||
WriteTimeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
//g.Go(func() error {
|
||||
// return serverCommon.ListenAndServe()
|
||||
//})
|
||||
|
||||
g.Go(func() error {
|
||||
return serverWeb.ListenAndServe()
|
||||
})
|
||||
|
|
|
@ -1 +1 @@
|
|||
[{"Name":"com1","Type":"LocalSerial","Param":{"Name":"/dev/ttyUSB0","BaudRate":"9600","DataBits":"8","StopBits":"1","Parity":"N","Timeout":"1000","Interval":"300"}}]
|
||||
[{"Name":"com1","Type":"LocalSerial","Param":{"Name":"/dev/ttyUSB0","BaudRate":"9600","DataBits":"8","StopBits":"1","Parity":"N","Timeout":"1500","Interval":"500"}}]
|
|
@ -1 +0,0 @@
|
|||
{"DeviceInterfaceParam":[{"InterfaceID":0,"PollPeriod":60,"OfflinePeriod":180,"DeviceNodeCnt":2,"DeviceAddrMap":["1","2"],"DeviceTypeMap":["wdt200","wdt200"]},{"InterfaceID":1,"PollPeriod":60,"OfflinePeriod":180,"DeviceNodeCnt":0,"DeviceAddrMap":null,"DeviceTypeMap":null}]}
|
Loading…
Reference in New Issue