From d35f415d8a926109fa7a1a805aa6a292be1b1ef2 Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Thu, 20 Jul 2023 18:21:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0ntool=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96nmodbus?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 25 +++++++++++++------------ go.sum | 25 +++++++++++++++++++++++++ nmodbus/events.go | 17 +++++++++++++++++ nmodbus/handler/fn_coils.go | 3 +++ nmodbus/handler/fn_discreteinputs.go | 3 ++- nmodbus/handler/fn_holdingregisters.go | 3 +++ nmodbus/handler/fn_inputregisters.go | 1 + nmodbus/handler/handler.go | 10 +++++++--- nmodbus/modbus_tcp.go | 2 +- nmodbus/modbus_tcp_test.go | 11 +---------- nmodbus/pipeline.go | 15 --------------- nmodbus/protocol/router.go | 8 ++++---- 12 files changed, 77 insertions(+), 46 deletions(-) create mode 100644 nmodbus/events.go delete mode 100644 nmodbus/pipeline.go diff --git a/go.mod b/go.mod index 82092f4..559cd01 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.20 require ( ariga.io/entcache v0.1.0 entgo.io/ent v0.12.3 - git.noahlan.cn/noahlan/nnet v1.1.2 - git.noahlan.cn/noahlan/ntool v1.1.1 + git.noahlan.cn/noahlan/nnet v1.2.0 + git.noahlan.cn/noahlan/ntool v1.1.7 github.com/BurntSushi/toml v1.3.2 github.com/Shopify/sarama v1.38.1 github.com/casbin/casbin/v2 v2.71.0 @@ -22,9 +22,9 @@ require ( github.com/pkg/errors v0.9.1 github.com/radovskyb/watcher v1.0.7 github.com/redis/go-redis/v9 v9.0.5 - github.com/zeromicro/go-zero v1.5.3 - golang.org/x/text v0.10.0 - google.golang.org/grpc v1.55.0 + github.com/zeromicro/go-zero v1.5.4 + golang.org/x/text v0.11.0 + google.golang.org/grpc v1.56.2 gopkg.in/yaml.v3 v3.0.1 gorm.io/gen v0.3.22 gorm.io/plugin/optimisticlock v1.1.1 @@ -73,13 +73,14 @@ require ( github.com/jcmturner/rpc/v2 v2.0.3 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect + github.com/jpillora/backoff v1.0.0 // indirect github.com/klauspost/compress v1.16.6 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect github.com/mitchellh/go-wordwrap v1.0.1 // indirect github.com/mitchellh/hashstructure v1.1.0 // indirect github.com/panjf2000/ants/v2 v2.7.5 // indirect - github.com/pelletier/go-toml/v2 v2.0.8 // indirect + github.com/pelletier/go-toml/v2 v2.0.9 // indirect github.com/pierrec/lz4/v4 v4.1.18 // indirect github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect github.com/rogpeppe/go-internal v1.10.0 // indirect @@ -89,15 +90,15 @@ require ( go.opentelemetry.io/otel v1.14.0 // indirect go.opentelemetry.io/otel/trace v1.14.0 // indirect go.uber.org/automaxprocs v1.5.2 // indirect - golang.org/x/crypto v0.10.0 // indirect + golang.org/x/crypto v0.11.0 // indirect golang.org/x/image v0.8.0 // indirect golang.org/x/mod v0.11.0 // indirect - golang.org/x/net v0.11.0 // indirect - golang.org/x/sys v0.9.0 // indirect - golang.org/x/term v0.9.0 // indirect + golang.org/x/net v0.12.0 // indirect + golang.org/x/sys v0.10.0 // indirect + golang.org/x/term v0.10.0 // indirect golang.org/x/tools v0.10.0 // indirect - google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect - google.golang.org/protobuf v1.30.0 // indirect + google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect + google.golang.org/protobuf v1.31.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gorm.io/datatypes v1.2.0 // indirect gorm.io/driver/mysql v1.5.1 // indirect diff --git a/go.sum b/go.sum index a43a937..61c2450 100644 --- a/go.sum +++ b/go.sum @@ -22,8 +22,14 @@ git.noahlan.cn/noahlan/nnet v1.1.1 h1:uZuAwxE/Y0lI6ZpUUPiT9zYIls8IzinTc2TGshILmc git.noahlan.cn/noahlan/nnet v1.1.1/go.mod h1:0lKUUkppwNQhr8kDw8frgzwKe0RuLLmeSnXbDNSPBZE= git.noahlan.cn/noahlan/nnet v1.1.2 h1:aHwOSa3sKPKWCqpKjErzqj6/KnFvJSUvxCa6+R9/PqY= git.noahlan.cn/noahlan/nnet v1.1.2/go.mod h1:0lKUUkppwNQhr8kDw8frgzwKe0RuLLmeSnXbDNSPBZE= +git.noahlan.cn/noahlan/nnet v1.2.0 h1:QPO5uDKLiBOKJInqOwPCuHNRbS7Ki2kspO6FHUOI2J4= +git.noahlan.cn/noahlan/nnet v1.2.0/go.mod h1:rCmGIUFEu0Vhi0azXiTwenmE+cQiW5qTvm1boD4IEYk= git.noahlan.cn/noahlan/ntool v1.1.1 h1:XtixcgmHj/BQ/9VQXFzSoQ7boKF+Q6gfjlrngFD5G0o= git.noahlan.cn/noahlan/ntool v1.1.1/go.mod h1:pzoXErnQDLaHhvEHOGDoINf5VP1MDiU8NAcnaMEhxa4= +git.noahlan.cn/noahlan/ntool v1.1.6 h1:cqhsD7RCW5MXj9bVp8MCactbbUTds+uIQeZudRH7770= +git.noahlan.cn/noahlan/ntool v1.1.6/go.mod h1:pzoXErnQDLaHhvEHOGDoINf5VP1MDiU8NAcnaMEhxa4= +git.noahlan.cn/noahlan/ntool v1.1.7 h1:UwR4zb0lgWqMdOQ4xsbhXAIaq2JjUmXV/uNHVhv9FiI= +git.noahlan.cn/noahlan/ntool v1.1.7/go.mod h1:pzoXErnQDLaHhvEHOGDoINf5VP1MDiU8NAcnaMEhxa4= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= @@ -272,6 +278,7 @@ github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgS github.com/jackc/pgx/v4 v4.18.1 h1:YP7G1KABtKpB5IHrO9vYwSrCOhs7p3uqhvhhQBptya0= github.com/jackc/pgx/v4 v4.18.1/go.mod h1:FydWkUyadDmdNH/mHnGob881GawxeEm7TcMCzkb+qQE= github.com/jackc/pgx/v5 v5.3.1 h1:Fcr8QJ1ZeLi5zsPZqQeUZhNhxfkkKBOgJuYkJHoBOtU= +github.com/jackc/pgx/v5 v5.4.2 h1:u1gmGDwbdRUZiwisBm/Ky2M14uQyUP65bG8+20nnyrg= github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= @@ -295,6 +302,8 @@ github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= +github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= @@ -376,6 +385,7 @@ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FI github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= +github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pierrec/lz4/v4 v4.1.18 h1:xaKrnTkyoqfh1YItXl56+6KJNVYWlEEPuAQW9xsplYQ= github.com/pierrec/lz4/v4 v4.1.18/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -450,6 +460,7 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= @@ -462,6 +473,8 @@ github.com/zclconf/go-cty v1.13.2/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4 github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= github.com/zeromicro/go-zero v1.5.3 h1:9poyd+raeL7gSMUu6P19N7bssTppieR2j7Oos2j1yFQ= github.com/zeromicro/go-zero v1.5.3/go.mod h1:dmoBpgJTxt9KWmgrNGpv06XxZRPXMakrxUVgROFAR3g= +github.com/zeromicro/go-zero v1.5.4 h1:kRvcYuxcHOkUZvg7887KQl77Qv4klGL7MqGkTBgkpS8= +github.com/zeromicro/go-zero v1.5.4/go.mod h1:x/aUyLmSwRECvOyjOf+lhwThBOilJIY+s3slmPAeboA= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= @@ -504,6 +517,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM= golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I= +golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA= +golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -556,6 +571,7 @@ golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.11.0 h1:Gi2tvZIJyBtO9SDr1q9h5hEQCp/4L2RQ+ar0qjx2oNU= golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ= +golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -600,12 +616,16 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s= golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= +golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.9.0 h1:GRRCnKYhdQrD8kfRAdQ6Zcw1P0OcELxGLKJvtjVMZ28= golang.org/x/term v0.9.0/go.mod h1:M6DEAAIenWoTxdKrOltXcmDY3rSplQUkrvaDU5FcQyo= +golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c= +golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= @@ -617,6 +637,8 @@ golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58= golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4= +golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -676,6 +698,7 @@ google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a/go.mod h1:n3cpQtvx google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 h1:DdoeryqhaXp1LtT/emMP1BRJPHHKFi5akj/nbx/zNTA= google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4/go.mod h1:NWraEVixdDnqcqQ30jipen1STv2r/n24Wb7twVTGR4s= +google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1/go.mod h1:nKE/iIaLqn2bQwXBg8f1g2Ylh6r5MN5CmZvuzZCgsCU= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= @@ -685,6 +708,7 @@ google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8 google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/grpc v1.55.0 h1:3Oj82/tFSCeUrRTg/5E/7d/W5A1tj6Ky1ABAuZuv5ag= google.golang.org/grpc v1.55.0/go.mod h1:iYEXKGkEBhg1PjZQvoYEVPTDkHo1/bjTnfwTeGONTY8= +google.golang.org/grpc v1.56.2/go.mod h1:I9bI3vqKfayGqPUAwGdOSu7kt6oIJLixfffKrpXqQ9s= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= @@ -698,6 +722,7 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/nmodbus/events.go b/nmodbus/events.go new file mode 100644 index 0000000..13059bb --- /dev/null +++ b/nmodbus/events.go @@ -0,0 +1,17 @@ +package nmodbus + +import ( + "git.noahlan.cn/noahlan/nnet" + "git.noahlan.cn/noahlan/nnet/conn" + "git.noahlan.cn/noahlan/nnet/event" + "git.noahlan.cn/noahlan/nnet/packet" +) + +func withEvents() nnet.RunOption { + var statusChanger event.OnReceiveFn = func(nc *conn.Connection, p packet.IPacket) { + nc.SetStatus(conn.StatusWorking) + } + return func(ngin *nnet.Engine) { + ngin.EventManager().RegisterEventFront(event.EvtOnReceive, statusChanger) + } +} diff --git a/nmodbus/handler/fn_coils.go b/nmodbus/handler/fn_coils.go index a686ed1..4c555d4 100644 --- a/nmodbus/handler/fn_coils.go +++ b/nmodbus/handler/fn_coils.go @@ -6,6 +6,7 @@ import ( "git.noahlan.cn/noahlan/ntool-biz/nmodbus/util" ) +// ReadCoils 读线圈寄存器(DO) 功能码0x01 type ReadCoils struct { fnCode uint8 } @@ -50,6 +51,7 @@ func (r *ReadCoils) Function(s *Handler, req protocol.Packet, resp protocol.Pack ////////////////////////////////////////////// write-single +// WriteSingleCoil 写单个线圈寄存器 功能码0x05 type WriteSingleCoil struct { fnCode uint8 } @@ -82,6 +84,7 @@ func (r *WriteSingleCoil) Function(s *Handler, req protocol.Packet, resp protoco ////////////////////////////////////////////// write-multiple +// WriteMultipleCoils 写多个线圈寄存器 功能码 0x0F=15 type WriteMultipleCoils struct { fnCode uint8 } diff --git a/nmodbus/handler/fn_discreteinputs.go b/nmodbus/handler/fn_discreteinputs.go index 42237dd..2dd0b33 100644 --- a/nmodbus/handler/fn_discreteinputs.go +++ b/nmodbus/handler/fn_discreteinputs.go @@ -5,6 +5,7 @@ import ( "git.noahlan.cn/noahlan/ntool-biz/nmodbus/serialize" ) +// ReadDiscreteInputs 读离散输入 功能码 0x02 type ReadDiscreteInputs struct { fnCode uint8 } @@ -17,7 +18,7 @@ func (r *ReadDiscreteInputs) FnCode() uint8 { return r.fnCode } -// Function 读离散输入 功能码 0x04 +// Function 读离散输入 功能码 0x02 func (r *ReadDiscreteInputs) Function(s *Handler, req protocol.Packet, resp protocol.Packet) *protocol.MError { register, numRegs, endRegister := serialize.RegisterAddressAndNumber(req.GetBody()) if endRegister > 65535 { diff --git a/nmodbus/handler/fn_holdingregisters.go b/nmodbus/handler/fn_holdingregisters.go index d6c2a5e..80714c4 100644 --- a/nmodbus/handler/fn_holdingregisters.go +++ b/nmodbus/handler/fn_holdingregisters.go @@ -6,6 +6,7 @@ import ( "git.noahlan.cn/noahlan/ntool-biz/nmodbus/util" ) +// ReadHoldingRegisters 读保持寄存器(AI) 0x03 type ReadHoldingRegisters struct { fnCode uint8 } @@ -37,6 +38,7 @@ func (r *ReadHoldingRegisters) Function(s *Handler, req protocol.Packet, resp pr ////////////////////////////////////////////////////// WriteHoldingRegister +// WriteHoldingRegister 写单个保持寄存器 功能码0x06 type WriteHoldingRegister struct { fnCode uint8 } @@ -66,6 +68,7 @@ func (r *WriteHoldingRegister) Function(s *Handler, req protocol.Packet, resp pr ////////////////////////////////////////////////////// WriteHoldingRegisters +// WriteHoldingRegisters 写入 多个保持寄存器 功能码 0x10=16 type WriteHoldingRegisters struct { fnCode uint8 } diff --git a/nmodbus/handler/fn_inputregisters.go b/nmodbus/handler/fn_inputregisters.go index b229aaa..2fd7ec8 100644 --- a/nmodbus/handler/fn_inputregisters.go +++ b/nmodbus/handler/fn_inputregisters.go @@ -6,6 +6,7 @@ import ( "git.noahlan.cn/noahlan/ntool-biz/nmodbus/util" ) +// ReadInputRegisters 读输入寄存器(AO) 功能码0x04 type ReadInputRegisters struct { fnCode uint8 } diff --git a/nmodbus/handler/handler.go b/nmodbus/handler/handler.go index 0faa78b..6054b12 100644 --- a/nmodbus/handler/handler.go +++ b/nmodbus/handler/handler.go @@ -2,7 +2,7 @@ package handler import ( "encoding/binary" - "git.noahlan.cn/noahlan/nnet/connection" + "git.noahlan.cn/noahlan/nnet/conn" "git.noahlan.cn/noahlan/nnet/packet" rt "git.noahlan.cn/noahlan/nnet/router" "git.noahlan.cn/noahlan/ntool-biz/nmodbus/protocol" @@ -57,10 +57,12 @@ func (p *Handler) RegisterFunction(fn FunctionHandler) { p.handlerMap[fn.FnCode()] = fn } +// RegisterLogic 注册功能码对应的 扩展 处理方法 func (p *Handler) RegisterLogic(fnCode uint8, fn LogicFunc) { p.logics[fnCode] = fn } +// DataManager 数据管理器,基于主机地址(客户端地址)(modbus协议规定的主机号) func (p *Handler) DataManager(address uint8) *DataMgr { p.mu.Lock() defer p.mu.Unlock() @@ -73,7 +75,8 @@ func (p *Handler) DataManager(address uint8) *DataMgr { return ret } -func (p *Handler) Handle(conn *connection.Connection, pkg packet.IPacket) { +// Handle 核心处理逻辑(核心路由) +func (p *Handler) Handle(nc *conn.Connection, pkg packet.IPacket) { req, ok := pkg.(protocol.Packet) if !ok { nlog.Error(packet.ErrWrongPacketType) @@ -93,9 +96,10 @@ func (p *Handler) Handle(conn *connection.Connection, pkg packet.IPacket) { if err != &protocol.Success { resp.SetError(err) } - _ = conn.Send(resp.GetHeader(), resp.GetBody()) + _ = nc.Send(resp.GetHeader(), resp.GetBody()) } +// HandleLogic 功能码对应的扩展处理方法 func (p *Handler) HandleLogic(fnCode uint8, pkg protocol.Packet, startAddr, number int, val any) { go func() { if fn, ok := p.logics[fnCode]; ok { diff --git a/nmodbus/modbus_tcp.go b/nmodbus/modbus_tcp.go index aaa10f6..afed70c 100644 --- a/nmodbus/modbus_tcp.go +++ b/nmodbus/modbus_tcp.go @@ -34,7 +34,7 @@ func NewModbusTCPEngine(conf ModbusTCPConf) (*nnet.Engine, *handler.Handler) { Nonblocking: false, DisablePurge: false, }), - withPipeline(), + withEvents(), nnet.WithPackerBuilder(func() packet.Packer { return protocol.NewTCPPacker(conf.ByteOrder) }), diff --git a/nmodbus/modbus_tcp_test.go b/nmodbus/modbus_tcp_test.go index 15b51e1..f3c3595 100644 --- a/nmodbus/modbus_tcp_test.go +++ b/nmodbus/modbus_tcp_test.go @@ -4,7 +4,6 @@ import ( "encoding/binary" "git.noahlan.cn/noahlan/nnet/config" "git.noahlan.cn/noahlan/ntool-biz/nmodbus" - "git.noahlan.cn/noahlan/ntool-biz/nmodbus/util" "testing" ) @@ -19,15 +18,7 @@ func TestA(t *testing.T) { defer ngin.Stop() - // read:0x03 write:0x06 - handler.HoldingRegisters[0] = 0b00000011 // > 0000000000000011 = 0x03 = 3 - // 1~2 浮点 [2]uint16 - copy(handler.HoldingRegisters[1:2], util.Float32ToUint16(binary.BigEndian, 1.25)) - - handler.HoldingRegisters[3] = 21 - - // read:0x04 non-write - handler.InputRegisters[0] = 0b00000011 + handler.DataManager(0x01).WriteSingleHoldingRegister(0, 3) _ = ngin.ListenTCP(config.TCPServerConf{ Protocol: "tcp", diff --git a/nmodbus/pipeline.go b/nmodbus/pipeline.go deleted file mode 100644 index b4287d3..0000000 --- a/nmodbus/pipeline.go +++ /dev/null @@ -1,15 +0,0 @@ -package nmodbus - -import ( - "git.noahlan.cn/noahlan/nnet" - "git.noahlan.cn/noahlan/nnet/connection" -) - -func withPipeline() nnet.RunOption { - return func(ngin *nnet.Engine) { - ngin.Pipeline().Inbound().PushFront(func(conn *connection.Connection, v interface{}) error { - conn.SetStatus(connection.StatusWorking) - return nil - }) - } -} diff --git a/nmodbus/protocol/router.go b/nmodbus/protocol/router.go index 14e2ec1..62c230a 100644 --- a/nmodbus/protocol/router.go +++ b/nmodbus/protocol/router.go @@ -1,7 +1,7 @@ package protocol import ( - "git.noahlan.cn/noahlan/nnet/connection" + "git.noahlan.cn/noahlan/nnet/conn" "git.noahlan.cn/noahlan/nnet/packet" rt "git.noahlan.cn/noahlan/nnet/router" ) @@ -16,11 +16,11 @@ func NewRouter(handler rt.Handler) rt.Router { } } -func (r *Router) Handle(c *connection.Connection, pkg packet.IPacket) { - r.handler.Handle(c, pkg) +func (r *Router) Handle(nc *conn.Connection, pkg packet.IPacket) { + r.handler.Handle(nc, pkg) } -func (r *Router) Register(_ interface{}, handler rt.Handler) error { +func (r *Router) Register(_ any, handler rt.Handler) error { r.handler = handler return nil }