#include "database.h" #include "usart.h" #include "tt808.h" uint16_t PDA_pushArr[1024]; extern TipsFlag tipsflag; extern Menu_table menu; DatabaseLoc dbloc; DatabaseEvent dbeve; DatabaseLoc *dblocptr = &dbloc; DatabaseEvent *dbeveptr = &dbeve; bool MiddleObj =false; /** * @breaf 数据库初始化 * @Explanation 向各头部写入关键信息 */ void Database_init(uint16_t pushsize_loc, uint16_t pullsize_loc,uint16_t pushsize_eve, uint16_t pullsize_eve) { dblocptr->location.alarmFlag=0x00000000; dblocptr->location.statusFlag=0x00000002; dblocptr->location.height=0x0000; dblocptr->location.direction=0x0000; dblocptr->location.signalCmd=0x30; dblocptr->location.signalCmd_len=0x01; dblocptr->location.atellitesCmd=0x31; dblocptr->location.atellitesCmd_len=0x01; Flash_ReadBytes((uint16_t*)&(dblocptr->temppush.loc_pushIndex),LocDbHeadStart,4);//读取loc头部是否发生写入 if(dblocptr->temppush.loc_pullIndex == 0xFFFF && dblocptr->temppush.loc_pushIndex==0xFFFF)//未写入 { dblocptr->headinfo.loc_pushIndex = 0; dblocptr->headinfo.loc_pullIndex = 0; dblocptr->headinfo.loc_pushSize = pushsize_loc; dblocptr->headinfo.loc_pullSize = pullsize_loc; Flash_WriteBytes((uint16_t*)&(dblocptr->headinfo),LocDbHeadStart,4); } else { //*高地址字节在前 dblocptr->headinfo.loc_pushIndex = dblocptr->temppush.loc_pushIndex; dblocptr->headinfo.loc_pullIndex = dblocptr->temppush.loc_pushIndex; dblocptr->headinfo.loc_pushSize = dblocptr->temppush.loc_pushSize; dblocptr->headinfo.loc_pullSize = dblocptr->temppush.loc_pullSize; } Flash_ReadBytes((uint16_t*)&(dbeveptr->temppush),EveDbHeadStart,4);//读取loc头部是否发生写入 if(dbeveptr->temppush.eve_pushIndex == 0xFFFF && dbeveptr->temppush.eve_pullIndex ==0xFFFF)//未写入 { dbeveptr->headinfo.eve_pushIndex = 0; dbeveptr->headinfo.eve_pullIndex = 0; dbeveptr->headinfo.eve_pushSize = pushsize_loc; dbeveptr->headinfo.eve_pullSize = pullsize_loc; Flash_WriteBytes((uint16_t*)&(dbeveptr->headinfo),EveDbHeadStart,4); } else { //*高地址字节在前 dbeveptr->headinfo.eve_pushIndex = dbeveptr->temppush.eve_pushIndex; dbeveptr->headinfo.eve_pullIndex = dbeveptr->temppush.eve_pullIndex; dbeveptr->headinfo.eve_pushSize = dbeveptr->temppush.eve_pushSize; dbeveptr->headinfo.eve_pullSize = dbeveptr->temppush.eve_pullSize; } } HAL_StatusTypeDef PushSta; //***************定位信息入库 void Database_Push(uint8_t object) { if(object==0)//位置信息写入 { if(dblocptr->temppush.loc_pushIndex!=0) { if((dblocptr->location.latitude-dblocptr->temppush.latitude >15) || (dblocptr->location.longitude-dblocptr->temppush.longitude >15))//乘以倍数后,差距大于15 { PushSta = Flash_WriteBytes((uint16_t*)&(dblocptr->location.alarmFlag),LocDbBodyStart + (dblocptr->headinfo.loc_pushIndex*34),17); TestPrintStruct(dblocptr,1,0);//读取Temp dblocptr->headinfo.loc_pushIndex+=1; Flash_WriteBytes((uint16_t*)&(dblocptr->headinfo),LocDbHeadStart,4);//更新刻度 } else { printf("经纬度差距过小不进行写入!\r\nlat:%08X/lon:%08X\r\nlat:%08X/lon:%08XN\r\n",dblocptr->temppush.latitude,dblocptr->temppush.longitude,dblocptr->location.latitude,dblocptr->location.longitude); } Flash_ReadBytes((uint16_t*)&(dblocptr->temppush.alarmFlag),LocDbBodyStart + ((dblocptr->headinfo.loc_pushIndex-1)*34),17);//读取body信息 } else if(dblocptr->location.latitude!=0 && dblocptr->headinfo.loc_pushIndex==0)//判断定位是否正常工作 { PushSta = Flash_WriteBytes((uint16_t*)&(dblocptr->location.alarmFlag),LocDbBodyStart,17);//34 printf("首次写入,->%d\r\n",dblocptr->temppush.loc_pushIndex); dblocptr->headinfo.loc_pushIndex+=1; Flash_WriteBytes((uint16_t*)&(dblocptr->headinfo),LocDbHeadStart,4);//更新刻度 Flash_ReadBytes((uint16_t*)&(dblocptr->temppush.loc_pushIndex),LocDbHeadStart,4);//读取loc头部写入信息 } } if(dblocptr->location.latitude!=0) { TestPrintStruct(dblocptr,0,0);//读取Worknow Flash_ReadBytes((uint16_t*)&(dblocptr->temppush.loc_pushIndex),LocDbHeadStart,4);//读取loc头部写入信息 if(PushSta==HAL_OK) { printf("写入%d完成,准备写入->%d\r\n",dblocptr->headinfo.loc_pushIndex-1,dblocptr->temppush.loc_pushIndex); } } if(dblocptr->temppush.loc_pushIndex == dblocptr->headinfo.loc_pushSize)//入库结束(返回 { printf("@@入库操作,当前索引%d,达边界值返回覆写\r\n",dblocptr->headinfo.loc_pushIndex); dblocptr->headinfo.loc_pushIndex=0; Flash_WriteBytes((uint16_t*)&(dblocptr->headinfo),LocDbHeadStart,4);//更新刻度 } } void Database_Pull(uint8_t object) { if(object==0)//位置信息出库 { Flash_ReadBytes((uint16_t*)&(dblocptr->headinfo.loc_pushIndex),LocDbHeadStart,4);//读取loc头部写入信息 printf("@@出库操作,应出库%d当前索引%d\r\n",dblocptr->headinfo.loc_pushIndex,dblocptr->headinfo.loc_pullIndex); if(dblocptr->headinfo.loc_pushIndex != dblocptr->headinfo.loc_pullIndex)//库内有数据 { Flash_ReadBytes((uint16_t*)&(dblocptr->temppull.alarmFlag),LocDbBodyStart + ((dblocptr->headinfo.loc_pullIndex)*34),17);//读取出库body信息 TestPrintStruct(dblocptr,2,1);//读取pullTemp dblocptr->headinfo.loc_pullIndex+=1; Flash_WriteBytes((uint16_t*)&(dblocptr->headinfo),LocDbHeadStart,4);//更新刻度 Retrans = true; } else { dblocptr->headinfo.loc_pushIndex=0; dblocptr->headinfo.loc_pullIndex=0; Flash_WriteBytes((uint16_t*)&(dblocptr->headinfo),LocDbHeadStart,4);//更新刻度 printf("@@出库完成,库内数据更新\r\n"); Retrans = false; } } } void TestPrintStruct(DatabaseLoc *ptr,uint8_t temppush,uint8_t temppull) { if(temppush==1) { printf("temp:%08X%08X%08X%08X%04X%04X%04X%d%d%d%d%d%d%02X%02X%02X%02X%02X%02X\r\n", \ ptr->temppush.alarmFlag, \ ptr->temppush.statusFlag, \ ptr->temppush.latitude, \ ptr->temppush.longitude, \ ptr->temppush.height, \ ptr->temppush.speed, \ ptr->temppush.direction, \ ptr->temppush.timeBCD[0], \ ptr->temppush.timeBCD[1], \ ptr->temppush.timeBCD[2], \ ptr->temppush.timeBCD[3], \ ptr->temppush.timeBCD[4], \ ptr->temppush.timeBCD[5], \ ptr->temppush.signalCmd, \ ptr->temppush.signalCmd_len, \ ptr->temppush.CSQ, \ ptr->temppush.atellitesCmd, \ ptr->temppush.atellitesCmd_len, \ ptr->temppush.satellitesNum); } else if(temppush==0) { printf("now :%08X%08X%08X%08X%04X%04X%04X%d%d%d%d%d%d%02X%02X%02X%02X%02X%02X\r\n", \ ptr->location.alarmFlag, \ ptr->location.statusFlag, \ ptr->location.latitude, \ ptr->location.longitude, \ ptr->location.height, \ ptr->location.speed, \ ptr->location.direction, \ ptr->location.timeBCD[0], \ ptr->location.timeBCD[1], \ ptr->location.timeBCD[2], \ ptr->location.timeBCD[3], \ ptr->location.timeBCD[4], \ ptr->location.timeBCD[5], \ ptr->location.signalCmd, \ ptr->location.signalCmd_len, \ ptr->location.CSQ, \ ptr->location.atellitesCmd, \ ptr->location.atellitesCmd_len, \ ptr->location.satellitesNum); } else if(temppull==1) { printf("temp:%08X%08X%08X%08X%04X%04X%04X%d%d%d%d%d%d%02X%02X%02X%02X%02X%02X\r\n", \ ptr->temppull.alarmFlag, \ ptr->temppull.statusFlag, \ ptr->temppull.latitude, \ ptr->temppull.longitude, \ ptr->temppull.height, \ ptr->temppull.speed, \ ptr->temppull.direction, \ ptr->temppull.timeBCD[0], \ ptr->temppull.timeBCD[1], \ ptr->temppull.timeBCD[2], \ ptr->temppull.timeBCD[3], \ ptr->temppull.timeBCD[4], \ ptr->temppull.timeBCD[5], \ ptr->temppull.signalCmd, \ ptr->temppull.signalCmd_len, \ ptr->temppull.CSQ, \ ptr->temppull.atellitesCmd, \ ptr->temppull.atellitesCmd_len, \ ptr->temppull.satellitesNum); } }