修改:1、解决lua脚本中缺少GetCRCModbus CheckCRCModbus函数 2、commManage.go中AnalysisRx增加修改休眠10ms,解决cpu占用过高的问题

This commit is contained in:
pengwang 2021-02-02 21:28:35 +08:00
parent 740ceafbcb
commit ee81298899
8 changed files with 78 additions and 84 deletions

BIN
.DS_Store vendored

Binary file not shown.

4
.gitignore vendored
View File

@ -2,4 +2,6 @@ goAdapter
.idea .idea
plugin/ plugin/
config/ config/
selfpara/ selfpara/
log/
.DS_Store

View File

@ -83,6 +83,7 @@ func (c *CommunicationManageTemplate) AnalysisRx() {
//清除本次接收数据 //清除本次接收数据
rxBufCnt = 0 rxBufCnt = 0
} }
time.Sleep(10 * time.Millisecond)
} }
} }

View File

@ -23,17 +23,17 @@ type DeviceNodeTypeTemplate struct {
//配置参数 //配置参数
type DeviceNodeTypeMapStruct struct { type DeviceNodeTypeMapStruct struct {
DeviceNodeType []DeviceNodeTypeTemplate DeviceNodeType []DeviceNodeTypeTemplate
} }
type DeviceNodeTypeLuaState struct{ type DeviceNodeTypeLuaState struct {
LuaState *lua.LState LuaState *lua.LState
TypeName string TypeName string
CollName string CollName string
} }
var DeviceNodeTypeMap = DeviceNodeTypeMapStruct{ var DeviceNodeTypeMap = DeviceNodeTypeMapStruct{
DeviceNodeType : make([]DeviceNodeTypeTemplate,0), DeviceNodeType: make([]DeviceNodeTypeTemplate, 0),
} }
//var DeviceTypePluginMap = make(map[int]*plugin.Plugin) //var DeviceTypePluginMap = make(map[int]*plugin.Plugin)
@ -41,8 +41,7 @@ var DeviceTypePluginMap = make(map[int]*lua.LState)
//var DeviceTypePluginMap = make([]DeviceNodeTypeLuaState,0) //var DeviceTypePluginMap = make([]DeviceNodeTypeLuaState,0)
func init() {
func init(){
} }
@ -68,31 +67,31 @@ func WriteDeviceNodeTypeMapToJson() {
log.Println("write DeviceNodeType.json sucess") log.Println("write DeviceNodeType.json sucess")
} }
func updataDeviceType(path string,fileName []string) ([]string,error){ func updataDeviceType(path string, fileName []string) ([]string, error) {
rd,err := ioutil.ReadDir(path) rd, err := ioutil.ReadDir(path)
if err != nil{ if err != nil {
log.Println("readDir err,",err) log.Println("readDir err,", err)
return fileName,err return fileName, err
} }
for _,fi := range rd{ for _, fi := range rd {
if fi.IsDir(){ if fi.IsDir() {
fullDir := path + "/" + fi.Name() fullDir := path + "/" + fi.Name()
fileName,_ = updataDeviceType(fullDir,fileName) fileName, _ = updataDeviceType(fullDir, fileName)
}else{ } else {
fullName := path + "/" + fi.Name() fullName := path + "/" + fi.Name()
if strings.Contains(fi.Name(),".json"){ if strings.Contains(fi.Name(), ".json") {
//log.Println("fullName ",fullName) //log.Println("fullName ",fullName)
fileName = append(fileName,fullName) fileName = append(fileName, fullName)
}else if strings.Contains(fi.Name(),".lua"){ } else if strings.Contains(fi.Name(), ".lua") {
//log.Println("fullName ",fullName) //log.Println("fullName ",fullName)
fileName = append(fileName,fullName) fileName = append(fileName, fullName)
} }
} }
} }
return fileName,nil return fileName, nil
} }
func ReadDeviceNodeTypeMap() bool { func ReadDeviceNodeTypeMap() bool {
@ -107,10 +106,10 @@ func ReadDeviceNodeTypeMap() bool {
//遍历json和so文件 //遍历json和so文件
pluginPath := exeCurDir + "/plugin" pluginPath := exeCurDir + "/plugin"
fileNameMap := make([]string,0) fileNameMap := make([]string, 0)
fileNameMap,_ = updataDeviceType(pluginPath,fileNameMap) fileNameMap, _ = updataDeviceType(pluginPath, fileNameMap)
for _,v := range fileNameMap{ for _, v := range fileNameMap {
if strings.Contains(v,".json"){ if strings.Contains(v, ".json") {
fp, err := os.OpenFile(v, os.O_RDONLY, 0777) fp, err := os.OpenFile(v, os.O_RDONLY, 0777)
if err != nil { if err != nil {
log.Printf("open %s err", v) log.Printf("open %s err", v)
@ -133,7 +132,7 @@ func ReadDeviceNodeTypeMap() bool {
nodeType.TemplateName = deviceTypeTemplate.TemplateName nodeType.TemplateName = deviceTypeTemplate.TemplateName
nodeType.TemplateMessage = deviceTypeTemplate.TemplateMessage nodeType.TemplateMessage = deviceTypeTemplate.TemplateMessage
DeviceNodeTypeMap.DeviceNodeType = append(DeviceNodeTypeMap.DeviceNodeType,nodeType) DeviceNodeTypeMap.DeviceNodeType = append(DeviceNodeTypeMap.DeviceNodeType, nodeType)
} }
} }
@ -149,6 +148,8 @@ func ReadDeviceNodeTypeMap() bool {
log.Printf("openPlug %s ok\n", fileName) log.Printf("openPlug %s ok\n", fileName)
} }
DeviceTypePluginMap[k] = template DeviceTypePluginMap[k] = template
DeviceTypePluginMap[k].SetGlobal("GetCRCModbus", DeviceTypePluginMap[k].NewFunction(setting.GetCRCModbus))
DeviceTypePluginMap[k].SetGlobal("CheckCRCModbus", DeviceTypePluginMap[k].NewFunction(setting.CheckCRCModbus))
} }
} }
} }
@ -189,6 +190,3 @@ func ReadDeviceNodeTypeMap() bool {
// } // }
// } // }
//} //}

1
go.mod
View File

@ -32,7 +32,6 @@ require (
github.com/yuin/gluamapper v0.0.0-20150323120927-d836955830e7 github.com/yuin/gluamapper v0.0.0-20150323120927-d836955830e7
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da
golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f // indirect golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f // indirect
gopkg.in/ini.v1 v1.57.0 gopkg.in/ini.v1 v1.57.0
layeh.com/gopher-luar v1.0.8 layeh.com/gopher-luar v1.0.8

2
go.sum
View File

@ -114,8 +114,6 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200625001655-4c5254603344 h1:vGXIOMxbNfDTk/aXCmfdLgkrSV+Z2tcbze+pEc3v5W4= golang.org/x/net v0.0.0-20200625001655-4c5254603344 h1:vGXIOMxbNfDTk/aXCmfdLgkrSV+Z2tcbze+pEc3v5W4=
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 h1:qwRHBd0NqMbJxfbotnDhm2ByMI1Shq4Y6oRJo21SGJA=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View File

@ -205,8 +205,5 @@ func LuaOpenFile(filePath string) (*lua.LState, error) {
//加载Lua //加载Lua
err := lState.DoFile(filePath) err := lState.DoFile(filePath)
lState.SetGlobal("GetCRCModbus", lState.NewFunction(GetCRCModbus))
lState.SetGlobal("CheckCRCModbus", lState.NewFunction(CheckCRCModbus))
return lState, err return lState, err
} }

View File

@ -19,17 +19,17 @@ type NetworkNameListTemplate struct {
Name []string `json:"Name"` Name []string `json:"Name"`
} }
type NetworkParamTemplate struct{ type NetworkParamTemplate struct {
Name string `json:"Name"` Name string `json:"Name"`
DHCP string `json:"DHCP"` DHCP string `json:"DHCP"`
IP string `json:"IP"` IP string `json:"IP"`
Netmask string `json:"Netmask"` Netmask string `json:"Netmask"`
Broadcast string `json:"Broadcast"` Broadcast string `json:"Broadcast"`
MAC string `json:"MAC"` MAC string `json:"MAC"`
LinkStatus uint32 `json:"-"` LinkStatus uint32 `json:"-"`
} }
type NetworkParamListTemplate struct{ type NetworkParamListTemplate struct {
NetworkParam []*NetworkParamTemplate NetworkParam []*NetworkParamTemplate
} }
@ -43,34 +43,33 @@ type NetInformation struct {
GatewayIP string GatewayIP string
} }
var NetworkNameList = NetworkNameListTemplate{} var NetworkNameList = NetworkNameListTemplate{}
var NetworkParamList = &NetworkParamListTemplate{ var NetworkParamList = &NetworkParamListTemplate{
NetworkParam: make([]*NetworkParamTemplate,0), NetworkParam: make([]*NetworkParamTemplate, 0),
} }
func init(){ func init() {
} }
func (n *NetworkParamListTemplate)CreatNetworkPara(name string){ func (n *NetworkParamListTemplate) CreatNetworkPara(name string) {
networkParam := &NetworkParamTemplate{ networkParam := &NetworkParamTemplate{
Name:name, Name: name,
DHCP:"1", DHCP: "1",
} }
n.NetworkParam = append(n.NetworkParam,networkParam) n.NetworkParam = append(n.NetworkParam, networkParam)
} }
//获取当前网络参数 //获取当前网络参数
func (n *NetworkParamListTemplate)GetNetworkParam(){ func (n *NetworkParamListTemplate) GetNetworkParam() {
for _,v := range n.NetworkParam{ for _, v := range n.NetworkParam {
ethInfo,err := GetNetInformation(v.Name) ethInfo, err := GetNetInformation(v.Name)
if err == nil{ if err == nil {
v.IP = ethInfo.IP v.IP = ethInfo.IP
v.Netmask = ethInfo.Mask v.Netmask = ethInfo.Mask
v.Broadcast = ethInfo.GatewayIP v.Broadcast = ethInfo.GatewayIP
@ -81,10 +80,10 @@ func (n *NetworkParamListTemplate)GetNetworkParam(){
} }
//设置网络参数 //设置网络参数
func (n *NetworkParamListTemplate)SetNetworkParam(param NetworkParamTemplate) { func (n *NetworkParamListTemplate) SetNetworkParam(param NetworkParamTemplate) {
for _,v := range n.NetworkParam{ for _, v := range n.NetworkParam {
if v.Name == param.Name{ if v.Name == param.Name {
v.DHCP = param.DHCP v.DHCP = param.DHCP
v.IP = param.IP v.IP = param.IP
v.Netmask = param.Netmask v.Netmask = param.Netmask
@ -95,7 +94,7 @@ func (n *NetworkParamListTemplate)SetNetworkParam(param NetworkParamTemplate) {
NetworkParaWrite() NetworkParaWrite()
} }
func (n *NetworkParamTemplate)GetNetworkStatus(){ func (n *NetworkParamTemplate) GetNetworkStatus() {
ethHandle, _ := ethtool.NewEthtool() ethHandle, _ := ethtool.NewEthtool()
defer ethHandle.Close() defer ethHandle.Close()
@ -103,23 +102,23 @@ func (n *NetworkParamTemplate)GetNetworkStatus(){
n.LinkStatus, _ = ethHandle.LinkState(n.Name) n.LinkStatus, _ = ethHandle.LinkState(n.Name)
} }
func (n *NetworkParamTemplate)CmdSetDHCP(){ func (n *NetworkParamTemplate) CmdSetDHCP() {
//cmd := exec.Command("udhcpc","-i",n.Name,"5") //cmd := exec.Command("udhcpc","-i",n.Name,"5")
cmd := exec.Command("udhcpc","-i",n.Name) cmd := exec.Command("udhcpc", "-i", n.Name)
var out bytes.Buffer var out bytes.Buffer
cmd.Stdout = &out cmd.Stdout = &out
cmd.Run() //执行到此会阻塞5s cmd.Run() //执行到此会阻塞5s
str := out.String() str := out.String()
log.Println(str) log.Println(str)
} }
func (n *NetworkParamTemplate)CmdSetStaticIP(){ func (n *NetworkParamTemplate) CmdSetStaticIP() {
strNetMask := "netmask " + n.Netmask strNetMask := "netmask " + n.Netmask
cmd := exec.Command("ifconfig", cmd := exec.Command("ifconfig",
n.Name, n.Name,
n.IP, n.IP,
@ -127,11 +126,11 @@ func (n *NetworkParamTemplate)CmdSetStaticIP(){
var out bytes.Buffer var out bytes.Buffer
cmd.Stdout = &out cmd.Stdout = &out
cmd.Start() //执行到此,直接往后执行 cmd.Start() //执行到此,直接往后执行
cmd2 := exec.Command("/sbin/route","add","default","gw",n.Broadcast) cmd2 := exec.Command("/sbin/route", "add", "default", "gw", n.Broadcast)
cmd2.Stdout = &out cmd2.Stdout = &out
cmd2.Start() //执行到此,直接往后执行 cmd2.Start() //执行到此,直接往后执行
} }
func findNetCard(name string) (string, error) { func findNetCard(name string) (string, error) {
@ -234,10 +233,10 @@ func NetworkParaRead() bool {
return false return false
} }
for _,v := range NetworkParamList.NetworkParam{ for _, v := range NetworkParamList.NetworkParam {
if v.DHCP == "1"{ if v.DHCP == "1" {
v.CmdSetDHCP() v.CmdSetDHCP()
}else if v.DHCP == "0"{ } else if v.DHCP == "0" {
v.CmdSetStaticIP() v.CmdSetStaticIP()
} }
} }
@ -246,19 +245,19 @@ func NetworkParaRead() bool {
} else { } else {
fmt.Println("networkpara.json is not exist") fmt.Println("networkpara.json is not exist")
os.MkdirAll(exeCurDir+"/selfpara", os.ModePerm) //os.MkdirAll(exeCurDir+"/selfpara", os.ModePerm)
fp, err := os.Create(fileDir) //fp, err := os.Create(fileDir)
if err != nil { //if err != nil {
fmt.Println("create networkpara.json err", err) // fmt.Println("create networkpara.json err", err)
return false // return false
} //}
defer fp.Close() //defer fp.Close()
//
log.Printf("networkName %v\n",NetworkNameList) //log.Printf("networkName %v\n",NetworkNameList)
for _,v := range NetworkNameList.Name{ //for _,v := range NetworkNameList.Name{
NetworkParamList.CreatNetworkPara(v) // NetworkParamList.CreatNetworkPara(v)
NetworkParaWrite() // NetworkParaWrite()
} //}
return true return true
} }
@ -285,4 +284,4 @@ func NetworkParaWrite() {
} }
fmt.Println("write networkpara.json ok") fmt.Println("write networkpara.json ok")
fp.Sync() fp.Sync()
} }