#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdint.h>
#include <syslog.h>
#include <stdarg.h>

#include "../../status.h"

static uint8_t status = 0;

int init(void * param)
{
	return 0;
}

typedef struct sb_t 
{
	double lat, lon;
	unsigned long long imei;
	unsigned char status;
} sb_t;

unsigned int
process(void *lib_data, int socket, unsigned char *data,
        unsigned int length, unsigned long long int *id, time_t * tm,
        double *result_array, uint64_t * sensors, unsigned int *type)
{
    struct sb_t * sb;
    unsigned int ret;
/*
    if (length != sizeof(sb_t))
    {
	printf("Sb: Bad length %d\n", length);
	return 0;
    }
*/
    ret = 0;
    if ((data != NULL) && (length != 0))
    {
	sb = (struct sb_t *) data;
	printf("SB: imei %llu, lat %lf, lon %lf, status %d\n", sb->imei, sb->lat, sb->lon, sb->status);
    
	*id = sb->imei;
	*tm = time(NULL);
//    *tm = localtime(&t);

        result_array[0] = sb->lat;
	result_array[1] = sb->lon;
        result_array[2] = 0.0;
	result_array[3] = 1.0;
        result_array[4] = 0;
        status = sb->status;
	sensors[0] = sensors[1] = sensors[2] = sensors[3] = sensors[4] = 0x10;
        *type = VALUES_TYPE_POS;
	ret = 5;
    }
    else
    {
	if (status == 3)
	{
	    status = 0;
	    
	    result_array[0] = 1;
	    sensors[0] = 250010000;
	    *type = VALUES_TYPE_ALERT;
	    ret = 1;
	}
    }

    return ret;
}
