Ignore:
Timestamp:
Sep 25, 2020, 7:28:35 PM (4 years ago)
Author:
coas-nagasima
Message:

Azure IoT への送信内容を、検出したpersonとcarの数を送信するよう変更。
Azure IoT CentralからLEDのON/OFFが出来るよう更新。

File:
1 edited

Legend:

Unmodified
Added
Removed
  • azure_iot_hub_riscv/trunk/app_iothub_client/src/client.c

    r453 r459  
    2323#include "serializer_devicetwin.h"
    2424#include "client.h"
     25#include "kpu_main.h"
    2526
    2627#ifdef _MSC_VER
     
    4142bool g_use_proxy;
    4243HTTP_PROXY_OPTIONS g_proxy_options;
    43 int ledOn;
    4444
    4545static int callbackCounter;
     
    133133
    134134// 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),
     135BEGIN_NAMESPACE(SecurityStation);
     136
     137DECLARE_MODEL(ContosoSecurityCamera,
     138WITH_DATA(int, is_person),
     139WITH_DATA(int, is_car),
    141140WITH_METHOD(quit),
    142141WITH_METHOD(turnLedOn),
     
    153152);
    154153
    155 DECLARE_DEVICETWIN_MODEL(AnemometerState,
     154DECLARE_DEVICETWIN_MODEL(SecurityCameraState,
    156155WITH_REPORTED_PROPERTY(ThresholdR, threshold)
    157156);
    158157
    159 DECLARE_DEVICETWIN_MODEL(AnemometerSettings,
     158DECLARE_DEVICETWIN_MODEL(SecurityCameraSettings,
    160159WITH_DESIRED_PROPERTY(ThresholdD, threshold, onDesiredThreshold)
    161160);
    162161
    163 END_NAMESPACE(WeatherStation);
     162END_NAMESPACE(SecurityStation);
    164163
    165164void anemometerReportedStateCallback(int status_code, void* userContextCallback)
    166165{
    167         AnemometerState *anemometer = (AnemometerState *)userContextCallback;
     166        SecurityCameraState *anemometer = (SecurityCameraState *)userContextCallback;
    168167
    169168        printf("received states \033[43m%d\033[49m, reported threshold = %.1f\n", status_code, anemometer->threshold.value);
     
    174173        // Note: The argument is NOT a pointer to threshold, but instead a pointer to the MODEL
    175174        //       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;
    179178        printf("received a new desired.threshold = \033[42m%.1f\033[49m\n", anemometer->threshold.value);
    180179
     
    182181}
    183182
    184 METHODRETURN_HANDLE quit(ContosoAnemometer* device)
     183METHODRETURN_HANDLE quit(ContosoSecurityCamera* device)
    185184{
    186185        (void)device;
     
    193192}
    194193
    195 METHODRETURN_HANDLE turnLedOn(ContosoAnemometer* device)
     194METHODRETURN_HANDLE turnLedOn(ContosoSecurityCamera* device)
    196195{
    197196        (void)device;
    198197        (void)printf("\033[41mTurning LED on with Method.\033[49m\r\n");
    199198
    200         ledOn = 1;
     199        digitalWrite(LED_G_PIN, LOW);
     200        digitalWrite(LED_R_PIN, LOW);
     201        digitalWrite(LED_B_PIN, LOW);
    201202
    202203        METHODRETURN_HANDLE result = MethodReturn_Create(1, "{\"Message\":\"Turning fan on with Method\"}");
     
    204205}
    205206
    206 METHODRETURN_HANDLE turnLedOff(ContosoAnemometer* device)
     207METHODRETURN_HANDLE turnLedOff(ContosoSecurityCamera* device)
    207208{
    208209        (void)device;
    209210        (void)printf("\033[44mTurning LED off with Method.\033[49m\r\n");
    210211
    211         ledOn = 0;
     212        digitalWrite(LED_G_PIN, HIGH);
     213        digitalWrite(LED_R_PIN, HIGH);
     214        digitalWrite(LED_B_PIN, HIGH);
    212215
    213216        METHODRETURN_HANDLE result = MethodReturn_Create(0, "{\"Message\":\"Turning fan off with Method\"}");
     
    295298        EVENT_INSTANCE messages[MESSAGE_COUNT];
    296299        int msg_id = 0;
    297         double avgWindSpeed = 10.0;
    298         double minTemperature = 20.0;
    299         double minHumidity = 60.0;
    300300        int receiveContext = 0;
    301301
     
    315315                        (void)printf("Failed in serializer_init.");
    316316                }
    317                 else if (SERIALIZER_REGISTER_NAMESPACE(WeatherStation) == NULL)
     317                else if (SERIALIZER_REGISTER_NAMESPACE(SecurityStation) == NULL)
    318318                {
    319319                        LogError("unable to SERIALIZER_REGISTER_NAMESPACE");
     
    389389                                }
    390390#endif // SET_TRUSTED_CERT_IN_SAMPLES
    391                                 AnemometerState *anemometerState = IoTHubDeviceTwin_LL_CreateAnemometerState(iotHubClientHandle);
     391                                SecurityCameraState *anemometerState = IoTHubDeviceTwin_LL_CreateSecurityCameraState(iotHubClientHandle);
    392392                                if (anemometerState == NULL)
    393393                                {
    394                                         printf("Failure in IoTHubDeviceTwin_LL_CreateAnemometerState");
     394                                        printf("Failure in IoTHubDeviceTwin_LL_CreateSecurityCameraState");
    395395                                }
    396396                                else
    397397                                {
    398                                         (void)printf("IoTHubDeviceTwin_LL_CreateAnemometerState...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);
    401401                                if (anemometerSettings == NULL)
    402402                                {
    403                                         printf("Failure in IoTHubDeviceTwin_LL_CreateAnemometerSettings");
     403                                        printf("Failure in IoTHubDeviceTwin_LL_CreateSecurityCameraSettings");
    404404                                }
    405405                                else
    406406                                {
    407                                         (void)printf("IoTHubDeviceTwin_LL_CreateAnemometerSettings...successful.\r\n");
    408                                 }
    409                                 ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer);
     407                                        (void)printf("IoTHubDeviceTwin_LL_CreateSecurityCameraSettings...successful.\r\n");
     408                                }
     409                                ContosoSecurityCamera* myWeather = CREATE_MODEL_INSTANCE(SecurityStation, ContosoSecurityCamera);
    410410                                if (myWeather == NULL)
    411411                                {
     
    440440                                                unsigned char *msgText;
    441441                                                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)
    446446                                                {
    447447                                                        (void)printf("Failed to serialize\r\n");
     
    459459
    460460                                                        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)
    463463                                                        {
    464464                                                                (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
     
    487487                                                anemometerState->threshold.value = anemometerSettings->threshold.value;
    488488                                                anemometerState->threshold.status = "success";
    489                                                 IoTHubDeviceTwin_LL_SendReportedStateAnemometerState(anemometerState, anemometerReportedStateCallback, anemometerState);
     489                                                IoTHubDeviceTwin_LL_SendReportedStateSecurityCameraState(anemometerState, anemometerReportedStateCallback, anemometerState);
    490490                                        }
    491491                                        iterator++;
     
    504504
    505505                                if (anemometerSettings != NULL)
    506                                         IoTHubDeviceTwin_LL_DestroyAnemometerSettings(anemometerSettings);
     506                                        IoTHubDeviceTwin_LL_DestroySecurityCameraSettings(anemometerSettings);
    507507                                if (anemometerState != NULL)
    508                                         IoTHubDeviceTwin_LL_DestroyAnemometerState(anemometerState);
     508                                        IoTHubDeviceTwin_LL_DestroySecurityCameraState(anemometerState);
    509509                                if (myWeather != NULL)
    510510                                        DESTROY_MODEL_INSTANCE(myWeather);
Note: See TracChangeset for help on using the changeset viewer.