|
| 1 | +package iec61850 |
| 2 | + |
| 3 | +/* |
| 4 | +#include <iec61850_client.h> |
| 5 | +
|
| 6 | +extern void reportCallbackFunctionBridge(void* parameter, ClientReport report); |
| 7 | +*/ |
| 8 | +import "C" |
| 9 | +import ( |
| 10 | + "unsafe" |
| 11 | +) |
| 12 | + |
| 13 | +var reportCallbacks = make(map[int32]*reportCallbackHandler) |
| 14 | + |
| 15 | +type ReasonForInclusion int |
| 16 | + |
| 17 | +const ( |
| 18 | + IEC61850_REASON_NOT_INCLUDED ReasonForInclusion = iota |
| 19 | + IEC61850_REASON_DATA_CHANGE |
| 20 | + IEC61850_REASON_QUALITY_CHANGE |
| 21 | + IEC61850_REASON_DATA_UPDATE |
| 22 | + IEC61850_REASON_INTEGRITY |
| 23 | + IEC61850_REASON_GI |
| 24 | + IEC61850_REASON_UNKNOWN |
| 25 | +) |
| 26 | + |
| 27 | +type reportCallbackHandler struct { |
| 28 | + handler ReportCallbackFunction |
| 29 | +} |
| 30 | + |
| 31 | +type ClientReport struct { |
| 32 | + Report C.ClientReport |
| 33 | +} |
| 34 | + |
| 35 | +type ReportCallbackFunction func(clientReport ClientReport) |
| 36 | + |
| 37 | +//export reportCallbackFunctionBridge |
| 38 | +func reportCallbackFunctionBridge(parameter unsafe.Pointer, report C.ClientReport) { |
| 39 | + callbackId := int32(uintptr(parameter)) |
| 40 | + if call, ok := reportCallbacks[callbackId]; ok { |
| 41 | + call.handler(ClientReport{ |
| 42 | + Report: report, |
| 43 | + }) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (reason ReasonForInclusion) GetValueAsString() string { |
| 48 | + return C.GoString(C.ReasonForInclusion_getValueAsString(C.ReasonForInclusion(reason))) |
| 49 | +} |
| 50 | + |
| 51 | +func (clientReport *ClientReport) GetElement(elementIndex int) (MmsValue, error) { |
| 52 | + dataSetValues := C.ClientReport_getDataSetValues(clientReport.Report) |
| 53 | + cMmsValue := C.MmsValue_getElement(dataSetValues, C.int(elementIndex)) |
| 54 | + mmsType := MmsType(C.MmsValue_getType(cMmsValue)) |
| 55 | + |
| 56 | + MmsVal, err := toGoValue(cMmsValue, mmsType) |
| 57 | + if err != nil { |
| 58 | + return MmsValue{}, err |
| 59 | + } |
| 60 | + |
| 61 | + return MmsValue{ |
| 62 | + Value: MmsVal, |
| 63 | + Type: mmsType, |
| 64 | + }, nil |
| 65 | +} |
| 66 | + |
| 67 | +func (clientReport *ClientReport) GetRcbReference() string { |
| 68 | + return C.GoString(C.ClientReport_getRcbReference(clientReport.Report)) |
| 69 | +} |
| 70 | + |
| 71 | +func (clientReport *ClientReport) GetRptId() string { |
| 72 | + return C.GoString(C.ClientReport_getRptId(clientReport.Report)) |
| 73 | +} |
| 74 | + |
| 75 | +func (clientReport *ClientReport) GetReasonForInclusion(elementIndex int) ReasonForInclusion { |
| 76 | + reason := C.ClientReport_getReasonForInclusion(clientReport.Report, C.int(elementIndex)) |
| 77 | + return ReasonForInclusion(reason) |
| 78 | +} |
| 79 | + |
| 80 | +func (clientReport *ClientReport) HasTimestamp() bool { |
| 81 | + return bool(C.ClientReport_hasTimestamp(clientReport.Report)) |
| 82 | +} |
| 83 | + |
| 84 | +func (clientReport *ClientReport) GetTimestamp() int64 { |
| 85 | + return int64(C.ClientReport_getTimestamp(clientReport.Report)) |
| 86 | +} |
| 87 | + |
| 88 | +func (clientReport *ClientReport) HasSeqNum() bool { |
| 89 | + return bool(C.ClientReport_hasSeqNum(clientReport.Report)) |
| 90 | +} |
| 91 | + |
| 92 | +func (clientReport *ClientReport) GetSeqNum() int16 { |
| 93 | + return int16(C.ClientReport_getSeqNum(clientReport.Report)) |
| 94 | +} |
| 95 | + |
| 96 | +func (clientReport *ClientReport) HasDataSetName() bool { |
| 97 | + return bool(C.ClientReport_hasDataSetName(clientReport.Report)) |
| 98 | +} |
| 99 | + |
| 100 | +func (clientReport *ClientReport) HasReasonForInclusion() bool { |
| 101 | + return bool(C.ClientReport_hasReasonForInclusion(clientReport.Report)) |
| 102 | +} |
| 103 | + |
| 104 | +func (clientReport *ClientReport) HasConfRev() bool { |
| 105 | + return bool(C.ClientReport_hasConfRev(clientReport.Report)) |
| 106 | +} |
| 107 | + |
| 108 | +func (clientReport *ClientReport) GetConfRev() int32 { |
| 109 | + return int32(C.ClientReport_getConfRev(clientReport.Report)) |
| 110 | +} |
| 111 | + |
| 112 | +func (clientReport *ClientReport) HasBufOvfl() bool { |
| 113 | + return bool(C.ClientReport_hasBufOvfl(clientReport.Report)) |
| 114 | +} |
| 115 | + |
| 116 | +func (clientReport *ClientReport) GetBufOvfl() bool { |
| 117 | + return bool(C.ClientReport_getBufOvfl(clientReport.Report)) |
| 118 | +} |
| 119 | + |
| 120 | +func (clientReport *ClientReport) HasDataReference() bool { |
| 121 | + return bool(C.ClientReport_hasDataReference(clientReport.Report)) |
| 122 | +} |
| 123 | + |
| 124 | +func (clientReport *ClientReport) GetDataReference(elementIndex int) string { |
| 125 | + return C.GoString(C.ClientReport_getDataReference(clientReport.Report, C.int(elementIndex))) |
| 126 | +} |
| 127 | + |
| 128 | +func (clientReport *ClientReport) GetDataSetName() string { |
| 129 | + return C.GoString(C.ClientReport_getDataSetName(clientReport.Report)) |
| 130 | +} |
| 131 | + |
| 132 | +func (clientReport *ClientReport) GetDataSetValues() (MmsValue, error) { |
| 133 | + cMmsValue := C.ClientReport_getDataSetValues(clientReport.Report) |
| 134 | + mmsType := MmsType(C.MmsValue_getType(cMmsValue)) |
| 135 | + |
| 136 | + mmsValue, err := toGoValue(cMmsValue, mmsType) |
| 137 | + if err != nil { |
| 138 | + return MmsValue{}, err |
| 139 | + } |
| 140 | + |
| 141 | + return MmsValue{ |
| 142 | + Value: mmsValue, |
| 143 | + Type: mmsType, |
| 144 | + }, nil |
| 145 | +} |
| 146 | + |
| 147 | +func (clientReport *ClientReport) HasSubSeqNum() bool { |
| 148 | + return bool(C.ClientReport_hasSubSeqNum(clientReport.Report)) |
| 149 | +} |
| 150 | + |
| 151 | +func (clientReport *ClientReport) GetSubSeqNum() int16 { |
| 152 | + return int16(C.ClientReport_getSubSeqNum(clientReport.Report)) |
| 153 | +} |
| 154 | + |
| 155 | +func (clientReport *ClientReport) GetMoreSeqmentsFollow() bool { |
| 156 | + return bool(C.ClientReport_getMoreSeqmentsFollow(clientReport.Report)) |
| 157 | +} |
| 158 | + |
| 159 | +func (c *Client) InstallReportHandler(objectReference string, function ReportCallbackFunction) error { |
| 160 | + var clientError C.IedClientError |
| 161 | + |
| 162 | + cObjectRef := C.CString(objectReference) |
| 163 | + defer C.free(unsafe.Pointer(cObjectRef)) |
| 164 | + |
| 165 | + rcb := C.IedConnection_getRCBValues(c.conn, &clientError, cObjectRef, nil) |
| 166 | + if err := GetIedClientError(clientError); err != nil { |
| 167 | + return err |
| 168 | + } |
| 169 | + defer C.ClientReportControlBlock_destroy(rcb) |
| 170 | + |
| 171 | + callbackId := callbackIdGen.Add(1) |
| 172 | + cPtr := intToPointerBug58625(callbackId) |
| 173 | + reportCallbacks[callbackId] = &reportCallbackHandler{ |
| 174 | + handler: function, |
| 175 | + } |
| 176 | + |
| 177 | + C.IedConnection_installReportHandler(c.conn, cObjectRef, C.ClientReportControlBlock_getRptId(rcb), (*[0]byte)(C.reportCallbackFunctionBridge), cPtr) |
| 178 | + |
| 179 | + return nil |
| 180 | +} |
| 181 | + |
| 182 | +func (c *Client) UninstallReportHandler(objectReference string) { |
| 183 | + cObjectRef := C.CString(objectReference) |
| 184 | + defer C.free(unsafe.Pointer(cObjectRef)) |
| 185 | + C.IedConnection_uninstallReportHandler(c.conn, cObjectRef) |
| 186 | +} |
| 187 | + |
| 188 | +func (c *Client) TriggerGIReport(objectReference string) error { |
| 189 | + var clientError C.IedClientError |
| 190 | + |
| 191 | + cObjectRef := C.CString(objectReference) |
| 192 | + defer C.free(unsafe.Pointer(cObjectRef)) |
| 193 | + |
| 194 | + rcb := C.IedConnection_getRCBValues(c.conn, &clientError, cObjectRef, nil) |
| 195 | + if err := GetIedClientError(clientError); err != nil { |
| 196 | + return err |
| 197 | + } |
| 198 | + defer C.ClientReportControlBlock_destroy(rcb) |
| 199 | + |
| 200 | + C.IedConnection_triggerGIReport(c.conn, &clientError, cObjectRef) |
| 201 | + if err := GetIedClientError(clientError); err != nil { |
| 202 | + return err |
| 203 | + } |
| 204 | + |
| 205 | + return nil |
| 206 | +} |
0 commit comments