int recorder::init(OBJECT *parent)
{
	// check the connection
	if ( get_connection()!=NULL )
		db = (database*)(get_connection()+1);
	if ( db==NULL )
		exception("no database connection available or specified");
	if ( !db->isa("database") )
		exception("connection is not a mysql database");
	gl_verbose("connection to mysql server '%s', schema '%s' ok", db->get_hostname(), db->get_schema());

	// check mode
	if ( strlen(mode)>0 )
	{
		options = 0xffffffff;
		struct {
			char *str;
			set bits;
		} modes[] = {
			{"r",	0xffff},
			{"r+",	0xffff},
			{"w",	MO_DROPTABLES},
			{"w+",	MO_DROPTABLES},
			{"a",	0x0000},
			{"a+",	0x0000},
		};
		int n;
		for ( n=0 ; n<sizeof(modes)/sizeof(modes[0]) ; n++ )
		{
			if ( strcmp(mode,modes[n].str)==0 )
			{
				options = modes[n].bits;
				break;
			}
		}
		if ( options==0xffffffff )
			exception("mode '%s' is not recognized",(const char*)mode);
		else if ( options==0xffff )
			exception("mode '%s' is not valid for a recorder", (const char*)mode);
	}

	// connect the target property
	if ( get_parent()==NULL )
		exception("parent is not set");
	target.set_object(get_parent());

	printf("****************************************before switch*********************************************************\n");

	char propname[64]="", propunit[64]="";
	switch ( sscanf(get_property(),"%[^[][%[^]]",propname,propunit) ) {
	case 2:
		printf("****************************************case 2*********************************************************\n");
		if ( !unit.set_unit(propunit) )
			exception("property '%s' has an invalid unit", get_property());
		// drop through
	case 1:
		printf("****************************************case 1*********************************************************\n");
		printf("propname : %s \n", propname);
		printf("propunit : %s \n", propunit);
		printf("field    : %s \n", field);
		strncpy(field, propname, sizeof(field) - 1);
		printf("****************************************before set property*********************************************************\n");
		target.set_property(propname);
		printf("****************************************after set property*********************************************************\n");
		
		scale = 1.0;
		if ( unit.is_valid() && target.get_unit()!=NULL )
		{
			target.get_unit()->convert(unit,scale);
			sprintf(field,"%s[%s]",propname,propunit);
		}
		else if ( propunit[0]=='\0' && options&MO_USEUNITS && target.get_unit() )
			sprintf(field,"%s[%s]",propname,target.get_unit()->get_name());
		break;
	default:
		printf("****************************************not valid*********************************************************\n");
		exception("property '%s' is not valid", get_property());
		break;
	}
	printf("****************************************after switch*********************************************************\n");

	// check for table existence and create if not found
	if ( target.is_valid() )
	{
		// drop table if exists and drop specified
		if ( db->table_exists(get_table()) )
		{
			if ( get_options()&MO_DROPTABLES && !db->query("DROP TABLE IF EXISTS `%s`", get_table()) )
				exception("unable to drop table '%s'", get_table());
		}
		
		// create table if not exists
		if ( !db->table_exists(get_table()) )
		{
			if ( !(options&MO_NOCREATE) )
			{
				if ( !db->query("CREATE TABLE IF NOT EXISTS `%s` ("
					"id INT AUTO_INCREMENT PRIMARY KEY, "
					"t TIMESTAMP, "
					"`%s` %s, "
					"INDEX i_t (t) "
					")", 
					get_table(), (const char*)field, db->get_sqltype(target)) )
					exception("unable to create table '%s' in schema '%s'", get_table(), db->get_schema());
				else
					gl_verbose("table %s created ok", get_table());
			}
			else
				exception("NOCREATE option prevents creation of table '%s'", get_table());
		}

		// check row count
		else 
		{
			if ( db->select("SELECT count(*) FROM `%s`", get_table())==NULL )
				exception("unable to get row count of table '%s'", get_table());

			gl_verbose("table '%s' ok", get_table());
		}
	}
	else
	{
		exception("property '%s' is not valid", get_property());
		return 0;
	}

	// set heartbeat
	if ( interval>0 )
	{
		set_heartbeat((TIMESTAMP)interval);
		enabled = true;
	}

	// arm trigger, if any
	if ( enabled && trigger[0]!='\0' )
	{
		// read trigger condition
		if ( sscanf(trigger,"%[<>=!]%s",compare_op,compare_val)==2 )
		{
			// rescale comparison value if necessary
			if ( scale!=1.0 )
				sprintf(compare_val,"%g",atof(compare_val)/scale);

			// enable trigger and suspend data collection
			trigger_on=true;
			enabled=false;
		}
	}

	// prepare insert statement
	char statement[1024];
	int len = sprintf(statement,"INSERT INTO `%s` (`%s`) VALUES ('?')", get_table(), (const char*)field);
	gl_verbose("preparing statement '%s'", statement);
	printf("****************************************preparing statement*********************************************************\n");
	printf("preparing statement '%s' \n", statement);
	insert = mysql_stmt_init(db->get_handle());
	if ( !db->get_sqlbind(value,target,&stmt_error) )
	{
		gl_warning("unable to bind target '%s'", target.get_name());
		mysql_stmt_close(insert);
		insert = NULL;
	}
	else if ( mysql_stmt_prepare(insert,statement,len)!=0 )
	{
		gl_warning("insert statement '%s' prepare failed (error='%s'), using slowing 'INSERT' method", statement, mysql_stmt_error(insert));
		mysql_stmt_close(insert);
		insert = NULL;
	}
	else if ( mysql_stmt_bind_param(insert,&value)!=0 )
	{
		gl_warning("insert statement '%s' bind failed (error='%s'), using slowing 'INSERT' method", statement, mysql_stmt_error(insert));
		mysql_stmt_close(insert);
		insert = NULL;
	}

	return 1;
}
Exemple #2
0
int collector::init(OBJECT *parent)
{
	// check the connection
	if ( get_connection()!=NULL )
		db = (database*)(get_connection()+1);
	if ( db==NULL )
		exception("no database connection available or specified");
	if ( !db->isa("database") )
		exception("connection is not a mysql database");
	gl_verbose("connection to mysql server '%s', schema '%s' ok", db->get_hostname(), db->get_schema());

	// check mode
	if ( strlen(mode)>0 )
	{
		options = 0xffffffff;
		struct {
			char *str;
			set bits;
		} modes[] = {
			{"r",	0xffff},
			{"r+",	0xffff},
			{"w",	MO_DROPTABLES},
			{"w+",	MO_DROPTABLES},
			{"a",	0x0000},
			{"a+",	0x0000},
		};
		int n;
		for ( n=0 ; n<sizeof(modes)/sizeof(modes[0]) ; n++ )
		{
			if ( strcmp(mode,modes[n].str)==0 )
			{
				options = modes[n].bits;
				break;
			}
		}
		if ( options==0xffffffff )
			exception("mode '%s' is not recognized",(const char*)mode);
		else if ( options==0xffff )
			exception("mode '%s' is not valid for a recorder", (const char*)mode);
	}

	// verify group is defined
	if ( strcmp(get_group(),"")==0 )
		exception("group must be specified");

	// verify property is defined
	if ( strcmp(get_property(),"")==0 )
		exception("at least one property aggregation must be specified");

	// copy property spec into working buffer
	char1024 propspecs;
	strcpy(propspecs,get_property());

	// count properties in specs
	char *p = propspecs;
	n_aggregates = 0;
	do {
		n_aggregates++;
		p = strchr(p,',');
	} while (p++!=NULL);
	list = new gld_aggregate[n_aggregates];
	names = new char*[n_aggregates];

	// load property structs
	int n;
	for ( p=propspecs,n=0 ; n<n_aggregates ; n++ )
	{
		char *np = strchr(p,',');
		if ( np!=NULL ) *np='\0';
		while ( *p!='\0' && isspace(*p) ) p++; // left trim
		int len = strlen(p);
		while ( len>0 && isspace(p[len-1]) ) p[--len]='\0'; // right trim
		if ( !list[n].set_aggregate(p,get_group()) )
			exception("unable to aggregate '%s' over group '%s'", p, get_group());
		gl_debug("%s: group '%s' aggregate '%s' initial value is '%lf'", get_name(), get_group(), p, list[n].get_value());
		names[n] = new char[strlen(p)+1];
		strcpy(names[n],p);
		p = np+1;
	}
	gl_debug("%s: %d aggregates ok", get_name(), n_aggregates);

	// drop table if exists and drop specified
	if ( db->table_exists(get_table()) )
	{
		if ( get_options()&MO_DROPTABLES && !db->query("DROP TABLE IF EXISTS `%s`", get_table()) )
			exception("unable to drop table '%s'", get_table());
	}
	
	// create table if not exists
	if ( !db->table_exists(get_table()) )
	{
		if ( !(options&MO_NOCREATE) )
		{
			char buffer[4096];
			size_t eos = sprintf(buffer,"CREATE TABLE IF NOT EXISTS `%s` ("
				"id INT AUTO_INCREMENT PRIMARY KEY, "
				"t TIMESTAMP, ", get_table());
			int n;
			for ( n=0 ; n<n_aggregates ; n++ )
				eos += sprintf(buffer+eos,"`%s` double, ",names[n]);
			eos += sprintf(buffer+eos,"%s","INDEX i_t (t))");

			if ( !db->query(buffer) )
				exception("unable to create table '%s' in schema '%s'", get_table(), db->get_schema());
			else
				gl_verbose("table %s created ok", get_table());
		}
		else
			exception("NOCREATE option prevents creation of table '%s'", get_table());
	}

	// check row count
	else 
	{
		if ( db->select("SELECT count(*) FROM `%s`", get_table())==NULL )
			exception("unable to get row count of table '%s'", get_table());

		gl_verbose("table '%s' ok", get_table());
	}
	
	// first event time
	TIMESTAMP dt = (TIMESTAMP)get_interval();
	if ( dt>0 )
		next_t = (TIMESTAMP)(gl_globalclock/dt+1)*dt;
	else if ( dt==0 )
		next_t = TS_NEVER;
	else
		exception("%s: interval must be zero or positive");
		
	// @todo use prepared statement instead of insert

	// set heartbeat
	if ( interval>0 )
		set_heartbeat((TIMESTAMP)interval);

	return 1;
}
Exemple #3
0
void client_process(void)
{
	fd_set readfds;		
	int ret;
	int num_read_from_socket;
	int num_to_send;	
	int res = -1;
	struct timeval tv;

	sys_log(FUNC, LOG_MSG, "start");
	while (1)
	{			
		heartbeat_timeout ++;
		sys_log(FUNC, LOG_DBG, "heartbeat_timeout = %d heartbeat_s=%d",heartbeat_timeout, heartbeat_s);
		if (heartbeat_timeout > heartbeat_s){
			sys_log(FUNC, LOG_ERR, "heartbeat timeout  --> reconnect");
			set_heartbeat(HEARTBEAT_OFFLINE,  HEARTBEAT_TIMEOUT);			
		}
		sleep(1);		
		if (g_reconnect_flag == RECONNECT_ON || g_reconnect_flag == HEARTBEAT_OFFLINE){
			if (SUCCESS == client_reconnect()){
				g_reconnect_flag = RECONNECT_OFF;	
			}			
		}
		if (g_sockfd_client <=0){
			//sys_log(FUNC, LOG_WARN, "g_sockfd_client");
			g_reconnect_flag = RECONNECT_ON;
			continue;
		}	
		
		FD_ZERO(&readfds);
		FD_SET(g_sockfd_client, &readfds);
		tv.tv_sec = 0;
		tv.tv_usec = 1000;		

		res = select(g_sockfd_client + 1, &readfds, NULL, NULL, &tv);
		if (res < 0){
			continue;
		}else if (res > 0){
			if (FD_ISSET(g_sockfd_client, &readfds))
				num_read_from_socket = read(g_sockfd_client, buf, 100);
		}else{
			continue;
		}
		
		if(num_read_from_socket <= 0){
			continue;
		}
		sys_log(FUNC, LOG_WARN, "recv %d bytes!\n",num_read_from_socket);
		printf_hex(buf,num_read_from_socket);		

		if (check_head(buf) == -1 ){
			continue;
		}		

		g_sockfd_client_status = SOCKFD_CLIENT_OK;
		
		switch (buf[2]){
			case PROTOCOL_GET_DEVICE_ATTR:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_GET_DEVICE_ATTR");
				
				if(!check_for_get_device_attr(buf,num_read_from_socket))
				{
					num_to_send = make_ack_get_device_attr(buf_send);
					ret = write(g_sockfd_client, buf_send, num_to_send);
					if(ret != num_to_send)
						printf("write socket error!\n");
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);
				}
				break;
			case PROTOCOL_SET_DEVICE_ATTR:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_SET_DEVICE_ATTR");
				if(!check_for_set_device_attr(buf,num_read_from_socket))
				{
					/*todo set device attr
						传感器有好几类,此处只处理报警传感器
					*/
					set_alarmin_device_attr(buf);
				
					num_to_send = make_ack_set_device_attr(buf_send);
					ret = write(g_sockfd_client, buf_send, num_to_send);
					if(ret != num_to_send)
						printf("write socket error!\n");
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);
				}
				break;
			case PROTOCOL_GET_ALARM_STATUS:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_GET_ALARM_STATUS");
				if(!check_for_get_alarm_status(buf,num_read_from_socket))
				{					
					/*alarmin*/
					
					num_to_send = make_ack_get_alarm_status(buf_send, g_alarm_in);
					ret = write(g_sockfd_client, buf_send, num_to_send);
					if(ret != num_to_send) 
						printf("write socket error!\n");
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);
				}
				break;
			case PROTOCOL_GET_TIME:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_GET_TIME");
				/*TODO ?*/
				
				break;
			case PROTOCOL_SET_TIME:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_SET_TIME");				
				if(!check_for_set_time(buf,num_read_from_socket))
				{
					/* TODO */
					/*set time*/
				
					num_to_send = make_ack_set_time(buf_send);
					ret = write(g_sockfd_client, buf_send, num_to_send);
					if(ret != num_to_send)
						printf("write socket error!\n");
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);
				}
				break;
			case PROTOCOL_SET_HEARTBEAT:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_SET_HEARTBEAT");
				if(!check_for_set_heartbeat(buf,num_read_from_socket))
				{
					num_to_send = make_ack_set_heartbeat(buf_send,heartbeat_s);
					ret = write(g_sockfd_client, buf_send, num_to_send);
					if(ret != num_to_send)
						printf("write socket error!\n");
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);

					set_heartbeat(HEARTBEAT_ONLINE, buf[4]);	
					led_ctrl(LED_D3_ALARM_SERVER_STATUS, LED_ON);
				}
				break;
			case PROTOCOL_GET_UART_QTY:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_GET_UART_QTY");
				if(!check_for_get_uart_qty(buf,num_read_from_socket))
				{					
					num_to_send = make_ack_get_uart_qty(buf_send);
					ret = write(g_sockfd_client, buf_send, num_to_send);
					if(ret != num_to_send) 
						printf("write socket error!\n");
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);
				}

				break;
			case PROTOCOL_GET_UART_ATTR:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_GET_UART_ATTR");
				if (!check_for_get_uart_attr(buf, num_read_from_socket)){
					num_to_send = make_ack_get_uart_attr(buf_send, buf);
					ret = write(g_sockfd_client, buf_send, num_to_send);
					if(ret != num_to_send) 
						printf("write socket error!\n");
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);
				}
				
				break;
			case PROTOCOL_SET_UART_ATTR:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_SET_UART_ATTR");
				if (!check_for_set_uart_attr(buf, num_read_from_socket)){
					num_to_send=make_ack_set_uart_attr(buf_send,buf);
					ret = write(g_sockfd_client, buf_send, num_to_send);
					if(ret != num_to_send) 
						printf("write socket error!\n");
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);
				}
				break;
			case PROTOCOL_QUERY_UART_SENDBUF:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_QUERY_UART_SENDBUF");
				if (!check_for_query_uart_sendbuf(buf, num_read_from_socket)){
					num_to_send = make_ack_query_uart_sendbuf(buf_send);

					/*TODO :buf*/

					ret = write(g_sockfd_client, buf_send, num_to_send);
					if(ret != num_to_send) 
						printf("write socket error!\n");
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);
				}
			break;

			case PROTOCOL_UART_SEND_DATA:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_UART_SEND_DATA");
				if (!check_for_uart_send_data(buf, num_read_from_socket)){
					num_to_send = make_ack_uart_send_data(buf_send);

					/*TODO*/

					ret = write(g_sockfd_client, buf_send, num_to_send);
					if(ret != num_to_send) 
						printf("write socket error!\n");
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);
				}
			break;
			case PROTOCOL_SET_UART_RECV_ATTR:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_SET_UART_RECV_ATTR");
				if (!check_for_set_uart_recv_attr(buf, num_read_from_socket)){
					
					/*TODO*/

					
				}
			break;

			case PROTOCOL_QUERY_UART_RECV_DATA:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_QUERY_UART_RECV_DATA");
				if (!check_for_query_uart_recv_data(buf, num_read_from_socket)){
					num_to_send = make_ack_query_uart_recv_data(buf_send);

					/*TODO*/

					ret = write(g_sockfd_client, buf_send, num_to_send);
					if(ret != num_to_send) 
						printf("write socket error!\n");
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);
				}
			break;

			case PROTOCOL_GET_IO_NUM:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_GET_IO_NUM");
				if (!check_for_get_io_num(buf, num_read_from_socket)){
					num_to_send = make_ack_get_io_num(buf_send);

					/*TODO*/

					ret = write(g_sockfd_client, buf_send, num_to_send);
					if(ret != num_to_send) 
						printf("write socket error!\n");
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);
				}
			break;

			case PROTOCOL_SET_IO:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_SET_IO");
				if (!check_for_set_io(buf, num_read_from_socket)){
					
					/*TODO*/

					set_io_out(buf);					
				}
			break;

			case PROTOCOL_GET_IO_STATUS:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_GET_IO_STATUS");
				if (!check_for_get_io_status(buf, num_read_from_socket)){
								
					num_to_send = make_ack_get_io_status(buf_send, buf);


					ret = write(g_sockfd_client, buf_send, num_to_send);
					if(ret != num_to_send) 
						printf("write socket error!\n");
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);
				}
			break;

			case PROTOCOL_GET_TEMP_NUM:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_GET_TEMP_NUM");
				if (!check_for_get_temp_num(buf, num_read_from_socket)){
								
					num_to_send = make_ack_get_temp_num(buf_send);


					ret = write(g_sockfd_client, buf_send, num_to_send);
					if(ret != num_to_send) 
						printf("write socket error!\n");
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);
				}
			break;

			case PROTOCOL_SET_TEMP:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_SET_TEMP");

				set_temp(buf);
			
			break;

			case PROTOCOL_GET_TEMP_STATUS:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_GET_TEMP_STATUS");
				if (!check_for_get_temp_status(buf, num_read_from_socket)){
								
					num_to_send = make_ack_get_temp_status(buf_send);


					ret = write(g_sockfd_client, buf_send, num_to_send);
					if(ret != num_to_send) 
						printf("write socket error!\n");
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);
				}
			break;

#if 0
			case PROTOCOL_GET_SENSOR_TYPE:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_GET_SENSOR_TYPE");
				if (!check_for_get_sensor_type(buf, num_read_from_socket)){
					num_to_send = make_ack_get_sensor_type(buf_send);

					ret = write(g_sockfd_client, buf_send, num_to_send);
					if (ret != num_to_send){
						printf("write socket error!\n");
					}
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);
				}
			break;

			case PROTOCOL_GET_SENSOR_DATA:
				sys_log(FUNC, LOG_DBG, "PROTOCOL_GET_SENSOR_DATA");
				if (!check_for_get_sensor_data(buf, num_read_from_socket)){
					num_to_send = make_ack_get_sensor_data(buf_send);

					ret = write(g_sockfd_client, buf_send, num_to_send);
					if (ret != num_to_send){
						printf("write socket error!\n");
					}
					sys_log(FUNC, LOG_DBG, "send %d bytes", num_to_send);
					printf_hex(buf_send, num_to_send);
				}
			break;
#endif			
			default:
				sys_log(FUNC, LOG_ERR, "wrong cmd from server ");
				break;				
		}
		
	}	
}