learnTask.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429
  1. #include "includes.h"
  2. #define LEARN_HTTP_URL "test.rtpoc.com"
  3. #define LEARN_PORT 8000
  4. #define PAGE_SIZE 5//一页能显示多少条
  5. #define LEARN_LIST "learnLists" //任务简表总表
  6. #define LEARN_LIST_TMP "learnListsTmp" //临时任务简表总表
  7. #define LEARN_RANK "learnRank" //排名临时表,不需要删除
  8. #define LEARN_CLASS "learnClass"
  9. #define LEARN_NOTICE "learnNotice"
  10. #define LEARN_UPDATE_MAX 10 //一次性最多拉取任务个数
  11. #define SHOW_TABLE_MAX_BYTES 40
  12. #define OBJECT_LIST "list"
  13. #define OBJECT_TID "tid"
  14. #define OBJECT_VER "ver"
  15. #define OBJECT_TITLE "t"
  16. #define OBJECT_SCORE "s"
  17. #define OBJECT_CONTENT "c"
  18. #define OBJECT_FINISH "f"
  19. #define OBJECT_UID "uid"
  20. #define OBJECT_GID "gid"
  21. #define OBJECT_GNAME "groupName"
  22. #define OBJECT_NNAME "nickname"
  23. #define OBJECT_TYPE "type"
  24. #define OBJECT_TOKEN "token"
  25. #define OBJECT_CONTENT_DETAIL "content"
  26. #define OBJECT_SPEED_PARAM "scoreSpeedParam"
  27. #define OBJECT_VOLUME_PARAM "scoreVolumeParam"
  28. static void httpCallBack(char *msg, int len);
  29. typedef enum{
  30. LEARN_DB_GET_INFO,//获取用户信息
  31. LEARN_DB_WAIT_INFO,//等待用户信息
  32. LEARN_GET_LIST,//获取任务列表
  33. LEARN_WAIT_LIST,//等待列表
  34. LEARN_DB_SYNC,//同步数据
  35. LEARN_DB_SYNC_DEL,//同步本地多了的任务(删除)
  36. LEARN_DB_SYNC_ADD,//同步本地没有的任务(获取)
  37. LEARN_DB_SYNC_ADD_PAGE_WAIT,//等待页任务获取
  38. LEARN_DB_SYNC_ADD_SOME_WAIT,//等待部分任务获取
  39. LEARN_DB_SYNC_DONE,//同步完成
  40. LEARN_DB_SYNC_FAILED,//同步失败
  41. LEARN_READ_TASK_INFO,//读取任务用于显示
  42. LEARN_READ_FAILED,//用于读取任务过程出现失败或错误时退出使用
  43. LEARN_SHOW_MENU,
  44. LEARN_WAIT_RESPONSE,//按键响应
  45. LEARN_RECORD_UPLOAD,
  46. LEARN_RELEASE_DB,
  47. LEARN_END,
  48. LEARN_RANK_GET_DETAIL,
  49. LEARN_CLASS_GET_DETAIL,
  50. LEARN_NOTICE_GET_DETAIL,
  51. LEARN_NOTICE_GET_DETAIL_SHOW,
  52. LEARN_GET_PARAM,
  53. }LEARN_STATUS;
  54. static unsigned char charaNum;//单词个数
  55. static unsigned int taskID[LEARN_UPDATE_MAX];
  56. static unsigned int taskNum=0;
  57. static unsigned char learnAddNum;//添加任务时,添加的个数
  58. static LEARN_STATUS learnStatus;
  59. static unsigned short learnPage;
  60. static unsigned int learnTick;
  61. static int learnTaskNum;
  62. static int learnPageNum;
  63. static bool learnDetailEnd;
  64. unsigned char rankType;//获取的排名类型 0 月排名 其它 周排名
  65. static void pocAudioCtl(bool status){
  66. if(status==false){
  67. msgAtSend("AT+POC=0C0000\r\n");//停止POC录音
  68. msgAtSend("AT+HTTS=0\r\n");//停止TTS,防止TTS播放还未完成时,就使用学习录音了
  69. sutApp.skipTTS=1;//本地也不能再使用TTS
  70. msgAtSend("AT+POC=060000\r\n");//强制停止POC使用对讲播放
  71. nwy_sleep(50);//给点时间POC完成切换
  72. }else{//恢复
  73. sutApp.skipTTS=0;
  74. msgAtSend("AT+POC=070000\r\n");
  75. }
  76. }
  77. bool isLearnBusy(void){
  78. static bool pocIsEnable=true;
  79. if(sutUIstatus.Status==UIS_MENU_TASK_QUERY){//需要禁止POC
  80. if(true==pocIsEnable){
  81. pocIsEnable=false;
  82. pocAudioCtl(pocIsEnable);
  83. MSG_INFO(1, "Disable poc ctl");
  84. }
  85. return true;
  86. }else{//需要开启POC
  87. if(false==pocIsEnable){
  88. pocIsEnable=true;
  89. pocAudioCtl(pocIsEnable);
  90. MSG_INFO(1, "Enable poc ctl");
  91. }
  92. return false;
  93. }
  94. }
  95. static char *getLearnFileJson(const char *fname);
  96. void sdCardCtl(bool status){
  97. if(status==true){
  98. CTL_SD_PWR(1);
  99. if(true!=nwy_sdk_sdcard_mount()){
  100. MSG_ERR(1, "SD card mount failed");
  101. CTL_SD_PWR(0);
  102. }else MSG_INFO(1, "SD card mount ok");
  103. }else{
  104. nwy_sdk_sdcard_unmount();
  105. CTL_SD_PWR(0);
  106. MSG_INFO(1, "SD card unmount");
  107. }
  108. }
  109. static void learnUpdateStatus(LEARN_STATUS newStatus){
  110. learnStatus=newStatus;
  111. }
  112. static LEARN_STATUS getLearnStatus(void){
  113. return learnStatus;
  114. }
  115. void showInfoBox(const char *info,int dly){
  116. guiShowMessageBox(info);
  117. uTimerStart(&learnTick, dly);
  118. }
  119. void releaseBuf(unsigned char **pointer){
  120. if(*pointer==NULL) return;
  121. free(*pointer);
  122. MSG_INFO(1, "[mem free]%x",*pointer);
  123. *pointer=NULL;
  124. }
  125. void releaseJson(cJSON** json){
  126. if(*json==NULL) return;
  127. cJSON_Delete(*json);
  128. MSG_INFO(1, "[json del]%x",*json);
  129. *json=NULL;
  130. }
  131. void *createBuf(unsigned int size){
  132. void *buf;
  133. buf=malloc(size);
  134. if(buf != NULL) MSG_INFO(1, "[mem malloc]%x", buf);
  135. return buf;
  136. }
  137. cJSON* createJson(char *msg){
  138. cJSON* tmp;
  139. tmp=cJSON_Parse(msg);
  140. if(tmp != NULL) MSG_INFO(1, "[json add]%x", tmp);
  141. return tmp;
  142. }
  143. //从json中获取objString的Value,只用于获取key:value格式
  144. static char *learnJsonGetItem(cJSON* json, const char *objString){
  145. if(NULL==json) return NULL;
  146. cJSON* t=cJSON_GetObjectItem(json, objString);
  147. if(NULL==t) return NULL;
  148. else return t->valuestring;
  149. }
  150. //删除simpleList,simpleListTmp重命名为simpleList
  151. void learnUpdateListFile(void){
  152. char oldFile[50],newFile[50];
  153. snprintf(oldFile, sizeof(oldFile), "%s/%s",SD_TOKEN, LEARN_LIST);
  154. snprintf(newFile, sizeof(newFile), "%s/%s",SD_TOKEN, LEARN_LIST_TMP);
  155. //删除文件
  156. if(true==nwy_sdk_fexist(oldFile)) nwy_sdk_file_unlink(oldFile);
  157. //重命名文件
  158. if(true==nwy_sdk_fexist(newFile)){
  159. nwy_sdk_frename(newFile, oldFile);
  160. MSG_INFO(1, "Learn Rename file:%s->%s",LEARN_LIST_TMP,LEARN_LIST);
  161. }
  162. }
  163. void learnGetNextPage(short direction){
  164. if(direction==0) learnPage=0;
  165. else if(direction>0){
  166. if(++learnPage>=learnPageNum) learnPage=0;
  167. }else{
  168. if(learnPage==0) learnPage=learnPageNum-1;
  169. else learnPage--;
  170. }
  171. //得到page后,先检测本地是否已存有任务
  172. }
  173. /*********************从文件中获取用户信息******************************/
  174. typedef struct{
  175. bool paramReady;
  176. double scoreSpeedParam;
  177. double scoreVolumeParam;
  178. bool ready;
  179. unsigned char type;
  180. unsigned int uid;
  181. unsigned int gid;
  182. char gname[30];
  183. char nname[30];
  184. char token[20];
  185. }USER_INFO;
  186. USER_INFO usr={false,1,1,false,0xff};
  187. unsigned int getLearnUid(void){return usr.uid;}
  188. unsigned int getLearnGid(void){return usr.gid;}
  189. char *getLearnGName(void){return usr.gname;}
  190. char *getLearnToken(void){return usr.token;}
  191. double getLearnSpeedParam(void){return usr.scoreSpeedParam;}
  192. bool isUserInfoReady(void){return usr.ready;}
  193. void learnUserInfoClear(void){usr.ready=false;}
  194. /*
  195. {"uid":"5028","groupName":"测试组3","gid":"21731","nickname":"用户30","type":"1","token":"120a1a7436"}
  196. */
  197. bool cJSONProcessUinfo(char *msg){
  198. cJSON* json,*item;
  199. json=createJson(msg);
  200. memset((unsigned char *)&usr, 0, sizeof(USER_INFO));
  201. if(json==NULL){
  202. MSG_WARN(1, "cJSONProcessUinfo json null");
  203. return false;
  204. }
  205. //uid
  206. item=cJSON_GetObjectItem(json, OBJECT_UID);
  207. if(NULL!=item) usr.uid=atoi(item->valuestring);
  208. //gid
  209. item=cJSON_GetObjectItem(json, OBJECT_GID);
  210. if(NULL!=item) usr.gid=atoi(item->valuestring);
  211. //type
  212. item=cJSON_GetObjectItem(json, OBJECT_TYPE);
  213. if(NULL!=item) usr.type=atoi(item->valuestring);
  214. //groupName
  215. item=cJSON_GetObjectItem(json, OBJECT_GNAME);
  216. if(NULL!=item) snprintf(usr.gname, sizeof(usr.gname), "%s",item->valuestring);
  217. //nickname
  218. item=cJSON_GetObjectItem(json, OBJECT_NNAME);
  219. if(NULL!=item) snprintf(usr.nname, sizeof(usr.nname), "%s",item->valuestring);
  220. //token
  221. item=cJSON_GetObjectItem(json, OBJECT_TOKEN);
  222. if(NULL!=item) snprintf(usr.token, sizeof(usr.token), "%s",item->valuestring);
  223. releaseJson(&json);
  224. MSG_INFO(1, "usr:%d,%s,%d,%s,%s", usr.uid, usr.gname, usr.gid, usr.nname, usr.token);
  225. usr.ready=true;
  226. return true;
  227. }
  228. /*************************从文件中获取用户信息******************************/
  229. bool learnTaskUpdate(LEARN_STATUS type, void *para1, void *para2, bool needShow){
  230. char url[200],tmp[10];
  231. unsigned char *msg=NULL;
  232. unsigned int len=0,i;
  233. UHTTP_ENUM httpType;
  234. //触发获取任务列表操作
  235. if(sutApp.pocPpp==0 || sutPocStatus.logined==0){
  236. if(true==needShow) showInfoBox("无网络/未登录",2000);
  237. learnUpdateStatus(LEARN_END);
  238. return false;
  239. }
  240. if(type==LEARN_DB_GET_INFO){//获取用户信息
  241. httpType=UHTTP_FILE_GET;
  242. snprintf(url, sizeof(url), "%s/api/cai/UserInfo?account=%d&password=%s", LEARN_HTTP_URL, sutApp.account.psn, sutApp.account.password);
  243. MSG_INFO(1, "Learn get user info");
  244. }else if(type==LEARN_GET_LIST){//获取任务列表
  245. httpType=UHTTP_FILE_GET;
  246. snprintf(url, sizeof(url), "%s/api/cai/BriefTask?gid=%d&uid=%d&token=%s", LEARN_HTTP_URL, getLearnGid(),getLearnUid(),getLearnToken());
  247. MSG_INFO(1, "Learn get total list");
  248. }else if(type==LEARN_DB_SYNC_ADD_PAGE_WAIT){//根据页获取详细表
  249. int *page=(int *)para1;
  250. int *size=(int *)para2;
  251. httpType=UHTTP_FILE_GET;
  252. snprintf(url, sizeof(url), "%s/api/cai/DetailedTask?gid=%d&uid=%d&page=%d&size=%d&token=%s", LEARN_HTTP_URL, getLearnGid(),getLearnUid(),*page,*size,getLearnToken());
  253. MSG_INFO(1, "Learn get detail,page:%d,size:%d", *page,*size);
  254. }else if(type==LEARN_DB_SYNC_ADD_SOME_WAIT){//根据tid获取详细表
  255. unsigned int *tids=(unsigned int *)para1;
  256. unsigned char *num=(unsigned char *)para2;
  257. httpType=UHTTP_FILE_GET;
  258. snprintf(url, sizeof(url), "%s/api/cai/DetailedTask?gid=%d&uid=%d&token=%s&tids=[%d", LEARN_HTTP_URL,getLearnGid(),getLearnUid(),getLearnToken(),tids[0]);
  259. for(i=1;i<*num;i++){
  260. snprintf(tmp, sizeof(tmp),",%d", tids[i]);
  261. strcat(url, tmp);
  262. }
  263. strcat(url, "]");
  264. MSG_INFO(1, "Learn get detail,tid num:%d", *num);
  265. }else if(type==LEARN_RECORD_UPLOAD){//上传语音数据到服务器
  266. msg=(unsigned char *)para1;
  267. len=*((unsigned int *)para2);
  268. httpType=UHTTP_POST;
  269. snprintf(url, sizeof(url), "%s/api/cai/HandIn",LEARN_HTTP_URL);
  270. MSG_INFO(1, "Learn upload audio:%d bytes", len);
  271. }else if(type==LEARN_RANK_GET_DETAIL){
  272. httpType=UHTTP_FILE_GET;
  273. unsigned char *rankType=(unsigned char *)para1;
  274. snprintf(url, sizeof(url), "%s/api/cai/%slyRanking?uid=%d&gid=%d&token=%s", LEARN_HTTP_URL,(*rankType==0)?"Month":"Week" ,getLearnUid(),getLearnGid(),getLearnToken());
  275. MSG_INFO(1, "Learn get %s rank", (*rankType==0)?"month":"week");
  276. }else if(type==LEARN_CLASS_GET_DETAIL){
  277. httpType=UHTTP_FILE_GET;
  278. snprintf(url, sizeof(url), "%s/api/cai/Timetable?uid=%d&gid=%d&token=%s", LEARN_HTTP_URL, getLearnUid(), getLearnGid(), getLearnToken());
  279. MSG_INFO(1, "Learn get class detail");
  280. }else if(type==LEARN_NOTICE_GET_DETAIL){
  281. httpType=UHTTP_FILE_GET;
  282. snprintf(url, sizeof(url), "%s/api/cai/NotificationBrief?uid=%d&gid=%d&token=%s", LEARN_HTTP_URL, getLearnUid(), getLearnGid(), getLearnToken());
  283. MSG_INFO(1, "Learn get notice detail");
  284. }else if(type==LEARN_NOTICE_GET_DETAIL_SHOW){
  285. int *id=(int *)para1;
  286. httpType=UHTTP_FILE_GET;
  287. snprintf(url, sizeof(url), "%s/api/cai/NotificationContent?uid=%d&id=%d&token=%s", LEARN_HTTP_URL, getLearnUid(), *id, getLearnToken());
  288. MSG_INFO(1, "Learn get notice detail show");
  289. }else if(type==LEARN_GET_PARAM){
  290. httpType=UHTTP_FILE_GET;
  291. snprintf(url, sizeof(url), "%s/api/cai/Config?uid=%d&token=%s", LEARN_HTTP_URL, getLearnUid(), getLearnToken());
  292. MSG_INFO(1, "Learn get param");
  293. }
  294. if(true==needShow) showInfoBox("请稍候",5000);
  295. learnHttpStart(httpType,url, LEARN_PORT, httpCallBack,msg, len);
  296. return true;
  297. }
  298. static void LearnTaskProcess(void);
  299. static void sutJsonInit(void);
  300. //学习任务菜单
  301. static unsigned char needUpdate=1;//是否需要更新,我们只从主菜单进此菜单时才同步,从里面的子菜单返回不同步
  302. void uiShowLearnTask(char update){
  303. if(update){
  304. sutJsonInit();
  305. guiClearAll(guiGetBackColor());
  306. guiShowCaption(30,"学习任务",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  307. uiMenuShowBottomLine();
  308. guiShowMessageBox("请稍候");
  309. uiMenuShowBottomIndacitor("查看", "返回");
  310. sdMount();
  311. #if 1
  312. if(isUserInfoReady()==false){
  313. if(true==learnTaskUpdate(LEARN_DB_GET_INFO,NULL, NULL,true)) learnUpdateStatus(LEARN_DB_WAIT_INFO);
  314. }else{
  315. if(needUpdate!=0){
  316. MSG_INFO(1, "learn task need sync");
  317. if(true==learnTaskUpdate(LEARN_GET_LIST,NULL, NULL,true)) learnUpdateStatus(LEARN_WAIT_LIST);
  318. }else learnUpdateStatus(LEARN_DB_SYNC_DONE);//子菜单返回时,直接读取显示,不去同步
  319. }
  320. needUpdate=0;
  321. #else
  322. if(needUpdate!=0){
  323. MSG_INFO(1, "learn task need sync");
  324. if(true==learnTaskUpdate(LEARN_GET_LIST,NULL, NULL,true)) learnUpdateStatus(LEARN_WAIT_LIST);
  325. }else learnUpdateStatus(LEARN_DB_SYNC_DONE);//子菜单返回时,直接读取显示,不去同步
  326. #endif
  327. }
  328. LearnTaskProcess();
  329. }
  330. static void learnTaskDel(const char *delFileName);
  331. typedef struct{//用于显示的时候使用的
  332. bool initStatus;
  333. unsigned char direction;//获取任务方向 0第一次获取,1向下,2向上
  334. unsigned char lastDirection;//上一次获取方向
  335. unsigned short index;//当前正查看询的索引
  336. unsigned short selIndex;//最后读出来的项目,未经处理后的项目索引值,用于显示第几条使用
  337. unsigned char validNum;//当前页有效项目数
  338. unsigned short totalNum;//总任务数
  339. cJSON* jsonList;//用于读取简表
  340. char *bufList;//用于读取简表文件时使用的内存
  341. cJSON* jsonTask[PAGE_SIZE];//用于读取每个任务详情
  342. char *bufTask[PAGE_SIZE];//用于读取每个任务文件使用的内存
  343. char showTable[PAGE_SIZE][SHOW_TABLE_MAX_BYTES];//用于菜单显示时使用
  344. char finishedFlag[PAGE_SIZE];//完成状态
  345. }LEARN_SHOW_DEF;
  346. LEARN_SHOW_DEF sutJson={false};
  347. static void sutJsonInit(void){
  348. if(sutJson.initStatus==true) return;
  349. MSG_INFO(1, "sutJson init");
  350. memset((unsigned char *)&sutJson, 0, sizeof(LEARN_SHOW_DEF));
  351. sutJson.initStatus=true;
  352. }
  353. static unsigned char learnGetNextPageInfo(LEARN_SHOW_DEF *learn){
  354. cJSON* item, * element, * t;
  355. cJSON* tmpJson,*sele;
  356. char *tmpBuf;
  357. char file[50];
  358. int i;
  359. MSG_INFO(1, "GET PAGE INFO START");
  360. item=cJSON_GetObjectItem(learn->jsonList, OBJECT_LIST);
  361. if(NULL==item) return 1;//没有找到有元素数组
  362. int num=cJSON_GetArraySize(item);
  363. if(num<=0) return 0;//没有任务,返回时会显示"无任务"
  364. learn->totalNum=num;
  365. int index=learn->index;//这里不处理learn->index
  366. if(learn->direction==1 && learn->lastDirection==2 && num<=PAGE_SIZE){//上次是向上,现在向下
  367. index += learn->validNum;
  368. if(index>=num) index=0;
  369. }else if(learn->direction==2 && learn->lastDirection==1 && num<=PAGE_SIZE){//上次是向下,现在是向上
  370. index -= learn->validNum;
  371. if(index>0 && index<5) index=0;
  372. else if(index<=0){
  373. if(num==PAGE_SIZE) index=0;//如果与本页显示数目匹配,直接设置成0
  374. else index=num-num%PAGE_SIZE;
  375. }else index -= PAGE_SIZE;
  376. }else if(learn->direction==0){
  377. learn->direction=1;
  378. }
  379. //需要先释放上次进来时使用过的资源
  380. for(i=0;i<PAGE_SIZE;i++){
  381. releaseBuf(&learn->bufTask[learn->validNum]);
  382. releaseJson(&learn->jsonTask[learn->validNum]);
  383. }
  384. learn->validNum=0;
  385. for(;;){
  386. element=cJSON_GetArrayItem(item, index);
  387. if(NULL==element){
  388. if(index==num){//读到结尾了
  389. learn->selIndex=index-learn->validNum;
  390. learn->index=0;//重置
  391. goto P_END;
  392. }else return 2;//出错了
  393. }else{
  394. //获取tid
  395. t=cJSON_GetObjectItem(element, OBJECT_TID);
  396. if(NULL==t) goto P_CONTINUE;
  397. if(t->valuestring==NULL) goto P_CONTINUE;//没有找到值
  398. //找到TID,根据TID获取文件内容出来/*{"s":4,"c":"What is your name?","t":"学习任务1","f":1,"tid":5126*/
  399. snprintf(file, sizeof(file), "%s.task", t->valuestring);
  400. tmpBuf=getLearnFileJson(file);
  401. if(NULL==tmpBuf) goto P_CONTINUE;
  402. tmpJson=createJson(tmpBuf);
  403. if(NULL==tmpJson){
  404. releaseBuf(&tmpBuf);
  405. goto P_CONTINUE;
  406. }
  407. //获取OBJECT_FINISH
  408. sele=cJSON_GetObjectItem(tmpJson, OBJECT_FINISH);
  409. if(NULL==sele){//获取finish失败
  410. releaseBuf(&tmpBuf);
  411. releaseJson(&tmpJson);
  412. goto P_CONTINUE;
  413. }
  414. if(sele->valuestring==NULL) goto P_CONTINUE;
  415. i=atoi(sele->valuestring);
  416. if(i==0) learn->finishedFlag[learn->validNum]=0;
  417. else learn->finishedFlag[learn->validNum]=1;
  418. //获取OBJECT_TITLE
  419. sele=cJSON_GetObjectItem(tmpJson, OBJECT_TITLE);
  420. if(NULL==sele){//获取TITLE失败
  421. releaseBuf(&tmpBuf);
  422. releaseJson(&tmpJson);
  423. goto P_CONTINUE;
  424. }
  425. if(sele->valuestring==NULL) goto P_CONTINUE;
  426. //格式化TITLE到显示Buff用于显示(看一下是否要转换编码格式)
  427. snprintf(learn->showTable[learn->validNum], SHOW_TABLE_MAX_BYTES, "%s", sele->valuestring);
  428. learn->jsonTask[learn->validNum]=tmpJson;
  429. learn->bufTask[learn->validNum]=tmpBuf;
  430. if(++learn->validNum>=PAGE_SIZE){
  431. //获取够了,暂时不需要去释放资源,因为如果按确定查看内容时,还需要读取任务详细内容
  432. index++;
  433. learn->selIndex=index-learn->validNum;
  434. if(index>=num) index=0;
  435. learn->index=index;
  436. goto P_END;
  437. }
  438. }
  439. P_CONTINUE:
  440. index++;
  441. }
  442. P_END:
  443. learn->lastDirection=learn->direction;
  444. return 0;
  445. }
  446. static void learnUIShowIndexAndTotal(unsigned int index, unsigned int total){
  447. char info[20];
  448. int i;
  449. snprintf(info, sizeof(info), "%d/%d", index, total);
  450. i=guiGetStrXLen(info,FONT_MODE_12X12);
  451. guiClearRect(GLCD_WIDTH-8*8, UI_STATUS_ITEM_Y, GLCD_WIDTH-1, UI_STATUS_ITEM_Y+UI_STATUS_ITEM_HEIGH-1, guiGetForeColor());
  452. guiShowStr(GLCD_WIDTH-i, UI_STATUS_ITEM_Y+1, info, FONT_MODE_12X12, REVERSED_NO, guiGetBackColor(), guiGetForeColor());
  453. }
  454. void LearnTaskSelResponse(void){
  455. unsigned short key;
  456. if(LEARN_WAIT_RESPONSE==getLearnStatus()){
  457. key=getKeyValue();
  458. if(sutJson.validNum!=0){
  459. if(sutListBox.handle==0 && MKEY_VALUE_UP==key){
  460. sutJson.direction=2;
  461. learnUpdateStatus(LEARN_READ_TASK_INFO);
  462. }else if(((sutListBox.handle+1) == sutListBox.itemnum) && MKEY_VALUE_DOWN==key){
  463. sutJson.direction=1;
  464. learnUpdateStatus(LEARN_READ_TASK_INFO);
  465. }
  466. key=ListBoxResponse(&sutListBox);
  467. if(MKEY_VALUE_ESC==key) learnUpdateStatus(LEARN_RELEASE_DB);
  468. else if(MKEY_VALUE_MENU==key){
  469. uiPushStack(sutListBox.handle);
  470. uISetNextStatus(UIS_MENU_TASK_QUERY);
  471. //里面的菜单如果不返回本菜单,直接退出此任务操作,需要释放资源LEARN_RELEASE_DB
  472. }else if(MKEY_VALUE_UP || MKEY_VALUE_DOWN){
  473. learnUIShowIndexAndTotal(sutJson.selIndex+sutListBox.handle+1,sutJson.totalNum);
  474. }
  475. }else if(MKEY_VALUE_ESC==key) learnUpdateStatus(LEARN_RELEASE_DB);
  476. }
  477. }
  478. static void LearnTaskProcess(void){
  479. unsigned char ret;
  480. static char *simpleList=NULL;
  481. static char *simpleListTmp=NULL;
  482. static cJSON* json=NULL,*item;
  483. cJSON* element,*subEle;
  484. static cJSON* jsonTmp=NULL,*itemTmp;
  485. cJSON* elementTmp,*subEleTmp;
  486. static int index;
  487. char tid[10],ver[10];
  488. static bool updateAll;
  489. int i,j,page,size;
  490. cJSON* readJson=NULL;
  491. static char *readBuf=NULL;
  492. static unsigned int tidTable[LEARN_UPDATE_MAX];
  493. static const char *learnIcon[]={
  494. "TASK1.bmp",
  495. "TASK2.bmp",
  496. ""
  497. };
  498. static char *menuTable[PAGE_SIZE+1];
  499. switch(getLearnStatus()){
  500. case LEARN_DB_WAIT_INFO:
  501. ret=getHttpLearnStatus();
  502. if(uTimerExpired(&learnTick) || ret!=1){
  503. MSG_INFO(1, "LEARN_DB_WAIT_INFO:%d",ret);
  504. switch(ret){
  505. case 0://获取用户信息成功,获取简表信息
  506. if(true==learnTaskUpdate(LEARN_GET_LIST,NULL, NULL,true)) learnUpdateStatus(LEARN_WAIT_LIST);
  507. else{
  508. showInfoBox("更新失败",2000);
  509. learnUpdateStatus(LEARN_END);
  510. }
  511. break;
  512. case 2://列表获取失败
  513. showInfoBox("更新失败",2000);
  514. learnUpdateStatus(LEARN_END);
  515. break;
  516. default://获取超时
  517. hearnHttpOverTime();
  518. showInfoBox("更新超时",2000);
  519. learnUpdateStatus(LEARN_END);
  520. break;
  521. }
  522. }
  523. break;
  524. case LEARN_WAIT_LIST:
  525. ret=getHttpLearnStatus();
  526. if(uTimerExpired(&learnTick) || ret!=1){
  527. MSG_INFO(1, "LEARN_WAIT_LIST:%d",ret);
  528. switch(ret){
  529. case 0://获取列表成功,同步数据
  530. learnUpdateStatus(LEARN_DB_SYNC);
  531. break;
  532. case 2://列表获取失败
  533. showInfoBox("更新失败",2000);
  534. learnUpdateStatus(LEARN_END);
  535. break;
  536. default://获取超时
  537. hearnHttpOverTime();
  538. showInfoBox("更新超时",2000);
  539. learnUpdateStatus(LEARN_END);
  540. break;
  541. }
  542. }
  543. break;
  544. case LEARN_DB_SYNC://根据LEARN_LIST表同步数据
  545. //读取LEARN_LIST_TMP数据用于同步
  546. simpleListTmp=getLearnFileJson(LEARN_LIST_TMP);
  547. if(NULL==simpleListTmp){
  548. learnUpdateStatus(LEARN_DB_SYNC_FAILED);
  549. break;
  550. }
  551. //加载到CJSON
  552. jsonTmp=createJson(simpleListTmp);
  553. if(NULL==jsonTmp){
  554. learnUpdateStatus(LEARN_DB_SYNC_FAILED);
  555. break;
  556. }
  557. itemTmp=cJSON_GetObjectItem(jsonTmp, OBJECT_LIST);
  558. if(NULL==itemTmp){
  559. learnUpdateStatus(LEARN_DB_SYNC_FAILED);
  560. break;
  561. }
  562. index=0;
  563. learnUpdateStatus(LEARN_DB_SYNC_ADD);//获取新任务
  564. //再检测LEARN_LIST
  565. simpleList=getLearnFileJson(LEARN_LIST);
  566. if(NULL==simpleList){//读取失败,则直接认为没有主简表,则直接全部更新
  567. updateAll=true;//全部拉取更新
  568. break;
  569. }
  570. //加载到CJSON
  571. json=createJson(simpleList);
  572. if(NULL==json){//加载失败,全部更新
  573. updateAll=true;//全部拉取更新
  574. break;
  575. }
  576. item=cJSON_GetObjectItem(json, OBJECT_LIST);
  577. if(NULL==item){//无OBJECT_LIST项目,全部更新
  578. updateAll=true;//全部拉取更新
  579. break;
  580. }
  581. MSG_INFO(1, "LEARN_DB_SYNC_DEL");
  582. learnUpdateStatus(LEARN_DB_SYNC_DEL);
  583. updateAll=false;
  584. //开始同步(先同步任务文件,最后更新任务简表总表)
  585. break;
  586. case LEARN_DB_SYNC_DEL://先删除多余的任务
  587. //读取主简表中有,但临时简表中没有的任务,然后删除之
  588. i=0;
  589. MSG_INFO(1, "LEARN_DB_SYNC_DEL Start");
  590. for(;;){
  591. element=cJSON_GetArrayItem(item, i);
  592. if(element!=NULL){
  593. subEle=cJSON_GetObjectItem(element, OBJECT_TID);
  594. if(subEle!=NULL){//找到了一个任务,检测在TMP任务中是否存在
  595. if(subEle->valuestring==NULL) goto C_NEXT;//tid值为空,忽视此项
  596. j=0;
  597. for(;;){
  598. elementTmp=cJSON_GetArrayItem(itemTmp, j);
  599. if(elementTmp!=NULL){
  600. subEleTmp=cJSON_GetObjectItem(elementTmp, OBJECT_TID);
  601. if(subEleTmp!=NULL){//比对
  602. if(subEleTmp->valuestring==NULL) break;//tid值为空,忽视此项,认为不存在
  603. if(0==strcmp(subEleTmp->valuestring, subEle->valuestring)) goto C_NEXT;//存在,不处理
  604. }
  605. }else break;//检查完了
  606. j++;
  607. }
  608. //在新(临时)任务简表总表中未找到此任务,即为多余任务,需要删除掉
  609. learnTaskDel(subEle->valuestring);
  610. }
  611. }else break;//检测完了
  612. C_NEXT:
  613. i++;
  614. }
  615. if(updateAll==true){//全部更新,就不需要再使用simpleList,simpleListTmp,json,jsonTmp
  616. releaseBuf(&simpleList);
  617. releaseBuf(&simpleListTmp);
  618. releaseJson(&json);
  619. releaseJson(&jsonTmp);
  620. MSG_INFO(1, "Free resorce");
  621. }else{/*不是全部更新时还是需要*/}
  622. MSG_INFO(1, "LEARN_DB_SYNC_DEL end");
  623. learnUpdateStatus(LEARN_DB_SYNC_ADD);//添加新任务了
  624. break;
  625. case LEARN_DB_SYNC_ADD:
  626. if(true==updateAll){//全部更新,同一次获取多个
  627. page=index/LEARN_UPDATE_MAX;
  628. size=LEARN_UPDATE_MAX;
  629. learnAddNum=0;
  630. learnUpdateStatus(LEARN_DB_SYNC_ADD_PAGE_WAIT);
  631. learnDetailEnd=false;//检测是否下载完毕使用
  632. learnTaskUpdate(LEARN_DB_SYNC_ADD_PAGE_WAIT,(void *)&page, (void *)&size,true);
  633. uTimerStart(&learnTick, 2000);
  634. }else{//根据两个简表,添加回LEARN_LIST中没有,但LEARN_LIST_TMP中有的任务
  635. for(i=0;i<LEARN_UPDATE_MAX;i++) tidTable[i]=0;
  636. learnAddNum=0;
  637. for(;;){
  638. elementTmp=cJSON_GetArrayItem(itemTmp, index++);
  639. if(NULL==elementTmp){//同步完了
  640. break;
  641. }
  642. subEleTmp=cJSON_GetObjectItem(elementTmp, OBJECT_TID);
  643. if(NULL==subEleTmp){//没有找到TID,忽视之
  644. continue;
  645. }
  646. if(subEleTmp->valuestring==NULL) continue;//tid值为空,忽视此项
  647. //找到任务ID subEleTmp->valuestring,遍历LEARN_LIST中是否不存在,是则需要获取
  648. int w=0;
  649. bool taskExist=false;
  650. for(;;){
  651. element=cJSON_GetArrayItem(item,w++);
  652. if(NULL==element) break;//遍历完了
  653. subEle=cJSON_GetObjectItem(element, OBJECT_TID);
  654. if(NULL==subEle) continue;
  655. if(subEle->valuestring==NULL) continue;//tid值为空,忽视此项
  656. if(strcmp(subEle->valuestring, subEleTmp->valuestring)==0){//任务存在
  657. //对比OBJECT_VER是否一样
  658. cJSON* tver, *tverTmp;
  659. tver=cJSON_GetObjectItem(element, OBJECT_VER);
  660. tverTmp=cJSON_GetObjectItem(elementTmp, OBJECT_VER);
  661. if(tver==NULL || tverTmp==NULL) break;//有找不到OBJECT_VER字段的,认为需要得新获取处理
  662. if(strcmp(tver->valuestring, tverTmp->valuestring)!=0) break;//OBJECT_VER变化了,需要获取处理
  663. taskExist=true;
  664. break;
  665. }
  666. }
  667. if(taskExist==false){//任务不存在添加到列表
  668. tidTable[learnAddNum++]=atoi(subEleTmp->valuestring);
  669. if(learnAddNum>=LEARN_UPDATE_MAX) break;//列表满了,需要先获取
  670. }
  671. }
  672. if(learnAddNum==0){//同步完成
  673. MSG_INFO(1, "LEARN_DB_SYNC_ADD_SOME_WAIT done");
  674. releaseBuf(&simpleList);
  675. releaseBuf(&simpleListTmp);
  676. releaseJson(&json);
  677. releaseJson(&jsonTmp);
  678. learnUpdateListFile();
  679. learnUpdateStatus(LEARN_DB_SYNC_DONE);
  680. break;
  681. }
  682. //发起新的文件获取(根据任务ID获取)
  683. //文件ID表在tidTable中,个数为learnAddNum个
  684. learnUpdateStatus(LEARN_DB_SYNC_ADD_SOME_WAIT);
  685. learnTaskUpdate(LEARN_DB_SYNC_ADD_SOME_WAIT,(void *)tidTable, (void *)&learnAddNum,true);
  686. MSG_ERR(1, "update tids:%d", learnAddNum);
  687. }
  688. break;
  689. case LEARN_DB_SYNC_ADD_PAGE_WAIT:
  690. case LEARN_DB_SYNC_ADD_SOME_WAIT:
  691. ret=getHttpLearnStatus();
  692. if(uTimerExpired(&learnTick) || ret!=1){
  693. MSG_INFO(1, "LEARN_DB_SYNC_ADD_PAGE/SOME_WAIT:%d,%d",getLearnStatus(),ret);
  694. switch(ret){
  695. case 0://获取任务详表成功
  696. if(getLearnStatus()==LEARN_DB_SYNC_ADD_PAGE_WAIT){
  697. if(learnDetailEnd==false){
  698. index += learnAddNum;
  699. learnUpdateStatus(LEARN_DB_SYNC_ADD);
  700. }else{//同步完成了
  701. releaseBuf(&simpleList);
  702. releaseBuf(&simpleListTmp);
  703. releaseJson(&json);
  704. releaseJson(&jsonTmp);
  705. //删除simpleList,simpleListTmp重命名为simpleList
  706. learnUpdateListFile();
  707. learnUpdateStatus(LEARN_DB_SYNC_DONE);
  708. }
  709. }else learnUpdateStatus(LEARN_DB_SYNC_ADD);//继续
  710. break;
  711. case 2://失败
  712. learnUpdateStatus(LEARN_DB_SYNC_FAILED);
  713. break;
  714. default://获取超时
  715. hearnHttpOverTime();
  716. learnUpdateStatus(LEARN_DB_SYNC_FAILED);
  717. break;
  718. }
  719. }
  720. break;
  721. case LEARN_DB_SYNC_DONE://同步完成,可以读取用于显示
  722. MSG_INFO(1, "LEARN_DB_SYNC_DONE");
  723. //释放
  724. releaseJson(&json);
  725. releaseJson(&jsonTmp);
  726. releaseBuf(&sutJson.bufList);
  727. releaseJson(&sutJson.jsonList);
  728. for(i=0;i<PAGE_SIZE;i++){
  729. releaseBuf(&sutJson.bufTask[i]);
  730. releaseJson(&sutJson.jsonTask[i]);
  731. }
  732. sutJson.direction=0;
  733. sutJson.lastDirection=0;
  734. sutJson.index=0;
  735. //先读取简表,得到任务ID列表,随着菜单选择,每次最多最取PAGE_SIZE个任务出来显示
  736. sutJson.bufList=getLearnFileJson(LEARN_LIST);
  737. if(NULL==sutJson.bufList){
  738. learnUpdateStatus(LEARN_READ_FAILED);
  739. break;
  740. }
  741. sutJson.jsonList=createJson(sutJson.bufList);
  742. if(NULL==sutJson.jsonList){
  743. learnUpdateStatus(LEARN_READ_FAILED);
  744. break;
  745. }
  746. learnUpdateStatus(LEARN_READ_TASK_INFO);
  747. break;
  748. case LEARN_READ_FAILED:
  749. showInfoBox("数据读取失败",2000);
  750. releaseBuf(&sutJson.bufList);
  751. releaseJson(&sutJson.jsonList);
  752. for(i=0;i<PAGE_SIZE;i++){
  753. releaseBuf(&sutJson.bufTask[i]);
  754. releaseJson(&sutJson.jsonTask[i]);
  755. }
  756. learnUpdateStatus(LEARN_END);
  757. break;
  758. case LEARN_DB_SYNC_FAILED:
  759. releaseBuf(&simpleList);
  760. releaseBuf(&simpleListTmp);
  761. releaseJson(&json);
  762. releaseJson(&jsonTmp);
  763. showInfoBox("数据同步失败",2000);
  764. learnUpdateStatus(LEARN_END);
  765. break;
  766. case LEARN_READ_TASK_INFO://根据方向,获取下/上page项目
  767. sutJson.validNum=0;
  768. ret=learnGetNextPageInfo(&sutJson);
  769. MSG_INFO(1, "learnGetNextPageInfo:%d", ret);
  770. if(ret!=0) learnUpdateStatus(LEARN_READ_FAILED);
  771. else learnUpdateStatus(LEARN_SHOW_MENU);
  772. break;
  773. case LEARN_SHOW_MENU://显示
  774. MSG_INFO(1, "Task valid num:%d", sutJson.validNum);
  775. guiClearRect(0,(GLCD_HEIGHT/2)-20,GLCD_WIDTH-1,(GLCD_HEIGHT/2)+20,guiGetBackColor());//清除消息框
  776. if(sutJson.validNum==0){
  777. guiShowMessageBox("无数据");
  778. }else{
  779. for(i=0;i<sutJson.validNum;i++) menuTable[i]=&sutJson.showTable[i];
  780. menuTable[sutJson.validNum]=learnIcon[2];
  781. ListBoxInit(&sutListBox, menuTable,0,learnIcon, sutJson.finishedFlag);
  782. learnUIShowIndexAndTotal(sutJson.selIndex+sutListBox.handle+1,sutJson.totalNum);
  783. }
  784. learnUpdateStatus(LEARN_WAIT_RESPONSE);
  785. break;
  786. case LEARN_WAIT_RESPONSE:
  787. break;
  788. case LEARN_RELEASE_DB:
  789. MSG_INFO(1, "Release DB");
  790. releaseBuf(&simpleList);
  791. releaseBuf(&simpleListTmp);
  792. releaseBuf(&sutJson.bufList);
  793. releaseJson(&sutJson.jsonList);
  794. for(i=0;i<PAGE_SIZE;i++){
  795. releaseBuf(&sutJson.bufTask[i]);
  796. releaseJson(&sutJson.jsonTask[i]);
  797. }
  798. sdUnmount();
  799. uISetNextStatus(UIS_MENU_MAIN);
  800. needUpdate=1;
  801. break;
  802. case LEARN_END:
  803. if(uTimerExpired(&learnTick)){
  804. sdUnmount();
  805. uISetNextStatus(UIS_MENU_MAIN);
  806. needUpdate=1;
  807. }
  808. break;
  809. }
  810. }
  811. ////////////////////////http 获取文件数据处理///////////////////////
  812. //#define USE_TEST_TASK //使用自己定义的任务
  813. static void cJSONSaveTaskFile(const char *jsonMsg);
  814. static void learnSaveTaskFile(const char *fname, const char *msg);
  815. //使用cJSON格式保存成文件
  816. void cJSONSaveFile(const char *fname, char *msg, int len){
  817. //保存到SD卡中
  818. char file[50];
  819. snprintf(file, sizeof(file), "%s/%s",SD_TOKEN, fname);
  820. int fd = nwy_sdk_fopen(file, NWY_CREAT | NWY_RDWR|NWY_TRUNC);//清0式
  821. if(fd<0){
  822. MSG_WARN(1, "fopen failed,%s", fname);
  823. return;
  824. }
  825. #ifdef USE_TEST_TASK
  826. const char *testmsg="{\"uid\":\"12345\",\"list\":[{\"ver\":\"1\",\"tid\":\"5126\"},{\"ver\":\"2\",\"tid\":\"5127\"},{\"ver\":\"3\",\"tid\":\"8867\"},{\"ver\":\"2\",\"tid\":\"8868\"},{\"ver\":\"5\",\"tid\":\"1221\"},{\"ver\":\"1\",\"tid\":\"2546\"}]}";
  827. nwy_sdk_fwrite(fd, testmsg, strlen(testmsg));
  828. #else
  829. nwy_sdk_fwrite(fd, msg, len);
  830. #endif
  831. nwy_sdk_fclose(fd);
  832. MSG_INFO(1, "%s save done:%d", fname, len);
  833. }
  834. static void getRankShowMessage(unsigned char ranktype);
  835. static void httpCallBack(char *msg, int len){
  836. //将长为len的UTF8数据msg转换成GBK
  837. int outLen;
  838. unsigned char ret;
  839. MSG_INFO(1, "httpCallBack UTF8:%d", len);
  840. if(0!=Utf8ToGbkOrUnicode(msg, len, msg, &outLen,0)){
  841. MSG_ERR(1, "UTF8 to GBK failed");
  842. }
  843. ret=getLearnStatus();
  844. MSG_INFO(1, "httpCallBack GBK:%d,%d", outLen,ret);
  845. msg[outLen]=0;
  846. if(ret==LEARN_DB_WAIT_INFO){//获取到了用户信息
  847. //解析一下
  848. cJSONProcessUinfo(msg);
  849. }else if(ret==LEARN_WAIT_LIST){//获取到了列表简表
  850. //保存成功为文件,文件名为临时总表LEARN_LIST_TMP
  851. cJSONSaveFile(LEARN_LIST_TMP, msg,outLen);
  852. }else if(ret==LEARN_DB_SYNC_ADD_PAGE_WAIT || ret==LEARN_DB_SYNC_ADD_SOME_WAIT){//获取到了任务详表
  853. //每个任务保存成一个.task文件
  854. cJSONSaveTaskFile(msg);
  855. }else if(ret==LEARN_RANK_GET_DETAIL){
  856. cJSONSaveFile(LEARN_RANK, msg,outLen);
  857. }else if(ret==LEARN_CLASS_GET_DETAIL){
  858. cJSONSaveFile(LEARN_CLASS, msg, outLen);
  859. }else if(ret==LEARN_NOTICE_GET_DETAIL){
  860. cJSONSaveFile(LEARN_NOTICE, msg, outLen);
  861. }else if(ret==LEARN_NOTICE_GET_DETAIL_SHOW){
  862. cJSONTakeContent(msg, outLen);
  863. }else if(ret==LEARN_GET_PARAM){
  864. cJSONTakeParam(msg, outLen);
  865. }
  866. MSG_INFO(1, "httpCallBack end");
  867. }
  868. //读取指令文件内容到缓存中(注意释放内存)
  869. static char *getLearnFileJson(const char *fname){
  870. char *buf=NULL,file[50];
  871. int fd;
  872. //只读方式打开文件
  873. snprintf(file, sizeof(file), "%s/%s",SD_TOKEN, fname);
  874. fd=nwy_sdk_fopen(file, NWY_RDONLY);
  875. if(fd<0) goto L_END;
  876. //获取文件大小
  877. long size=nwy_sdk_fsize_fd(fd);
  878. if(size<=0) goto L_END;
  879. //分配内存空间
  880. buf=(char *)createBuf(size);
  881. //buf=(char *)malloc(size);
  882. if(NULL==buf) goto L_END;
  883. nwy_sdk_fread(fd, buf, size);
  884. L_END:
  885. if(fd>0) nwy_sdk_fclose(fd);
  886. return buf;
  887. }
  888. //delFileName:任务ID
  889. //删除以delFileName为名字的.task .rec文件
  890. static void learnTaskDel(const char *delFileName){
  891. char file[50];
  892. //删除.task文件
  893. snprintf(file, sizeof(file), "%s/%s.task", SD_TOKEN, delFileName);
  894. if(true==nwy_sdk_fexist(file)){
  895. nwy_sdk_file_unlink(file);
  896. MSG_INFO(1, "Del %s.task", delFileName);
  897. }
  898. //删除.rec文件
  899. snprintf(file, sizeof(file), "%s/%s.rec", SD_TOKEN, delFileName);
  900. if(true==nwy_sdk_fexist(file)){
  901. nwy_sdk_file_unlink(file);
  902. MSG_INFO(1, "Del %s.rec", delFileName);
  903. }
  904. MSG_INFO(1, "Del tid %s end", delFileName);
  905. }
  906. void printfJsonValue(cJSON* json){
  907. if(NULL==json) return;
  908. char *buf=cJSON_Print(json);
  909. if(NULL==buf) return;
  910. MSG_INFO(1, "json:%s",buf);
  911. free(buf);
  912. }
  913. //解析JSON得到tid作为文件名,同时将任务JCSON保存起来
  914. static void cJSONSaveTaskFile(const char *jsonMsg){
  915. int i,fd;
  916. cJSON* json,*item,*element,*t;
  917. char *buf;
  918. #ifdef USE_TEST_TASK
  919. static char nextFlag=0;
  920. const *testBuf1="{\"uid\":\"12348\",\"list\":[{\"s\":\"4\",\"c\":\"What is your name?0123一二三四567890123一二三四567890123一二三四567890123一二三四567890123一二三四567890123一二三四567890123一二三四567890123一二三四567890123一二三四56789\",\"t\":\"学习任务1\",\"f\":\"1\",\"tid\":\"5126\"}, \
  921. {\"s\":\"3\",\"c\":\"Where are you?\",\"t\":\"学习任务2\",\"f\":\"1\",\"tid\":\"5127\"}, \
  922. {\"s\":\"5\",\"c\":\"How do you do!\",\"t\":\"学习任务3\",\"f\":\"1\",\"tid\":\"8867\"}, \
  923. {\"s\":\"3\",\"c\":\"Good morning!\",\"t\":\"学习任务4\",\"f\":\"1\",\"tid\":\"8868\"}, \
  924. {\"s\":\"0\",\"c\":\"How old are you?\",\"t\":\"学习任务5\",\"f\":\"0\",\"tid\":\"1221\"}, \
  925. {\"s\":\"0\",\"c\":\"I am come from Shenzhen, China\",\"t\":\"学习任务6\",\"f\":\"0\",\"tid\":\"2546\"} \
  926. ]}";
  927. const *testBuf2="{\"uid\":\"12348\",\"list\":[]}";
  928. char *ptr;
  929. if(nextFlag==0){
  930. ptr=testBuf1;
  931. nextFlag=1;
  932. }else if(nextFlag==1){
  933. ptr=testBuf2;
  934. nextFlag=0;
  935. }
  936. json=createJson(ptr);
  937. #else
  938. json=createJson(jsonMsg);
  939. #endif
  940. if(json==NULL){
  941. MSG_WARN(1, "cJSONSaveTaskFile json null");
  942. return;
  943. }
  944. item=cJSON_GetObjectItem(json, OBJECT_LIST);
  945. if(NULL==item){
  946. MSG_WARN(1, "cJSONSaveTaskFile item null");
  947. goto S_END;
  948. }
  949. //获取OBJECT_LIST元素个数
  950. int num=cJSON_GetArraySize(item);
  951. MSG_INFO(1, "Num:%d", num);
  952. if(0==num){//获取完成了
  953. learnDetailEnd=true;
  954. MSG_INFO(1, "cJSONSaveTaskFile done");
  955. }else{
  956. //解析每个任务的任务ID
  957. i=0;
  958. for(;;){
  959. element=cJSON_GetArrayItem(item, i);
  960. if(NULL==element){
  961. MSG_INFO(1, "json array end");
  962. break;
  963. }
  964. t=cJSON_GetObjectItem(element, OBJECT_TID);
  965. if(NULL != t){
  966. //得到一个任务ID,内容为cJSON_Print(element),保存起来
  967. buf=cJSON_Print(element);
  968. if(NULL==buf){
  969. MSG_ERR(1, "cJSONSaveTaskFile cJSON_Print null");
  970. break;
  971. }
  972. learnSaveTaskFile(t->valuestring, buf);
  973. releaseBuf(&buf);
  974. //MSG_INFO(1, "[mem free]%x",buf);
  975. //free(buf);
  976. learnAddNum++;//已添加一个文件(任务)
  977. MSG_INFO(1, "Save one task:%s.task", t->valuestring);
  978. }else MSG_WARN(1, "No find tid");
  979. i++;
  980. }
  981. }
  982. S_END:
  983. releaseJson(&json);
  984. //cJSON_Delete(json);
  985. }
  986. static void learnSaveTaskFile(const char *fname, const char *msg){
  987. int fd;
  988. char file[50];
  989. if(fname==NULL) return;
  990. snprintf(file, sizeof(file), "%s/%s.task", SD_TOKEN, fname);
  991. fd = nwy_sdk_fopen(file, NWY_CREAT | NWY_RDWR|NWY_TRUNC);//清0式
  992. if(fd<0){
  993. MSG_WARN(1, "learnSaveTaskFile fopen failed,%s", fname);
  994. return;
  995. }
  996. nwy_sdk_fwrite(fd, msg, strlen(msg));
  997. nwy_sdk_fclose(fd);
  998. MSG_INFO(1, "Save task %s", fname);
  999. }
  1000. unsigned char haveNextPage;
  1001. unsigned char taskIndex;
  1002. char tidString[20];//保存正在查看的任务的tid串
  1003. char *learnGetTid(void){return tidString;}
  1004. /*
  1005. 统计有多少个单词
  1006. 原理:单词间用标点(, .)或空格分开
  1007. */
  1008. static void calWordNum(char *msg){
  1009. int i,len=strlen(msg);
  1010. unsigned char num=0;
  1011. unsigned char lashChara=0;
  1012. MSG_INFO(1, "content:%s",msg);
  1013. for(i=0;i<len;i++){
  1014. if(msg[i]==',' || msg[i]=='.' || msg[i]==' ' || msg[i]=='?' || msg[i]=='!'){
  1015. if(lashChara!=0){//记录为一个单词出现
  1016. num++;
  1017. lashChara=0;
  1018. }
  1019. }else if(lashChara==0) lashChara=msg[i];//有单词出现
  1020. }
  1021. if(lashChara!=0) num++;//没有标点结尾时
  1022. charaNum=num;
  1023. MSG_INFO(1, "Chara Num:%d", num);
  1024. }
  1025. void readTaskToShow(void){
  1026. char isDetailValid=0;
  1027. char info[50];
  1028. static cJSON* eleScore ,* eleContent;
  1029. //读取当前任务的uid保存起来先
  1030. tidString[0]=0;
  1031. eleScore=cJSON_GetObjectItem(sutJson.jsonTask[taskIndex], OBJECT_TID);
  1032. if(NULL != eleScore) snprintf(tidString, sizeof(tidString), "%s", eleScore->valuestring);
  1033. eleScore=cJSON_GetObjectItem(sutJson.jsonTask[taskIndex], OBJECT_SCORE);
  1034. eleContent=cJSON_GetObjectItem(sutJson.jsonTask[taskIndex], OBJECT_CONTENT);
  1035. //制造分数串
  1036. if(NULL==eleScore){//没有找到分数字段,显示'--'
  1037. snprintf(info, sizeof(info), "分数:--");
  1038. }else if(eleScore->valuestring==NULL){
  1039. snprintf(info, sizeof(info), "分数:--");
  1040. }else snprintf(info, sizeof(info), "分数:%s", eleScore->valuestring);
  1041. //内容
  1042. if(NULL!=eleContent && eleContent->valuestring!=NULL){
  1043. isDetailValid=1;
  1044. calWordNum(eleContent->valuestring);//计算单词个数
  1045. }
  1046. if(0!=newMessageInit(FONT_MODE_12X12, info, eleContent->valuestring, NULL)){
  1047. MSG_WARN(1, "learn task show get error");
  1048. }
  1049. }
  1050. unsigned char recoderVolume=0;
  1051. LEARN_REC_STATUS learnRecordStatus;
  1052. void learnRecSetStatus(LEARN_REC_STATUS newStatus){
  1053. learnRecordStatus=newStatus;
  1054. }
  1055. void learnRecSetVol(unsigned char newVol){
  1056. recoderVolume=newVol;
  1057. }
  1058. static void learnShowResult(void);
  1059. static void learnShowStartScreen(void){
  1060. const char *repeatListen="[PTT]录音";
  1061. if(usr.type==1) uiMenuShowBottomIndacitor("[OK]重听", repeatListen);
  1062. else uiMenuShowBottomIndacitor(NULL, repeatListen);
  1063. }
  1064. /*播放录音中止/完成后,返回原定界面及操作*/
  1065. void learnRetrunStatus(void){
  1066. if(0==learnGetAudioType()){//返回原定界面及操作
  1067. learnRecSetStatus(LEARN_REC_DONE);
  1068. uiMenuShowBottomIndacitor("[OK]播放","[F1]提交");//选择提交还是播放
  1069. learnShowResult();
  1070. }else{//返回原定界面及操作
  1071. learnRecSetStatus(LEARN_REC_IDLE);
  1072. learnShowStartScreen();
  1073. }
  1074. }
  1075. static void learnRecordDoneProcess(void);
  1076. static void learnDetailBackToTaskListMenu(void){
  1077. uISetNextStatus(UIS_MENU_TASK_SEL);//返回上层菜单
  1078. }
  1079. /*任务查看*/
  1080. void uiShowLearnTaskDetail(char update){
  1081. unsigned char ret;
  1082. if(update){
  1083. guiClearAll(guiGetBackColor());
  1084. guiShowCaption(0,"任务详情",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  1085. uiMenuShowBottomLine();
  1086. taskIndex=sutListBox.handle;
  1087. learnRecSetStatus(LEARN_REC_IDLE);
  1088. readTaskToShow();
  1089. learnShowStartScreen();
  1090. learnTick=0;
  1091. }
  1092. switch(learnRecordStatus){
  1093. case LEARN_REC_BUSY://动态显示正在录音效果
  1094. learnRecordVolShow(recoderVolume);
  1095. break;
  1096. if(uTimerExpired(&learnTick)){
  1097. uTimerStart(&learnTick, 500);
  1098. learnRecordVolShow(recoderVolume);
  1099. }
  1100. break;
  1101. case LEARN_REC_OT://录音已超过最大时间,停止录音
  1102. learnRecordDoneProcess();
  1103. #ifdef SAVE_PCM_SD
  1104. pcmFileClose();
  1105. #endif
  1106. break;
  1107. case LEARN_REC_PLY://正在播放
  1108. switch(learnPlyGetResult()){
  1109. case LEARN_PLY_PRE_ERR://播放失败
  1110. guiShowMessageBox("播放失败");
  1111. nwy_sleep(1500);
  1112. learnDetailBackToTaskListMenu();
  1113. break;
  1114. case LEARN_PLY_FAILED://播放异常
  1115. guiShowMessageBox("播放异常");//返回上层菜单
  1116. nwy_sleep(1500);
  1117. learnDetailBackToTaskListMenu();
  1118. break;
  1119. case LEARN_PLY_DONE://播放成功,结束
  1120. learnRetrunStatus();
  1121. break;
  1122. }
  1123. break;
  1124. case LEARN_RECORD_UPLOAD://等待上传结果
  1125. ret=getHttpLearnStatus();
  1126. if(uTimerExpired(&learnTick) || ret!=1){
  1127. MSG_INFO(1, "LEARN_RECORD_UPLOAD:%d",ret);
  1128. switch(ret){
  1129. case 0://上传完成
  1130. guiShowMessageBox("上传完成");
  1131. recordSaveTmpToLocal(tidString);
  1132. break;
  1133. case 2://上传失败
  1134. guiShowMessageBox("上传失败");
  1135. break;
  1136. default://上传超时
  1137. hearnHttpOverTime();
  1138. guiShowMessageBox("上传超时");
  1139. break;
  1140. }
  1141. nwy_sleep(1500);
  1142. //返回任务详情内容页面,录音临时信息及资源回收
  1143. learnRecordBufRelease();
  1144. learnShowStartScreen();
  1145. newMessageShow(SHOW_HEAD);
  1146. learnRecSetStatus(LEARN_REC_IDLE);
  1147. }
  1148. break;
  1149. }
  1150. }
  1151. //根据录制的语音内容算出平均值用于显示
  1152. //volLev:经过计算后得出的0~10等级数值
  1153. void learnRecordVolShow(unsigned char volLev){
  1154. unsigned char vol=volLev;
  1155. unsigned short y1,y2,x,i;
  1156. unsigned int colorID;
  1157. static unsigned char lashVel=99;
  1158. unsigned char width=8;//一格音量宽度
  1159. unsigned char tx=1;//音量柱间隔
  1160. unsigned char sx=0;//音量柱开始坐标
  1161. if(lashVel == volLev) return;
  1162. lashVel=volLev;
  1163. //然后使用绿色显示实际音量
  1164. for(i=0;i<LEV_NUM;i++){
  1165. if(i<vol) colorID=COLOR_BLUE;
  1166. else colorID=guiGetBackColor();//COLOR_GRAY;
  1167. y2=GLCD_HEIGHT-2;
  1168. y1=y2-i-3;if(y1<=UI_BOTTOM_LINE_Y) y1=UI_BOTTOM_LINE_Y+1;
  1169. x=sx+i*(width+tx);
  1170. guiDrawVLine(y1, y2, x, width, colorID);
  1171. }
  1172. }
  1173. /*复位用户超时定时器*/
  1174. void httpPostResetUserTimer(unsigned int persent){
  1175. char info[30];
  1176. snprintf(info,sizeof(info), "正在上传:%d%%",persent);
  1177. guiShowMessageBox(info);
  1178. uTimerStart(&learnTick, 5000);
  1179. }
  1180. void uiShowLearnTaskDetailResponse(void){
  1181. unsigned char *pcm;
  1182. unsigned int plen;
  1183. unsigned short key=getKeyValue();
  1184. if(MKEY_VALUE_ESC==key){
  1185. switch(learnRecordStatus){
  1186. case LEARN_REC_IDLE://未录音,退出
  1187. learnRecordBufRelease();
  1188. learnDetailBackToTaskListMenu();
  1189. break;
  1190. case LEARN_REC_DONE://刚录音完成,选择放弃而返回到任务详情
  1191. learnRecordBufRelease();
  1192. learnShowStartScreen();
  1193. newMessageShow(SHOW_HEAD);
  1194. learnRecSetStatus(LEARN_REC_IDLE);
  1195. break;
  1196. case LEARN_REC_PLY://停止正在播放的操作
  1197. learnStopPlaying();
  1198. learnRetrunStatus();
  1199. break;
  1200. case LEARN_UPLOAD_CONFIRM://取消提交
  1201. learnRetrunStatus();
  1202. break;
  1203. }
  1204. }else if(MKEY_VALUE_PTT==key){
  1205. switch(learnRecordStatus){
  1206. case LEARN_REC_IDLE://选择了录音做作业
  1207. if(usr.type != 1){
  1208. MSG_WARN(1, "Only student have rec feature!");
  1209. break;
  1210. }
  1211. if(false==learnStartRecord(charaNum)){
  1212. MSG_ERR(1, "try recoder failed");
  1213. guiShowMessageBox("录音失败");
  1214. nwy_sleep(1500);
  1215. learnRecordBufRelease();
  1216. learnDetailBackToTaskListMenu();
  1217. break;
  1218. }
  1219. #ifdef SAVE_PCM_SD
  1220. pcmFileOpen();
  1221. #endif
  1222. uiMenuShowBottomIndacitor(NULL, "[PTT]停止");
  1223. learnRecSetStatus(LEARN_REC_BUSY);
  1224. break;
  1225. case LEARN_REC_BUSY://正在录音,选择停止
  1226. learnRecordDoneProcess();
  1227. #ifdef SAVE_PCM_SD
  1228. pcmFileClose();
  1229. #endif
  1230. break;
  1231. }
  1232. }else if(MKEY_VALUE_MENU==key){
  1233. switch(learnRecordStatus){
  1234. case LEARN_REC_IDLE://选择回放已提交的录音
  1235. learnPlayAudio(1, tidString);
  1236. learnRecSetStatus(LEARN_REC_PLY);
  1237. uiMenuShowBottomIndacitor(NULL, "停止");
  1238. break;
  1239. case LEARN_REC_DONE://选择播放录音(临时)
  1240. learnPlayAudio(0, NULL);
  1241. learnRecSetStatus(LEARN_REC_PLY);
  1242. uiMenuShowBottomIndacitor(NULL, "停止");
  1243. break;
  1244. case LEARN_UPLOAD_CONFIRM://确定提交
  1245. //暂时不做上传操作,将临时录音保存为.rec文件
  1246. httpPostResetUserTimer(0);
  1247. uiMenuShowBottomIndacitor(NULL, "取消");
  1248. pcm=learnGetTmpAudioBuf();
  1249. plen=learnGetTmpAudioLength();
  1250. learnRecSetStatus(LEARN_RECORD_UPLOAD);
  1251. learnTaskUpdate(LEARN_RECORD_UPLOAD,(void *)pcm, (void *)&plen,true);
  1252. break;
  1253. }
  1254. }else if(MKEY_VALUE_DOWN==key){
  1255. switch(learnRecordStatus){
  1256. case LEARN_REC_IDLE:
  1257. case LEARN_REC_BUSY:
  1258. newMessageShow(SHOW_DOWN);
  1259. break;
  1260. }
  1261. }else if(MKEY_VALUE_UP==key){
  1262. switch(learnRecordStatus){
  1263. case LEARN_REC_IDLE:
  1264. case LEARN_REC_BUSY:
  1265. newMessageShow(SHOW_UP);
  1266. break;
  1267. }
  1268. }else if(MKEY_VALUE_P1==key){
  1269. switch(learnRecordStatus){
  1270. case LEARN_REC_DONE://选择提交录音(临时)
  1271. learnRecSetStatus(LEARN_UPLOAD_CONFIRM);
  1272. guiShowMessageBox("确定提交?");
  1273. uiMenuShowBottomIndacitor("[ok]确定", "取消");
  1274. break;
  1275. }
  1276. }
  1277. }
  1278. static void learnShowResult(void){
  1279. float devscore;
  1280. float speedscore=learnGetRecordRecSpeed();
  1281. float volumescore;
  1282. char info[30];
  1283. unsigned int y=UI_CONTENT_SHOW_Y+5;
  1284. volumescore=usr.scoreVolumeParam*learnGetRecordMaxVolume();
  1285. devscore=speedscore+volumescore;
  1286. guiClearRect(0,UI_CONTENT_SHOW_Y,GLCD_WIDTH-1,GLCD_HEIGHT-16-1,guiGetBackColor());
  1287. snprintf(info, sizeof(info), "设备评分:%.1f", devscore);
  1288. guiShowStr(5, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1289. y+= 18;
  1290. snprintf(info, sizeof(info), "速度评分:%.1f", speedscore);
  1291. guiShowStr(5, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1292. y+= 18;
  1293. snprintf(info, sizeof(info), "音量评分:%.1f", volumescore);
  1294. guiShowStr(5, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1295. y+= 18;
  1296. }
  1297. static void learnRecordDoneProcess(void){
  1298. learnRecordFree();
  1299. learnStopRecord();
  1300. learnRecSetStatus(LEARN_REC_DONE);
  1301. uiMenuShowBottomIndacitor("[OK]播放","[F1]提交");//选择提交还是播放
  1302. learnShowResult();
  1303. }
  1304. //学习排名菜单
  1305. void uiShowLearnRank(char update){
  1306. static const char *rankMode[]={
  1307. "月排名",
  1308. "周排名",
  1309. ""
  1310. };
  1311. static const char *learnIcon[]={
  1312. "RANK-M.bmp",
  1313. "RANK-W.bmp",
  1314. ""
  1315. };
  1316. char Freatrue[2]={0,1};
  1317. if(update){
  1318. sdMount();
  1319. guiClearAll(guiGetBackColor());
  1320. guiShowCaption(0,"学习排名",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  1321. uiMenuShowBottomLine();
  1322. ListBoxInit(&sutListBox, rankMode, 0, learnIcon, Freatrue);
  1323. uiMenuShowBottomIndacitor("查看", "返回");
  1324. }
  1325. }
  1326. void LearnRankSelResponse(void){
  1327. unsigned short key=ListBoxResponse(&sutListBox);
  1328. if(MKEY_VALUE_ESC==key){
  1329. sdUnmount();
  1330. uISetNextStatus(UIS_MENU_MAIN);
  1331. }else if(MKEY_VALUE_MENU==key){
  1332. if(sutListBox.handle==0){
  1333. rankType=0;
  1334. uiPushStack(sutListBox.handle);
  1335. uISetNextStatus(UIS_MENU_RANK_DETAIL);
  1336. }else if(sutListBox.handle==1){
  1337. rankType=1;
  1338. uiPushStack(sutListBox.handle);
  1339. uISetNextStatus(UIS_MENU_RANK_DETAIL);
  1340. }
  1341. }
  1342. }
  1343. typedef struct{
  1344. char type;//表示是month还是week
  1345. int typeIndex;//type的值
  1346. int avg;
  1347. int score;
  1348. int ranking;
  1349. int num;
  1350. }RANK_DEF;
  1351. typedef struct{
  1352. unsigned int item;//rankResult实际使用的个数
  1353. unsigned int num;//rankResult分配的个数(释放时使用)
  1354. RANK_DEF **rankResult;
  1355. }RANK_RESULT_DEF;
  1356. RANK_RESULT_DEF ranks={0,0,0,NULL};
  1357. bool rankShowReady;
  1358. static void rankInfoDetailShow(unsigned char direction){
  1359. static int tIndex=0;
  1360. if(ranks.item==0){
  1361. guiShowMessageBox("无数据");
  1362. return;
  1363. }else if(ranks.item==1) tIndex=0;
  1364. else{
  1365. if(direction==0) tIndex=0;
  1366. else if(direction==1){
  1367. if(++tIndex>=ranks.item) tIndex=0;
  1368. }else if(direction==2){
  1369. if(tIndex==0) tIndex=ranks.item-1;
  1370. else tIndex--;
  1371. }
  1372. }
  1373. guiClearRect(0,UI_CONTENT_SHOW_Y,GLCD_WIDTH-1,UI_BOTTOM_SHOW_Y-2,guiGetBackColor());
  1374. //显示
  1375. if(ranks.rankResult==NULL){
  1376. MSG_WARN(1, "rank no data");
  1377. return;
  1378. }
  1379. char info[50];
  1380. int x=1,y=UI_CONTENT_SHOW_Y;
  1381. //显示排名类型
  1382. if(0==ranks.rankResult[tIndex]->type) snprintf(info, sizeof(info), "month:%d", ranks.rankResult[tIndex]->typeIndex);
  1383. else snprintf(info, sizeof(info), "week:%d", ranks.rankResult[tIndex]->typeIndex);
  1384. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1385. y+=17;
  1386. //显示avg
  1387. snprintf(info, sizeof(info), "avg:%d", ranks.rankResult[tIndex]->avg);
  1388. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1389. y+=17;
  1390. //显示score
  1391. snprintf(info, sizeof(info), "score:%d", ranks.rankResult[tIndex]->score);
  1392. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1393. y+=17;
  1394. //显示ranking
  1395. snprintf(info, sizeof(info), "ranking:%d", ranks.rankResult[tIndex]->ranking);
  1396. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1397. y+=17;
  1398. //显示num
  1399. snprintf(info, sizeof(info), "num:%d", ranks.rankResult[tIndex]->num);
  1400. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1401. }
  1402. void uiShowLearnRankDetail(char update){
  1403. int ret;
  1404. static bool needGetUinfo=false;
  1405. if(update){
  1406. guiClearAll(guiGetBackColor());
  1407. if(rankType==0)
  1408. guiShowCaption(0,"月排名",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  1409. else
  1410. guiShowCaption(0,"周排名",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  1411. uiMenuShowBottomLine();
  1412. uiMenuShowBottomIndacitor(NULL, "返回");
  1413. if(sutApp.pocPpp==0){
  1414. guiShowMessageBox("无网络");
  1415. nwy_sleep(1500);
  1416. uISetNextStatus(UIS_MENU_RANK_SEL);
  1417. return;
  1418. }
  1419. rankShowReady=false;
  1420. showInfoBox("请稍候",3000);
  1421. if(isUserInfoReady()==false){//如果未获取用户信息,先获取用户信息
  1422. needGetUinfo=true;
  1423. learnUpdateStatus(LEARN_DB_WAIT_INFO);
  1424. learnTaskUpdate(LEARN_DB_GET_INFO,NULL, NULL,true);
  1425. }else{//否则可以直接获取排名
  1426. needGetUinfo=false;
  1427. learnUpdateStatus(LEARN_RANK_GET_DETAIL);
  1428. learnTaskUpdate(LEARN_RANK_GET_DETAIL,(void*)&rankType, NULL,true);
  1429. MSG_ERR(1, "update rank detail %s", (rankType==0)?"month":"week");
  1430. }
  1431. }
  1432. if(false==rankShowReady){
  1433. ret=getHttpLearnStatus();
  1434. if(uTimerExpired(&learnTick) || ret!=1){
  1435. MSG_INFO(1, "WAIT %s RESULT:%d",(rankType==0)?"MONTH":"WEEK",ret);
  1436. switch(ret){
  1437. case 0://功获取到了信息
  1438. if(needGetUinfo==true){
  1439. needGetUinfo=false;
  1440. learnUpdateStatus(LEARN_RANK_GET_DETAIL);
  1441. learnTaskUpdate(LEARN_RANK_GET_DETAIL,(void*)&rankType, NULL,true);
  1442. MSG_ERR(1, "update rank detail %s", (rankType==0)?"month":"week");
  1443. }else{
  1444. rankShowReady=true;
  1445. getRankShowMessage(rankType);
  1446. rankInfoDetailShow(0);
  1447. }
  1448. return;
  1449. case 2://获取失败
  1450. guiShowMessageBox("获取失败");
  1451. break;
  1452. default://获取超时
  1453. hearnHttpOverTime();
  1454. guiShowMessageBox("获取超时");
  1455. break;
  1456. }
  1457. nwy_sleep(1500);
  1458. getStackStruct()->ok_back=1;
  1459. uISetNextStatus(UIS_MENU_RANK_SEL);
  1460. }
  1461. }
  1462. }
  1463. static void learnRankReleaseDB(void);
  1464. void LearnRankDetailSelResponse(void){
  1465. if(rankShowReady==true){
  1466. switch(getKeyValue()){
  1467. case MKEY_VALUE_ESC:
  1468. learnRankReleaseDB();
  1469. uISetNextStatus(UIS_MENU_RANK_SEL);
  1470. break;
  1471. case MKEY_VALUE_DOWN:rankInfoDetailShow(1);break;
  1472. case MKEY_VALUE_UP:rankInfoDetailShow(2);break;
  1473. }
  1474. }
  1475. }
  1476. typedef struct{
  1477. int week;
  1478. char time[15];
  1479. char subject[30*2];
  1480. }CLASS_DEF;
  1481. typedef struct{
  1482. unsigned int item;//rankResult实际使用的个数
  1483. unsigned int num;//rankResult分配的个数(释放时使用)
  1484. CLASS_DEF **classResult;
  1485. }CLASS_RESULT_DEF;
  1486. CLASS_RESULT_DEF classes={0,0,0,NULL};
  1487. bool classShowReady;
  1488. static void classInfoDetailShow(unsigned char direction){
  1489. static int tIndex=0;
  1490. if(classes.item==0){
  1491. guiShowMessageBox("无数据");
  1492. return;
  1493. }else if(classes.item==1) tIndex=0;
  1494. else{
  1495. if(direction==0) tIndex=0;
  1496. else if(direction==1){
  1497. if(++tIndex>=classes.item) tIndex=0;
  1498. }else if(direction==2){
  1499. if(tIndex==0) tIndex=classes.item-1;
  1500. else tIndex--;
  1501. }
  1502. }
  1503. guiClearRect(0,UI_CONTENT_SHOW_Y,GLCD_WIDTH-1,UI_BOTTOM_SHOW_Y-2,guiGetBackColor());
  1504. //显示
  1505. if(classes.classResult==NULL){
  1506. MSG_WARN(1, "class no data");
  1507. return;
  1508. }
  1509. #if 0
  1510. char info[50];
  1511. int x=1,y=UI_CONTENT_SHOW_Y;
  1512. //显示week
  1513. snprintf(info, sizeof(info), "week:%d", classes.classResult[tIndex]->week);
  1514. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1515. y+=17;
  1516. //显示time
  1517. snprintf(info, sizeof(info), "time:%s", classes.classResult[tIndex]->time);
  1518. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1519. y+=17;
  1520. //显示subject
  1521. snprintf(info, sizeof(info), "subject:%s", classes.classResult[tIndex]->subject);
  1522. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1523. #else
  1524. char info1[50],info2[100];
  1525. //显示索引和总数
  1526. learnUIShowIndexAndTotal(tIndex+1,classes.item);
  1527. //显示内容
  1528. snprintf(info1, sizeof(info1), "week:%d%ctime:%s", classes.classResult[tIndex]->week,0x0a,classes.classResult[tIndex]->time);
  1529. snprintf(info2, sizeof(info2), "subject:%s", classes.classResult[tIndex]->subject);
  1530. newMessageInit(FONT_MODE_16X16,info1, info2, NULL);
  1531. #endif
  1532. }
  1533. static void getClassShowMessage(void);
  1534. /*课程表*/
  1535. void uiShowLearnClassDetail(char update){
  1536. int ret;
  1537. static bool needGetUinfo=false;
  1538. if(update){
  1539. guiClearAll(guiGetBackColor());
  1540. guiShowCaption(30,"课程表",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  1541. uiMenuShowBottomLine();
  1542. uiMenuShowBottomIndacitor(NULL, "返回");
  1543. if(sutApp.pocPpp==0){
  1544. guiShowMessageBox("无网络");
  1545. nwy_sleep(1500);
  1546. uISetNextStatus(UIS_MENU_LOCATION_SEL);
  1547. return;
  1548. }
  1549. classShowReady=false;
  1550. showInfoBox("请稍候",3000);
  1551. sdMount();
  1552. if(isUserInfoReady()==false){//如果未获取用户信息,先获取用户信息
  1553. needGetUinfo=true;
  1554. learnUpdateStatus(LEARN_DB_WAIT_INFO);
  1555. learnTaskUpdate(LEARN_DB_GET_INFO,NULL, NULL,true);
  1556. }else{//否则可以直接获取课程表
  1557. needGetUinfo=false;
  1558. learnUpdateStatus(LEARN_CLASS_GET_DETAIL);
  1559. learnTaskUpdate(LEARN_CLASS_GET_DETAIL,NULL, NULL,true);
  1560. MSG_ERR(1, "update class detail");
  1561. }
  1562. }
  1563. if(false==classShowReady){
  1564. ret=getHttpLearnStatus();
  1565. if(uTimerExpired(&learnTick) || ret!=1){
  1566. MSG_INFO(1, "WAIT CLASS:%d",ret);
  1567. switch(ret){
  1568. case 0://功获取到了信息
  1569. if(needGetUinfo==true){
  1570. needGetUinfo=false;
  1571. learnUpdateStatus(LEARN_CLASS_GET_DETAIL);
  1572. learnTaskUpdate(LEARN_CLASS_GET_DETAIL,NULL, NULL,true);
  1573. MSG_ERR(1, "update class detail");
  1574. }else{//处理
  1575. MSG_INFO(1, "class info get ok");
  1576. classShowReady=true;
  1577. getClassShowMessage();
  1578. classInfoDetailShow(0);
  1579. }
  1580. return;
  1581. case 2://获取失败
  1582. guiShowMessageBox("获取失败");
  1583. break;
  1584. default://获取超时
  1585. hearnHttpOverTime();
  1586. guiShowMessageBox("获取超时");
  1587. break;
  1588. }
  1589. nwy_sleep(1500);
  1590. getStackStruct()->ok_back=1;
  1591. uISetNextStatus(UIS_MENU_LOCATION_SEL);
  1592. }
  1593. }
  1594. }
  1595. static void learnClassReleaseDB(void);
  1596. void LearnClassDetailResponse(void){
  1597. if(classShowReady==true){
  1598. switch(getKeyValue()){
  1599. case MKEY_VALUE_ESC:
  1600. learnClassReleaseDB();
  1601. uISetNextStatus(UIS_MENU_LOCATION_SEL);
  1602. break;
  1603. case MKEY_VALUE_DOWN:classInfoDetailShow(1);break;
  1604. case MKEY_VALUE_UP:classInfoDetailShow(2);break;
  1605. }
  1606. }
  1607. }
  1608. typedef struct{
  1609. int id;
  1610. char time[15];
  1611. char title[30];
  1612. }NOTICE_DEF;
  1613. typedef struct{
  1614. unsigned int item;//rankResult实际使用的个数
  1615. unsigned int num;//rankResult分配的个数(释放时使用)
  1616. NOTICE_DEF **noticeResult;
  1617. }NOTICE_RESULT_DEF;
  1618. NOTICE_RESULT_DEF notices={0,0,0,NULL};
  1619. bool noticeShowReady;
  1620. static int noticetIndex=0;
  1621. static void noticeInfoDetailShow(unsigned char direction){
  1622. if(notices.item==0){
  1623. guiShowMessageBox("无数据");
  1624. return;
  1625. }else if(notices.item==1) noticetIndex=0;
  1626. else{
  1627. if(direction==0) noticetIndex=0;
  1628. else if(direction==1){
  1629. if(++noticetIndex>=notices.item) noticetIndex=0;
  1630. }else if(direction==2){
  1631. if(noticetIndex==0) noticetIndex=notices.item-1;
  1632. else noticetIndex--;
  1633. }
  1634. }
  1635. guiClearRect(0,UI_CONTENT_SHOW_Y,GLCD_WIDTH-1,UI_BOTTOM_SHOW_Y-2,guiGetBackColor());
  1636. //显示
  1637. if(notices.noticeResult==NULL){
  1638. MSG_WARN(1, "notice no data");
  1639. return;
  1640. }
  1641. #if 0
  1642. char info[50];
  1643. int x=1,y=UI_CONTENT_SHOW_Y;
  1644. //显示title
  1645. snprintf(info, sizeof(info), "title:%s", notices.noticeResult[noticetIndex]->title);
  1646. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1647. y+=17;
  1648. //显示time
  1649. snprintf(info, sizeof(info), "time:%s", notices.noticeResult[noticetIndex]->time);
  1650. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1651. #else
  1652. learnUIShowIndexAndTotal(noticetIndex+1,notices.item);
  1653. newMessageInit(FONT_MODE_16X16, notices.noticeResult[noticetIndex]->time,notices.noticeResult[noticetIndex]->title, NULL);
  1654. #endif
  1655. }
  1656. static void getNoticeShowMessage(void);
  1657. /*消息通知*/
  1658. void uiShowLearnNoticeDetail(char update){
  1659. int ret;
  1660. static bool needGetUinfo=false;
  1661. if(update){
  1662. guiClearAll(guiGetBackColor());
  1663. guiShowCaption(30,"消息通知",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  1664. uiMenuShowBottomLine();
  1665. uiMenuShowBottomIndacitor("阅读", "返回");
  1666. if(sutApp.pocPpp==0){
  1667. guiShowMessageBox("无网络");
  1668. nwy_sleep(1500);
  1669. uISetNextStatus(UIS_MENU_LOCATION_SEL);
  1670. return;
  1671. }
  1672. if(noticeShowReady==false){
  1673. showInfoBox("请稍候",3000);
  1674. sdMount();
  1675. if(isUserInfoReady()==false){//如果未获取用户信息,先获取用户信息
  1676. needGetUinfo=true;
  1677. learnUpdateStatus(LEARN_DB_WAIT_INFO);
  1678. learnTaskUpdate(LEARN_DB_GET_INFO,NULL, NULL,true);
  1679. }else{//否则可以直接获取课程表
  1680. needGetUinfo=false;
  1681. learnUpdateStatus(LEARN_NOTICE_GET_DETAIL);
  1682. learnTaskUpdate(LEARN_NOTICE_GET_DETAIL,NULL, NULL,true);
  1683. MSG_ERR(1, "update notice detail");
  1684. }
  1685. }
  1686. }
  1687. if(false==noticeShowReady){
  1688. ret=getHttpLearnStatus();
  1689. if(uTimerExpired(&learnTick) || ret!=1){
  1690. MSG_INFO(1, "WAIT NOTICE:%d",ret);
  1691. switch(ret){
  1692. case 0://功获取到了信息
  1693. if(needGetUinfo==true){
  1694. needGetUinfo=false;
  1695. learnUpdateStatus(LEARN_NOTICE_GET_DETAIL);
  1696. learnTaskUpdate(LEARN_NOTICE_GET_DETAIL,NULL, NULL,true);
  1697. MSG_ERR(1, "update notice detail");
  1698. }else{//处理
  1699. MSG_INFO(1, "notice info get ok");
  1700. noticeShowReady=true;
  1701. getNoticeShowMessage();
  1702. noticeInfoDetailShow(0);
  1703. }
  1704. return;
  1705. case 2://获取失败
  1706. guiShowMessageBox("获取失败");
  1707. break;
  1708. default://获取超时
  1709. hearnHttpOverTime();
  1710. guiShowMessageBox("获取超时");
  1711. break;
  1712. }
  1713. nwy_sleep(1500);
  1714. getStackStruct()->ok_back=1;
  1715. uISetNextStatus(UIS_MENU_LOCATION_SEL);
  1716. }
  1717. }
  1718. }
  1719. static void learnNoticeReleaseDB(void);
  1720. unsigned char noticeHandle;
  1721. void LearnNoticeResponse(void){
  1722. if(noticeShowReady==true){
  1723. switch(getKeyValue()){
  1724. case MKEY_VALUE_ESC:
  1725. learnNoticeReleaseDB();
  1726. noticeShowReady=false;
  1727. uISetNextStatus(UIS_MENU_LOCATION_SEL);
  1728. break;
  1729. case MKEY_VALUE_DOWN:noticeInfoDetailShow(1);break;
  1730. case MKEY_VALUE_UP:noticeInfoDetailShow(2);break;
  1731. case MKEY_VALUE_MENU:
  1732. uISetNextStatus(UIS_MENU_LOCATION_NOTICE_DETAIL);
  1733. break;
  1734. }
  1735. }
  1736. }
  1737. bool noticeDetailReady=false;
  1738. /*消息通知详情*/
  1739. void uiShowLearnNoticeDetailShow(char update){
  1740. int ret;
  1741. if(update){
  1742. guiClearAll(guiGetBackColor());
  1743. guiShowCaption(0,notices.noticeResult[noticetIndex]->time,UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  1744. uiMenuShowBottomLine();
  1745. uiMenuShowBottomIndacitor(NULL, "返回");
  1746. if(sutApp.pocPpp==0){
  1747. guiShowMessageBox("无网络");
  1748. nwy_sleep(1500);
  1749. uISetNextStatus(UIS_MENU_LOCATION_SEL);
  1750. return;
  1751. }
  1752. showInfoBox("请稍候",3000);
  1753. sdMount();
  1754. noticeDetailReady=false;
  1755. learnUpdateStatus(LEARN_NOTICE_GET_DETAIL_SHOW);
  1756. learnTaskUpdate(LEARN_NOTICE_GET_DETAIL_SHOW,(void *)&notices.noticeResult[noticetIndex]->id, NULL,true);
  1757. MSG_ERR(1, "update notice detail show");
  1758. }
  1759. if(false==noticeDetailReady){
  1760. ret=getHttpLearnStatus();
  1761. if(uTimerExpired(&learnTick) || ret!=1){
  1762. MSG_INFO(1, "WAIT NOTICE SHOW:%d",ret);
  1763. switch(ret){
  1764. case 0://功获取到了信息
  1765. //处理
  1766. noticeDetailReady=true;
  1767. MSG_INFO(1, "notice info get ok");
  1768. noticeInfoDetailShowContent(0);
  1769. return;
  1770. case 2://获取失败
  1771. guiShowMessageBox("获取失败");
  1772. break;
  1773. default://获取超时
  1774. hearnHttpOverTime();
  1775. guiShowMessageBox("获取超时");
  1776. break;
  1777. }
  1778. nwy_sleep(1500);
  1779. noticeShowReady=false;
  1780. getStackStruct()->ok_back=1;
  1781. uISetNextStatus(UIS_MENU_LOCATION_SEL);
  1782. }
  1783. }
  1784. }
  1785. void LearnNoticeDetailResponse(void){
  1786. if(noticeDetailReady==true){
  1787. switch(getKeyValue()){
  1788. case MKEY_VALUE_ESC:
  1789. noticeShowReady=false;
  1790. uISetNextStatus(UIS_MENU_LOCATION_NOTICE);
  1791. break;
  1792. case MKEY_VALUE_DOWN:noticeInfoDetailShowContent(1);break;
  1793. case MKEY_VALUE_UP:noticeInfoDetailShowContent(2);break;
  1794. }
  1795. }
  1796. }
  1797. #define OBJECT_AVG_NAME "avg"
  1798. #define OBJECT_SCORE_NAME "score"
  1799. #define OBJECT_RANKING_NAME "ranking"
  1800. #define OBJECT_NUM_NAME "num"
  1801. /*
  1802. msg:json buffer 数据,长度为len
  1803. {
  1804. "gid":555,
  1805. "uid":12345,
  1806. "list":[
  1807. {"week":29,"avg":518,"score":562,"ranking":5,"num":35},
  1808. {"week":28,"avg":518,"score":581,"ranking":8,"num":28},
  1809. {"week":27,"avg":518,"score":558,"ranking":10,"num":40},
  1810. {"week":26,"avg":518,"score":592,"ranking":2,"num":37},
  1811. ]
  1812. }
  1813. {
  1814. "gid":555,
  1815. "uid":12345,
  1816. "list":[
  1817. {"month":8,"avg":518,"score":562,"ranking":5,"num":35},
  1818. {"month":7,"avg":518,"score":581,"ranking":8,"num":28},
  1819. {"month":6,"avg":518,"score":558,"ranking":10,"num":40},
  1820. {"month":5,"avg":518,"score":592,"ranking":2,"num":37},
  1821. ]
  1822. }
  1823. 这里将list提取出到buffer并输出
  1824. */
  1825. static char *rankBuffer=NULL;
  1826. cJSON* rankJson=NULL;
  1827. static void learnRankReleaseDB(void){
  1828. int i;
  1829. releaseBuf(&rankBuffer);
  1830. if(NULL!=ranks.rankResult){
  1831. for(i=0;i<ranks.num;i++){
  1832. if(ranks.rankResult[i]!=NULL) releaseBuf(&ranks.rankResult[i]);
  1833. }
  1834. releaseBuf(&ranks.rankResult);
  1835. }
  1836. releaseJson(&rankJson);
  1837. }
  1838. static void getRankShowMessage(unsigned char ranktype){
  1839. //从LEARN_RANK文件中读取内容出来解析
  1840. RANK_DEF **rankPtr=NULL;
  1841. int fd=-1,i=0;
  1842. char info[50];
  1843. ranks.item=0;
  1844. snprintf(info, sizeof(info), "%s/%s", SD_TOKEN,LEARN_RANK);
  1845. fd=nwy_sdk_fopen(info, NWY_RDONLY);
  1846. if(fd<0){
  1847. MSG_WARN(1, "Rank file open failed");
  1848. goto R_EXIT;
  1849. }
  1850. long size=nwy_sdk_fsize_fd(fd);
  1851. if(size<=0){
  1852. MSG_WARN(1, "Rank file data error");
  1853. goto R_EXIT;
  1854. }
  1855. releaseBuf(&rankBuffer);
  1856. rankBuffer=(char *)createBuf(size);
  1857. //rankBuffer=(char *)malloc(size);
  1858. if(NULL == rankBuffer){
  1859. MSG_ERR(1, "rank buffer malloc error");
  1860. goto R_EXIT;
  1861. }
  1862. //MSG_INFO(1, "[mem malloc]%x", rankBuffer);
  1863. memset(rankBuffer, 0, size);
  1864. nwy_sdk_fread(fd, rankBuffer, size);
  1865. nwy_sdk_fclose(fd);
  1866. fd=-1;
  1867. //解析json
  1868. MSG_INFO(1, "Rank data analyse len:%d, type:%d",size, ranktype);
  1869. if(NULL!=ranks.rankResult){
  1870. for(i=0;i<ranks.num;i++){
  1871. if(ranks.rankResult[i]!=NULL) releaseBuf(&ranks.rankResult[i]);
  1872. }
  1873. releaseBuf(&ranks.rankResult);
  1874. }
  1875. ranks.num=0;
  1876. if(0==ranktype) snprintf(info, sizeof(info), "month");
  1877. else snprintf(info, sizeof(info), "week");
  1878. rankJson = createJson(rankBuffer);
  1879. //rankJson = cJSON_Parse(rankBuffer);
  1880. if(NULL==rankJson){
  1881. MSG_WARN(1, "rank json null");
  1882. goto R_EXIT;
  1883. }
  1884. //MSG_INFO(1, "rank json:%x", rankJson);
  1885. cJSON* item = cJSON_GetObjectItem(rankJson, OBJECT_LIST);
  1886. if(NULL==item){
  1887. MSG_ERR(1, "rank json failed");
  1888. goto R_EXIT;
  1889. }
  1890. int num=cJSON_GetArraySize(item);
  1891. if(num<=0){
  1892. MSG_WARN(1, "rank array num:%d",num);
  1893. goto R_EXIT;
  1894. }
  1895. MSG_INFO(1, "rank num:%d",num);
  1896. rankPtr=(RANK_DEF **)createBuf(sizeof(RANK_DEF *)*num);
  1897. //rankPtr=(RANK_DEF **)malloc(sizeof(RANK_DEF *)*num);
  1898. if(NULL==rankPtr){
  1899. MSG_ERR(1,"rank malloc failed");
  1900. goto R_EXIT;
  1901. }
  1902. //MSG_INFO(1, "[mem malloc]%x", rankPtr);
  1903. ranks.num=num;
  1904. for(i=0;i<num;i++) rankPtr[i]=NULL;
  1905. for(i=0;i<num;i++){
  1906. rankPtr[i]=(RANK_DEF *)createBuf(sizeof(RANK_DEF));
  1907. //rankPtr[i]=(RANK_DEF *)malloc(sizeof(RANK_DEF));
  1908. if(NULL==rankPtr[i]){
  1909. MSG_ERR(1, "rank[%d] malloc failed", i);
  1910. goto R_EXIT;
  1911. }
  1912. //MSG_INFO(1, "[mem malloc %d]%x", i, rankPtr[i]);
  1913. }
  1914. cJSON* element,*t;
  1915. i=0;
  1916. for(;;){
  1917. element=cJSON_GetArrayItem(item,i);
  1918. if(NULL==element) break;
  1919. //month/week
  1920. rankPtr[i]->type=ranktype;
  1921. t=cJSON_GetObjectItem(element, info);
  1922. if(NULL==t) rankPtr[i]->typeIndex=-1;
  1923. else rankPtr[i]->typeIndex=atoi(t->valuestring);
  1924. //avg
  1925. t=cJSON_GetObjectItem(element, OBJECT_AVG_NAME);
  1926. if(NULL==t) rankPtr[i]->avg=-1;
  1927. else rankPtr[i]->avg=atoi(t->valuestring);
  1928. //score
  1929. t=cJSON_GetObjectItem(element, OBJECT_SCORE_NAME);
  1930. if(NULL==t) rankPtr[i]->score=-1;
  1931. else rankPtr[i]->score=atoi(t->valuestring);
  1932. //ranking
  1933. t=cJSON_GetObjectItem(element, OBJECT_RANKING_NAME);
  1934. if(NULL==t) rankPtr[i]->ranking=-1;
  1935. else rankPtr[i]->ranking=atoi(t->valuestring);
  1936. //num
  1937. t=cJSON_GetObjectItem(element, OBJECT_NUM_NAME);
  1938. if(NULL==t) rankPtr[i]->num=-1;
  1939. else rankPtr[i]->num=atoi(t->valuestring);
  1940. i++;
  1941. }
  1942. ranks.item=i;
  1943. ranks.rankResult=rankPtr;
  1944. MSG_INFO(1, "rank valid num:%d",i);
  1945. return;
  1946. R_EXIT:
  1947. releaseJson(&rankJson);
  1948. releaseBuf(&rankBuffer);
  1949. if(fd>0) nwy_sdk_fclose(fd);
  1950. if(rankPtr != NULL){
  1951. for(i=0;i<num;i++) releaseBuf(&rankPtr[i]);
  1952. releaseBuf(&rankPtr);
  1953. }
  1954. }
  1955. #define OBJECT_WEEK_NAME "week"
  1956. #define OBJECT_TIME_NAME "time"
  1957. #define OBJECT_SUBJECT_NAME "subject"
  1958. static char *classBuffer=NULL;
  1959. cJSON* classJson=NULL;
  1960. static void learnClassReleaseDB(void){
  1961. int i;
  1962. releaseBuf(&classBuffer);
  1963. if(NULL!=classes.classResult){
  1964. for(i=0;i<classes.num;i++){
  1965. if(classes.classResult[i]!=NULL) releaseBuf(&classes.classResult[i]);
  1966. }
  1967. releaseBuf(&classes.classResult);
  1968. }
  1969. releaseJson(&classJson);
  1970. }
  1971. static void getClassShowMessage(void){
  1972. //从LEARN_CLASS文件中读取内容出来解析
  1973. CLASS_DEF **classPtr=NULL;
  1974. int fd=-1,i=0;
  1975. char info[50];
  1976. ranks.item=0;
  1977. snprintf(info, sizeof(info), "%s/%s", SD_TOKEN,LEARN_CLASS);
  1978. fd=nwy_sdk_fopen(info, NWY_RDONLY);
  1979. if(fd<0){
  1980. MSG_WARN(1, "Class file open failed");
  1981. goto R_EXIT;
  1982. }
  1983. long size=nwy_sdk_fsize_fd(fd);
  1984. if(size<=0){
  1985. MSG_WARN(1, "Class file data error");
  1986. goto R_EXIT;
  1987. }
  1988. releaseBuf(&classBuffer);
  1989. classBuffer=(char *)createBuf(size);
  1990. //classBuffer=(char *)malloc(size);
  1991. if(NULL == classBuffer){
  1992. MSG_ERR(1, "class buffer malloc error");
  1993. goto R_EXIT;
  1994. }
  1995. //MSG_INFO(1, "[mem malloc]%x", classBuffer);
  1996. memset(classBuffer, 0, size);
  1997. nwy_sdk_fread(fd, classBuffer, size);
  1998. nwy_sdk_fclose(fd);
  1999. fd=-1;
  2000. //解析json
  2001. MSG_INFO(1, "Class data analyse len:%d",size);
  2002. if(NULL!=classes.classResult){
  2003. for(i=0;i<classes.num;i++){
  2004. if(classes.classResult[i]!=NULL) releaseBuf(&classes.classResult[i]);
  2005. }
  2006. releaseBuf(&classes.classResult);
  2007. }
  2008. classes.num=0;
  2009. classJson = createJson(classBuffer);
  2010. //classJson = cJSON_Parse(classBuffer);
  2011. if(NULL==classJson){
  2012. MSG_WARN(1, "class json null");
  2013. goto R_EXIT;
  2014. }
  2015. //MSG_INFO(1, "class json:%x", classJson);
  2016. cJSON* item = cJSON_GetObjectItem(classJson, OBJECT_LIST);
  2017. if(NULL==item){
  2018. MSG_ERR(1, "class json failed");
  2019. goto R_EXIT;
  2020. }
  2021. int num=cJSON_GetArraySize(item);
  2022. if(num<=0){
  2023. MSG_WARN(1, "class array num:%d",num);
  2024. goto R_EXIT;
  2025. }
  2026. MSG_INFO(1, "class num:%d",num);
  2027. classPtr=(CLASS_DEF **)createBuf(sizeof(CLASS_DEF *)*num);
  2028. //classPtr=(CLASS_DEF **)malloc(sizeof(CLASS_DEF *)*num);
  2029. if(NULL==classPtr){
  2030. MSG_ERR(1,"class malloc failed");
  2031. goto R_EXIT;
  2032. }
  2033. //MSG_INFO(1, "[mem malloc]%x", classPtr);
  2034. classes.num=num;
  2035. for(i=0;i<num;i++) classPtr[i]=NULL;
  2036. for(i=0;i<num;i++){
  2037. classPtr[i]=(CLASS_DEF *)createBuf(sizeof(CLASS_DEF));
  2038. //classPtr[i]=(CLASS_DEF *)malloc(sizeof(CLASS_DEF));
  2039. if(NULL==classPtr[i]){
  2040. MSG_ERR(1, "class[%d] malloc failed", i);
  2041. goto R_EXIT;
  2042. }
  2043. //MSG_INFO(1, "[mem malloc %d]%x", i, classPtr[i]);
  2044. }
  2045. cJSON* element,*t;
  2046. i=0;
  2047. for(;;){
  2048. element=cJSON_GetArrayItem(item,i);
  2049. if(NULL==element) break;
  2050. //week
  2051. t=cJSON_GetObjectItem(element, OBJECT_WEEK_NAME);
  2052. if(NULL==t) classPtr[i]->week=-1;
  2053. else classPtr[i]->week=atoi(t->valuestring);
  2054. //time
  2055. t=cJSON_GetObjectItem(element, OBJECT_TIME_NAME);
  2056. if(NULL==t) classPtr[i]->time[0]='\0';
  2057. else snprintf(classPtr[i]->time, sizeof(classPtr[i]->time), "%s", t->valuestring);
  2058. //subject
  2059. t=cJSON_GetObjectItem(element, OBJECT_SUBJECT_NAME);
  2060. if(NULL==t) classPtr[i]->subject[0]='\0';
  2061. else snprintf(classPtr[i]->subject, sizeof(classPtr[i]->subject), "%s", t->valuestring);
  2062. i++;
  2063. }
  2064. classes.item=i;
  2065. classes.classResult=classPtr;
  2066. MSG_INFO(1, "class valid num:%d",i);
  2067. return;
  2068. R_EXIT:
  2069. releaseJson(&classJson);
  2070. releaseBuf(&classBuffer);
  2071. if(fd>0) nwy_sdk_fclose(fd);
  2072. if(classPtr != NULL){
  2073. for(i=0;i<num;i++) releaseBuf(&classPtr[i]);
  2074. releaseBuf(&classPtr);
  2075. }
  2076. }
  2077. #define OBJECT_ID_NAME "id"
  2078. #define OBJECT_TITLE_NAME "title"
  2079. static char *noticeBuffer=NULL;
  2080. cJSON* noticeJson=NULL;
  2081. static void learnNoticeReleaseDB(void){
  2082. int i;
  2083. releaseBuf(&noticeBuffer);
  2084. if(NULL!=notices.noticeResult){
  2085. for(i=0;i<notices.num;i++){
  2086. if(notices.noticeResult[i]!=NULL) releaseBuf(&notices.noticeResult[i]);
  2087. }
  2088. releaseBuf(&notices.noticeResult);
  2089. }
  2090. releaseJson(&noticeJson);
  2091. }
  2092. static void getNoticeShowMessage(void){
  2093. //从LEARN_CLASS文件中读取内容出来解析
  2094. NOTICE_DEF **noticePtr=NULL;
  2095. int fd=-1,i=0;
  2096. char info[50];
  2097. notices.item=0;
  2098. snprintf(info, sizeof(info), "%s/%s", SD_TOKEN,LEARN_NOTICE);
  2099. fd=nwy_sdk_fopen(info, NWY_RDONLY);
  2100. if(fd<0){
  2101. MSG_WARN(1, "Notice file open failed");
  2102. goto R_EXIT;
  2103. }
  2104. long size=nwy_sdk_fsize_fd(fd);
  2105. if(size<=0){
  2106. MSG_WARN(1, "Notice file data error");
  2107. goto R_EXIT;
  2108. }
  2109. releaseBuf(&noticeBuffer);
  2110. noticeBuffer=(char *)createBuf(size);
  2111. //noticeBuffer=(char *)malloc(size);
  2112. if(NULL == noticeBuffer){
  2113. MSG_ERR(1, "notice buffer malloc error");
  2114. goto R_EXIT;
  2115. }
  2116. //MSG_INFO(1, "[mem malloc]%x", noticeBuffer);
  2117. memset(noticeBuffer, 0, size);
  2118. nwy_sdk_fread(fd, noticeBuffer, size);
  2119. nwy_sdk_fclose(fd);
  2120. fd=-1;
  2121. //解析json
  2122. MSG_INFO(1, "Notice data analyse len:%d",size);
  2123. if(NULL!=notices.noticeResult){
  2124. for(i=0;i<notices.num;i++){
  2125. if(notices.noticeResult[i]!=NULL) releaseBuf(&notices.noticeResult[i]);
  2126. }
  2127. releaseBuf(&notices.noticeResult);
  2128. }
  2129. notices.num=0;
  2130. noticeJson = createJson(noticeBuffer);
  2131. //noticeJson = cJSON_Parse(noticeBuffer);
  2132. if(NULL==noticeJson){
  2133. MSG_WARN(1, "notice json null");
  2134. goto R_EXIT;
  2135. }
  2136. //MSG_INFO(1, "notice json:%x", noticeJson);
  2137. cJSON* item = cJSON_GetObjectItem(noticeJson, OBJECT_LIST);
  2138. if(NULL==item){
  2139. MSG_ERR(1, "notice json failed");
  2140. goto R_EXIT;
  2141. }
  2142. int num=cJSON_GetArraySize(item);
  2143. if(num<=0){
  2144. MSG_WARN(1, "notice array num:%d",num);
  2145. goto R_EXIT;
  2146. }
  2147. MSG_INFO(1, "notice num:%d",num);
  2148. noticePtr=(NOTICE_DEF **)createBuf(sizeof(NOTICE_DEF *)*num);
  2149. //noticePtr=(NOTICE_DEF **)malloc(sizeof(NOTICE_DEF *)*num);
  2150. if(NULL==noticePtr){
  2151. MSG_ERR(1,"notice malloc failed");
  2152. goto R_EXIT;
  2153. }
  2154. //MSG_INFO(1, "[mem malloc]%x", noticePtr);
  2155. notices.num=num;
  2156. for(i=0;i<num;i++) noticePtr[i]=NULL;
  2157. for(i=0;i<num;i++){
  2158. noticePtr[i]=(NOTICE_DEF *)createBuf(sizeof(NOTICE_DEF));
  2159. //noticePtr[i]=(CLASS_DEF *)malloc(sizeof(CLASS_DEF));
  2160. if(NULL==noticePtr[i]){
  2161. MSG_ERR(1, "notice[%d] malloc failed", i);
  2162. goto R_EXIT;
  2163. }
  2164. //MSG_INFO(1, "[mem malloc %d]%x", i, noticePtr[i]);
  2165. }
  2166. cJSON* element,*t;
  2167. i=0;
  2168. for(;;){
  2169. element=cJSON_GetArrayItem(item,i);
  2170. if(NULL==element) break;
  2171. //id
  2172. t=cJSON_GetObjectItem(element, OBJECT_ID_NAME);
  2173. if(NULL==t) noticePtr[i]->id=-1;
  2174. else noticePtr[i]->id=atoi(t->valuestring);
  2175. //time
  2176. t=cJSON_GetObjectItem(element, OBJECT_TIME_NAME);
  2177. if(NULL==t) noticePtr[i]->time[0]='\0';
  2178. else snprintf(noticePtr[i]->time, sizeof(noticePtr[i]->time), "%s", t->valuestring);
  2179. //title
  2180. t=cJSON_GetObjectItem(element, OBJECT_TITLE_NAME);
  2181. if(NULL==t) noticePtr[i]->title[0]='\0';
  2182. else snprintf(noticePtr[i]->title, sizeof(noticePtr[i]->title), "%s", t->valuestring);
  2183. i++;
  2184. }
  2185. notices.item=i;
  2186. notices.noticeResult=noticePtr;
  2187. MSG_INFO(1, "notice valid num:%d",i);
  2188. return;
  2189. R_EXIT:
  2190. releaseJson(&noticeJson);
  2191. releaseBuf(&noticeBuffer);
  2192. if(fd>0) nwy_sdk_fclose(fd);
  2193. if(noticePtr != NULL){
  2194. for(i=0;i<num;i++) releaseBuf(&noticePtr[i]);
  2195. releaseBuf(&noticePtr);
  2196. }
  2197. }
  2198. char *noticeDetailContent=NULL;
  2199. //char *testShowData="这是一条测试信息,长度为100,然后呢就一直显示吧,我也不知道能显示多少,所以只能临时搞一段文字来显示看看效果如何了,然后再给100000000000元作者吧,作为酬劳,我也不知道是否足够呢。";
  2200. void noticeInfoDetailShowContent(char direction){
  2201. #if 0
  2202. int x=1,y=UI_CONTENT_SHOW_Y;
  2203. char *ptr=noticeDetailContent;
  2204. static int showlen,showIndex,len;
  2205. if(direction==0){
  2206. showlen=strlen(ptr);
  2207. showIndex=0;
  2208. }
  2209. guiClearRect(0,UI_CONTENT_SHOW_Y,GLCD_WIDTH-1,UI_BOTTOM_SHOW_Y-2,guiGetBackColor());
  2210. MessageEditInit(&sutMEdit, x, y+2 , ptr, FONT_MODE_16X16);
  2211. len=MessageEditShow(&sutMEdit,ptr+showIndex);
  2212. if(len !=0) showIndex+=len;
  2213. else showIndex=0;
  2214. #else
  2215. if(direction==0) newMessageInit(FONT_MODE_16X16, noticeDetailContent, NULL, NULL);
  2216. else if(direction==1) newMessageShow(SHOW_DOWN);
  2217. else if(direction==2) newMessageShow(SHOW_UP);
  2218. #endif
  2219. }
  2220. void cJSONTakeContent(char *msg, int len){
  2221. //msg={"id":"7","content":"通知测试2"}
  2222. //提取"content"内容项到noticeInfoDetailShowContent中
  2223. releaseBuf(&noticeDetailContent);
  2224. cJSON* nJson=NULL;
  2225. nJson = createJson(msg);
  2226. if(NULL==nJson){
  2227. MSG_WARN(1, "notice detail show json null");
  2228. goto R_EXIT;
  2229. }
  2230. cJSON* item = cJSON_GetObjectItem(nJson, OBJECT_CONTENT_DETAIL);
  2231. if(NULL==item){
  2232. MSG_ERR(1, "notice detail show json failed");
  2233. goto R_EXIT;
  2234. }
  2235. int length=strlen(item->valuestring)+1;
  2236. char *ptr=(char *)createBuf(len*sizeof(char));
  2237. if(ptr==NULL){
  2238. MSG_ERR(1, "notice detail show malloc err");
  2239. goto R_EXIT;
  2240. }
  2241. memcpy(ptr, item->valuestring, len);
  2242. ptr[len]=0;
  2243. noticeDetailContent=ptr;
  2244. R_EXIT:
  2245. releaseJson(&nJson);
  2246. }
  2247. void cJSONTakeParam(char *msg, int len){
  2248. //msg={"scoreSpeedParam": "1.0","scoreVolumeParam": "1.0"}
  2249. bool sparamOk=false;
  2250. bool vparamOk=false;
  2251. cJSON* nJson=NULL;
  2252. nJson = createJson(msg);
  2253. if(NULL==nJson){
  2254. MSG_WARN(1, "param detail json null");
  2255. goto R_EXIT;
  2256. }
  2257. cJSON* item = cJSON_GetObjectItem(nJson, OBJECT_SPEED_PARAM);
  2258. if(NULL==item) MSG_ERR(1, "param speed json failed");
  2259. else{
  2260. usr.scoreSpeedParam=atof(item->valuestring);
  2261. sparamOk=true;
  2262. }
  2263. item = cJSON_GetObjectItem(nJson, OBJECT_VOLUME_PARAM);
  2264. if(NULL==item) MSG_ERR(1, "param volume json failed");
  2265. else{
  2266. usr.scoreVolumeParam=atof(item->valuestring);
  2267. vparamOk=true;
  2268. }
  2269. if(sparamOk==true && vparamOk==true){
  2270. MSG_INFO(1, "sparam:%f, vparam:%f", usr.scoreSpeedParam, usr.scoreVolumeParam);
  2271. usr.paramReady=true;
  2272. }
  2273. R_EXIT:
  2274. releaseJson(&nJson);
  2275. }
  2276. //没获取账户信息,则定时尝试获取
  2277. void LearnUsrInfoCheck(unsigned int interval){
  2278. static char step=0;
  2279. static unsigned int waitTimer;
  2280. unsigned char ret;
  2281. if(sutPocStatus.logined==0) return;
  2282. if(true==isUserInfoReady() && true==usr.paramReady){
  2283. if(step!=0) step=0;
  2284. return;
  2285. }
  2286. switch(step){
  2287. case 0://发起获取账户信息请求
  2288. step=1;
  2289. learnUpdateStatus(LEARN_DB_WAIT_INFO);
  2290. learnTaskUpdate(LEARN_DB_GET_INFO,NULL, NULL,false);
  2291. uTimerStart(&waitTimer, 5000);
  2292. break;
  2293. case 1://等待获取结果
  2294. ret=getHttpLearnStatus();
  2295. if(uTimerExpired(&waitTimer) || ret !=1){
  2296. MSG_INFO(1, "Wait user account:%d", ret);
  2297. if(ret==0) step=2;
  2298. else step=0;//重新获取
  2299. }
  2300. break;
  2301. case 2://发起参数获取请求
  2302. step=3;
  2303. learnUpdateStatus(LEARN_GET_PARAM);
  2304. learnTaskUpdate(LEARN_GET_PARAM,NULL, NULL,false);
  2305. uTimerStart(&waitTimer, 5000);
  2306. break;
  2307. case 3://等待获取结果
  2308. ret=getHttpLearnStatus();
  2309. if(uTimerExpired(&waitTimer) || ret !=1){
  2310. MSG_INFO(1, "Wait param:%d", ret);
  2311. if(ret==0) step=4;//停止
  2312. else step=2;//重新获取
  2313. }
  2314. break;
  2315. case 4://IDLE
  2316. break;
  2317. }
  2318. }
  2319. void uiShowLearnGroup(char type){
  2320. static unsigned int gid=0xffffffff;
  2321. if(false==isUserInfoReady()) return;
  2322. if(type) goto S_NOW;
  2323. if(gid == getLearnGid()) return;
  2324. gid=getLearnGid();
  2325. S_NOW:
  2326. uiShowLearnGName(getLearnGName());
  2327. }
  2328. static bool sdMountStatus=false;
  2329. void sdUnmount(void){
  2330. sdCardCtl(false);
  2331. sdMountStatus=false;
  2332. }
  2333. void sdMount(void){
  2334. if(sdMountStatus==false){
  2335. sdCardCtl(true);
  2336. sdMountStatus=true;
  2337. }
  2338. }
  2339. bool sdCardTest(void){
  2340. const char *table="sdTest.txt";
  2341. char file[50];
  2342. int fd=NULL;
  2343. bool ret=false;
  2344. sdCardCtl(true);
  2345. snprintf(file, sizeof(file), "%s/%s",SD_TOKEN,table);
  2346. fd = nwy_sdk_fopen(file, NWY_CREAT | NWY_RDWR|NWY_TRUNC);//清0式
  2347. if(fd<0){
  2348. MSG_WARN(1, "fopen to write failed:%s",table);
  2349. goto SD_END;
  2350. }
  2351. nwy_sdk_fwrite(fd, table, strlen(table));
  2352. nwy_sdk_fclose(fd);
  2353. fd = nwy_sdk_fopen(file, NWY_RDONLY);
  2354. if(fd < 0){
  2355. MSG_WARN(1, "fopen to read failed:%s",table);
  2356. goto SD_END;
  2357. }
  2358. memset(file, 0, sizeof(file));
  2359. nwy_sdk_fread(fd, file, strlen(table));
  2360. if(0==memcmp(file, table, strlen(file))) ret=true;
  2361. SD_END:
  2362. if(fd != NULL) nwy_sdk_fclose(fd);
  2363. sdCardCtl(false);
  2364. return ret;
  2365. }