-
Notifications
You must be signed in to change notification settings - Fork 38
/
client_control.go
61 lines (49 loc) · 1.96 KB
/
client_control.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package iec61850
// #include <iec61850_client.h>
import "C"
import "unsafe"
// ControlForDirectWithNormalSecurity 控制模式 1[direct-with-normal-security]
func (c *Client) ControlForDirectWithNormalSecurity(objectRef string, value bool) error {
return c.control(objectRef, value, false, true, false)
}
// ControlForSboWithNormalSecurity 控制模式 2[sbo-with-normal-security]
func (c *Client) ControlForSboWithNormalSecurity(objectRef string, value bool) error {
return c.control(objectRef, value, true, false, false)
}
// ControlForDirectWithEnhancedSecurity 控制模式 3[direct-with-enhanced-security]
func (c *Client) ControlForDirectWithEnhancedSecurity(objectRef string, value bool) error {
return c.control(objectRef, value, false, false, true)
}
// ControlForSboWithEnhancedSecurity 控制模式 4[sbo-with-enhanced-security]
func (c *Client) ControlForSboWithEnhancedSecurity(objectRef string, value bool) error {
return c.control(objectRef, value, true, false, true)
}
func (c *Client) control(objectRef string, value, _select, direct, enhanced bool) error {
cObjectRef := C.CString(objectRef)
defer C.free(unsafe.Pointer(cObjectRef))
control := C.ControlObjectClient_create(cObjectRef, c.conn)
if control == nil {
return CreateControlObjectClientFail
}
// Select before operate
defer C.ControlObjectClient_destroy(control)
if _select && !enhanced && !bool(C.ControlObjectClient_select(control)) {
return ControlSelectFail
}
// Direct control with enhanced security
if enhanced {
C.ControlObjectClient_setCommandTerminationHandler(control, nil, nil)
}
ctlVal := C.MmsValue_newBoolean(C.bool(value))
defer C.MmsValue_delete(ctlVal)
// Select before operate with enhanced security
if _select && enhanced && !bool(C.ControlObjectClient_selectWithValue(control, ctlVal)) {
return ControlSelectFail
}
// Direct control
if direct {
C.ControlObjectClient_setOrigin(control, nil, 3)
}
C.ControlObjectClient_operate(control, ctlVal, 0)
return nil
}