/**
 * Copyright (c) 2017 China Mobile IOT.
 * All rights reserved.
**/
#include <nbiot.h>
#include <utils.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "delay.h"
#include "SHT20.h"


void write_callback( uint16_t       objid,
                     uint16_t       instid,
                     uint16_t       resid,
                     nbiot_value_t *data )
{
    nbiot_printf( "write /%d/%d/%d\r\n",
                  objid,
                  instid,
                  resid );
}

void execute_callback( uint16_t       objid,
                       uint16_t       instid,
                       uint16_t       resid,
                       nbiot_value_t *data,
                       const void    *buff,
                       size_t         size )
{
    nbiot_printf( "execute /%d/%d/%d\r\n",
                  objid,
                  instid,
                  resid );
    nbiot_buffer_printf( buff, size );
}

#ifdef NOTIFY_ACK
void notify_ack_callback( uint16_t ackid,
                          bool    ack )
{
	nbiot_printf( "data stream %d stat:%s\r\n",
                  ackid,
                  ack ? "true":"false" );
}
#endif

char uri[] = "coap://183.230.40.40:5683";
const char endpoint_name[] = "IMEI;IMSI";
nbiot_device_t *dev = NULL;

nbiot_value_t at;   /* ipso digital input - application type */
time_t last_time=0;
time_t cur_time=0;

void res_update(time_t interval)
{
	     char tmp[100],len;
	     SHT20_INFO sht20;
       if(cur_time>=last_time+interval){ 
            cur_time=0;
            last_time=0;				 
             
				    at.flag |= NBIOT_UPDATED;
				    if(at.value.as_buf.val!=NULL)
            nbiot_free( at.value.as_buf.val );
						
            sht20=SHT20_GetValue();
						sprintf(tmp,"%f",sht20.humidity);
						len=strlen(tmp);
				    tmp[len++]=':';
				   	sprintf(tmp+len,"%f",sht20.tempreture);
            at.value.as_buf.len = strlen(tmp);
            at.value.as_buf.val = nbiot_strdup( tmp, at.value.as_buf.len );

    
			}else if(cur_time==0&&last_time==0){
			
			    cur_time=nbiot_time();
			    last_time=cur_time;
			
			}else{
			
			   cur_time=nbiot_time();
			   
			} 	 

}	
int main( int argc, char *argv[] )
{
   int life_time = 300;
	 int ret;
     nbiot_init_environment( argc, argv );
	   
		 
 #ifdef NOTIFY_ACK
     ret = nbiot_device_create( &dev,
                                   endpoint_name,
                                   uri,
                                   life_time,
                                   write_callback,
                                   execute_callback,
                                   notify_ack_callback );
#else
      ret = nbiot_device_create( &dev,
                                   endpoint_name,
                                   uri,
                                   life_time,
                                   write_callback,
                                   execute_callback );
#endif

        at.type = NBIOT_STRING;;
        at.flag = NBIOT_READABLE | NBIOT_WRITABLE;
        ret = nbiot_resource_add( dev,
                                  3200,
                                  0,
                                  5750,
                                  &at );
        if ( ret )
        {
            nbiot_device_destroy( dev );
            nbiot_printf( "device add resource(/3200/0/5750) failed, code = %d.\r\n", ret );
        }
        ret = nbiot_device_connect( dev,60);

        if ( ret )
        {
            nbiot_device_close( dev, 10);
            nbiot_device_destroy( dev );
            nbiot_printf( "connect server failed.\r\n" );
					 
        }else{			
		        nbiot_printf( "connect server success.\r\n" );
					
				}	
      do
       {
             ret = nbiot_device_step( dev, 1);
             if ( ret )
             {
                 nbiot_printf( "device step error, code = %d.\r\n", ret );
				         break;
             }						 
           res_update(30);						 
			      
      } while(1);
      nbiot_clear_environment();

    nbiot_printf( "something error,please reset\r\n" );

    return 0;
}
