修改: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.

2
.gitignore vendored
View File

@ -3,3 +3,5 @@ goAdapter
plugin/
config/
selfpara/
log/
.DS_Store

View File

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

View File

@ -26,14 +26,14 @@ type DeviceNodeTypeMapStruct struct {
DeviceNodeType []DeviceNodeTypeTemplate
}
type DeviceNodeTypeLuaState struct{
type DeviceNodeTypeLuaState struct {
LuaState *lua.LState
TypeName string
CollName string
}
var DeviceNodeTypeMap = DeviceNodeTypeMapStruct{
DeviceNodeType : make([]DeviceNodeTypeTemplate,0),
DeviceNodeType: make([]DeviceNodeTypeTemplate, 0),
}
//var DeviceTypePluginMap = make(map[int]*plugin.Plugin)
@ -41,8 +41,7 @@ var DeviceTypePluginMap = make(map[int]*lua.LState)
//var DeviceTypePluginMap = make([]DeviceNodeTypeLuaState,0)
func init(){
func init() {
}
@ -68,31 +67,31 @@ func WriteDeviceNodeTypeMapToJson() {
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)
if err != nil{
log.Println("readDir err,",err)
return fileName,err
rd, err := ioutil.ReadDir(path)
if err != nil {
log.Println("readDir err,", err)
return fileName, err
}
for _,fi := range rd{
if fi.IsDir(){
for _, fi := range rd {
if fi.IsDir() {
fullDir := path + "/" + fi.Name()
fileName,_ = updataDeviceType(fullDir,fileName)
}else{
fileName, _ = updataDeviceType(fullDir, fileName)
} else {
fullName := path + "/" + fi.Name()
if strings.Contains(fi.Name(),".json"){
if strings.Contains(fi.Name(), ".json") {
//log.Println("fullName ",fullName)
fileName = append(fileName,fullName)
}else if strings.Contains(fi.Name(),".lua"){
fileName = append(fileName, fullName)
} else if strings.Contains(fi.Name(), ".lua") {
//log.Println("fullName ",fullName)
fileName = append(fileName,fullName)
fileName = append(fileName, fullName)
}
}
}
return fileName,nil
return fileName, nil
}
func ReadDeviceNodeTypeMap() bool {
@ -107,10 +106,10 @@ func ReadDeviceNodeTypeMap() bool {
//遍历json和so文件
pluginPath := exeCurDir + "/plugin"
fileNameMap := make([]string,0)
fileNameMap,_ = updataDeviceType(pluginPath,fileNameMap)
for _,v := range fileNameMap{
if strings.Contains(v,".json"){
fileNameMap := make([]string, 0)
fileNameMap, _ = updataDeviceType(pluginPath, fileNameMap)
for _, v := range fileNameMap {
if strings.Contains(v, ".json") {
fp, err := os.OpenFile(v, os.O_RDONLY, 0777)
if err != nil {
log.Printf("open %s err", v)
@ -133,7 +132,7 @@ func ReadDeviceNodeTypeMap() bool {
nodeType.TemplateName = deviceTypeTemplate.TemplateName
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)
}
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/gopher-lua v0.0.0-20200816102855-ee81675732da
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
gopkg.in/ini.v1 v1.57.0
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-20200625001655-4c5254603344 h1:vGXIOMxbNfDTk/aXCmfdLgkrSV+Z2tcbze+pEc3v5W4=
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-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
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
err := lState.DoFile(filePath)
lState.SetGlobal("GetCRCModbus", lState.NewFunction(GetCRCModbus))
lState.SetGlobal("CheckCRCModbus", lState.NewFunction(CheckCRCModbus))
return lState, err
}

View File

@ -19,7 +19,7 @@ type NetworkNameListTemplate struct {
Name []string `json:"Name"`
}
type NetworkParamTemplate struct{
type NetworkParamTemplate struct {
Name string `json:"Name"`
DHCP string `json:"DHCP"`
IP string `json:"IP"`
@ -29,7 +29,7 @@ type NetworkParamTemplate struct{
LinkStatus uint32 `json:"-"`
}
type NetworkParamListTemplate struct{
type NetworkParamListTemplate struct {
NetworkParam []*NetworkParamTemplate
}
@ -45,32 +45,31 @@ type NetInformation struct {
var NetworkNameList = NetworkNameListTemplate{}
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{
Name:name,
DHCP:"1",
Name: name,
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.Netmask = ethInfo.Mask
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{
if v.Name == param.Name{
for _, v := range n.NetworkParam {
if v.Name == param.Name {
v.DHCP = param.DHCP
v.IP = param.IP
v.Netmask = param.Netmask
@ -95,7 +94,7 @@ func (n *NetworkParamListTemplate)SetNetworkParam(param NetworkParamTemplate) {
NetworkParaWrite()
}
func (n *NetworkParamTemplate)GetNetworkStatus(){
func (n *NetworkParamTemplate) GetNetworkStatus() {
ethHandle, _ := ethtool.NewEthtool()
defer ethHandle.Close()
@ -103,10 +102,10 @@ func (n *NetworkParamTemplate)GetNetworkStatus(){
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)
cmd := exec.Command("udhcpc", "-i", n.Name)
var out bytes.Buffer
cmd.Stdout = &out
@ -117,7 +116,7 @@ func (n *NetworkParamTemplate)CmdSetDHCP(){
log.Println(str)
}
func (n *NetworkParamTemplate)CmdSetStaticIP(){
func (n *NetworkParamTemplate) CmdSetStaticIP() {
strNetMask := "netmask " + n.Netmask
cmd := exec.Command("ifconfig",
@ -129,7 +128,7 @@ func (n *NetworkParamTemplate)CmdSetStaticIP(){
cmd.Stdout = &out
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.Start() //执行到此,直接往后执行
}
@ -234,10 +233,10 @@ func NetworkParaRead() bool {
return false
}
for _,v := range NetworkParamList.NetworkParam{
if v.DHCP == "1"{
for _, v := range NetworkParamList.NetworkParam {
if v.DHCP == "1" {
v.CmdSetDHCP()
}else if v.DHCP == "0"{
} else if v.DHCP == "0" {
v.CmdSetStaticIP()
}
}
@ -246,19 +245,19 @@ func NetworkParaRead() bool {
} else {
fmt.Println("networkpara.json is not exist")
os.MkdirAll(exeCurDir+"/selfpara", os.ModePerm)
fp, err := os.Create(fileDir)
if err != nil {
fmt.Println("create networkpara.json err", err)
return false
}
defer fp.Close()
log.Printf("networkName %v\n",NetworkNameList)
for _,v := range NetworkNameList.Name{
NetworkParamList.CreatNetworkPara(v)
NetworkParaWrite()
}
//os.MkdirAll(exeCurDir+"/selfpara", os.ModePerm)
//fp, err := os.Create(fileDir)
//if err != nil {
// fmt.Println("create networkpara.json err", err)
// return false
//}
//defer fp.Close()
//
//log.Printf("networkName %v\n",NetworkNameList)
//for _,v := range NetworkNameList.Name{
// NetworkParamList.CreatNetworkPara(v)
// NetworkParaWrite()
//}
return true
}