#!/usr/bin/python3
# -*- coding: utf-8 -*-

import socket
import time
import pprint

import sys
sys.path.insert(0,'..')

from functions import *

pp = pprint.PrettyPrinter(indent=4)
port = 1234

f = open('new struct.log','bw')

	
dataset = """
/*-------RAW-----------*/
	uint32_t pressure_cis_raw;    // pack 1 x 4
	uint32_t temperature_cis_raw; //    + 1 x 4
	
	uint32_t pressure_i2c_raw;
	uint32_t temperature_i2c_raw;
	
	uint32_t temperature_spi_raw;
	uint32_t adc_temperature_raw;
	
	int32_t  temperature_mpu_raw;
	uint32_t adc_temperature_internal_raw;
	
	uint32_t	adc_voltage_raw;
	uint32_t	adc_current_raw;
	
	struct vector_Uint accel_raw; // pack 3 x 4
	struct vector_Uint gyro_raw;  //    + 3 x 4
	
	/*-----Computed--------*/
	
	double pressure_cis; // no packing needed 1 x 8
	
	double temperature_cis;	
	
	double pressure_i2c;	
	
	double temperature_i2c;	
	
	double temperature_spi;	
	
	double adc_temperature;	
	
	double adc_temperature_internal;			
	
	double adc_voltage;	

	double adc_current;		

	double temperature_mpu;	
	
	struct vector accel; // no packing needed 3 x 8
	struct vector gyro;
	struct vector rot;

	/*-----------gps------------*/
	int32_t longitude;
	int32_t longitude_mod2;
	
	int32_t latitude;
	int32_t latitude_mod2;
	
	int32_t altitude;
	int32_t altitude_mod2;
	
	uint32_t time;
	uint32_t time_mod2;
	
	uint32_t hdop;
	uint32_t hdop_mod2;
	/*--------------------------*/	

	/*---------magnet-----------*/
	struct vector_Uint mag_raw;  // pack 3 x 4
	int32_t temperature_mag_raw; //    + 1 x 4
	
	struct vector mag;

	double temperature_mag;
	/*--------------------------*/
	uint32_t count; // pack 1 x 4
	uint32_t crc;   //    + 1 x 4
"""
print("master:")
master = PktParser(dataset)
print("slave:")
slave = PktParser(dataset, master.end)

#x = master.parse(bytes("\00\00\00\01"*slave.end, 'ascii'))

#pp.pprint(x)



udpServer = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udpServer.bind(("", port))

print("UDP Server is listening on port " + str(port))
	
while True:
	try:
		data, addr = udpServer.recvfrom(2048)

	except WindowsError as e:
		print(e)
		time.sleep(1)
		continue
	if data == None:
			continue
	f.write(data + bytes("\r\n", 'ascii'))
	
	
	pp.pprint(master.parse(data))
	pp.pprint(slave.parse(data))
	
	
	
	
	