Files
HBshuiwuConcentrator/hb/hbdatacount.c
2025-12-15 16:07:49 +08:00

237 lines
4.7 KiB
C

#include "uart.h"
#include "addr.h"
#include "debug_printf.h"
#include "bl24c512.h"
#include "WaterMetermanager.h"
#include "Led.h"
#include "keywd.h"
#include "update.h"
#include "Flash.h"
#include "PHY.h"
#include "rtc_ext.h"
#include "hbframe.h"
#define DATA_INVALID 0xFF //无效
#define DATA_VALID 0xAA //有效
#define REPORT_SUCESS_FLAG 0xBB //上报成功
#define REPORT_READY_FLAG 0xFF //未上报的状态
#define DATA_VALID_POS 0
#define DATA_COUNT_POS 1
#define DATA_RPFLAG_POS 3
#define DATA_YEARFLAG_POS 4
#define DATA_MONTHFLAG_POS 5
#define DATA_DAYFLAG_POS 6
#define DATA_HOURFLAG_POS 7
#define DATA_CRCFLAG_POS 8
#define DATA_CRC_LEN 8 //未用
extern bool I2C_eeprom_write_buf(u16 startAddr, u8 * buf, u16 length);
extern bool I2C_eeprom_read_buf(u16 addr, u8 * buf, u16 length);
#pragma pack(1)
struct datacount_struct
{
u8 valid; //0
u16 count; //1
u8 rpflag; //3
u8 year; //4
u8 month; //5
u8 day; //6
u8 hour; //7
u8 crc; //8
};
#pragma pack()
static u16 count = 0;
static u16 rpcount = 0;
static u16 rphistroycount = 0;
static u16 index_rphistroycount = 0;
static struct datacount_struct datacount[DATA_COUNT_MAX];
u16 datacount_getcurcount()
{
return count;
}
//得到当前上报索引值
u16 datacount_getrpcurcount()
{
return rpcount;
}
//得到历史上报索引值
u16 datacount_getrphistroycount()
{
return rphistroycount;
}
//判断当前数据抄成功
bool datacount_dlsucess(u8 year,u8 month,u8 day,u8 hour)
{
u16 i = 0;
for(i=0;i<DATA_COUNT_MAX;i++)
{
if( (datacount[i].year == year) &&
(datacount[i].month == month) &&
(datacount[i].day == day) &&
(datacount[i].hour == hour) )
{
return true;
}
}
return false;
}
//得到当前上报索引值
bool datacount_getrpcur(u8 year,u8 month,u8 day,u8 hour)
{
u16 i = 0;
for(i=0;i<DATA_COUNT_MAX;i++)
{
if( (datacount[i].year == year) &&
(datacount[i].month == month) &&
(datacount[i].day == day) &&
(datacount[i].hour == hour) )
{
if(datacount[i].rpflag != REPORT_SUCESS_FLAG)
{
rpcount = i;
return true;
}
}
}
return false;
}
//得到以前未上报索引值
bool datacount_gethistroy()
{
u16 i = 0;
for(i=index_rphistroycount;i<DATA_COUNT_MAX;i++)
{
if(datacount[i].rpflag != REPORT_SUCESS_FLAG)
{
if(datacount[i].year != 0xFF && datacount[i].year >=0x21 )
{
if(datacount[i].month <= 0x12 && datacount[i].day <= 0x31 && datacount[i].hour <= 0x24) //数据有效
{
rphistroycount = i;
index_rphistroycount++;
return true;
}
}
}
index_rphistroycount++;
}
index_rphistroycount = 0;
return false;
}
void datacount_read()
{
u16 i = 0;
u8 valid= 0;
I2C_eeprom_read_buf(0,(u8*)&datacount[0],sizeof(datacount));
for(i = 0;i<DATA_COUNT_MAX;i++)
{
valid = datacount[i].valid;
if(valid == DATA_VALID)
{
if(i == DATA_COUNT_MAX - 1)
{
count = 0;
}
else
{
count = i+1;
}
return;
}
}
count = 0;
}
void datacount_write(u16 index)
{
u32 addr = 0;
addr = index * sizeof(struct datacount_struct);
datacount[index].crc = GetSum((u8*)&datacount[index],sizeof(struct datacount_struct)-1);
I2C_eeprom_write_buf(addr,(u8*)&datacount[index],sizeof(struct datacount_struct));
}
void datarp_sucess_write(u16 index)
{
datacount[index].rpflag = REPORT_SUCESS_FLAG;
datacount_write(index);
}
void addcount_addnew(u16 count,u8 year,u8 month,u8 day,u8 hour)
{
datacount[count].year = year;
datacount[count].month = month;
datacount[count].day = day;
datacount[count].hour = hour;
datacount[count].valid = DATA_VALID;
datacount[count].count = count;
datacount[count].rpflag = REPORT_READY_FLAG;
//保存现在的标记
datacount_write(count);
}
void datacount_add(u8 year,u8 month,u8 day,u8 hour)
{
if(count<DATA_COUNT_MAX)
{
//保存现在的标记
addcount_addnew(count,year,month,day,hour);
//清除以前的计数标志
if(0 == count)
{
datacount[DATA_COUNT_MAX-1].valid = DATA_INVALID;
datacount_write(DATA_COUNT_MAX-1);
}
else
{
datacount[count-1].valid = DATA_INVALID;
datacount_write(count-1);
}
count++;
}
else
{
count = 0;
//保存现在的标记
addcount_addnew(count,year,month,day,hour);
//清除以前的计数标志
datacount[DATA_COUNT_MAX-1].valid = DATA_INVALID;
datacount_write(DATA_COUNT_MAX-1);
}
}
void datacount_printf()
{
printf("\r\n索引值 有效(AA有效) 计数 上报(BB上报成功) 时间\r\n");
for(u16 i = 0;i<DATA_COUNT_MAX;i++)
{
printf("%04d %02x %05d %02x %02x-%02x-%02x %02x:00:00\r\n",
i+1,
datacount[i].valid,
datacount[i].count,
datacount[i].rpflag,
datacount[i].year,datacount[i].month,datacount[i].day,datacount[i].hour);
}
}