设备接口

Navigation:  C++ > 采集器管理接口 >

设备接口

Previous pageReturn to chapter overviewNext page

 

《设备接口》中的接口主要包括:

ct3_add_device

ct3_delete_device

ct3_query_device

ct3_query_device_id_by_name

ct3_query_devices_of_collector

ct3_modify_device

ct3_update_device_status

ct3_query_devices_status

device

copycode!MISSING PHRASE 'COPYCODE'!

//*Device Function

 

#include "hdKingAPI.h"

#include "error_code.h"

#include "hd3Struct.h"

#include <stdio.h>

#include <string.h>

#include <time.h>

#include <Windows.h>

 

int32 main()

{

 int32 nRet = RD_SUCCESS;

 HD3Connection conn;

 HD3HANDLE hIter = NULL;

 int32 nTryTime = 3;

 uint32 nCollectorID = 0;

 uint32 nDeviceID = 0;

 HD3Collector hdCollector;

 HD3Device hdDevice;

 HD3Device hdDevices[3];

 int32 i = 0;

 int32 nDeviceNum = 3;

 HD3Time hdTime;

 

 memset(&hdCollector, 0, sizeof(HD3Collector));

 memset(&hdDevice, 0, sizeof(HD3Device));

 for (i = 0; i < 3; ++i)

 {

         memset(&hdDevices[i], 0, sizeof(HD3Device));

 }

 

 strncpy(hdCollector.szName"collector"strlen("collector"));

 hdCollector.nType = HD3_COLLECTOR_TYPE_OPC;

 sprintf(hdCollector.szDescription"name: %s, type: %d"hdCollector.szNamehdCollector.nType);

 strncpy(hdCollector.szExtParam"***", 3);

 

 //connect

 strcpy(conn.szAddress"127.0.0.1");

 conn.nPort = 5673;

 conn.nTimeout = 3;

 nRet = nt3_connect(&conn);

 if (nRet != RD_SUCCESS)

 {

         printf("connect to server failed, error code[%d]!\n"nRet);

         return -1;

 }

 

 //login

 nRet = sc3_login("admin""admin");

 if (nRet != RD_SUCCESS)

 {

         printf("login failed, error code[%d]!\n"nRet);

         return -1;

 }

 printf("login successful!\n");

 

 //add collector

 uint32 nID=0;

 nRet = ct3_add_collector(&hdCollector, &nID);

 if (nRet != RD_SUCCESS)

 {

         printf("add collector[name:%s] failed, error code [%d]!\n"hdCollector.szNamenRet);

         nt3_disconnect();

         return -1;

 }

 printf("add collector[name:%s] successful!\n"hdCollector.szName);

 

 //query collector id

 nRet = ct3_query_collector_id_by_name(hdCollector.szName, &nCollectorID);

 if (nRet != RD_SUCCESS)

 {

         printf("query collector id  by name[%s] failed, error code [%d]!\n"hdCollector.szNamenRet);

         nt3_disconnect();

         return -1;

 }

 printf("query collector id  by name[%s] successful, collector id  [%d].\n"hdCollector.szNamenCollectorID);

 

 // add device

 for (i = 0; i < nDeviceNum; ++i)

 {

         sprintf(hdDevices[i].szName"device%d"i);

         hdDevices[i].nPort = 5678;

         hdDevices[i].nTimeout = 15;

         hdDevices[i].nCollectorID = nCollectorID;

         hdDevices[i].nTryTime = nTryTime;

         sprintf(hdDevices[i].szAddr"%d.%d.%d.%d"iiii);

         sprintf(hdDevices[i].szDescription"name: %s, collector id: %d"hdDevices[i].szNamehdDevices[i].nCollectorID);

         strncpy(hdDevices[i].szExtParam"***", 3);

 }

 

 for (i = 0; i < nDeviceNum; ++i)

 {

         nRet = ct3_add_device(&hdDevices[i], &hdDevices[i].nID);

         if (nRet != RD_SUCCESS)

         {

                 printf("add device[name:%s] failed, error code [%d]!\n"hdDevices[i].szNamenRet);

                 nt3_disconnect();

                 return -1;

         }

         printf("add device[name:%s] successful!\n"hdDevices[i].szName);

 }

 

 //query device id , query device, modify device

 for (i = 0; i < nDeviceNum; ++i)

 {

         nRet = ct3_query_device_id_by_name(nCollectorIDhdDevices[i].szName, &nDeviceID);

         if (nRet != RD_SUCCESS)

         {

                 printf("query device id  by name[%s] failed, error code [%d]!\n"hdDevices[i].szNamenRet);

                 nt3_disconnect();

                 return -1;

         }

         printf("query device id  by name[%s] successful, device id  [%d].\n"hdDevices[i].szNamenDeviceID);

 

         nRet = ct3_query_device(nDeviceID, &hdDevices[i]);

         if (nRet != RD_SUCCESS)

         {

                 printf("query device [id:%d] failed, error code [%d]!\n"nDeviceIDnRet);

                 nt3_disconnect();

                 return -1;

         }

         printf("query device [id:%d] successful.\n"nDeviceID);

 

         nRet = ct3_modify_device(nDeviceID, &hdDevices[i]);

         if (nRet != RD_SUCCESS)

         {

                 printf("modify device [id:%d] failed, error code [%d]!\n"nDeviceIDnRet);

                 nt3_disconnect();

                 return -1;

         }

         printf("modify device [id:%d] successful.\n"nDeviceID);

 

         Sleep(30000);

         // Update status

         hdTime.nSec = (int32)time(NULL);

         hdTime.nMsec = 0;

         nRet = ct3_update_device_status(nDeviceIDHD3_QUALITY_DEVICE_CONN, 100, &hdTime);

         if(nRet != RD_SUCCESS)

         {

                 printf("failed to update device status, error code[%d]!\n"nRet);

                 return -1;

         }

         printf("update device  status successful.\n");

 

         HD3CtStatusInfo ctStatusInfo;

         int32 nErr = 0;

         nRet = ct3_query_devices_status(1, &nDeviceID, &ctStatusInfo, &nErr);

         if(nRet != RD_SUCCESS)

         {

                 printf("failed to query device status, error code[%d]!\n"nRet);

                 return -1;

         }

         printf("query device  status successful.\n");

 }

 

 //query devices belong to the collector

 nRet = ct3_query_devices_of_collector(nCollectorID, &hIter);

 if (nRet != RD_SUCCESS)

 {

         printf("query all device of collector[id: %d] failed,  error code [%d]!\n"nCollectorIDnRet);

         nt3_disconnect();

         return -1;

 }

 printf("query all device of collector[id: %d] successful!\n"nCollectorID);

 

 while (true)

 {

         memset(&hdDevice, 0, sizeof(HD3Device));

         nRet = ut3_get_item_step(hIter, &hdDevice);

         if (RD_SUCCESS == nRet)

         {

                 printf("Query one device [name:%s] successful!\n"hdCollector.szName);

         }

         else if (EC_HD_API_QUERY_END == nRet)

         {

                 printf("query all device complete!\n");

                 break;

         }

         else

         {

                 printf("query device failed, error code [%d]!\n"nRet);

                 break;

         }        

 }

 ut3_free_handle(hIter);

 

 //delete device

 for (i = 0; i < 3; ++i)

 {

         nRet = ct3_delete_device(hdDevices[i].nID);

         if (nRet != RD_SUCCESS)

         {

                 printf("delete device failed,  error code [%d]!\n"nRet);

                 nt3_disconnect();

                 return -1;

         }

         printf("delete device [id:%d] successful.\n",  hdDevices[i].nID);

 }

 

 //disconnect

 nt3_disconnect();

 

 return 0;

}

 

//*/

 

 

©2015. All Rights Reserved.