-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch adds support for the Method Service Set to call methods on the server. Fixes #251
- Loading branch information
1 parent
df55217
commit e53badd
Showing
7 changed files
with
150 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
vendor/ | ||
dist/ | ||
__pycache__/ |
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,49 @@ | ||
// Copyright 2018-2019 opcua authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"log" | ||
|
||
"github.com/gopcua/opcua" | ||
"github.com/gopcua/opcua/debug" | ||
"github.com/gopcua/opcua/ua" | ||
) | ||
|
||
func main() { | ||
var ( | ||
endpoint = flag.String("endpoint", "opc.tcp://localhost:4840", "OPC UA Endpoint URL") | ||
) | ||
flag.BoolVar(&debug.Enable, "debug", false, "enable debug logging") | ||
flag.Parse() | ||
log.SetFlags(0) | ||
|
||
ctx := context.Background() | ||
|
||
c := opcua.NewClient(*endpoint, opcua.SecurityMode(ua.MessageSecurityModeNone)) | ||
if err := c.Connect(ctx); err != nil { | ||
log.Fatal(err) | ||
} | ||
defer c.Close() | ||
|
||
in := int64(12) | ||
req := &ua.CallMethodRequest{ | ||
ObjectID: ua.NewStringNodeID(2, "main"), | ||
MethodID: ua.NewStringNodeID(2, "even"), | ||
InputArguments: []*ua.Variant{ua.MustVariant(in)}, | ||
} | ||
|
||
resp, err := c.Call(req) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if got, want := resp.StatusCode, ua.StatusOK; got != want { | ||
log.Fatalf("got status %v want %v", got, want) | ||
} | ||
out := resp.OutputArguments[0].Value() | ||
log.Printf("%d is even: %v", in, out) | ||
} |
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,25 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from opcua import ua, Server | ||
|
||
def even(parent, variant): | ||
print("even method call with parameters: ", variant.Value) | ||
ret = (variant.Value % 2 == 0) | ||
print("even", type(ret)) | ||
return [ua.Variant(ret, ua.VariantType.Boolean)] | ||
|
||
def square(parent, variant): | ||
print("square method call with parameters: ", variant.Value) | ||
variant.Value *= variant.Value | ||
return [variant] | ||
|
||
if __name__ == "__main__": | ||
server = Server() | ||
server.set_endpoint("opc.tcp://0.0.0.0:4840/") | ||
|
||
ns = server.register_namespace("http://gopcua.com/") | ||
main = server.nodes.objects.add_object(ua.NodeId("main", ns), "main") | ||
fnEven = main.add_method(ua.NodeId("even", ns), "even", even, [ua.VariantType.Int64], [ua.VariantType.Boolean]) | ||
fnSquare = main.add_method(ua.NodeId("square", ns), "square", square, [ua.VariantType.Int64], [ua.VariantType.Int64]) | ||
|
||
server.start() |
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,54 @@ | ||
// +build integration | ||
|
||
package uatest | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/gopcua/opcua" | ||
"github.com/gopcua/opcua/ua" | ||
"github.com/pascaldekloe/goe/verify" | ||
) | ||
|
||
func TestCallMethod(t *testing.T) { | ||
tests := []struct { | ||
req *ua.CallMethodRequest | ||
out []*ua.Variant | ||
}{ | ||
{ | ||
req: &ua.CallMethodRequest{ | ||
ObjectID: ua.NewStringNodeID(2, "main"), | ||
MethodID: ua.NewStringNodeID(2, "even"), | ||
InputArguments: []*ua.Variant{ | ||
ua.MustVariant(int64(12)), | ||
}, | ||
}, | ||
out: []*ua.Variant{ua.MustVariant(true)}, | ||
}, | ||
} | ||
|
||
srv := NewServer("method_server.py") | ||
defer srv.Close() | ||
|
||
c := opcua.NewClient(srv.Endpoint, srv.Opts...) | ||
if err := c.Connect(context.Background()); err != nil { | ||
t.Fatal(err) | ||
} | ||
defer c.Close() | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.req.ObjectID.String(), func(t *testing.T) { | ||
resp, err := c.Call(tt.req) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if got, want := resp.StatusCode, ua.StatusOK; got != want { | ||
t.Fatalf("got status %v want %v", got, want) | ||
} | ||
if got, want := resp.OutputArguments, tt.out; !verify.Values(t, "", got, want) { | ||
t.Fail() | ||
} | ||
}) | ||
} | ||
} |
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