162 lines
4.1 KiB
Go
162 lines
4.1 KiB
Go
package device
|
|
|
|
import (
|
|
"encoding/json"
|
|
lua "github.com/yuin/gopher-lua"
|
|
"goAdapter/setting"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
//最大设备模板数
|
|
var MaxDeviceNodeTypeCnt int = 10
|
|
|
|
type DeviceNodeTypeTemplate struct {
|
|
TemplateID int `json:"TemplateID"` //模板ID
|
|
TemplateName string `json:"TemplateName"` //模板名称
|
|
TemplateType string `json:"TemplateType"` //模板型号
|
|
TemplateMessage string `json:"TemplateMessage"` //备注信息
|
|
}
|
|
|
|
//配置参数
|
|
type DeviceNodeTypeMapStruct struct {
|
|
DeviceNodeType []DeviceNodeTypeTemplate
|
|
}
|
|
|
|
var DeviceNodeTypeMap = DeviceNodeTypeMapStruct{
|
|
DeviceNodeType : make([]DeviceNodeTypeTemplate,0),
|
|
}
|
|
|
|
//var DeviceTypePluginMap = make(map[int]*plugin.Plugin)
|
|
var DeviceTypePluginMap = make(map[int]*lua.LState)
|
|
|
|
func WriteDeviceNodeTypeMapToJson() {
|
|
|
|
exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
|
fileDir := exeCurDir + "/selfpara/deviceNodeType.json"
|
|
|
|
fp, err := os.OpenFile(fileDir, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
|
|
if err != nil {
|
|
log.Println("open DeviceNodeType.json err", err)
|
|
return
|
|
}
|
|
defer fp.Close()
|
|
|
|
sJson, _ := json.Marshal(DeviceNodeTypeMap)
|
|
|
|
_, err = fp.Write(sJson)
|
|
if err != nil {
|
|
log.Println("write DeviceNodeType.json err", err)
|
|
}
|
|
log.Println("write DeviceNodeType.json sucess")
|
|
}
|
|
|
|
func ReadDeviceNodeTypeMapFromJson() bool {
|
|
|
|
deviceTypeTemplate := struct {
|
|
TemplateName string `json:"TemplateName"` //模板名称
|
|
TemplateType string `json:"TemplateType"` //模板型号
|
|
TemplateMessage string `json:"TemplateMessage"` //备注信息
|
|
}{}
|
|
|
|
exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
|
//遍历json和so文件
|
|
pluginPath := exeCurDir + "/plugin"
|
|
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)
|
|
return false
|
|
}
|
|
defer fp.Close()
|
|
|
|
data := make([]byte, 2048)
|
|
dataCnt, err := fp.Read(data)
|
|
|
|
err = json.Unmarshal(data[:dataCnt], &deviceTypeTemplate)
|
|
if err != nil {
|
|
log.Println("deviceTypeTemplate unmarshal err", err)
|
|
return false
|
|
}
|
|
|
|
nodeType := DeviceNodeTypeTemplate{}
|
|
nodeType.TemplateID = len(DeviceNodeTypeMap.DeviceNodeType)
|
|
nodeType.TemplateType = deviceTypeTemplate.TemplateType
|
|
nodeType.TemplateName = deviceTypeTemplate.TemplateName
|
|
nodeType.TemplateMessage = deviceTypeTemplate.TemplateMessage
|
|
|
|
DeviceNodeTypeMap.DeviceNodeType = append(DeviceNodeTypeMap.DeviceNodeType,nodeType)
|
|
}
|
|
}
|
|
//打开lua文件
|
|
for k,v := range DeviceNodeTypeMap.DeviceNodeType{
|
|
for _,fileName := range fileNameMap{
|
|
if strings.Contains(fileName,".lua") {
|
|
if strings.Contains(fileName, v.TemplateType) {
|
|
template, err := setting.LuaOpenFile(fileName)
|
|
if err != nil {
|
|
log.Printf("openPlug %s err,%s\n",fileName,err)
|
|
}else{
|
|
log.Printf("openPlug %s ok\n", fileName)
|
|
}
|
|
DeviceTypePluginMap[k] = template
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//for k,v := range DeviceNodeTypeMap.DeviceNodeType{
|
|
// for _,fileName := range fileNameMap{
|
|
// if strings.Contains(fileName,".so") {
|
|
// if strings.Contains(fileName, v.TemplateType) {
|
|
// template, err := plugin.Open(fileName)
|
|
// if err != nil {
|
|
// log.Printf("openPlug err,%s\n", err)
|
|
// }else{
|
|
// log.Printf("openPlug %s ok\n", fileName)
|
|
// }
|
|
// DeviceTypePluginMap[k] = template
|
|
// }
|
|
// }
|
|
// }
|
|
//}
|
|
|
|
return true
|
|
}
|
|
|
|
func updataDeviceType(path string,fileName []string) ([]string,error){
|
|
|
|
rd,err := ioutil.ReadDir(path)
|
|
if err != nil{
|
|
log.Println("readDir err,",err)
|
|
return fileName,err
|
|
}
|
|
|
|
for _,fi := range rd{
|
|
if fi.IsDir(){
|
|
fullDir := path + "/" + fi.Name()
|
|
fileName,_ = updataDeviceType(fullDir,fileName)
|
|
}else{
|
|
fullName := path + "/" + fi.Name()
|
|
if strings.Contains(fi.Name(),".json"){
|
|
//log.Println("fullName ",fullName)
|
|
fileName = append(fileName,fullName)
|
|
}else if strings.Contains(fi.Name(),".lua"){
|
|
//log.Println("fullName ",fullName)
|
|
fileName = append(fileName,fullName)
|
|
}
|
|
}
|
|
}
|
|
|
|
return fileName,nil
|
|
}
|
|
|