T&A API Tutorial
Run the example
- Install and run the gateway
- Download the Go client library
- Copy the root certificate of the gateway to your working directory. As default, the certificate(ca.crt) resides in cert of the installation directory.
-
Change the gateway and the device information in src/example/tna/test/test.go as needed.
// the path of the root certificate GATEWAY_CA_FILE = "../../../../cert/gateway/ca.crt" // the address of the device gateway GATEWAY_IP = "192.168.0.2" GATEWAY_PORT = 4000 // the ip address of the target device DEV_IP = "192.168.0.110" DEV_PORT = 51211 USE_SSL = false
-
Build.
cd src/example/tna/test go build .
-
Run.
./test
1. Connect to the gateway and the device
The example assumes you use the device gateway. For the master gateway or other connection options, refer to the Connect or ConnectMaster examples.
gatewayClient := &client.GatewayClient{}
gatewayClient.Connect(GATEWAY_CA_FILE, GATEWAY_IP, GATEWAY_PORT)
connectSvc = connect.NewConnectSvc(gatewayClient.GetConn())
deviceID, _ := connectSvc.Connect(DEV_IP, DEV_PORT, USE_SSL)
2. Test the T&A configuration
There are several options you can configure related to the T&A functions. The example shows how to configure some of these options and let you check the results.
// (1) BY_USER
newConfig := &tna.TNAConfig{
Mode: tna.Mode_BY_USER,
Labels: []string{
"In",
"Out",
"Scheduled In",
"Fixed Out",
},
}
tnaSvc.SetConfig(deviceID, newConfig)
fmt.Printf("(1) The T&A mode is set to BY_USER(optional). You can select a T&A key before authentication. Try to authenticate after selecting a T&A key.\n\n")
cli.PressEnter(">> Press ENTER if you finish testing this mode.\n")
// (2) IsRequired
newConfig.IsRequired = true
tnaSvc.SetConfig(deviceID, newConfig)
fmt.Printf("(2) The T&A mode is set to BY_USER(mandatory). Try to authenticate without selecting a T&A key.\n\n")
cli.PressEnter(">> Press ENTER if you finish testing this mode.\n")
// (3) LAST_CHOICE
newConfig.Mode = tna.Mode_LAST_CHOICE
tnaSvc.SetConfig(deviceID, newConfig)
fmt.Printf("(3) The T&A mode is set to LAST_CHOICE. The T&A key selected by the previous user will be used. Try to authenticate multiple users.\n\n")
cli.PressEnter(">> Press ENTER if you finish testing this mode.\n")
// (4) BY_SCHEDULE
newConfig.Mode = tna.Mode_BY_SCHEDULE
newConfig.Schedules = []uint32{ 0, 0, 1 } // Always for KEY_3 (Scheduled In)
tnaSvc.SetConfig(deviceID, newConfig)
fmt.Printf("(4) The T&A mode is set to BY_SCHEDULE. The T&A key will be determined automatically by schedule. Try to authenticate without selecting a T&A key.\n\n")
cli.PressEnter(">> Press ENTER if you finish testing this mode.\n")
// (5) FIXED
newConfig.Mode = tna.Mode_FIXED
newConfig.Key = tna.Key_KEY_4
tnaSvc.SetConfig(deviceID, newConfig)
fmt.Printf("(5) The T&A mode is set to FIXED(KEY_4). Try to authenticate without selecting a T&A key.\n\n")
cli.PressEnter(">> Press ENTER if you finish testing this mode.\n")
3. Get the T&A logs
You can read the log records with T&A key information using TNA.GetTNALog. To get the label of the key, you have to look up the TNAConfig.labels.
events, _ := tnaSvc.GetTNALog(deviceID, firstEventID, 0)
// ...
func getTNALabel(key tna.Key, config *tna.TNAConfig) string {
if len(config.Labels) > int(key - 1) {
return fmt.Sprintf("%v(%v)", config.Labels[key - 1], key)
} else {
return fmt.Sprintf("%v", key)
}
}
You can also subscribe to an event stream for receiving realtime events.
enableReq := &event.EnableMonitoringRequest{
DeviceID: deviceID,
}
s.client.EnableMonitoring(context.Background(), enableReq)
subReq := &event.SubscribeRealtimeLogRequest{
QueueSize: MONITORING_QUEUE_SIZE,
DeviceIDs: []uint32 { deviceID },
}
ctx, cancelFunc := context.WithCancel(context.Background())
eventStream, _ = s.client.SubscribeRealtimeLog(ctx, subReq)
go func() {
for {
eventLog, _ := eventStream.Recv()
eventCallback(eventLog)
}
} ()