代码拉取完成,页面将自动刷新
package driver
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"time"
ncmodels "gitee.com/cmtba/nclink/models"
)
var axisChanSystemData = [][]interface{}{
{"X", 1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 35.7, 0, 0, 0, 1, 0, "0.0", "180UD-35A", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{"Y", 1, 0, 1, -1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 35.7, 0, 0, 0, 1, 0, "0.0", "180UD-35A", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{"Z", 1, 0, 2, -1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 35.7, 0, 0, 0, 1, 0, "0.0", "180UD-35A", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{},
{},
{"C", 10, 0, -1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 35.7, 0, 0, 0, 1, 0, "0.0", "180US-35A", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 39, 0, "CH0", 0, 0, 0, 0, 100, 25, -1, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 58, 58, 0, 0, nil, 810211375270854656, nil, 0, "X", "S", -1, 5, 5, 0, 0, 100, 1.62245919e-254, 0, 0, 0, 0, 0, 0, 0, 10000, 0, 0, nil, 0, 0, 0, 0},
{},
{},
{},
{1, 100000, 100000, 1, 1, 1, 0, 1.62191936e-254, 4, 2, 0, "274.40992", "BAA08", "12.40992", 0, 1, 0, 0, 0, 0, nil, nil, nil, 0, "2.41.00_a", "122FFD6BEF36C6E", "", "", "", "", "", "", 1, 810210962953994241, 0, nil, 8, 8, 8, 0, "0.0", "2.41.00_a", "0.0", ".2020-04-16 23:53:20", ".2020-04-16 23:53:20", "V1.0", "V1.0", "V1.0", "0.0", "V1.0"},
}
type sampleBuffer struct {
Status [1000]int
Axis [6][5][1000]float32
Prog [1000]int
Tool [1000]int
Line [1000]int
}
type progInfoItem struct {
name string
lineNumber uint32
}
type virtualMachine struct {
WorkDir string
PluginDir string
ProgDir string
ToolParam [20]toolParam
Crds [180]crd
Axes [6]axis
Regs [14][]uint32
RunState *runState
SampleHeartBeat uint64
SampleBuffer sampleBuffer
ProgList []progInfoItem
paramItems [][][]paramValue
isconnect bool
}
func copy(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}
func NewDevice() ncmodels.Machine {
device := &virtualMachine{}
return device
}
func (_this *virtualMachine) Get(request ncmodels.QueryRequest, privilegeLevel int) (*ncmodels.QueryResponse, bool) {
response := ncmodels.QueryResponse{}
response.CommandID = request.CommandID
for _, item := range request.Ids {
respItem := &item
if item.ID == "" {
respItem.Code = ncmodels.NG
respItem.Reason = ncmodels.Reason_Id_Not_Exists
} else if executor, ok := drivers[item.Node.NodePath()]; !ok || executor == nil || executor.Get == nil {
respItem.Code = ncmodels.NG
respItem.Reason = ncmodels.Reason_Debied
} else if executor.PrivilegeLevel < privilegeLevel {
respItem.Code = ncmodels.NG
respItem.Reason = ncmodels.Reason_Debied
} else {
respItem, _ = executor.Get(item, _this)
}
response.Values = append(response.Values, *respItem)
}
return &response, true
}
func (_this *virtualMachine) Set(request ncmodels.SetRequest, privilegeLevel int) (*ncmodels.SetResponse, bool) {
response := ncmodels.SetResponse{}
response.CommandID = request.CommandID
for _, item := range request.Values {
if item.ID == "" {
item.Code = ncmodels.NG
item.Reason = ncmodels.Reason_Id_Not_Exists
} else if leafNode, ok := item.Node.(ncmodels.LeafNode); !ok {
item.Code = ncmodels.NG
item.Reason = ncmodels.Reason_Not_Match
} else if !leafNode.NodeSetable() {
item.Code = ncmodels.NG
item.Reason = ncmodels.Reason_Debied
} else if executor, ok := drivers[item.Node.NodePath()]; !ok || executor == nil || executor.Set == nil {
item.Code = ncmodels.NG
item.Reason = ncmodels.Reason_Unspported
} else if executor.PrivilegeLevel > privilegeLevel {
item.Code = ncmodels.NG
item.Reason = ncmodels.Reason_Debied
} else {
newitem, _ := executor.Set(item, _this)
item = *newitem
}
response.Results = append(response.Results, item)
}
return &response, true
}
func (_this *virtualMachine) Sample(st *ncmodels.SampleTask) (*ncmodels.SampleData, bool) {
var n uint16 = 0
if st.CurTime == 0 {
st.CurTime = time.Now().UnixNano()
n = st.UploadInterval / st.SampleInterval
} else {
t1 := time.Now().UnixNano()
n = uint16((t1 - st.CurTime) / (int64(st.SampleInterval) * 1000000))
if n > 1000 {
n = 1000
}
st.CurTime = t1
}
sampleData := new(ncmodels.SampleData)
sampleData.ID = st.ID
sampleData.BeginTime = fmt.Sprintf("%d", st.CurTime/1000000)
if n < 1 {
n = 1
}
for i := 0; i < len(st.SampleItems); i++ {
dataRow := new(ncmodels.SampleDataItem)
driverItem, ok := drivers[st.SampleItems[i].Node.NodePath()]
if !ok || driverItem == nil || driverItem.Sample == nil {
dataRow.Data = nil
continue
}
dataRow.Data, _ = driverItem.Sample(uint64(n), _this)
sampleData.Data = append(sampleData.Data, *dataRow)
}
_this.SampleHeartBeat += uint64(n)
return sampleData, true
}
func (_this *virtualMachine) Connect(cfg string) bool {
cfgs := strings.Split(cfg, "\n")
for i := 0; i < len(cfgs); i++ {
items := strings.Split(cfgs[i], "=")
if len(items) == 2 {
key := strings.TrimSpace(items[0])
val := strings.TrimSpace(items[1])
switch key {
case "PLUGINDIR":
_this.PluginDir = val
case "WORKDIR":
_this.WorkDir = val
}
}
}
_this.ProgDir = _this.WorkDir + "/prog"
if fileinfo, err := os.Stat(_this.ProgDir); err != nil || !fileinfo.IsDir() {
err = os.MkdirAll(_this.ProgDir, os.ModePerm)
if err == nil {
defaultFile := _this.WorkDir + "/O1000.cn"
myfile := _this.ProgDir + "/O1000.cn"
copy(defaultFile, myfile)
}
}
if !_this.paramInit(_this.PluginDir) {
return false
}
for i := 0; i < len(_this.ToolParam); i++ {
_this.ToolParam[i].Length = new(float32)
_this.ToolParam[i].Radius = new(float32)
_this.ToolParam[i].LengthAbrasion = new(float32)
_this.ToolParam[i].RadiusAbrasion = new(float32)
}
for i := 0; i < len(_this.Crds); i++ {
_this.Crds[i].X = new(float32)
_this.Crds[i].Y = new(float32)
_this.Crds[i].Z = new(float32)
_this.Crds[i].C = new(float32)
}
_this.Axes[0].Name = "X"
_this.Axes[0].Type = "linear"
_this.Axes[0].Number = 0
_this.Axes[1].Name = "Y"
_this.Axes[1].Type = "linear"
_this.Axes[1].Number = 1
_this.Axes[2].Name = "Z"
_this.Axes[2].Type = "linear"
_this.Axes[2].Number = 2
_this.Axes[3].Name = "C"
_this.Axes[3].Type = "rotary"
_this.Axes[3].Number = 5
_this.Regs[0] = make([]uint32, 512) //x
_this.Regs[1] = make([]uint32, 512) //y
_this.Regs[2] = make([]uint32, 3728) //f
_this.Regs[3] = make([]uint32, 3728) //g
_this.Regs[4] = make([]uint32, 2048) //r
_this.Regs[5] = make([]uint32, 1722) //b
_this.Regs[6] = make([]uint32, 512) //w
_this.Regs[7] = make([]uint32, 512) //d
_this.Regs[8] = make([]uint32, 512) //P
_this.Regs[9] = make([]uint32, 512) //I
_this.Regs[10] = make([]uint32, 512) //Q
_this.Regs[11] = make([]uint32, 512) //K
_this.Regs[12] = make([]uint32, 512) //T
_this.Regs[13] = make([]uint32, 512) //C
_this.RunState = &runState{state: 1}
prefixLen := len(_this.ProgDir)
LineNumberOf := func(fileName string) uint32 {
file, err := os.Open(fileName)
if err != nil {
return 0
}
defer file.Close()
fd := bufio.NewReader(file)
count := uint32(0)
for {
_, err := fd.ReadString('\n')
if err != nil {
break
}
count++
}
return count
}
var IterateDir func(dir string) []progInfoItem
IterateDir = func(dir string) []progInfoItem {
files := make([]progInfoItem, 0)
fileInfos, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Printf("%s %s\n", dir, err.Error())
return files
}
for _, fileInfo := range fileInfos {
files = append(files, progInfoItem{(dir + fileInfo.Name())[prefixLen:], LineNumberOf(dir + fileInfo.Name())})
if fileInfo.IsDir() {
files = append(files, IterateDir(dir+fileInfo.Name())...)
}
}
return files
}
_this.ProgList = IterateDir(_this.ProgDir)
_this.isconnect = true
return true
}
func (_this *virtualMachine) DisConnect() {
_this.isconnect = false
}
func (_this *virtualMachine) IsConnect() bool {
return _this.isconnect
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。