Beispiel #1
0
void temperatureIOHandler(OCAttributeT *attribute, int IOType, OCResourceHandle handle,
                     bool *underObservation)
{
    if(IOType == INPUT)
    {
        // Read the ADC value
        int reading = analogRead(attribute->port->pin);
        double temperature = reading / TEMPERATURE_CONSTANT;

        if(attribute)
        {
            dtostrf(temperature, 5, 2, attribute->value.data.str);
            OIC_LOG_V(DEBUG, TAG, "temperature is: %s", attribute->value.data.str);
        }

        // Only update if the change is only larger than 0.01 C.
        double floorCurrentTemp = floor(temperature * 10) / 10;
        double floorPrevTemp = floor(tempPrevValue * 10) / 10;
        if(*underObservation && (floorCurrentTemp != floorPrevTemp))
        {
            //OIC_LOG(DEBUG, TAG, "Notifying observers");
            if(OCNotifyAllObservers(handle, OC_NA_QOS) == OC_STACK_NO_OBSERVERS)
            {
                OIC_LOG(DEBUG, TAG, "No more observers!");
                *underObservation = false;
            }
        }

        tempPrevValue = temperature;
    }
    else
    {
        OIC_LOG(ERROR, TAG, "BUTTON is read only!");
    }
}
void *ChangeLightRepresentation (void *param)
{
    (void)param;
    OCStackResult result = OC_STACK_ERROR;

    uint8_t j = 0;
    uint8_t numNotifies = (SAMPLE_MAX_NUM_OBSERVATIONS)/2;
    OCObservationId obsNotify[numNotifies];

    while (!gQuitFlag)
    {
        sleep(3);
        Light.power += 5;
        if (gLightUnderObservation)
        {
            OIC_LOG_V(INFO, TAG, " =====> Notifying stack of new power level %d\n", Light.power);
            if (gObserveNotifyType == 1)
            {
                // Notify list of observers. Alternate observers on the list will be notified.
                j = 0;
                for (uint8_t i = 0; i < SAMPLE_MAX_NUM_OBSERVATIONS; (i=i+2))
                {
                    if (interestedObservers[i].valid == true)
                    {
                        obsNotify[j] = interestedObservers[i].observationId;
                        j++;
                    }
                }

                OCRepPayload* payload = getPayload(gResourceUri, Light.power, Light.state);
                result = OCNotifyListOfObservers (Light.handle, obsNotify, j,
                        payload, OC_NA_QOS);
                OCRepPayloadDestroy(payload);
            }
            else if (gObserveNotifyType == 0)
            {
                // Notifying all observers
                result = OCNotifyAllObservers (Light.handle, OC_NA_QOS);
                if (OC_STACK_NO_OBSERVERS == result)
                {
                    OIC_LOG (INFO, TAG,
                            "=======> No more observers exist, stop sending observations");
                    gLightUnderObservation = 0;
                }
            }
            else
            {
                OIC_LOG (ERROR, TAG, "Incorrect notification type selected");
            }
        }
    }
    return NULL;
}
Beispiel #3
0
void piObserveNotificationUpdate(PIPluginBase * plugin, OCResourceHandle resourceHandle)
{
    if(!plugin)
    {
        return;
    }

    OCStackResult result = OCNotifyAllObservers(resourceHandle, OC_LOW_QOS);
    if(result != OC_STACK_OK && result != OC_STACK_NO_OBSERVERS)
    {
        OIC_LOG_V(ERROR, TAG, "Failed to notify observers of update. Result: %d", result);
    }
}
Beispiel #4
0
int notify_things_observers(const char *uri, const char *query)
{
	THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY);

	int res = 0;

	THINGS_LOG_D(TAG, "uri = %s", uri);
	if (NULL != uri) {
		int remainLen = MAX_RESOURCE_LEN - 1;
		char tempUri[MAX_RESOURCE_LEN] = { 0 };

		if (NULL != g_get_notify_obs_uri) {
			char *temp = g_get_notify_obs_uri(uri, query);
			if (NULL != temp) {
				things_strncpy(tempUri, temp, remainLen);
				remainLen -= strnlen(tempUri, MAX_RESOURCE_LEN - 1);
				things_free(temp);
			}
		}

		if (strnlen(tempUri, MAX_RESOURCE_LEN - 1) < 1) {
			things_strncpy(tempUri, uri, remainLen);
			remainLen -= strnlen(tempUri, MAX_RESOURCE_LEN - 1);
		}

		THINGS_LOG_D(TAG, "%s resource notifies to observers.", tempUri);

		for (int iter = 0; iter < g_builder->res_num; iter++) {
			if (strstr(g_builder->gres_arr[iter]->uri, tempUri) != NULL) {
				OCStackResult ret2 = OCNotifyAllObservers((OCResourceHandle) g_builder->gres_arr[iter]->resource_handle,
									 OC_MEDIUM_QOS);

				THINGS_LOG_D(TAG, "%s resource has notified to observers.", g_builder->gres_arr[iter]->uri);

				if (OC_STACK_OK == ret2) {
					THINGS_LOG_V(TAG, "Success: Sent notification to Observers");
				} else {
					THINGS_LOG_V(TAG, "Failed: No Observers to notify : %d ", ret2);
				}
				res = 1;
				break;
			}
		}
	}

	THINGS_LOG_D(TAG, THINGS_FUNC_EXIT);
	return res;
}
void button_observer()
{
	OCStackResult result = OC_STACK_ERROR;

	if(button.observer) {
		button_get();

		if(button.button != button.button_old || button.touch != button.touch_old) {
			result = OCNotifyAllObservers(button.handle, OC_NA_QOS);
			button.button_old = button.button;
			button.touch_old = button.touch;

			if(result == OC_STACK_NO_OBSERVERS)
				button.observer = 0;
		}
	}
}
void lightIOHandler(OCRepPayloadValue *attribute, OCIOPort *port, OCResourceHandle handle, bool *underObservation)
{
    if(port->type == OUTPUT)
    {
        bool power(false);
        int brightness(0);
       // OIC_LOG(DEBUG, TAG, "LightIOHandler: OUTPUT");
        OCRepPayloadValue *current = attribute;
        while(current != NULL)
        {
            //OIC_LOG_V(DEBUG, TAG, "Attribute name: %s", current->name);
            //OIC_LOG(DEBUG, TAG, "Searching light");
            if(strcmp(current->name, "power") == 0)
            {
                power = current->b;
            }
            else if (strcmp(current->name, "brightness") == 0)
            {
                brightness = current->i;
            }

            current = current->next;
        }

        if(power)
        {
            analogWrite(port->pin, brightness);
        }
        else
        {
            analogWrite(port->pin, 0);
        }

        if(*underObservation)
        {
            OIC_LOG(DEBUG, TAG, "LIGHT: Notifying observers");
            if(OCNotifyAllObservers(handle, OC_LOW_QOS) == OC_STACK_NO_OBSERVERS)
            {
                OIC_LOG(DEBUG, TAG, "No more observers!");
                *underObservation = false;
            }
        }
    }
}
Beispiel #7
0
void *ChangeLightRepresentation (void *param)
{
    (void)param;
    OCStackResult result = OC_STACK_ERROR;
    modCounter += 1;
    if(modCounter % 10 == 0)  // Matching the timing that the Linux Sample Server App uses for the same functionality.
    {
        Light.power += 5;
        if (gLightUnderObservation)
        {
            OC_LOG_V(INFO, TAG, " =====> Notifying stack of new power level %d\n", Light.power);
            result = OCNotifyAllObservers (Light.handle, OC_NA_QOS);
            if (OC_STACK_NO_OBSERVERS == result)
            {
                gLightUnderObservation = 0;
            }
        }
    }
    return NULL;
}
Beispiel #8
0
void buttonIOHandler(OCAttributeT *attribute, int IOType, OCResourceHandle handle,
                     bool *underObservation)
{
    if(IOType == INPUT)
    {
       // OIC_LOG(DEBUG, TAG, "ButtonIOHandler: INPUT");
        bool readValue(false);
        if(digitalRead(attribute->port->pin))
        {
            readValue = true;
        }
        else
        {
            readValue = false;
        }

        if(attribute)
        {
            attribute->value.data.b = readValue;
        }

        // Check if it's being observed
        if(*underObservation && buttonPrevValue != readValue)
        {
            OIC_LOG(DEBUG, TAG, "BUTTON: Notifying observers");
            if(OCNotifyAllObservers(handle, OC_NA_QOS) == OC_STACK_NO_OBSERVERS)
            {
                OIC_LOG(DEBUG, TAG, "No more observers!");
                *underObservation = false;
            }
        }

        buttonPrevValue = readValue;
    }
    else
    {
        OIC_LOG(ERROR, TAG, "BUTTON is read only!");
    }
}
void *ChangeLightRepresentation (void *param)
{
    (void)param;
    OCStackResult result = OC_STACK_ERROR;

    while (!gQuitFlag)
    {
        sleep(10);
        light.power += 5;
        if (gLightUnderObservation)
        {
            OC_LOG_V(INFO, TAG,
                " =====> Notifying stack of new power level %d\n", light.power);
            result = OCNotifyAllObservers (light.handle, OC_NA_QOS);
            if (OC_STACK_NO_OBSERVERS == result)
            {
                gLightUnderObservation = 0;
            }
        }
    }
    return NULL;
}
Beispiel #10
0
void ChangePROXIRepresentation (void *param)
{
    (void)param;
    OCStackResult result = OC_STACK_ERROR;
    float avg[SLAVER_EA] = {0,};

    for (int i = 0; i < SLAVER_EA; i++)
    {
        if ( rssicnt[i] > arraysize - 1)
        {
            rssicnt[i] = rssicnt[i] % arraysize;
        }

        ble.streamDummy(NULL, NULL);
        /*  if( ble.pollingConnect(&slaveList[slaver_num][0]) == false )
            {
                OC_LOG_V("Error : Not Connected.\r\n");
            }*/
        while (ble.pollingConnect(&slaveList[slaver_num][0]) == false)
        {
            ble.streamDummy(NULL, NULL);
        }

        if ( ble.IsConnected() == true )
        {
            // print the string when a newline arrives:
            OC_LOG_V(INFO, TAG, "Connected. (%s)\r\n", slaveList[slaver_num]);

//          time = millis();
            for (int j = 0; j < RSSI_EA; j++)
            {
                rssi[i][rssicnt[i]] = ble.pollingGetRSSI();
                OC_LOG_V(INFO, TAG, "rssi val : %d \r\n", rssi[i][rssicnt[i]]);
                rssicnt[i]++;
            }
//          oldTime = millis();
//          float TpR = (float)(((oldTime-time)/1000.0)/20.0);
//          OC_LOG_V("time per rssi : %d.%03d\r\n", (int)TpR, (int)(TpR*1000.0 -((int)TpR)*1000.0) );

            while ( ble.IsSelfArduino() == false )
                ble.pollingDisconnect();

            slaver_num++;
            slaver_num = slaver_num % SLAVER_EA;
        }

        avg[i] = CalculateExponentialAverage(RSSI_EA, rssi[i], startindex[i], flag[i]);
        Serial.println(avg[i]);
        Serial.print("distance : ");

        PROXI.m_distance[i] = calculateDistance(avg[i], -58);

        if (PROXI.m_distance[i] <= 1)
        {
            PROXI.m_proximity[i] = 1;
        }
        else if (PROXI.m_distance[i] <= 2)
        {
            PROXI.m_proximity[i] = 2;
        }
        else
        {
            PROXI.m_proximity[i] = 3;
        }

        Serial.println(PROXI.m_distance[i]);
        Serial.println(PROXI.m_proximity[i]);
        //Serial.println(calculateDistance(avg[i], -58));
        startindex[i] += RSSI_EA;

        // This call displays the amount of free SRAM available on Arduino
        PrintArduinoMemoryStats();

        if (startindex[i] >= arraysize)
        {
            startindex[i] = 0;
        }

        if (flag[i] < (arraysize / RSSI_EA))
        {
            flag[i]++;
        }
    }

    result = OCNotifyAllObservers (m_handle, OC_NA_QOS);

    if (OC_STACK_NO_OBSERVERS == result)
    {
        OC_LOG_V(INFO, TAG, "g_PROXIUnderObservation is 0." );
        g_PROXIUnderObservation = 0;
    }

}
Beispiel #11
0
void *ChangeLightRepresentation (void *param)
{
    (void)param;
    OCStackResult result = OC_STACK_ERROR;

    uint8_t j = 0;
    uint8_t numNotifies = (SAMPLE_MAX_NUM_OBSERVATIONS)/2;
    OCObservationId obsNotify[numNotifies];

    while (!gQuitFlag)
    {
        sleep(10);
        Light.power += 5;
        if (gLightUnderObservation)
        {
            OC_LOG_V(INFO, TAG, " =====> Notifying stack of new power level %d\n", Light.power);
            if (gObserveNotifyType == 1)
            {
                // Notify list of observers. Alternate observers on the list will be notified.
                j = 0;
                for (uint8_t i = 0; i < SAMPLE_MAX_NUM_OBSERVATIONS; (i=i+2))
                {
                    if (interestedObservers[i].valid == true)
                    {
                        obsNotify[j] = interestedObservers[i].observationId;
                        j++;
                    }
                }

                cJSON *json = cJSON_CreateObject();
                cJSON *format;
                cJSON_AddStringToObject(json,"href",gResourceUri);
                cJSON_AddItemToObject(json, "rep", format=cJSON_CreateObject());
                cJSON_AddStringToObject(format, "state", (char *) (Light.state ? "on":"off"));
                cJSON_AddNumberToObject(format, "power", Light.power);
                char * obsResp = cJSON_Print(json);
                cJSON_Delete(json);
                result = OCNotifyListOfObservers (Light.handle, obsNotify, j,
                        (unsigned char *)obsResp, OC_NA_QOS);
                free(obsResp);
            }
            else if (gObserveNotifyType == 0)
            {
                // Notifying all observers
                result = OCNotifyAllObservers (Light.handle, OC_NA_QOS);
                if (OC_STACK_NO_OBSERVERS == result)
                {
                    OC_LOG (INFO, TAG,
                            "=======> No more observers exist, stop sending observations");
                    gLightUnderObservation = 0;
                }
            }
            else
            {
                OC_LOG (ERROR, TAG, "Incorrect notification type selected");
            }
        }
#ifdef WITH_PRESENCE
        if(stopPresenceCount > 0)
        {
            OC_LOG_V(INFO, TAG, "================ presence count %d", stopPresenceCount);
        }
        if(!stopPresenceCount--)
        {
            OC_LOG(INFO, TAG, "================ stopping presence");
            OCStopPresence();
        }
#endif
    }
    return NULL;
}
Beispiel #12
0
void *ChangeTHRepresentation (void *param)
{
    (void)param;
    OCStackResult result = OC_STACK_ERROR;
    modCounter += 1;
    if (modCounter % 10 ==
        0) // Matching the timing that the Linux Sample Server App uses for the same functionality.
    {

        byte dht11_dat[5];
        byte i;// start condition

        digitalWrite(dht11_pin, LOW);
        delay(18);
        digitalWrite(dht11_pin, HIGH);
        delayMicroseconds(1);
        pinMode(dht11_pin, INPUT);
        delayMicroseconds(40);

        if (digitalRead(dht11_pin))
        {
            Serial.println("dht11 start condition 1 not met"); // wait for DHT response signal: LOW
            delay(1000);
            return NULL;
        }
        delayMicroseconds(80);
        if (!digitalRead(dht11_pin))
        {
            Serial.println("dht11 start condition 2 not met");  //wair for second response signal:HIGH
            return NULL;
        }

        delayMicroseconds(80);// now ready for data reception
        for (i = 0; i < 5; i++)
        {
            dht11_dat[i] = read_dht11_dat();
        }  //recieved 40 bits data. Details are described in datasheet

        pinMode(dht11_pin, OUTPUT);
        digitalWrite(dht11_pin, HIGH);
        byte dht11_check_sum = dht11_dat[0] + dht11_dat[2]; // check check_sum
        if (dht11_dat[4] != dht11_check_sum)
        {
            Serial.println("DHT11 checksum error");
        }
        Serial.print("Current humdity = ");
        Serial.print(dht11_dat[0], DEC);
        Serial.print("%  ");
        Serial.print("temperature = ");
        Serial.print(dht11_dat[2], DEC);
        Serial.println("C  ");

        TH.m_humid = dht11_dat[0];
        TH.m_temp = dht11_dat[2];

        if (g_THUnderObservation)
        {
            OC_LOG_V(INFO, TAG, " =====> Notifying stack of new humid level %d\n", TH.m_humid);
            OC_LOG_V(INFO, TAG, " =====> Notifying stack of new temp level %d\n", TH.m_temp);

            result = OCNotifyAllObservers (TH.m_handle, OC_NA_QOS);

            if (OC_STACK_NO_OBSERVERS == result)
            {
                g_THUnderObservation = 0;
            }
        }
    }
    return NULL;
}