我正在我的设备(raspi)上运行一个直接方法处理程序模块,该模块在调用熟悉的直接方法调用时记录有效负载。但是当我打印有效负载时,它的末尾附加了垃圾数据。 设备上的模块使用 Azure C SDK 和基于 WebSockets 的 MQTT。
我尝试调用以下直接方法调用:
az iot hub invoke-module-method --device-id raspi --module-id raspiNayax --method-name start-transaction --hub-name dcfcdev --method-payload '{\"deviceId\":\"pedestal1\",\"type\":\"start-transaction\",\"timestamp\":\"2024-06-07T04:13:21.9172154Z\",\"sequenceId\":\"1ABC\",\"parameters\":[{\"name\":\"connectorOcppNumber\",\"value\":5},{\"name\":\"productCode\",\"value\":2},{\"name\":\"productPrice\",\"value\":"test1"}]}'
模块在设备上打印的内容:
Method name: start-transaction, Payload: {"deviceId":"pedestal1","type":"start-transaction","timestamp":"2024-06-07T04:13:21.9172154Z","sequenceId":"1ABC","parameters":[{"name":"connectorOcppNumber","value":5},{"name":"productCode","value":2},{"name":"productPrice","value":"test1"}]}**767733**
这就是我创建在设备上运行的直接方法处理程序的方法:
int DirectMethodCb(const char* method_name, const unsigned char* payload, size_t size, unsigned char** response, size_t* resp_size, void* userContextCallback)
{
char deviceMethodResponse[MAX_RESPONSE_LENGTH];
int responseCode = 400;
printf("+++++++++++++ Direct method call received +++++++++++++\n");
__log_ts("Azure Direct Method Received \n Method name: %s, Payload: %s\n", method_name, payload);
if(strcmp("is-ready", method_name) == 0){
__log_ts("Direct method: is-ready\n");
responseCode = payload_parser(payload, IS_READY);
strcpy(deviceMethodResponse, "{}");
}else if(strcmp("is-available", method_name) == 0){
__log_ts("Direct method: is-available\n");
responseCode = payload_parser(payload, IS_AVAILABLE);
responseCode = get_current_session(IS_AVAILABLE);
strcpy(deviceMethodResponse, "{}");
}else if(strcmp("start-transaction", method_name) == 0){
__log_ts("Direct method: start-transaction\n");
responseCode = start_transaction_handler(payload);
strcpy(deviceMethodResponse, "{}");
}else if(strcmp("close-transaction", method_name) == 0){
__log_ts("Direct method: close-transaction\n");
// responseCode = payload_parser(payload, CLOSE_TRANSACTION);
responseCode = close_cancel_session_handler(payload, CLOSE_TRANSACTION);
strcpy(deviceMethodResponse, "{}");
}else if(strcmp("cancel-transaction", method_name) == 0){
__log_ts("Direct method: cancel-transaction\n");
// responseCode = cancel_session_handler(payload);
responseCode = close_cancel_session_handler(payload, CANCEL_TRANSACTION);
strcpy(deviceMethodResponse, "{}");
}else{
responseCode = 700;
strcpy(deviceMethodResponse, "{}");
}
*resp_size = strlen(deviceMethodResponse);
*response = malloc(*resp_size + 1); // Allocate an extra byte for the null-terminator
strncpy((char *)*response, deviceMethodResponse, *resp_size); // Use strncpy to copy the string
(*response)[*resp_size] = '\0';
__log_ts("Azure Direct Method to method call:%s\n Response Code: %d, Response Payload: %s\n", method_name, responseCode, deviceMethodResponse);
fflush(NULL);
return responseCode;
}
void *init_az_service(void *arg) {
IOTHUB_MODULE_CLIENT_LL_HANDLE iotHubModuleClientHandle = NULL;
int retval = 1;
do{
printf("\n\n=======================\n");
fflush(NULL);
srand((unsigned int)time(NULL));
// Ensuring platform is initialized.
if (0 != IoTHub_Init())
{
// printf("Failed to initialize the platform.");
__log_ts("Failed to initialize the platform.\n");
break;
}
// |** Establish MQTT protocol. **|
iotHubModuleClientHandle = IoTHubModuleClient_LL_CreateFromEnvironment(MQTT_Protocol);
if (NULL == iotHubModuleClientHandle)
{
// printf("IoTHubModuleClient_LL_CreateFromEnvironment failed");
__log_ts("IoTHubModuleClient_LL_CreateFromEnvironment failed\n");
break;
}
// Setting up the Direct Method Callback function.
IOTHUB_CLIENT_RESULT result = IoTHubModuleClient_LL_SetModuleMethodCallback(iotHubModuleClientHandle, DirectMethodCb, iotHubModuleClientHandle);
if (IOTHUB_CLIENT_OK != result)
{
// printf("IoTHubModuleClient_SetModuleMethodCallback failed: %d", result);
__log_ts("IoTHubModuleClient_SetModuleMethodCallback failed: %d\n", result);
break;
}
while (true)
{
IoTHubModuleClient_LL_DoWork(iotHubModuleClientHandle);
ThreadAPI_Sleep(100);
}
}while(false);
// Destroy iotHubModuleClientHandle if function errored out.
if (NULL != iotHubModuleClientHandle)
{
IoTHubModuleClient_LL_Destroy(iotHubModuleClientHandle);
}
IoTHub_Deinit();
return retval;
}
这可能是 Azure 中反复出现的问题。 您可以尝试探索其他物联网平台,例如www.iotellect.com