#include "node_data_acquisition.h"
#include "node_message.h"
#include "malloc.h"

#include <math.h>
uint8_t protocol_485;                      //485回调函数会调用那个callback dlt645还是modbus dlt645为2 modbus为1
void data_acquisition(void)
{
	NODE_DEVICE_PARAMS *node;
	node = get_node_receive_params(); 
	int i = 0;
	while (i != 20)
	{
		if (node->params[i].protcol == MODBUS_READ)
		{
			protocol_485=1;
			uint16_t data[node->params[i].modbus_read->registerLength / 2]; // modbus寄存器长度
			mmodbus_set16bitOrder(node->params[i].modbus_read->bigLittleFormat);
			if (node->params[i].modbus_read->functionCode == 0x03)
			{
				bool success = mmodbus_readHoldingRegisters16i(node->params[i].modbus_read->slaveAddress,
															   node->params[i].modbus_read->registerAddress,
															   node->params[i].modbus_read->registerLength / 2,
															   data);
				if (success)
				{
					uint32_t value;
					if (node->params[i].modbus_read->registerLength == 4)
					{
						value = (uint32_t)data[0] | data[1];
						node->params[i].modbus_read->rxLen = 4;
					}
					else if (node->params[i].modbus_read->registerLength == 2)
					{
						value = data[0];
						node->params[i].modbus_read->rxLen = 4;
					}
					if (node->params[i].modbus_read->precision == 0)
					{
						node->params[i].modbus_read->callback[0] = value;
						node->params[i].modbus_read->callback[1] = value << 8;
						node->params[i].modbus_read->callback[2] = value << 16;
						node->params[i].modbus_read->callback[3] = value << 24;
					}
					else
					{
						float convertedValue = (float)value / pow(10, node->params[i].modbus_read->precision);
						memcpy(node->params[i].modbus_read->callback, &convertedValue, 4);
					}
				}
			}
		}
		else if (node->params[i].protcol == MODBUS_WRITE)
		{
			protocol_485=1;
		}
		else if (node->params[i].protcol == DLT645_07 || node->params[i].protcol == DLT645_97)
		{
			protocol_485=2;
			uint8_t read_buf[10];
			
			node->params[i].dlt645_params->rxLen = 0;
			memset(read_buf, 0, 10);
			memset(node->params[i].dlt645_params->value, 0, 10);

			dlt645_set_addr(&dlt645, node->params[i].dlt645_params->deviceType645);
			int8_t rs;
			if (node->params[i].protcol == DLT645_07)
			{
				rs = dlt645_read_data(&dlt645, node->params[i].dlt645_params->dataType645, read_buf, DLT645_2007);
			}
			else if (node->params[i].protcol == DLT645_97)
			{
				rs = dlt645_read_data(&dlt645, node->params[i].dlt645_params->dataType645, read_buf, DLT645_1997);
			}
			if (rs != -1)
			{
				if (rs <= 4)
				{
					memcpy(node->params[i].dlt645_params->value, read_buf, 4);
					node->params[i].dlt645_params->rxLen = 4;
				}
				else if (rs == 5)
				{
					memcpy(node->params[i].dlt645_params->value, read_buf, 5);
					node->params[i].dlt645_params->rxLen = 5;
				}
				else if (rs > 5)
				{
					memcpy(node->params[i].dlt645_params->value, read_buf, 9);
					node->params[i].dlt645_params->rxLen = 9;
				}
			}
			else
			{
				node->params[i].dlt645_params->rxLen =0;
			}
		}
		i++;
	}
}
void rs_485_test()
{
	uint16_t *data=mymalloc(SRAMEX,50);
	bool success = mmodbus_readHoldingRegisters16i(0x01,0x00,0x0C,data);
	if(success)
	{
		int i=0;
	}
}
