/*
 * gbt_packet.c
 *
 *  Created on: Jul 23, 2024
 *      Author: colorbass
 */


// GB/T Time Synchronization Packet
#include "main.h"
#include "soft_rtc.h"
#include "charger_gbt.h"

void GBT_SendCTS(){

    uint8_t data[7];
    unix_to_bcd(get_Current_Time(), data);
//	data[0] = 0x00; //seconds
//	data[1] = 0x30; //minutes
//	data[2] = 0x23; //hours
//	data[3] = 0x05;  //days
//	data[4] = 0x05;  //month
//	data[5] = 0x24; //years
//	data[6] = 0x20; //centuries

	J_SendPacket(0x000700, 6, 7, data);
}

//GB/T Max Load Packet
void GBT_SendCML(){
//    uint8_t data[8];
//	data[0] = 0x94; //450V max output voltage
//	data[1] = 0x11; //
//	data[2] = 0xB0; //120V min output voltage
//	data[3] = 0x04; //
//	data[4] = 0xC4; //-150A maximum output current
//	data[5] = 0x09; //
//	data[6] = 0x8C; //-2A minimum output current
//	data[7] = 0x0F; //

	J_SendPacket(0x000800, 6, 8, (uint8_t*)&GBT_MaxLoad);

}

//GB/T Version packet
void GBT_SendCHM(){
    uint8_t data[3];
    data[0] = 0x01;
    data[1] = 0x01;
    data[2] = 0x00;
	J_SendPacket(0x2600, 6, 3, data);
}

//GB/T CRM Packet (state=BMS identified)
void GBT_SendCRM(uint8_t state){
//    uint8_t data[8];
//    data[0] = state; // 0x00 / 0xAA
//    data[1] = 0x40; //TODO: Charger Number 123456
//    data[2] = 0xE2;
//    data[3] = 0x01;
//    data[4] = 0x00;
//    data[5] = 0x42; //TODO: location BFG
//    data[6] = 0x46;
//    data[7] = 0x47;
	GBT_ChargerInfo.bmsIdentified = state;
	J_SendPacket(0x100, 6, 8, (uint8_t *)&GBT_ChargerInfo);
}

//GB/T CRO packet (Charger ready)
void GBT_SendCRO(uint8_t state){
    uint8_t data[1];
    data[0] = state;
	J_SendPacket(0xA00, 4, 1, data);
}

//GB/T CCS packet (Charger current status)
void GBT_SendCCS(){
//    uint8_t data[8];
//    data[0] = GBT_CurrPower.requestedVoltage; //
//    data[1] = GBT_CurrPower.requestedVoltage>>8; //output voltage
//    data[2] = GBT_CurrPower.requestedCurrent; //смещение 400а, границы
//    //-400A = 0
//    //	0A = 4000
//    // -100A = 3000
//    data[3] = GBT_CurrPower.requestedCurrent>>8; //TODO: current
//    data[4] = GBT_StateTick()/60000; //charging time (min)
//    data[5] = 0; //TODO: 255 min+
//    data[6] = 0b11111101; //charging not permitted
//    data[7] = 0xFF;
	J_SendPacket(0x1200, 6, 8, (uint8_t *)&GBT_ChargerCurrentStatus);
}

// GB/T Charging Stop packet
void GBT_SendCST(uint32_t Cause){
    uint8_t data[8];
    data[0] = (Cause>>24) & 0xFF; // Error
    data[1] = (Cause>>16) & 0xFF; //
    data[2] = (Cause>>8) & 0xFF; //
    data[3] = Cause & 0xFF; //

	J_SendPacket(0x1A00, 4, 4, data);
}

void GBT_SendCSD(){
	GBT_ChargerStop.chargerNumber = GBT_ChargerInfo.chargerNumber;
	GBT_ChargerStop.outputEnergy = 0; //TODO Energy meters
	GBT_ChargerStop.chargingTime = GBT_ChargerCurrentStatus.chargingTime;
	J_SendPacket(0x1D00, 6, 7, (uint8_t *)&GBT_ChargerStop);

}

void GBT_SendCEM(uint32_t ErrorCode){
    uint8_t data[8];
    data[0] = (ErrorCode>>24) & 0xFF; // Error
    data[1] = (ErrorCode>>16) & 0xFF; //
    data[2] = (ErrorCode>>8) & 0xFF; //
    data[3] = ErrorCode & 0xFF; //

	J_SendPacket(0x1F00, 4, 4, data);
}
