-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
322 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/wendy512/go-iecp5/asdu" | ||
"github.com/wendy512/go-iecp5/clog" | ||
"github.com/wendy512/go-iecp5/cs104" | ||
"strconv" | ||
) | ||
|
||
// Settings 连接配置 | ||
type Settings struct { | ||
Host string | ||
Port int | ||
Cfg104 *cs104.Config //104协议规范配置 | ||
Params *asdu.Params //ASDU相关特定参数 | ||
LogCfg *LogCfg | ||
} | ||
|
||
type LogCfg struct { | ||
Enable bool //是否开启log | ||
LogProvider clog.LogProvider | ||
} | ||
|
||
type Server struct { | ||
settings *Settings | ||
cs104Server *cs104.Server | ||
} | ||
|
||
func NewSettings() *Settings { | ||
cfg104 := cs104.DefaultConfig() | ||
return &Settings{ | ||
Host: "localhost", | ||
Port: 2404, | ||
Cfg104: &cfg104, | ||
Params: asdu.ParamsWide, | ||
} | ||
} | ||
|
||
func New(settings *Settings, handler CommandHandler) *Server { | ||
cs104Server := cs104.NewServer(&serverHandler{h: handler}) | ||
cs104Server.SetConfig(*settings.Cfg104) | ||
cs104Server.SetParams(settings.Params) | ||
|
||
logCfg := settings.LogCfg | ||
if logCfg != nil { | ||
cs104Server.LogMode(logCfg.Enable) | ||
cs104Server.SetLogProvider(logCfg.LogProvider) | ||
} | ||
|
||
return &Server{ | ||
settings: settings, | ||
cs104Server: cs104Server, | ||
} | ||
} | ||
|
||
func (s *Server) Start() { | ||
addr := s.settings.Host + ":" + strconv.Itoa(s.settings.Port) | ||
go s.cs104Server.ListenAndServer(addr) | ||
} | ||
|
||
func (s *Server) Stop() { | ||
_ = s.cs104Server.Close() | ||
} | ||
|
||
// SetOnConnectionHandler set on connect handler | ||
func (s *Server) SetOnConnectionHandler(f func(asdu.Connect)) { | ||
s.SetOnConnectionHandler(f) | ||
} | ||
|
||
// SetConnectionLostHandler set connect lost handler | ||
func (s *Server) SetConnectionLostHandler(f func(asdu.Connect)) { | ||
s.SetConnectionLostHandler(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/wendy512/go-iecp5/asdu" | ||
"time" | ||
) | ||
|
||
type serverHandler struct { | ||
h CommandHandler | ||
} | ||
|
||
func (s *serverHandler) InterrogationHandler(conn asdu.Connect, pack *asdu.ASDU, quality asdu.QualifierOfInterrogation) error { | ||
return s.h.OnInterrogation(conn, pack, quality) | ||
} | ||
|
||
func (s *serverHandler) CounterInterrogationHandler(conn asdu.Connect, pack *asdu.ASDU, quality asdu.QualifierCountCall) error { | ||
return s.h.OnCounterInterrogation(conn, pack, quality) | ||
} | ||
|
||
func (s *serverHandler) ReadHandler(conn asdu.Connect, pack *asdu.ASDU, addr asdu.InfoObjAddr) error { | ||
return s.h.OnRead(conn, pack, addr) | ||
} | ||
|
||
func (s *serverHandler) ClockSyncHandler(conn asdu.Connect, pack *asdu.ASDU, time time.Time) error { | ||
return s.h.OnClockSync(conn, pack, time) | ||
} | ||
|
||
func (s *serverHandler) ResetProcessHandler(conn asdu.Connect, pack *asdu.ASDU, quality asdu.QualifierOfResetProcessCmd) error { | ||
return s.h.OnResetProcess(conn, pack, quality) | ||
} | ||
|
||
func (s *serverHandler) DelayAcquisitionHandler(conn asdu.Connect, pack *asdu.ASDU, msec uint16) error { | ||
return s.h.OnDelayAcquisition(conn, pack, msec) | ||
} | ||
|
||
func (s *serverHandler) ASDUHandler(conn asdu.Connect, pack *asdu.ASDU) error { | ||
return s.h.OnASDU(conn, pack) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/wendy512/go-iecp5/asdu" | ||
"time" | ||
) | ||
|
||
type CommandHandler interface { | ||
// OnInterrogation 总召唤请求 | ||
OnInterrogation(asdu.Connect, *asdu.ASDU, asdu.QualifierOfInterrogation) error | ||
// OnCounterInterrogation 总计数器请求 | ||
OnCounterInterrogation(asdu.Connect, *asdu.ASDU, asdu.QualifierCountCall) error | ||
// OnRead 读定值请求 | ||
OnRead(asdu.Connect, *asdu.ASDU, asdu.InfoObjAddr) error | ||
// OnClockSync 时钟同步请求 | ||
OnClockSync(asdu.Connect, *asdu.ASDU, time.Time) error | ||
// OnResetProcess 进程重置请求 | ||
OnResetProcess(asdu.Connect, *asdu.ASDU, asdu.QualifierOfResetProcessCmd) error | ||
// OnDelayAcquisition 延迟获取请求 | ||
OnDelayAcquisition(asdu.Connect, *asdu.ASDU, uint16) error | ||
// OnASDU 控制命令请求 | ||
OnASDU(asdu.Connect, *asdu.ASDU) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"github.com/wendy512/go-iecp5/asdu" | ||
"github.com/wendy512/iec104/client" | ||
"github.com/wendy512/iec104/server" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestClient(t *testing.T) { | ||
srv := startServer() | ||
settings := client.NewSettings() | ||
settings.LogCfg = &client.LogCfg{Enable: true} | ||
c := client.New(settings, &clientCall{}) | ||
|
||
wg := sync.WaitGroup{} | ||
wg.Add(1) | ||
c.SetOnConnectHandler(func(c *client.Client) { | ||
// 连接成功以后做的操作 | ||
fmt.Printf("connected %s iec104 server\n", settings.Host) | ||
}) | ||
|
||
// server active确认后回调 | ||
c.SetServerActiveHandler(func(c *client.Client) { | ||
//// 发送总召唤 | ||
if err := c.SendInterrogationCmd(commonAddr); err != nil { | ||
t.Errorf("send interrogation cmd error %v\n", err) | ||
t.FailNow() | ||
} | ||
|
||
// 累积量召唤 | ||
if err := c.SendCounterInterrogationCmd(commonAddr); err != nil { | ||
t.Errorf("send counter interrogation cmd error %v\n", err) | ||
t.FailNow() | ||
} | ||
|
||
// read cmd | ||
if err := c.SendReadCmd(commonAddr, 100); err != nil { | ||
t.Errorf("send counter interrogation cmd error %v\n", err) | ||
t.FailNow() | ||
} | ||
|
||
// 时钟同步 | ||
if err := c.SendClockSynchronizationCmd(commonAddr, time.Now()); err != nil { | ||
t.Errorf("send clock sync cmd error %v\n", err) | ||
t.FailNow() | ||
} | ||
|
||
// test cmd | ||
if err := c.SendTestCmd(commonAddr); err != nil { | ||
t.Errorf("send test cmd error %v\n", err) | ||
t.FailNow() | ||
} | ||
|
||
// 单点控制 | ||
if err := c.SendCmd(commonAddr, asdu.C_SC_NA_1, asdu.InfoObjAddr(1000), false); err != nil { | ||
t.Errorf("send single cmd error %v\n", err) | ||
t.FailNow() | ||
} | ||
|
||
// 测试等待回复,不能结束太快 | ||
time.Sleep(time.Second * 10) | ||
wg.Done() | ||
}) | ||
|
||
// Connect后会发送server active | ||
if err := c.Connect(); err != nil { | ||
t.Errorf("client connect error %v\n", err) | ||
t.FailNow() | ||
} | ||
wg.Wait() | ||
|
||
if err := c.Close(); err != nil { | ||
t.Errorf("close error %v\n", err) | ||
t.FailNow() | ||
} | ||
srv.Stop() | ||
} | ||
|
||
func startServer() *server.Server { | ||
s := server.New(server.NewSettings(), &myServerHandler{}) | ||
s.Start() | ||
return s | ||
} |
Oops, something went wrong.