forked from SZV10X_Software/SZV103_FM33A0xxEV_SiZhu

wujiazhi
2024-06-13 72def895431ad7a08e635b11f3da738e2b2c4618
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "system_log.h"
#include "e2p.h"
#include "uart.h"
#include "gprs.h"
#include "low_pwr_test.h"
#include "rtc.h"
 
TYPE_CHECK_SIZE(SAVE_DATA_START_PARA_T,6);//ºË²é½á¹¹Ìå´óС    
SAVE_DATA_START_PARA_T save_day_month_timing_g = {
    .set_minute_bcd = 0,
    .set_hour_bcd = 0};
 
/*ÉèÖÃĬÈÏʱ¼ä£¬Á÷Á¿ºÍ¼Û¸ñµÈÐÅÏ¢*/        
TYPE_CHECK_SIZE(SAVE_REALTIME_DATA_T,119);//ºË²é½á¹¹Ìå´óС    
SAVE_REALTIME_DATA_T save_realtime_data_g = {
    .record_time_bcd = {0},
    .realtime_flow_data_s = {0},
    .realtime_billing_data_s = {0},
    .lith_bat_working_time_remain_hour = LITH_BAT_WORKING_TIME_MAX, //Ê£Ó๤×÷ʱ¼ä µ¥Î»£ºÐ¡Ê±
};
 
static SYS_FLOW_DATA_LOG_T sys_flow_data_log_flag_g = SYS_FLOW_DATA_NONE;
 
uint8_t pay_flag_g = 0;
FlagStatus key_save_send_data_flag_g = RESET;
 
/*ʵʱÊý¾Ý¼Ç¼´¢´æ
·µ»ØÖµ£ºERROR_1 Ê§°Ü SUCCESS_0 ³É¹¦*/
ErrorStatus_STM32 RealTimeData_LogStore(SAVE_REALTIME_DATA_T * save_realtime_data_p,SYS_REAL_TIME_DATA_T *realtime_data_p,
                               BILLING_REAL_PARA_T *billing_realtime_p, __IO RTC_TimeDateTypeDef *calendar_p)
{
    static double last_forward_cumulate_sc = 0;
//    if(realtime_data_p->measure_para_s.forward_cumulate_sc > last_forward_cumulate_sc)
//    {
        save_realtime_data_p->record_time_bcd[0] = calendar_p->Year & 0xFF; // Ê±¼ä¼Ç¼¹¹Ôì
        save_realtime_data_p->record_time_bcd[1] = calendar_p->Month;
        save_realtime_data_p->record_time_bcd[2] = calendar_p->Date;
        save_realtime_data_p->record_time_bcd[3] = calendar_p->Hour;
        save_realtime_data_p->record_time_bcd[4] = calendar_p->Minute;
        save_realtime_data_p->record_time_bcd[5] = calendar_p->Second;
    
        memcpy(&save_realtime_data_p->realtime_flow_data_s,&realtime_data_p->measure_para_s,sizeof(save_realtime_data_p->realtime_flow_data_s));
        save_realtime_data_p->realtime_flow_data_s.temp_avg = realtime_data_p->measure_para_s.temperature;
        save_realtime_data_p->realtime_flow_data_s.press_avg = realtime_data_p->measure_para_s.pressure;
        
        memcpy(&save_realtime_data_p->realtime_billing_data_s,&billing_real_para_g,sizeof(save_realtime_data_p->realtime_billing_data_s));
        
        return Sys_Log_E2pParaMultipleWrite(&sys_store_para_table[PARA_SAVE_REALTIME_DATA],1);
//    }
//    return ERROR_1;
}
 
/*·¢ËÍÁ÷Á¿Êý¾Ý´æ´¢µÄÌØÊâ´¦Àí
write_cnt£ºÐ´´ÎÊý
*/
ErrorStatus_STM32 SendFlowData_StoreSpecialHandler(uint8_t write_cnt,__IO RTC_TimeDateTypeDef *calendar_p)
{
    SYS_SEND_FLOW_DATA_SYSLOG_FORMAT_T send_data_buffer;
    SendFlowData_LogRecordCreated(&send_data_buffer, &sys_realtime_data_g, &billing_real_para_g, calendar_p);
    /*¼ÆËãУÑéºÍ*/
    send_data_buffer.check_sum = CalcCheckSum(&send_data_buffer.record_time[0],SEND_FLOW_DATA_LOG_FORMAT_SIZE - 1);
    /*´¢´æ*/
    return SendFlowData_SpecialWrite((uint8_t *)&send_data_buffer,write_cnt);
}
 
 
void System_FlowData_CyclicStorageHandler(__IO RTC_TimeDateTypeDef *calendar_p)
{
  SYS_FLOW_DATA_SYSLOG_FORMAT_T data_buffer;
  if (calendar_p->Second == 0x03)
  {
    if ((BCD_2_HEX_U8(calendar_p->Minute) % 5 == 0) || cal_mode_exit_save_cumulate_flag_g) // ÏµÍ³5·ÖÖÓÖÜÆÚ´¢´æÒ»´Î
    {
            cal_mode_exit_save_cumulate_flag_g = 0;
            E2p_GPIO_Clk_Init();
            RealTimeData_LogStore(&save_realtime_data_g, &sys_realtime_data_g, &billing_real_para_g, calendar_p);
    }
    
  }
    if(((calendar_p->Second == 0) && (BCD_2_HEX_U8(calendar_p->Minute) % gprs_network_cfg_para_g.save_data_period_min_time == 0))
            || key_save_send_data_flag_g)// ·¢ËÍÁ÷Á¿¶³½áÊý¾ÝµÄ´æ´¢
    {
        key_save_send_data_flag_g = RESET;
        E2p_GPIO_Clk_Init();
        SendFlowData_StoreSpecialHandler(1,calendar_p);
        gprs_save_send_para_g.send_data_log_save_cnt++; // Éϴε½Õâ´Î·¢Ëͼä¸ôµÄ²É¼¯ÌõÊý
    }
  if (calendar_p->Second == 0)
  {
        if (calendar_p->Minute == 0) // Ã¿Ð¡Ê±´¢´æÒ»´Î
    {
            E2p_GPIO_Clk_Init();
      FlowData_LogRecordCreated(&data_buffer, &sys_realtime_data_g, &billing_real_para_g, calendar_p); // ¹¹ÔìÊý¾Ý
      Sys_Log_E2pCyclicMultipleWrite(&sys_log_cyclic_table[CYCL_HOUR_FLOW_DATA], (uint8_t *)&data_buffer,1);
    }
    if (calendar_p->Hour == save_day_month_timing_g.set_hour_bcd && calendar_p->Minute == save_day_month_timing_g.set_minute_bcd) // Ã¿Ìì
    {
            E2p_GPIO_Clk_Init();
      FlowData_LogRecordCreated(&data_buffer, &sys_realtime_data_g, &billing_real_para_g, calendar_p);
      Sys_Log_E2pCyclicMultipleWrite(&sys_log_cyclic_table[CYCL_DAY_FLOW_DATA], (uint8_t *)&data_buffer,1);
      if (calendar_p->Date == 1) // Ã¿ÔÂ
        Sys_Log_E2pCyclicMultipleWrite(&sys_log_cyclic_table[CYCL_MONTH_FLOW_DATA], (uint8_t *)&data_buffer,1);
    }
  }
}
 
void System_LogProcess(void)
{
  /*ÆÕͨ±¨¾¯ÈÕÖ¾¼Ç¼*/
    AlarmLog_CyclicStorageHandler(&sys_alarm_id_g, &sys_realtime_data_g, &billing_real_para_g, &calendar_g);
  /*Á÷Á¿Êý¾Ý¼Ç¼*/
  System_FlowData_CyclicStorageHandler(&calendar_g);
  /*·§¿ØÈÕÖ¾¼Ç¼*/
  System_ValveLog_CyclicStorageHandler(&op_cl_valve_log_flag_g, op_cl_valve_cause_id, &valve_log_g, &calendar_g);
  /*³äÖµ¼Ç¼*/
  PayLog_CyclicStorageHandler(&pay_flag_g,&pay_para_g,&calendar_g);
    /*Уʱ¼Ç¼*/
    TimingLog_CyclicStorageHandler(&timing_log_g,&calendar_g);
  /*²ÎÊýÐ޸ļǼ*/
  ParaAmendLog_CyclicStorageHandler(&sys_para_amend_log_flag_g,&para_amend_log_g,&calendar_g);
}
 
#define REALTIME_DATA_CUMULATE_LEN     sizeof(save_realtime_data_g.realtime_flow_data_s) -  \
                                                                        sizeof(save_realtime_data_g.realtime_flow_data_s.temp_avg) - \
                                                                        sizeof(save_realtime_data_g.realtime_flow_data_s.press_avg)
/*ϵͳÊ×´ÎÉϵ紦Àí*/
void Sys_FirstPower_E2pProcess(void)
{
    E2P_PWR_ON; // ¿ªÆôE2pµçÔ´
    /*ÅжÏϵͳÊÇ·ñÊ×´ÎÉϵ磬Ê×´ÎÉϵçÐèдÈëĬÈÏÖµ£¬·ÇÊ×´ÎÖ»Ðè´ÓE2PÖжÁÈ¡Êý¾Ý*/
  if (Sys_Log_E2pParaDataFirstInit() == RESET) // ·ÇÊ×´ÎÉϵç
  {
        /*Ë÷ÒýºÍÂúд±ê־λ³õʼ»¯*/
        Sys_Log_E2pPara_NoFirstPower_FlagInit();
        Sys_Log_E2pCyclic_NoFirstPower_FlagInit();
        
    for (uint8_t i = 1; i < SYS_LOG_PARA_TABLE_NUM; i++) // ²ÎÊýÊý¾Ý´Ó´æ´¢ÖжÁ³ö
      Sys_Log_E2pParaRead_1_Data(&sys_store_para_table[i]);
    /*¶Áʵʱʱ¼ä£¬×ÜÁ¿£¬Î¶È,ѹÁ¦Êý¾Ý ÊµÊ±¼Æ·ÑÊý¾Ý*/
        memcpy(&sys_realtime_data_g.measure_para_s,&save_realtime_data_g.realtime_flow_data_s,REALTIME_DATA_CUMULATE_LEN);
        sys_realtime_data_g.measure_para_s.temp_avg = sys_realtime_data_g.measure_para_s.temperature = save_realtime_data_g.realtime_flow_data_s.temp_avg;
        sys_realtime_data_g.measure_para_s.press_avg = sys_realtime_data_g.measure_para_s.pressure = save_realtime_data_g.realtime_flow_data_s.press_avg;
        
        memcpy(&billing_real_para_g,&save_realtime_data_g.realtime_billing_data_s,sizeof(save_realtime_data_g.realtime_billing_data_s));
    /*¶ÁÈ¡·§ÃÅ¿ª¹Ø·§´ÎÊý*/
    Sys_Log_E2pCyclicMultipleReverseRead(&sys_log_cyclic_table[CYCL_VALVE], (uint8_t *)&valve_log_g, 1);
        /*Éϵç»ñÈ¡·§ÃÅ״̬*/
        /*ÓÉÓÚûÓÐÓ²¼þÉϵÄ״̬Ðźţ¬ËùÒÔÀûÓ÷§ÃÅÀúÊ·¼Ç¼»ñÈ¡*/
        op_cl_valve_cause_id = valve_log_g.switch_cause;
        if(valve_log_g.work_type == VWT_OPEN || valve_log_g.work_type == VWT_NONE)
        {
            __SYS_STATUS_BYTES_STATUS(valve_state, VALVE_OP, AV_NORMAL);
        }
        else
        {
            __SYS_STATUS_BYTES_STATUS(valve_state, VALVE_CL, AV_NORMAL);
            if(valve_log_g.work_type == VWT_FORCED_CLOSE || valve_log_g.work_type == VWT_LOCK)
                __SYS_STATUS_BYTES_STATUS(valve_lock, SET, AV_NORMAL); // ÉÏËø
        }    
    /*¶ÁÈ¡³äÖµ´ÎÊý*/
    Sys_Log_E2pCyclicMultipleReverseRead(&sys_log_cyclic_table[CYCL_PAY], (uint8_t *)&pay_para_g, 1);
  }
  else // Ê×´ÎÉϵç
  {
//        /*Ë÷ÒýºÍÂúд±ê־λ³õʼ»¯*/
//        Sys_Log_E2pCyclic_FirstPower_FlagInit();
//        /*²¿·Ö²ÎÊýÉèΪĬÈÏÖµ*/
//        Sys_Log_E2pParaMultipleWrite(&sys_store_para_table[PARA_SAVE_REALTIME_DATA],1);
//    Sys_Log_E2pCyclicMultipleWrite(&sys_log_cyclic_table[CYCL_VALVE],(uint8_t *)&valve_log_g,1);
//    Sys_Log_E2pCyclicMultipleWrite(&sys_log_cyclic_table[CYCL_PAY], (uint8_t *)&pay_para_g,1);
  }
    /*Óà¶î/Ìõ¼þ¹Ø·§ÅäÖòÎÊýÖеĹط§×Ö¸³Öµ*/
    billing_alarm_cfg_g.alarm_ctl_byte_2 = sys_alarm_ctl_bytes_g.alarm_ctl_send_union[1];
    billing_alarm_cfg_g.alarm_send_byte_2 = sys_alarm_send_bytes_g.alarm_ctl_send_union[1];
    memcpy(&billing_alarm_cfg_g.valve_close_ctl,&valve_ctl_bytes_g.valve_ctr_2,SIZEOF_OF(BILLING_ALARM_CFG_T,valve_close_ctl));
    /*ÉèÖÃĬÈÏʱ¼ä*/
    Set_Sys_Rtc_Time(save_realtime_data_g.record_time_bcd[0], save_realtime_data_g.record_time_bcd[1], 
                                        save_realtime_data_g.record_time_bcd[2], save_realtime_data_g.record_time_bcd[3], 
                                        save_realtime_data_g.record_time_bcd[4], save_realtime_data_g.record_time_bcd[5],1);
    Get_Sys_Rtc_Time(&calendar_g); // »ñȡʱ¼ä
    auto_exit_start_min_time_g = Bcd_2_Hex(calendar_g.Hour) * 60 + Bcd_2_Hex(calendar_g.Minute);// ×Ô¶¯Í˳öģʽµÄÆðʼ·ÖÖÓʱ¼ä
  E2P_PWR_OFF; // ¹Ø±ÕE2pµçÔ´
}
 
 
//#define PARA_UPDATE_1     0x24030701
//#define PARA_UPDATE_2     0x24030702
//#define PARA_UPDATE_3        0x24030801
//typedef struct
//{
//    uint32_t add_para_update_flag;
//    uint8_t reserve[4];
//}ADD_PARA_UPDATE_T;
//ADD_PARA_UPDATE_T add_para_update_g = {0};
 
//void NewAddPara1_Handler(int update_flag)
//{
//    if((update_flag < PARA_UPDATE_1) || (update_flag = 0) || (update_flag = 0xFFFFFFFF))
//    {
//        //ÐÂÔö²ÎÊýдÈëĬÈÏÖµ
//        add_para_update_g.add_para_update_flag = PARA_UPDATE_1;
//        Sys_Log_E2pParaMultipleWrite(&sys_store_para_table[PARA_UPDATE_CFG],1);//дÈë²¹¶¡±ê־λ
//    }
//}
//void OldAddPara1_Handler(int update_flag)
//{
//    if(update_flag < PARA_UPDATE_2)
//    {
//        //ÐÂÔö²ÎÊýдÈëĬÈÏÖµ²¢´æ´¢
//        {
//        
//        }
//        add_para_update_g.add_para_update_flag = PARA_UPDATE_2;
//        Sys_Log_E2pParaMultipleWrite(&sys_store_para_table[PARA_UPDATE_CFG],1);//дÈë²¹¶¡±ê־λ
//    }
//}
//void NewAddPara2_Handler(int update_flag)
//{
//    if(update_flag < PARA_UPDATE_3)
//    {
//        //ÐÂÔö²ÎÊýдÈëĬÈÏÖµ²¢´æ´¢
//        {
//        
//        }
//        add_para_update_g.add_para_update_flag = PARA_UPDATE_3;
//        Sys_Log_E2pParaMultipleWrite(&sys_store_para_table[PARA_UPDATE_CFG],1);//дÈë²¹¶¡±ê־λ
//    }
//}
 
//void AddParaProcess(void)
//{
//    uint8_t para_update_flag = Sys_Log_E2pParaRead_1_Data(&sys_store_para_table[PARA_UPDATE_CFG]);
//    NewAddPara1_Handler(para_update_flag);
//    OldAddPara1_Handler(para_update_flag);
//    NewAddPara2_Handler(para_update_flag);        
//}