Changeset 459 for azure_iot_hub_riscv/trunk/app_iothub_client/src/client.c
- Timestamp:
- Sep 25, 2020, 7:28:35 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
azure_iot_hub_riscv/trunk/app_iothub_client/src/client.c
r453 r459 23 23 #include "serializer_devicetwin.h" 24 24 #include "client.h" 25 #include "kpu_main.h" 25 26 26 27 #ifdef _MSC_VER … … 41 42 bool g_use_proxy; 42 43 HTTP_PROXY_OPTIONS g_proxy_options; 43 int ledOn;44 44 45 45 static int callbackCounter; … … 133 133 134 134 // Define the Model 135 BEGIN_NAMESPACE(WeatherStation); 136 137 DECLARE_MODEL(ContosoAnemometer, 138 WITH_DATA(double, windSpeed), 139 WITH_DATA(double, temperature), 140 WITH_DATA(double, humidity), 135 BEGIN_NAMESPACE(SecurityStation); 136 137 DECLARE_MODEL(ContosoSecurityCamera, 138 WITH_DATA(int, is_person), 139 WITH_DATA(int, is_car), 141 140 WITH_METHOD(quit), 142 141 WITH_METHOD(turnLedOn), … … 153 152 ); 154 153 155 DECLARE_DEVICETWIN_MODEL( AnemometerState,154 DECLARE_DEVICETWIN_MODEL(SecurityCameraState, 156 155 WITH_REPORTED_PROPERTY(ThresholdR, threshold) 157 156 ); 158 157 159 DECLARE_DEVICETWIN_MODEL( AnemometerSettings,158 DECLARE_DEVICETWIN_MODEL(SecurityCameraSettings, 160 159 WITH_DESIRED_PROPERTY(ThresholdD, threshold, onDesiredThreshold) 161 160 ); 162 161 163 END_NAMESPACE( WeatherStation);162 END_NAMESPACE(SecurityStation); 164 163 165 164 void anemometerReportedStateCallback(int status_code, void* userContextCallback) 166 165 { 167 AnemometerState *anemometer = (AnemometerState *)userContextCallback;166 SecurityCameraState *anemometer = (SecurityCameraState *)userContextCallback; 168 167 169 168 printf("received states \033[43m%d\033[49m, reported threshold = %.1f\n", status_code, anemometer->threshold.value); … … 174 173 // Note: The argument is NOT a pointer to threshold, but instead a pointer to the MODEL 175 174 // that contains threshold as one of its arguments. In this case, it 176 // is AnemometerSettings*.177 178 AnemometerSettings *anemometer = (AnemometerSettings *)argument;175 // is SecurityCameraSettings*. 176 177 SecurityCameraSettings *anemometer = (SecurityCameraSettings *)argument; 179 178 printf("received a new desired.threshold = \033[42m%.1f\033[49m\n", anemometer->threshold.value); 180 179 … … 182 181 } 183 182 184 METHODRETURN_HANDLE quit(Contoso Anemometer* device)183 METHODRETURN_HANDLE quit(ContosoSecurityCamera* device) 185 184 { 186 185 (void)device; … … 193 192 } 194 193 195 METHODRETURN_HANDLE turnLedOn(Contoso Anemometer* device)194 METHODRETURN_HANDLE turnLedOn(ContosoSecurityCamera* device) 196 195 { 197 196 (void)device; 198 197 (void)printf("\033[41mTurning LED on with Method.\033[49m\r\n"); 199 198 200 ledOn = 1; 199 digitalWrite(LED_G_PIN, LOW); 200 digitalWrite(LED_R_PIN, LOW); 201 digitalWrite(LED_B_PIN, LOW); 201 202 202 203 METHODRETURN_HANDLE result = MethodReturn_Create(1, "{\"Message\":\"Turning fan on with Method\"}"); … … 204 205 } 205 206 206 METHODRETURN_HANDLE turnLedOff(Contoso Anemometer* device)207 METHODRETURN_HANDLE turnLedOff(ContosoSecurityCamera* device) 207 208 { 208 209 (void)device; 209 210 (void)printf("\033[44mTurning LED off with Method.\033[49m\r\n"); 210 211 211 ledOn = 0; 212 digitalWrite(LED_G_PIN, HIGH); 213 digitalWrite(LED_R_PIN, HIGH); 214 digitalWrite(LED_B_PIN, HIGH); 212 215 213 216 METHODRETURN_HANDLE result = MethodReturn_Create(0, "{\"Message\":\"Turning fan off with Method\"}"); … … 295 298 EVENT_INSTANCE messages[MESSAGE_COUNT]; 296 299 int msg_id = 0; 297 double avgWindSpeed = 10.0;298 double minTemperature = 20.0;299 double minHumidity = 60.0;300 300 int receiveContext = 0; 301 301 … … 315 315 (void)printf("Failed in serializer_init."); 316 316 } 317 else if (SERIALIZER_REGISTER_NAMESPACE( WeatherStation) == NULL)317 else if (SERIALIZER_REGISTER_NAMESPACE(SecurityStation) == NULL) 318 318 { 319 319 LogError("unable to SERIALIZER_REGISTER_NAMESPACE"); … … 389 389 } 390 390 #endif // SET_TRUSTED_CERT_IN_SAMPLES 391 AnemometerState *anemometerState = IoTHubDeviceTwin_LL_CreateAnemometerState(iotHubClientHandle);391 SecurityCameraState *anemometerState = IoTHubDeviceTwin_LL_CreateSecurityCameraState(iotHubClientHandle); 392 392 if (anemometerState == NULL) 393 393 { 394 printf("Failure in IoTHubDeviceTwin_LL_Create AnemometerState");394 printf("Failure in IoTHubDeviceTwin_LL_CreateSecurityCameraState"); 395 395 } 396 396 else 397 397 { 398 (void)printf("IoTHubDeviceTwin_LL_Create AnemometerState...successful.\r\n");399 } 400 AnemometerSettings *anemometerSettings = IoTHubDeviceTwin_LL_CreateAnemometerSettings(iotHubClientHandle);398 (void)printf("IoTHubDeviceTwin_LL_CreateSecurityCameraState...successful.\r\n"); 399 } 400 SecurityCameraSettings *anemometerSettings = IoTHubDeviceTwin_LL_CreateSecurityCameraSettings(iotHubClientHandle); 401 401 if (anemometerSettings == NULL) 402 402 { 403 printf("Failure in IoTHubDeviceTwin_LL_Create AnemometerSettings");403 printf("Failure in IoTHubDeviceTwin_LL_CreateSecurityCameraSettings"); 404 404 } 405 405 else 406 406 { 407 (void)printf("IoTHubDeviceTwin_LL_Create AnemometerSettings...successful.\r\n");408 } 409 Contoso Anemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer);407 (void)printf("IoTHubDeviceTwin_LL_CreateSecurityCameraSettings...successful.\r\n"); 408 } 409 ContosoSecurityCamera* myWeather = CREATE_MODEL_INSTANCE(SecurityStation, ContosoSecurityCamera); 410 410 if (myWeather == NULL) 411 411 { … … 440 440 unsigned char *msgText; 441 441 size_t msgSize; 442 myWeather-> windSpeed = avgWindSpeed + (rand() % 4 + 2);443 myWeather-> temperature = minTemperature + (rand() % 10);444 myWeather->humidity = minHumidity + (rand() % 20);445 if (SERIALIZE(&msgText, &msgSize, myWeather-> windSpeed, myWeather->temperature, myWeather->humidity) != CODEFIRST_OK)442 myWeather->is_person = yolo_result.person; 443 myWeather->is_car = yolo_result.car; 444 yolo_result.reset = 1; 445 if (SERIALIZE(&msgText, &msgSize, myWeather->is_person, myWeather->is_car) != CODEFIRST_OK) 446 446 { 447 447 (void)printf("Failed to serialize\r\n"); … … 459 459 460 460 propMap = IoTHubMessage_Properties(messages[msgPos].messageHandle); 461 (void)sprintf_s(propText, sizeof(propText), myWeather->temperature > anemometerSettings->threshold.value? "true" : "false");462 if (Map_AddOrUpdate(propMap, " temperatureAlert", propText) != MAP_OK)461 (void)sprintf_s(propText, sizeof(propText), (yolo_result.person > 0) ? "true" : "false"); 462 if (Map_AddOrUpdate(propMap, "personAlert", propText) != MAP_OK) 463 463 { 464 464 (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n"); … … 487 487 anemometerState->threshold.value = anemometerSettings->threshold.value; 488 488 anemometerState->threshold.status = "success"; 489 IoTHubDeviceTwin_LL_SendReportedState AnemometerState(anemometerState, anemometerReportedStateCallback, anemometerState);489 IoTHubDeviceTwin_LL_SendReportedStateSecurityCameraState(anemometerState, anemometerReportedStateCallback, anemometerState); 490 490 } 491 491 iterator++; … … 504 504 505 505 if (anemometerSettings != NULL) 506 IoTHubDeviceTwin_LL_Destroy AnemometerSettings(anemometerSettings);506 IoTHubDeviceTwin_LL_DestroySecurityCameraSettings(anemometerSettings); 507 507 if (anemometerState != NULL) 508 IoTHubDeviceTwin_LL_Destroy AnemometerState(anemometerState);508 IoTHubDeviceTwin_LL_DestroySecurityCameraState(anemometerState); 509 509 if (myWeather != NULL) 510 510 DESTROY_MODEL_INSTANCE(myWeather);
Note:
See TracChangeset
for help on using the changeset viewer.