代码拉取完成,页面将自动刷新
package opc
import (
// "log"
// "fmt"
"math/rand"
"sync"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
//Embedded type for mock servers
type emptyServer struct{}
func (es *emptyServer) Add(...string) error { return nil }
func (es *emptyServer) Remove(string) {}
func (es *emptyServer) Write(string, interface{}) error { return nil }
func (es *emptyServer) Close() {}
func (es *emptyServer) Tags() []string { return []string{} }
//OpcMockServerStatic implements an OPC Server that returns the index value plus 1 for each tag.
type OpcMockServerStatic struct {
*emptyServer
TagList []string
}
func (oms *OpcMockServerStatic) ReadItem(tag string) Item {
items := oms.Read()
return items[tag]
}
func (oms *OpcMockServerStatic) Read() map[string]Item {
answer := make(map[string]Item)
for i, tag := range oms.TagList {
answer[tag] = Item{float64(i) + 1.0, OPCQualityGood, time.Now()}
}
return answer
}
//OpcMockServerRandom implements an OPC Server that returns a random value for each tag.
type OpcMockServerRandom struct {
*emptyServer
TagList []string
}
func (oms *OpcMockServerRandom) ReadItem(tag string) Item {
items := oms.Read()
return items[tag]
}
func (oms *OpcMockServerRandom) Read() map[string]Item {
answer := make(map[string]Item)
for _, tag := range oms.TagList {
answer[tag] = Item{rand.Float64(), OPCQualityGood, time.Now()}
}
return answer
}
//OpcMockServerWakeUp implements an OPC Server that returns 1.0 for a certain duration then a random value for each tag.
type OpcMockServerWakeUp struct {
*emptyServer
TagList []string
AtSleep bool
mu sync.Mutex
}
func (oms *OpcMockServerWakeUp) WakeUpAfter(sleep time.Duration) {
oms.AtSleep = true
go func() {
time.Sleep(sleep)
oms.mu.Lock()
oms.AtSleep = false
oms.mu.Unlock()
}()
}
func (oms *OpcMockServerWakeUp) ReadItem(tag string) Item {
items := oms.Read()
return items[tag]
}
func (oms *OpcMockServerWakeUp) Read() map[string]Item {
answer := make(map[string]Item)
oms.mu.Lock()
defer oms.mu.Unlock()
for _, tag := range oms.TagList {
if oms.AtSleep {
answer[tag] = Item{1.0, OPCQualityGood, time.Now()}
} else {
answer[tag] = Item{rand.Float64(), OPCQualityGood, time.Now()}
}
}
return answer
}
//FallAsleep Server, sets to 2.0 after time period (opposite of WakeUp server)
type OpcMockServerFallAsleep struct {
*emptyServer
TagList []string
AtSleep bool
mu sync.Mutex
}
func (oms *OpcMockServerFallAsleep) FallAsleepAfter(sleep time.Duration) {
oms.AtSleep = false
go func() {
time.Sleep(sleep)
oms.mu.Lock()
oms.AtSleep = true
oms.mu.Unlock()
}()
}
func (oms *OpcMockServerFallAsleep) ReadItem(tag string) Item {
items := oms.Read()
return items[tag]
}
func (oms *OpcMockServerFallAsleep) Read() map[string]Item {
answer := make(map[string]Item)
oms.mu.Lock()
defer oms.mu.Unlock()
for _, tag := range oms.TagList {
if oms.AtSleep {
answer[tag] = Item{2.0, OPCQualityGood, time.Now()}
} else {
answer[tag] = Item{rand.Float64(), OPCQualityGood, time.Now()}
}
}
return answer
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。