learnTask.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407
  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. int 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) index=num-num%PAGE_SIZE;
  373. else index -= PAGE_SIZE;
  374. }else if(learn->direction==0){
  375. learn->direction=1;
  376. }
  377. //需要先释放上次进来时使用过的资源
  378. for(i=0;i<PAGE_SIZE;i++){
  379. releaseBuf(&learn->bufTask[learn->validNum]);
  380. releaseJson(&learn->jsonTask[learn->validNum]);
  381. }
  382. learn->validNum=0;
  383. for(;;){
  384. element=cJSON_GetArrayItem(item, index);
  385. if(NULL==element){
  386. if(index==num){//读到结尾了
  387. learn->selIndex=index-learn->validNum;
  388. learn->index=0;//重置
  389. goto P_END;
  390. }else return 2;//出错了
  391. }else{
  392. //获取tid
  393. t=cJSON_GetObjectItem(element, OBJECT_TID);
  394. if(NULL==t) goto P_CONTINUE;
  395. if(t->valuestring==NULL) goto P_CONTINUE;//没有找到值
  396. //找到TID,根据TID获取文件内容出来/*{"s":4,"c":"What is your name?","t":"学习任务1","f":1,"tid":5126*/
  397. snprintf(file, sizeof(file), "%s.task", t->valuestring);
  398. tmpBuf=getLearnFileJson(file);
  399. if(NULL==tmpBuf) goto P_CONTINUE;
  400. tmpJson=createJson(tmpBuf);
  401. if(NULL==tmpJson){
  402. releaseBuf(&tmpBuf);
  403. goto P_CONTINUE;
  404. }
  405. //获取OBJECT_FINISH
  406. sele=cJSON_GetObjectItem(tmpJson, OBJECT_FINISH);
  407. if(NULL==sele){//获取finish失败
  408. releaseBuf(&tmpBuf);
  409. releaseJson(&tmpJson);
  410. goto P_CONTINUE;
  411. }
  412. if(sele->valuestring==NULL) goto P_CONTINUE;
  413. i=atoi(sele->valuestring);
  414. if(i==0) learn->finishedFlag[learn->validNum]=0;
  415. else learn->finishedFlag[learn->validNum]=1;
  416. //获取OBJECT_TITLE
  417. sele=cJSON_GetObjectItem(tmpJson, OBJECT_TITLE);
  418. if(NULL==sele){//获取TITLE失败
  419. releaseBuf(&tmpBuf);
  420. releaseJson(&tmpJson);
  421. goto P_CONTINUE;
  422. }
  423. if(sele->valuestring==NULL) goto P_CONTINUE;
  424. //格式化TITLE到显示Buff用于显示(看一下是否要转换编码格式)
  425. snprintf(learn->showTable[learn->validNum], SHOW_TABLE_MAX_BYTES, "%s", sele->valuestring);
  426. learn->jsonTask[learn->validNum]=tmpJson;
  427. learn->bufTask[learn->validNum]=tmpBuf;
  428. if(++learn->validNum>=PAGE_SIZE){
  429. //获取够了,暂时不需要去释放资源,因为如果按确定查看内容时,还需要读取任务详细内容
  430. index++;
  431. learn->selIndex=index-learn->validNum;
  432. if(index>=num) index=0;
  433. learn->index=index;
  434. goto P_END;
  435. }
  436. }
  437. P_CONTINUE:
  438. index++;
  439. }
  440. P_END:
  441. learn->lastDirection=learn->direction;
  442. return 0;
  443. }
  444. static void learnUIShowIndexAndTotal(void){
  445. char info[20];
  446. int i,total,index;
  447. total=sutJson.totalNum;
  448. index=sutJson.selIndex+sutListBox.handle+1;
  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();
  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. guiClearRect(0,(GLCD_HEIGHT/2)-20,GLCD_WIDTH-1,(GLCD_HEIGHT/2)+20,guiGetBackColor());//清除消息框
  775. if(sutJson.validNum==0){
  776. guiShowMessageBox("无数据");
  777. }else{
  778. for(i=0;i<sutJson.validNum;i++) menuTable[i]=&sutJson.showTable[i];
  779. sutJson.showTable[sutJson.validNum][0]=0;
  780. menuTable[sutJson.validNum]=learnIcon[2];
  781. ListBoxInit(&sutListBox, menuTable,0,learnIcon, sutJson.finishedFlag);
  782. learnUIShowIndexAndTotal();
  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. bool showFirstFlag;
  1001. unsigned char haveNextPage;
  1002. unsigned char taskIndex;
  1003. unsigned short showIndex;//内容显示的索引
  1004. char tidString[20];//保存正在查看的任务的tid串
  1005. char *learnGetTid(void){return tidString;}
  1006. /*
  1007. 统计有多少个单词
  1008. 原理:单词间用标点(, .)或空格分开
  1009. */
  1010. static void calWordNum(char *msg){
  1011. int i,len=strlen(msg);
  1012. unsigned char num=0;
  1013. unsigned char lashChara=0;
  1014. MSG_INFO(1, "content:%s",msg);
  1015. for(i=0;i<len;i++){
  1016. if(msg[i]==',' || msg[i]=='.' || msg[i]==' ' || msg[i]=='?' || msg[i]=='!'){
  1017. if(lashChara!=0){//记录为一个单词出现
  1018. num++;
  1019. lashChara=0;
  1020. }
  1021. }else if(lashChara==0) lashChara=msg[i];//有单词出现
  1022. }
  1023. if(lashChara!=0) num++;//没有标点结尾时
  1024. charaNum=num;
  1025. MSG_INFO(1, "Chara Num:%d", num);
  1026. }
  1027. void readTaskAndShow(void){
  1028. char info[50];
  1029. int y=UI_LOGIN_SHOW_Y;
  1030. static cJSON* eleScore ,* eleContent;
  1031. unsigned short len;
  1032. char *ptr;
  1033. guiClearRect(0,y, GLCD_WIDTH-1, UI_BOTTOM_LINE_Y-1, guiGetBackColor());
  1034. if(showFirstFlag==true){
  1035. showFirstFlag=false;
  1036. showIndex=0;
  1037. //读取当前任务的uid保存起来先
  1038. tidString[0]=0;
  1039. eleScore=cJSON_GetObjectItem(sutJson.jsonTask[taskIndex], OBJECT_TID);
  1040. if(NULL != eleScore) snprintf(tidString, sizeof(tidString), "%s", eleScore->valuestring);
  1041. eleScore=cJSON_GetObjectItem(sutJson.jsonTask[taskIndex], OBJECT_SCORE);
  1042. eleContent=cJSON_GetObjectItem(sutJson.jsonTask[taskIndex], OBJECT_CONTENT);
  1043. }
  1044. if(showIndex==0){
  1045. //显示分数
  1046. if(NULL==eleScore){//没有找到分数字段,显示'--'
  1047. snprintf(info, sizeof(info), "分数:--");
  1048. }else if(eleScore->valuestring==NULL){
  1049. snprintf(info, sizeof(info), "分数:--");
  1050. }else snprintf(info, sizeof(info), "分数:%s", eleScore->valuestring);
  1051. guiShowStr(1, y+1, info, FONT_MODE_16X16,REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1052. y += 18;
  1053. }
  1054. //显示内容
  1055. if(NULL==eleContent) return;
  1056. if(eleContent->valuestring==NULL) return;
  1057. //计算单词个数
  1058. calWordNum(eleContent->valuestring);
  1059. ptr=eleContent->valuestring;
  1060. MessageEditInit(&sutMEdit, 1, y , ptr, FONT_MODE_16X16);
  1061. len=MessageEditShow(&sutMEdit,ptr+showIndex);
  1062. if(len !=0) showIndex+=len;
  1063. else showIndex=0;
  1064. }
  1065. unsigned char recoderVolume=0;
  1066. LEARN_REC_STATUS learnRecordStatus;
  1067. void learnRecSetStatus(LEARN_REC_STATUS newStatus){
  1068. learnRecordStatus=newStatus;
  1069. }
  1070. void learnRecSetVol(unsigned char newVol){
  1071. recoderVolume=newVol;
  1072. }
  1073. static void learnShowResult(void);
  1074. static void learnShowStartScreen(void){
  1075. const char *repeatListen="[F1]重听";
  1076. if(usr.type==1) uiMenuShowBottomIndacitor("[OK]录音", repeatListen);
  1077. else uiMenuShowBottomIndacitor(NULL, repeatListen);
  1078. }
  1079. /*播放录音中止/完成后,返回原定界面及操作*/
  1080. void learnRetrunStatus(void){
  1081. if(0==learnGetAudioType()){//返回原定界面及操作
  1082. learnRecSetStatus(LEARN_REC_DONE);
  1083. uiMenuShowBottomIndacitor("[OK]提交","[F1]播放");//选择提交还是播放
  1084. learnShowResult();
  1085. }else{//返回原定界面及操作
  1086. learnRecSetStatus(LEARN_REC_IDLE);
  1087. learnShowStartScreen();
  1088. }
  1089. }
  1090. static void learnRecordDoneProcess(void);
  1091. static void learnDetailBackToTaskListMenu(void){
  1092. uISetNextStatus(UIS_MENU_TASK_SEL);//返回上层菜单
  1093. }
  1094. /*任务查看*/
  1095. void uiShowLearnTaskDetail(char update){
  1096. unsigned char ret;
  1097. if(update){
  1098. guiClearAll(guiGetBackColor());
  1099. guiShowCaption(0,"任务详情",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  1100. uiMenuShowBottomLine();
  1101. showFirstFlag=true;
  1102. taskIndex=sutListBox.handle;
  1103. learnRecSetStatus(LEARN_REC_IDLE);
  1104. readTaskAndShow();
  1105. learnShowStartScreen();
  1106. learnTick=0;
  1107. }
  1108. switch(learnRecordStatus){
  1109. case LEARN_REC_BUSY://动态显示正在录音效果
  1110. learnRecordVolShow(recoderVolume);
  1111. break;
  1112. if(uTimerExpired(&learnTick)){
  1113. uTimerStart(&learnTick, 500);
  1114. learnRecordVolShow(recoderVolume);
  1115. }
  1116. break;
  1117. case LEARN_REC_OT://录音已超过最大时间,停止录音
  1118. learnRecordDoneProcess();
  1119. #ifdef SAVE_PCM_SD
  1120. pcmFileClose();
  1121. #endif
  1122. break;
  1123. case LEARN_REC_PLY://正在播放
  1124. switch(learnPlyGetResult()){
  1125. case LEARN_PLY_PRE_ERR://播放失败
  1126. guiShowMessageBox("播放失败");
  1127. nwy_sleep(1500);
  1128. learnDetailBackToTaskListMenu();
  1129. break;
  1130. case LEARN_PLY_FAILED://播放异常
  1131. guiShowMessageBox("播放异常");//返回上层菜单
  1132. nwy_sleep(1500);
  1133. learnDetailBackToTaskListMenu();
  1134. break;
  1135. case LEARN_PLY_DONE://播放成功,结束
  1136. learnRetrunStatus();
  1137. break;
  1138. }
  1139. break;
  1140. case LEARN_RECORD_UPLOAD://等待上传结果
  1141. ret=getHttpLearnStatus();
  1142. if(uTimerExpired(&learnTick) || ret!=1){
  1143. MSG_INFO(1, "LEARN_RECORD_UPLOAD:%d",ret);
  1144. switch(ret){
  1145. case 0://上传完成
  1146. guiShowMessageBox("上传完成");
  1147. recordSaveTmpToLocal(tidString);
  1148. break;
  1149. case 2://上传失败
  1150. guiShowMessageBox("上传失败");
  1151. break;
  1152. default://上传超时
  1153. hearnHttpOverTime();
  1154. guiShowMessageBox("上传超时");
  1155. break;
  1156. }
  1157. nwy_sleep(1500);
  1158. //返回任务详情内容页面,录音临时信息及资源回收
  1159. learnRecordBufRelease();
  1160. learnShowStartScreen();
  1161. readTaskAndShow();
  1162. learnRecSetStatus(LEARN_REC_IDLE);
  1163. }
  1164. break;
  1165. }
  1166. }
  1167. //根据录制的语音内容算出平均值用于显示
  1168. //volLev:经过计算后得出的0~10等级数值
  1169. void learnRecordVolShow(unsigned char volLev){
  1170. unsigned char vol=volLev;
  1171. unsigned short y1,y2,x,i;
  1172. unsigned int colorID;
  1173. static unsigned char lashVel=99;
  1174. unsigned char width=10;//一格音量宽度
  1175. unsigned char tx=1;//音量柱间隔
  1176. unsigned char sx=0;//音量柱开始坐标
  1177. if(lashVel == volLev) return;
  1178. lashVel=volLev;
  1179. //然后使用绿色显示实际音量
  1180. for(i=0;i<LEV_NUM;i++){
  1181. if(i<vol) colorID=COLOR_BLUE;
  1182. else colorID=guiGetBackColor();//COLOR_GRAY;
  1183. y2=GLCD_HEIGHT-2;
  1184. y1=y2-i-3;if(y1<=UI_BOTTOM_LINE_Y) y1=UI_BOTTOM_LINE_Y+1;
  1185. x=sx+i*(width+tx);
  1186. guiDrawVLine(y1, y2, x, width, colorID);
  1187. }
  1188. }
  1189. /*复位用户超时定时器*/
  1190. void httpPostResetUserTimer(unsigned int persent){
  1191. char info[30];
  1192. snprintf(info,sizeof(info), "正在上传:%d%%",persent);
  1193. guiShowMessageBox(info);
  1194. uTimerStart(&learnTick, 5000);
  1195. }
  1196. void uiShowLearnTaskDetailResponse(void){
  1197. unsigned char *pcm;
  1198. unsigned int plen;
  1199. unsigned short key=getKeyValue();
  1200. if(MKEY_VALUE_ESC==key){
  1201. switch(learnRecordStatus){
  1202. case LEARN_REC_IDLE://未录音,退出
  1203. learnRecordBufRelease();
  1204. learnDetailBackToTaskListMenu();
  1205. break;
  1206. case LEARN_REC_BUSY://正在录音,选择停止
  1207. learnRecordDoneProcess();
  1208. #ifdef SAVE_PCM_SD
  1209. pcmFileClose();
  1210. #endif
  1211. break;
  1212. case LEARN_REC_DONE://刚录音完成,选择放弃而返回到任务详情
  1213. learnRecordBufRelease();
  1214. learnShowStartScreen();
  1215. readTaskAndShow();
  1216. learnRecSetStatus(LEARN_REC_IDLE);
  1217. break;
  1218. case LEARN_REC_PLY://停止正在播放的操作
  1219. learnStopPlaying();
  1220. learnRetrunStatus();
  1221. break;
  1222. }
  1223. }else if(MKEY_VALUE_MENU==key){
  1224. switch(learnRecordStatus){
  1225. case LEARN_REC_IDLE://选择了录音做作业
  1226. if(usr.type != 1){
  1227. MSG_WARN(1, "Only student have rec feature!");
  1228. break;
  1229. }
  1230. if(false==learnStartRecord(charaNum)){
  1231. MSG_ERR(1, "try recoder failed");
  1232. guiShowMessageBox("录音失败");
  1233. nwy_sleep(1500);
  1234. learnRecordBufRelease();
  1235. learnDetailBackToTaskListMenu();
  1236. break;
  1237. }
  1238. #ifdef SAVE_PCM_SD
  1239. pcmFileOpen();
  1240. #endif
  1241. uiMenuShowBottomIndacitor(NULL, "停止");
  1242. learnRecSetStatus(LEARN_REC_BUSY);
  1243. break;
  1244. case LEARN_REC_DONE://选择提交录音(临时)
  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. readTaskAndShow();//显示任务其它内容
  1259. break;
  1260. }
  1261. }else if(MKEY_VALUE_P1==key){
  1262. switch(learnRecordStatus){
  1263. case LEARN_REC_IDLE://选择回放已提交的录音
  1264. learnPlayAudio(1, tidString);
  1265. learnRecSetStatus(LEARN_REC_PLY);
  1266. uiMenuShowBottomIndacitor(NULL, "停止");
  1267. break;
  1268. case LEARN_REC_DONE://选择播放录音(临时)
  1269. learnPlayAudio(0, NULL);
  1270. learnRecSetStatus(LEARN_REC_PLY);
  1271. uiMenuShowBottomIndacitor(NULL, "停止");
  1272. break;
  1273. }
  1274. }
  1275. }
  1276. static void learnShowResult(void){
  1277. float devscore;
  1278. float speedscore=learnGetRecordRecSpeed();
  1279. float volumescore;
  1280. char info[30];
  1281. unsigned int y=UI_CONTENT_SHOW_Y+5;
  1282. volumescore=usr.scoreVolumeParam*learnGetRecordMaxVolume();
  1283. devscore=speedscore+volumescore;
  1284. guiClearRect(0,UI_CONTENT_SHOW_Y,GLCD_WIDTH-1,GLCD_HEIGHT-16-1,guiGetBackColor());
  1285. snprintf(info, sizeof(info), "设备评分:%.1f", devscore);
  1286. guiShowStr(5, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1287. y+= 18;
  1288. snprintf(info, sizeof(info), "速度评分:%.1f", speedscore);
  1289. guiShowStr(5, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1290. y+= 18;
  1291. snprintf(info, sizeof(info), "音量评分:%.1f", volumescore);
  1292. guiShowStr(5, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1293. y+= 18;
  1294. }
  1295. static void learnRecordDoneProcess(void){
  1296. learnRecordFree();
  1297. learnStopRecord();
  1298. learnRecSetStatus(LEARN_REC_DONE);
  1299. uiMenuShowBottomIndacitor("[OK]提交","[F1]播放");//选择提交还是播放
  1300. learnShowResult();
  1301. }
  1302. //学习排名菜单
  1303. void uiShowLearnRank(char update){
  1304. static const char *rankMode[]={
  1305. "月排名",
  1306. "周排名",
  1307. ""
  1308. };
  1309. static const char *learnIcon[]={
  1310. "RANK-M.bmp",
  1311. "RANK-W.bmp",
  1312. ""
  1313. };
  1314. char Freatrue[2]={0,1};
  1315. if(update){
  1316. sdMount();
  1317. guiClearAll(guiGetBackColor());
  1318. guiShowCaption(0,"学习排名",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  1319. uiMenuShowBottomLine();
  1320. ListBoxInit(&sutListBox, rankMode, 0, learnIcon, Freatrue);
  1321. uiMenuShowBottomIndacitor("查看", "返回");
  1322. }
  1323. }
  1324. void LearnRankSelResponse(void){
  1325. unsigned short key=ListBoxResponse(&sutListBox);
  1326. if(MKEY_VALUE_ESC==key){
  1327. sdUnmount();
  1328. uISetNextStatus(UIS_MENU_MAIN);
  1329. }else if(MKEY_VALUE_MENU==key){
  1330. if(sutListBox.handle==0){
  1331. rankType=0;
  1332. uiPushStack(sutListBox.handle);
  1333. uISetNextStatus(UIS_MENU_RANK_DETAIL);
  1334. }else if(sutListBox.handle==1){
  1335. rankType=1;
  1336. uiPushStack(sutListBox.handle);
  1337. uISetNextStatus(UIS_MENU_RANK_DETAIL);
  1338. }
  1339. }
  1340. }
  1341. typedef struct{
  1342. char type;//表示是month还是week
  1343. int typeIndex;//type的值
  1344. int avg;
  1345. int score;
  1346. int ranking;
  1347. int num;
  1348. }RANK_DEF;
  1349. typedef struct{
  1350. unsigned int item;//rankResult实际使用的个数
  1351. unsigned int num;//rankResult分配的个数(释放时使用)
  1352. RANK_DEF **rankResult;
  1353. }RANK_RESULT_DEF;
  1354. RANK_RESULT_DEF ranks={0,0,0,NULL};
  1355. bool rankShowReady;
  1356. static void rankInfoDetailShow(unsigned char direction){
  1357. static int tIndex=0;
  1358. if(ranks.item==0){
  1359. guiShowMessageBox("无数据");
  1360. return;
  1361. }else if(ranks.item==1) tIndex=0;
  1362. else{
  1363. if(direction==0) tIndex=0;
  1364. else if(direction==1){
  1365. if(++tIndex>=ranks.item) tIndex=0;
  1366. }else if(direction==2){
  1367. if(tIndex==0) tIndex=ranks.item-1;
  1368. else tIndex--;
  1369. }
  1370. }
  1371. guiClearRect(0,UI_CONTENT_SHOW_Y,GLCD_WIDTH-1,UI_BOTTOM_SHOW_Y-2,guiGetBackColor());
  1372. //显示
  1373. if(ranks.rankResult==NULL){
  1374. MSG_WARN(1, "rank no data");
  1375. return;
  1376. }
  1377. char info[50];
  1378. int x=1,y=UI_CONTENT_SHOW_Y;
  1379. //显示排名类型
  1380. if(0==ranks.rankResult[tIndex]->type) snprintf(info, sizeof(info), "month:%d", ranks.rankResult[tIndex]->typeIndex);
  1381. else snprintf(info, sizeof(info), "week:%d", ranks.rankResult[tIndex]->typeIndex);
  1382. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1383. y+=17;
  1384. //显示avg
  1385. snprintf(info, sizeof(info), "avg:%d", ranks.rankResult[tIndex]->avg);
  1386. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1387. y+=17;
  1388. //显示score
  1389. snprintf(info, sizeof(info), "score:%d", ranks.rankResult[tIndex]->score);
  1390. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1391. y+=17;
  1392. //显示ranking
  1393. snprintf(info, sizeof(info), "ranking:%d", ranks.rankResult[tIndex]->ranking);
  1394. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1395. y+=17;
  1396. //显示num
  1397. snprintf(info, sizeof(info), "num:%d", ranks.rankResult[tIndex]->num);
  1398. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1399. }
  1400. void uiShowLearnRankDetail(char update){
  1401. int ret;
  1402. static bool needGetUinfo=false;
  1403. if(update){
  1404. guiClearAll(guiGetBackColor());
  1405. if(rankType==0)
  1406. guiShowCaption(0,"月排名",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  1407. else
  1408. guiShowCaption(0,"周排名",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  1409. uiMenuShowBottomLine();
  1410. uiMenuShowBottomIndacitor(NULL, "返回");
  1411. if(sutApp.pocPpp==0){
  1412. guiShowMessageBox("无网络");
  1413. nwy_sleep(1500);
  1414. uISetNextStatus(UIS_MENU_RANK_SEL);
  1415. return;
  1416. }
  1417. rankShowReady=false;
  1418. showInfoBox("请稍候",3000);
  1419. if(isUserInfoReady()==false){//如果未获取用户信息,先获取用户信息
  1420. needGetUinfo=true;
  1421. learnUpdateStatus(LEARN_DB_WAIT_INFO);
  1422. learnTaskUpdate(LEARN_DB_GET_INFO,NULL, NULL,true);
  1423. }else{//否则可以直接获取排名
  1424. needGetUinfo=false;
  1425. learnUpdateStatus(LEARN_RANK_GET_DETAIL);
  1426. learnTaskUpdate(LEARN_RANK_GET_DETAIL,(void*)&rankType, NULL,true);
  1427. MSG_ERR(1, "update rank detail %s", (rankType==0)?"month":"week");
  1428. }
  1429. }
  1430. if(false==rankShowReady){
  1431. ret=getHttpLearnStatus();
  1432. if(uTimerExpired(&learnTick) || ret!=1){
  1433. MSG_INFO(1, "WAIT %s RESULT:%d",(rankType==0)?"MONTH":"WEEK",ret);
  1434. switch(ret){
  1435. case 0://功获取到了信息
  1436. if(needGetUinfo==true){
  1437. needGetUinfo=false;
  1438. learnUpdateStatus(LEARN_RANK_GET_DETAIL);
  1439. learnTaskUpdate(LEARN_RANK_GET_DETAIL,(void*)&rankType, NULL,true);
  1440. MSG_ERR(1, "update rank detail %s", (rankType==0)?"month":"week");
  1441. }else{
  1442. rankShowReady=true;
  1443. getRankShowMessage(rankType);
  1444. rankInfoDetailShow(0);
  1445. }
  1446. return;
  1447. case 2://获取失败
  1448. guiShowMessageBox("获取失败");
  1449. break;
  1450. default://获取超时
  1451. hearnHttpOverTime();
  1452. guiShowMessageBox("获取超时");
  1453. break;
  1454. }
  1455. nwy_sleep(1500);
  1456. getStackStruct()->ok_back=1;
  1457. uISetNextStatus(UIS_MENU_RANK_SEL);
  1458. }
  1459. }
  1460. }
  1461. static void learnRankReleaseDB(void);
  1462. void LearnRankDetailSelResponse(void){
  1463. if(rankShowReady==true){
  1464. switch(getKeyValue()){
  1465. case MKEY_VALUE_ESC:
  1466. learnRankReleaseDB();
  1467. uISetNextStatus(UIS_MENU_RANK_SEL);
  1468. break;
  1469. case MKEY_VALUE_DOWN:rankInfoDetailShow(1);break;
  1470. case MKEY_VALUE_UP:rankInfoDetailShow(2);break;
  1471. }
  1472. }
  1473. }
  1474. typedef struct{
  1475. int week;
  1476. char time[15];
  1477. char subject[30*2];
  1478. }CLASS_DEF;
  1479. typedef struct{
  1480. unsigned int item;//rankResult实际使用的个数
  1481. unsigned int num;//rankResult分配的个数(释放时使用)
  1482. CLASS_DEF **classResult;
  1483. }CLASS_RESULT_DEF;
  1484. CLASS_RESULT_DEF classes={0,0,0,NULL};
  1485. bool classShowReady;
  1486. static void classInfoDetailShow(unsigned char direction){
  1487. static int tIndex=0;
  1488. if(classes.item==0){
  1489. guiShowMessageBox("无数据");
  1490. return;
  1491. }else if(classes.item==1) tIndex=0;
  1492. else{
  1493. if(direction==0) tIndex=0;
  1494. else if(direction==1){
  1495. if(++tIndex>=classes.item) tIndex=0;
  1496. }else if(direction==2){
  1497. if(tIndex==0) tIndex=classes.item-1;
  1498. else tIndex--;
  1499. }
  1500. }
  1501. guiClearRect(0,UI_CONTENT_SHOW_Y,GLCD_WIDTH-1,UI_BOTTOM_SHOW_Y-2,guiGetBackColor());
  1502. //显示
  1503. if(classes.classResult==NULL){
  1504. MSG_WARN(1, "class no data");
  1505. return;
  1506. }
  1507. char info[50];
  1508. int x=1,y=UI_CONTENT_SHOW_Y;
  1509. //显示week
  1510. snprintf(info, sizeof(info), "week:%d", classes.classResult[tIndex]->week);
  1511. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1512. y+=17;
  1513. //显示time
  1514. snprintf(info, sizeof(info), "time:%s", classes.classResult[tIndex]->time);
  1515. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1516. y+=17;
  1517. //显示subject
  1518. snprintf(info, sizeof(info), "subject:%s", classes.classResult[tIndex]->subject);
  1519. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1520. }
  1521. static void getClassShowMessage(void);
  1522. /*课程表*/
  1523. void uiShowLearnClassDetail(char update){
  1524. int ret;
  1525. static bool needGetUinfo=false;
  1526. if(update){
  1527. guiClearAll(guiGetBackColor());
  1528. guiShowCaption(0,"课程表",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  1529. uiMenuShowBottomLine();
  1530. uiMenuShowBottomIndacitor(NULL, "返回");
  1531. if(sutApp.pocPpp==0){
  1532. guiShowMessageBox("无网络");
  1533. nwy_sleep(1500);
  1534. uISetNextStatus(UIS_MENU_LOCATION_SEL);
  1535. return;
  1536. }
  1537. classShowReady=false;
  1538. showInfoBox("请稍候",3000);
  1539. sdMount();
  1540. if(isUserInfoReady()==false){//如果未获取用户信息,先获取用户信息
  1541. needGetUinfo=true;
  1542. learnUpdateStatus(LEARN_DB_WAIT_INFO);
  1543. learnTaskUpdate(LEARN_DB_GET_INFO,NULL, NULL,true);
  1544. }else{//否则可以直接获取课程表
  1545. needGetUinfo=false;
  1546. learnUpdateStatus(LEARN_CLASS_GET_DETAIL);
  1547. learnTaskUpdate(LEARN_CLASS_GET_DETAIL,NULL, NULL,true);
  1548. MSG_ERR(1, "update class detail");
  1549. }
  1550. }
  1551. if(false==classShowReady){
  1552. ret=getHttpLearnStatus();
  1553. if(uTimerExpired(&learnTick) || ret!=1){
  1554. MSG_INFO(1, "WAIT CLASS:%d",ret);
  1555. switch(ret){
  1556. case 0://功获取到了信息
  1557. if(needGetUinfo==true){
  1558. needGetUinfo=false;
  1559. learnUpdateStatus(LEARN_CLASS_GET_DETAIL);
  1560. learnTaskUpdate(LEARN_CLASS_GET_DETAIL,NULL, NULL,true);
  1561. MSG_ERR(1, "update class detail");
  1562. }else{//处理
  1563. MSG_INFO(1, "class info get ok");
  1564. classShowReady=true;
  1565. getClassShowMessage();
  1566. classInfoDetailShow(0);
  1567. }
  1568. return;
  1569. case 2://获取失败
  1570. guiShowMessageBox("获取失败");
  1571. break;
  1572. default://获取超时
  1573. hearnHttpOverTime();
  1574. guiShowMessageBox("获取超时");
  1575. break;
  1576. }
  1577. nwy_sleep(1500);
  1578. getStackStruct()->ok_back=1;
  1579. uISetNextStatus(UIS_MENU_LOCATION_SEL);
  1580. }
  1581. }
  1582. }
  1583. static void learnClassReleaseDB(void);
  1584. void LearnClassDetailResponse(void){
  1585. if(classShowReady==true){
  1586. switch(getKeyValue()){
  1587. case MKEY_VALUE_ESC:
  1588. learnClassReleaseDB();
  1589. uISetNextStatus(UIS_MENU_LOCATION_SEL);
  1590. break;
  1591. case MKEY_VALUE_DOWN:classInfoDetailShow(1);break;
  1592. case MKEY_VALUE_UP:classInfoDetailShow(2);break;
  1593. }
  1594. }
  1595. }
  1596. typedef struct{
  1597. int id;
  1598. char time[15];
  1599. char title[30];
  1600. }NOTICE_DEF;
  1601. typedef struct{
  1602. unsigned int item;//rankResult实际使用的个数
  1603. unsigned int num;//rankResult分配的个数(释放时使用)
  1604. NOTICE_DEF **noticeResult;
  1605. }NOTICE_RESULT_DEF;
  1606. NOTICE_RESULT_DEF notices={0,0,0,NULL};
  1607. bool noticeShowReady;
  1608. static int noticetIndex=0;
  1609. static void noticeInfoDetailShow(unsigned char direction){
  1610. if(notices.item==0){
  1611. guiShowMessageBox("无数据");
  1612. return;
  1613. }else if(notices.item==1) noticetIndex=0;
  1614. else{
  1615. if(direction==0) noticetIndex=0;
  1616. else if(direction==1){
  1617. if(++noticetIndex>=notices.item) noticetIndex=0;
  1618. }else if(direction==2){
  1619. if(noticetIndex==0) noticetIndex=notices.item-1;
  1620. else noticetIndex--;
  1621. }
  1622. }
  1623. guiClearRect(0,UI_CONTENT_SHOW_Y,GLCD_WIDTH-1,UI_BOTTOM_SHOW_Y-2,guiGetBackColor());
  1624. //显示
  1625. if(notices.noticeResult==NULL){
  1626. MSG_WARN(1, "notice no data");
  1627. return;
  1628. }
  1629. char info[50];
  1630. int x=1,y=UI_CONTENT_SHOW_Y;
  1631. //显示title
  1632. snprintf(info, sizeof(info), "title:%s", notices.noticeResult[noticetIndex]->title);
  1633. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1634. y+=17;
  1635. //显示time
  1636. snprintf(info, sizeof(info), "time:%s", notices.noticeResult[noticetIndex]->time);
  1637. guiShowStr(x, y, info, FONT_MODE_16X16, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  1638. }
  1639. static void getNoticeShowMessage(void);
  1640. /*消息通知*/
  1641. void uiShowLearnNoticeDetail(char update){
  1642. int ret;
  1643. static bool needGetUinfo=false;
  1644. if(update){
  1645. guiClearAll(guiGetBackColor());
  1646. guiShowCaption(0,"消息通知",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  1647. uiMenuShowBottomLine();
  1648. uiMenuShowBottomIndacitor("阅读", "返回");
  1649. if(sutApp.pocPpp==0){
  1650. guiShowMessageBox("无网络");
  1651. nwy_sleep(1500);
  1652. uISetNextStatus(UIS_MENU_LOCATION_SEL);
  1653. return;
  1654. }
  1655. if(noticeShowReady==false){
  1656. showInfoBox("请稍候",3000);
  1657. sdMount();
  1658. if(isUserInfoReady()==false){//如果未获取用户信息,先获取用户信息
  1659. needGetUinfo=true;
  1660. learnUpdateStatus(LEARN_DB_WAIT_INFO);
  1661. learnTaskUpdate(LEARN_DB_GET_INFO,NULL, NULL,true);
  1662. }else{//否则可以直接获取课程表
  1663. needGetUinfo=false;
  1664. learnUpdateStatus(LEARN_NOTICE_GET_DETAIL);
  1665. learnTaskUpdate(LEARN_NOTICE_GET_DETAIL,NULL, NULL,true);
  1666. MSG_ERR(1, "update notice detail");
  1667. }
  1668. }
  1669. }
  1670. if(false==noticeShowReady){
  1671. ret=getHttpLearnStatus();
  1672. if(uTimerExpired(&learnTick) || ret!=1){
  1673. MSG_INFO(1, "WAIT NOTICE:%d",ret);
  1674. switch(ret){
  1675. case 0://功获取到了信息
  1676. if(needGetUinfo==true){
  1677. needGetUinfo=false;
  1678. learnUpdateStatus(LEARN_NOTICE_GET_DETAIL);
  1679. learnTaskUpdate(LEARN_NOTICE_GET_DETAIL,NULL, NULL,true);
  1680. MSG_ERR(1, "update notice detail");
  1681. }else{//处理
  1682. MSG_INFO(1, "notice info get ok");
  1683. noticeShowReady=true;
  1684. getNoticeShowMessage();
  1685. noticeInfoDetailShow(0);
  1686. }
  1687. return;
  1688. case 2://获取失败
  1689. guiShowMessageBox("获取失败");
  1690. break;
  1691. default://获取超时
  1692. hearnHttpOverTime();
  1693. guiShowMessageBox("获取超时");
  1694. break;
  1695. }
  1696. nwy_sleep(1500);
  1697. getStackStruct()->ok_back=1;
  1698. uISetNextStatus(UIS_MENU_LOCATION_SEL);
  1699. }
  1700. }
  1701. }
  1702. static void learnNoticeReleaseDB(void);
  1703. unsigned char noticeHandle;
  1704. void LearnNoticeResponse(void){
  1705. if(noticeShowReady==true){
  1706. switch(getKeyValue()){
  1707. case MKEY_VALUE_ESC:
  1708. learnNoticeReleaseDB();
  1709. noticeShowReady=false;
  1710. uISetNextStatus(UIS_MENU_LOCATION_SEL);
  1711. break;
  1712. case MKEY_VALUE_DOWN:noticeInfoDetailShow(1);break;
  1713. case MKEY_VALUE_UP:noticeInfoDetailShow(2);break;
  1714. case MKEY_VALUE_MENU:
  1715. uISetNextStatus(UIS_MENU_LOCATION_NOTICE_DETAIL);
  1716. break;
  1717. }
  1718. }
  1719. }
  1720. bool noticeDetailReady=false;
  1721. /*消息通知详情*/
  1722. void uiShowLearnNoticeDetailShow(char update){
  1723. int ret;
  1724. if(update){
  1725. guiClearAll(guiGetBackColor());
  1726. guiShowCaption(0,notices.noticeResult[noticetIndex]->title,UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  1727. uiMenuShowBottomLine();
  1728. uiMenuShowBottomIndacitor(NULL, "返回");
  1729. if(sutApp.pocPpp==0){
  1730. guiShowMessageBox("无网络");
  1731. nwy_sleep(1500);
  1732. uISetNextStatus(UIS_MENU_LOCATION_SEL);
  1733. return;
  1734. }
  1735. showInfoBox("请稍候",3000);
  1736. sdMount();
  1737. noticeDetailReady=false;
  1738. learnUpdateStatus(LEARN_NOTICE_GET_DETAIL_SHOW);
  1739. learnTaskUpdate(LEARN_NOTICE_GET_DETAIL_SHOW,(void *)&notices.noticeResult[noticetIndex]->id, NULL,true);
  1740. MSG_ERR(1, "update notice detail show");
  1741. }
  1742. if(false==noticeDetailReady){
  1743. ret=getHttpLearnStatus();
  1744. if(uTimerExpired(&learnTick) || ret!=1){
  1745. MSG_INFO(1, "WAIT NOTICE SHOW:%d",ret);
  1746. switch(ret){
  1747. case 0://功获取到了信息
  1748. //处理
  1749. noticeDetailReady=true;
  1750. MSG_INFO(1, "notice info get ok");
  1751. noticeInfoDetailShowContent(0);
  1752. return;
  1753. case 2://获取失败
  1754. guiShowMessageBox("获取失败");
  1755. break;
  1756. default://获取超时
  1757. hearnHttpOverTime();
  1758. guiShowMessageBox("获取超时");
  1759. break;
  1760. }
  1761. nwy_sleep(1500);
  1762. noticeShowReady=false;
  1763. getStackStruct()->ok_back=1;
  1764. uISetNextStatus(UIS_MENU_LOCATION_SEL);
  1765. }
  1766. }
  1767. }
  1768. void LearnNoticeDetailResponse(void){
  1769. if(noticeDetailReady==true){
  1770. switch(getKeyValue()){
  1771. case MKEY_VALUE_ESC:
  1772. noticeShowReady=false;
  1773. uISetNextStatus(UIS_MENU_LOCATION_NOTICE);
  1774. break;
  1775. case MKEY_VALUE_DOWN:noticeInfoDetailShowContent(1);break;
  1776. //case MKEY_VALUE_UP:noticeInfoDetailShowContent(2);break;
  1777. }
  1778. }
  1779. }
  1780. #define OBJECT_AVG_NAME "avg"
  1781. #define OBJECT_SCORE_NAME "score"
  1782. #define OBJECT_RANKING_NAME "ranking"
  1783. #define OBJECT_NUM_NAME "num"
  1784. /*
  1785. msg:json buffer 数据,长度为len
  1786. {
  1787. "gid":555,
  1788. "uid":12345,
  1789. "list":[
  1790. {"week":29,"avg":518,"score":562,"ranking":5,"num":35},
  1791. {"week":28,"avg":518,"score":581,"ranking":8,"num":28},
  1792. {"week":27,"avg":518,"score":558,"ranking":10,"num":40},
  1793. {"week":26,"avg":518,"score":592,"ranking":2,"num":37},
  1794. ]
  1795. }
  1796. {
  1797. "gid":555,
  1798. "uid":12345,
  1799. "list":[
  1800. {"month":8,"avg":518,"score":562,"ranking":5,"num":35},
  1801. {"month":7,"avg":518,"score":581,"ranking":8,"num":28},
  1802. {"month":6,"avg":518,"score":558,"ranking":10,"num":40},
  1803. {"month":5,"avg":518,"score":592,"ranking":2,"num":37},
  1804. ]
  1805. }
  1806. 这里将list提取出到buffer并输出
  1807. */
  1808. static char *rankBuffer=NULL;
  1809. cJSON* rankJson=NULL;
  1810. static void learnRankReleaseDB(void){
  1811. int i;
  1812. releaseBuf(&rankBuffer);
  1813. if(NULL!=ranks.rankResult){
  1814. for(i=0;i<ranks.num;i++){
  1815. if(ranks.rankResult[i]!=NULL) releaseBuf(&ranks.rankResult[i]);
  1816. }
  1817. releaseBuf(&ranks.rankResult);
  1818. }
  1819. releaseJson(&rankJson);
  1820. }
  1821. static void getRankShowMessage(unsigned char ranktype){
  1822. //从LEARN_RANK文件中读取内容出来解析
  1823. RANK_DEF **rankPtr=NULL;
  1824. int fd=-1,i=0;
  1825. char info[50];
  1826. ranks.item=0;
  1827. snprintf(info, sizeof(info), "%s/%s", SD_TOKEN,LEARN_RANK);
  1828. fd=nwy_sdk_fopen(info, NWY_RDONLY);
  1829. if(fd<0){
  1830. MSG_WARN(1, "Rank file open failed");
  1831. goto R_EXIT;
  1832. }
  1833. long size=nwy_sdk_fsize_fd(fd);
  1834. if(size<=0){
  1835. MSG_WARN(1, "Rank file data error");
  1836. goto R_EXIT;
  1837. }
  1838. releaseBuf(&rankBuffer);
  1839. rankBuffer=(char *)createBuf(size);
  1840. //rankBuffer=(char *)malloc(size);
  1841. if(NULL == rankBuffer){
  1842. MSG_ERR(1, "rank buffer malloc error");
  1843. goto R_EXIT;
  1844. }
  1845. //MSG_INFO(1, "[mem malloc]%x", rankBuffer);
  1846. memset(rankBuffer, 0, size);
  1847. nwy_sdk_fread(fd, rankBuffer, size);
  1848. nwy_sdk_fclose(fd);
  1849. fd=-1;
  1850. //解析json
  1851. MSG_INFO(1, "Rank data analyse len:%d, type:%d",size, ranktype);
  1852. if(NULL!=ranks.rankResult){
  1853. for(i=0;i<ranks.num;i++){
  1854. if(ranks.rankResult[i]!=NULL) releaseBuf(&ranks.rankResult[i]);
  1855. }
  1856. releaseBuf(&ranks.rankResult);
  1857. }
  1858. ranks.num=0;
  1859. if(0==ranktype) snprintf(info, sizeof(info), "month");
  1860. else snprintf(info, sizeof(info), "week");
  1861. rankJson = createJson(rankBuffer);
  1862. //rankJson = cJSON_Parse(rankBuffer);
  1863. if(NULL==rankJson){
  1864. MSG_WARN(1, "rank json null");
  1865. goto R_EXIT;
  1866. }
  1867. //MSG_INFO(1, "rank json:%x", rankJson);
  1868. cJSON* item = cJSON_GetObjectItem(rankJson, OBJECT_LIST);
  1869. if(NULL==item){
  1870. MSG_ERR(1, "rank json failed");
  1871. goto R_EXIT;
  1872. }
  1873. int num=cJSON_GetArraySize(item);
  1874. if(num<=0){
  1875. MSG_WARN(1, "rank array num:%d",num);
  1876. goto R_EXIT;
  1877. }
  1878. MSG_INFO(1, "rank num:%d",num);
  1879. rankPtr=(RANK_DEF **)createBuf(sizeof(RANK_DEF *)*num);
  1880. //rankPtr=(RANK_DEF **)malloc(sizeof(RANK_DEF *)*num);
  1881. if(NULL==rankPtr){
  1882. MSG_ERR(1,"rank malloc failed");
  1883. goto R_EXIT;
  1884. }
  1885. //MSG_INFO(1, "[mem malloc]%x", rankPtr);
  1886. ranks.num=num;
  1887. for(i=0;i<num;i++) rankPtr[i]=NULL;
  1888. for(i=0;i<num;i++){
  1889. rankPtr[i]=(RANK_DEF *)createBuf(sizeof(RANK_DEF));
  1890. //rankPtr[i]=(RANK_DEF *)malloc(sizeof(RANK_DEF));
  1891. if(NULL==rankPtr[i]){
  1892. MSG_ERR(1, "rank[%d] malloc failed", i);
  1893. goto R_EXIT;
  1894. }
  1895. //MSG_INFO(1, "[mem malloc %d]%x", i, rankPtr[i]);
  1896. }
  1897. cJSON* element,*t;
  1898. i=0;
  1899. for(;;){
  1900. element=cJSON_GetArrayItem(item,i);
  1901. if(NULL==element) break;
  1902. //month/week
  1903. rankPtr[i]->type=ranktype;
  1904. t=cJSON_GetObjectItem(element, info);
  1905. if(NULL==t) rankPtr[i]->typeIndex=-1;
  1906. else rankPtr[i]->typeIndex=atoi(t->valuestring);
  1907. //avg
  1908. t=cJSON_GetObjectItem(element, OBJECT_AVG_NAME);
  1909. if(NULL==t) rankPtr[i]->avg=-1;
  1910. else rankPtr[i]->avg=atoi(t->valuestring);
  1911. //score
  1912. t=cJSON_GetObjectItem(element, OBJECT_SCORE_NAME);
  1913. if(NULL==t) rankPtr[i]->score=-1;
  1914. else rankPtr[i]->score=atoi(t->valuestring);
  1915. //ranking
  1916. t=cJSON_GetObjectItem(element, OBJECT_RANKING_NAME);
  1917. if(NULL==t) rankPtr[i]->ranking=-1;
  1918. else rankPtr[i]->ranking=atoi(t->valuestring);
  1919. //num
  1920. t=cJSON_GetObjectItem(element, OBJECT_NUM_NAME);
  1921. if(NULL==t) rankPtr[i]->num=-1;
  1922. else rankPtr[i]->num=atoi(t->valuestring);
  1923. i++;
  1924. }
  1925. ranks.item=i;
  1926. ranks.rankResult=rankPtr;
  1927. MSG_INFO(1, "rank valid num:%d",i);
  1928. return;
  1929. R_EXIT:
  1930. releaseJson(&rankJson);
  1931. releaseBuf(&rankBuffer);
  1932. if(fd>0) nwy_sdk_fclose(fd);
  1933. if(rankPtr != NULL){
  1934. for(i=0;i<num;i++) releaseBuf(&rankPtr[i]);
  1935. releaseBuf(&rankPtr);
  1936. }
  1937. }
  1938. #define OBJECT_WEEK_NAME "week"
  1939. #define OBJECT_TIME_NAME "time"
  1940. #define OBJECT_SUBJECT_NAME "subject"
  1941. static char *classBuffer=NULL;
  1942. cJSON* classJson=NULL;
  1943. static void learnClassReleaseDB(void){
  1944. int i;
  1945. releaseBuf(&classBuffer);
  1946. if(NULL!=classes.classResult){
  1947. for(i=0;i<classes.num;i++){
  1948. if(classes.classResult[i]!=NULL) releaseBuf(&classes.classResult[i]);
  1949. }
  1950. releaseBuf(&classes.classResult);
  1951. }
  1952. releaseJson(&classJson);
  1953. }
  1954. static void getClassShowMessage(void){
  1955. //从LEARN_CLASS文件中读取内容出来解析
  1956. CLASS_DEF **classPtr=NULL;
  1957. int fd=-1,i=0;
  1958. char info[50];
  1959. ranks.item=0;
  1960. snprintf(info, sizeof(info), "%s/%s", SD_TOKEN,LEARN_CLASS);
  1961. fd=nwy_sdk_fopen(info, NWY_RDONLY);
  1962. if(fd<0){
  1963. MSG_WARN(1, "Class file open failed");
  1964. goto R_EXIT;
  1965. }
  1966. long size=nwy_sdk_fsize_fd(fd);
  1967. if(size<=0){
  1968. MSG_WARN(1, "Class file data error");
  1969. goto R_EXIT;
  1970. }
  1971. releaseBuf(&classBuffer);
  1972. classBuffer=(char *)createBuf(size);
  1973. //classBuffer=(char *)malloc(size);
  1974. if(NULL == classBuffer){
  1975. MSG_ERR(1, "class buffer malloc error");
  1976. goto R_EXIT;
  1977. }
  1978. //MSG_INFO(1, "[mem malloc]%x", classBuffer);
  1979. memset(classBuffer, 0, size);
  1980. nwy_sdk_fread(fd, classBuffer, size);
  1981. nwy_sdk_fclose(fd);
  1982. fd=-1;
  1983. //解析json
  1984. MSG_INFO(1, "Class data analyse len:%d",size);
  1985. if(NULL!=classes.classResult){
  1986. for(i=0;i<classes.num;i++){
  1987. if(classes.classResult[i]!=NULL) releaseBuf(&classes.classResult[i]);
  1988. }
  1989. releaseBuf(&classes.classResult);
  1990. }
  1991. classes.num=0;
  1992. classJson = createJson(classBuffer);
  1993. //classJson = cJSON_Parse(classBuffer);
  1994. if(NULL==classJson){
  1995. MSG_WARN(1, "class json null");
  1996. goto R_EXIT;
  1997. }
  1998. //MSG_INFO(1, "class json:%x", classJson);
  1999. cJSON* item = cJSON_GetObjectItem(classJson, OBJECT_LIST);
  2000. if(NULL==item){
  2001. MSG_ERR(1, "class json failed");
  2002. goto R_EXIT;
  2003. }
  2004. int num=cJSON_GetArraySize(item);
  2005. if(num<=0){
  2006. MSG_WARN(1, "class array num:%d",num);
  2007. goto R_EXIT;
  2008. }
  2009. MSG_INFO(1, "class num:%d",num);
  2010. classPtr=(CLASS_DEF **)createBuf(sizeof(CLASS_DEF *)*num);
  2011. //classPtr=(CLASS_DEF **)malloc(sizeof(CLASS_DEF *)*num);
  2012. if(NULL==classPtr){
  2013. MSG_ERR(1,"class malloc failed");
  2014. goto R_EXIT;
  2015. }
  2016. //MSG_INFO(1, "[mem malloc]%x", classPtr);
  2017. classes.num=num;
  2018. for(i=0;i<num;i++) classPtr[i]=NULL;
  2019. for(i=0;i<num;i++){
  2020. classPtr[i]=(CLASS_DEF *)createBuf(sizeof(CLASS_DEF));
  2021. //classPtr[i]=(CLASS_DEF *)malloc(sizeof(CLASS_DEF));
  2022. if(NULL==classPtr[i]){
  2023. MSG_ERR(1, "class[%d] malloc failed", i);
  2024. goto R_EXIT;
  2025. }
  2026. //MSG_INFO(1, "[mem malloc %d]%x", i, classPtr[i]);
  2027. }
  2028. cJSON* element,*t;
  2029. i=0;
  2030. for(;;){
  2031. element=cJSON_GetArrayItem(item,i);
  2032. if(NULL==element) break;
  2033. //week
  2034. t=cJSON_GetObjectItem(element, OBJECT_WEEK_NAME);
  2035. if(NULL==t) classPtr[i]->week=-1;
  2036. else classPtr[i]->week=atoi(t->valuestring);
  2037. //time
  2038. t=cJSON_GetObjectItem(element, OBJECT_TIME_NAME);
  2039. if(NULL==t) classPtr[i]->time[0]='\0';
  2040. else snprintf(classPtr[i]->time, sizeof(classPtr[i]->time), "%s", t->valuestring);
  2041. //subject
  2042. t=cJSON_GetObjectItem(element, OBJECT_SUBJECT_NAME);
  2043. if(NULL==t) classPtr[i]->subject[0]='\0';
  2044. else snprintf(classPtr[i]->subject, sizeof(classPtr[i]->subject), "%s", t->valuestring);
  2045. i++;
  2046. }
  2047. classes.item=i;
  2048. classes.classResult=classPtr;
  2049. MSG_INFO(1, "class valid num:%d",i);
  2050. return;
  2051. R_EXIT:
  2052. releaseJson(&classJson);
  2053. releaseBuf(&classBuffer);
  2054. if(fd>0) nwy_sdk_fclose(fd);
  2055. if(classPtr != NULL){
  2056. for(i=0;i<num;i++) releaseBuf(&classPtr[i]);
  2057. releaseBuf(&classPtr);
  2058. }
  2059. }
  2060. #define OBJECT_ID_NAME "id"
  2061. #define OBJECT_TITLE_NAME "title"
  2062. static char *noticeBuffer=NULL;
  2063. cJSON* noticeJson=NULL;
  2064. static void learnNoticeReleaseDB(void){
  2065. int i;
  2066. releaseBuf(&noticeBuffer);
  2067. if(NULL!=notices.noticeResult){
  2068. for(i=0;i<notices.num;i++){
  2069. if(notices.noticeResult[i]!=NULL) releaseBuf(&notices.noticeResult[i]);
  2070. }
  2071. releaseBuf(&notices.noticeResult);
  2072. }
  2073. releaseJson(&noticeJson);
  2074. }
  2075. static void getNoticeShowMessage(void){
  2076. //从LEARN_CLASS文件中读取内容出来解析
  2077. NOTICE_DEF **noticePtr=NULL;
  2078. int fd=-1,i=0;
  2079. char info[50];
  2080. notices.item=0;
  2081. snprintf(info, sizeof(info), "%s/%s", SD_TOKEN,LEARN_NOTICE);
  2082. fd=nwy_sdk_fopen(info, NWY_RDONLY);
  2083. if(fd<0){
  2084. MSG_WARN(1, "Notice file open failed");
  2085. goto R_EXIT;
  2086. }
  2087. long size=nwy_sdk_fsize_fd(fd);
  2088. if(size<=0){
  2089. MSG_WARN(1, "Notice file data error");
  2090. goto R_EXIT;
  2091. }
  2092. releaseBuf(&noticeBuffer);
  2093. noticeBuffer=(char *)createBuf(size);
  2094. //noticeBuffer=(char *)malloc(size);
  2095. if(NULL == noticeBuffer){
  2096. MSG_ERR(1, "notice buffer malloc error");
  2097. goto R_EXIT;
  2098. }
  2099. //MSG_INFO(1, "[mem malloc]%x", noticeBuffer);
  2100. memset(noticeBuffer, 0, size);
  2101. nwy_sdk_fread(fd, noticeBuffer, size);
  2102. nwy_sdk_fclose(fd);
  2103. fd=-1;
  2104. //解析json
  2105. MSG_INFO(1, "Notice data analyse len:%d",size);
  2106. if(NULL!=notices.noticeResult){
  2107. for(i=0;i<notices.num;i++){
  2108. if(notices.noticeResult[i]!=NULL) releaseBuf(&notices.noticeResult[i]);
  2109. }
  2110. releaseBuf(&notices.noticeResult);
  2111. }
  2112. notices.num=0;
  2113. noticeJson = createJson(noticeBuffer);
  2114. //noticeJson = cJSON_Parse(noticeBuffer);
  2115. if(NULL==noticeJson){
  2116. MSG_WARN(1, "notice json null");
  2117. goto R_EXIT;
  2118. }
  2119. //MSG_INFO(1, "notice json:%x", noticeJson);
  2120. cJSON* item = cJSON_GetObjectItem(noticeJson, OBJECT_LIST);
  2121. if(NULL==item){
  2122. MSG_ERR(1, "notice json failed");
  2123. goto R_EXIT;
  2124. }
  2125. int num=cJSON_GetArraySize(item);
  2126. if(num<=0){
  2127. MSG_WARN(1, "notice array num:%d",num);
  2128. goto R_EXIT;
  2129. }
  2130. MSG_INFO(1, "notice num:%d",num);
  2131. noticePtr=(NOTICE_DEF **)createBuf(sizeof(NOTICE_DEF *)*num);
  2132. //noticePtr=(NOTICE_DEF **)malloc(sizeof(NOTICE_DEF *)*num);
  2133. if(NULL==noticePtr){
  2134. MSG_ERR(1,"notice malloc failed");
  2135. goto R_EXIT;
  2136. }
  2137. //MSG_INFO(1, "[mem malloc]%x", noticePtr);
  2138. notices.num=num;
  2139. for(i=0;i<num;i++) noticePtr[i]=NULL;
  2140. for(i=0;i<num;i++){
  2141. noticePtr[i]=(NOTICE_DEF *)createBuf(sizeof(NOTICE_DEF));
  2142. //noticePtr[i]=(CLASS_DEF *)malloc(sizeof(CLASS_DEF));
  2143. if(NULL==noticePtr[i]){
  2144. MSG_ERR(1, "notice[%d] malloc failed", i);
  2145. goto R_EXIT;
  2146. }
  2147. //MSG_INFO(1, "[mem malloc %d]%x", i, noticePtr[i]);
  2148. }
  2149. cJSON* element,*t;
  2150. i=0;
  2151. for(;;){
  2152. element=cJSON_GetArrayItem(item,i);
  2153. if(NULL==element) break;
  2154. //id
  2155. t=cJSON_GetObjectItem(element, OBJECT_ID_NAME);
  2156. if(NULL==t) noticePtr[i]->id=-1;
  2157. else noticePtr[i]->id=atoi(t->valuestring);
  2158. //time
  2159. t=cJSON_GetObjectItem(element, OBJECT_TIME_NAME);
  2160. if(NULL==t) noticePtr[i]->time[0]='\0';
  2161. else snprintf(noticePtr[i]->time, sizeof(noticePtr[i]->time), "%s", t->valuestring);
  2162. //title
  2163. t=cJSON_GetObjectItem(element, OBJECT_TITLE_NAME);
  2164. if(NULL==t) noticePtr[i]->title[0]='\0';
  2165. else snprintf(noticePtr[i]->title, sizeof(noticePtr[i]->title), "%s", t->valuestring);
  2166. i++;
  2167. }
  2168. notices.item=i;
  2169. notices.noticeResult=noticePtr;
  2170. MSG_INFO(1, "notice valid num:%d",i);
  2171. return;
  2172. R_EXIT:
  2173. releaseJson(&noticeJson);
  2174. releaseBuf(&noticeBuffer);
  2175. if(fd>0) nwy_sdk_fclose(fd);
  2176. if(noticePtr != NULL){
  2177. for(i=0;i<num;i++) releaseBuf(&noticePtr[i]);
  2178. releaseBuf(&noticePtr);
  2179. }
  2180. }
  2181. char *noticeDetailContent=NULL;
  2182. //char *testShowData="这是一条测试信息,长度为100,然后呢就一直显示吧,我也不知道能显示多少,所以只能临时搞一段文字来显示看看效果如何了,然后再给100000000000元作者吧,作为酬劳,我也不知道是否足够呢。";
  2183. void noticeInfoDetailShowContent(char direction){
  2184. int x=1,y=UI_CONTENT_SHOW_Y;
  2185. char *ptr=noticeDetailContent;
  2186. static int showlen,showIndex,len;
  2187. if(direction==0){
  2188. showlen=strlen(ptr);
  2189. showIndex=0;
  2190. }
  2191. guiClearRect(0,UI_CONTENT_SHOW_Y,GLCD_WIDTH-1,UI_BOTTOM_SHOW_Y-2,guiGetBackColor());
  2192. MessageEditInit(&sutMEdit, x, y+2 , ptr, FONT_MODE_16X16);
  2193. len=MessageEditShow(&sutMEdit,ptr+showIndex);
  2194. if(len !=0) showIndex+=len;
  2195. else showIndex=0;
  2196. }
  2197. void cJSONTakeContent(char *msg, int len){
  2198. //msg={"id":"7","content":"通知测试2"}
  2199. //提取"content"内容项到noticeInfoDetailShowContent中
  2200. releaseBuf(&noticeDetailContent);
  2201. cJSON* nJson=NULL;
  2202. nJson = createJson(msg);
  2203. if(NULL==nJson){
  2204. MSG_WARN(1, "notice detail show json null");
  2205. goto R_EXIT;
  2206. }
  2207. cJSON* item = cJSON_GetObjectItem(nJson, OBJECT_CONTENT_DETAIL);
  2208. if(NULL==item){
  2209. MSG_ERR(1, "notice detail show json failed");
  2210. goto R_EXIT;
  2211. }
  2212. int length=strlen(item->valuestring)+1;
  2213. char *ptr=(char *)createBuf(len*sizeof(char));
  2214. if(ptr==NULL){
  2215. MSG_ERR(1, "notice detail show malloc err");
  2216. goto R_EXIT;
  2217. }
  2218. memcpy(ptr, item->valuestring, len);
  2219. ptr[len]=0;
  2220. noticeDetailContent=ptr;
  2221. R_EXIT:
  2222. releaseJson(&nJson);
  2223. }
  2224. void cJSONTakeParam(char *msg, int len){
  2225. //msg={"scoreSpeedParam": "1.0","scoreVolumeParam": "1.0"}
  2226. bool sparamOk=false;
  2227. bool vparamOk=false;
  2228. cJSON* nJson=NULL;
  2229. nJson = createJson(msg);
  2230. if(NULL==nJson){
  2231. MSG_WARN(1, "param detail json null");
  2232. goto R_EXIT;
  2233. }
  2234. cJSON* item = cJSON_GetObjectItem(nJson, OBJECT_SPEED_PARAM);
  2235. if(NULL==item) MSG_ERR(1, "param speed json failed");
  2236. else{
  2237. usr.scoreSpeedParam=atof(item->valuestring);
  2238. sparamOk=true;
  2239. }
  2240. item = cJSON_GetObjectItem(nJson, OBJECT_VOLUME_PARAM);
  2241. if(NULL==item) MSG_ERR(1, "param volume json failed");
  2242. else{
  2243. usr.scoreVolumeParam=atof(item->valuestring);
  2244. vparamOk=true;
  2245. }
  2246. if(sparamOk==true && vparamOk==true){
  2247. MSG_INFO(1, "sparam:%f, vparam:%f", usr.scoreSpeedParam, usr.scoreVolumeParam);
  2248. usr.paramReady=true;
  2249. }
  2250. R_EXIT:
  2251. releaseJson(&nJson);
  2252. }
  2253. //没获取账户信息,则定时尝试获取
  2254. void LearnUsrInfoCheck(unsigned int interval){
  2255. static char step=0;
  2256. static unsigned int waitTimer;
  2257. unsigned char ret;
  2258. if(sutPocStatus.logined==0) return;
  2259. if(true==isUserInfoReady() && true==usr.paramReady){
  2260. if(step!=0) step=0;
  2261. return;
  2262. }
  2263. switch(step){
  2264. case 0://发起获取账户信息请求
  2265. step=1;
  2266. learnUpdateStatus(LEARN_DB_WAIT_INFO);
  2267. learnTaskUpdate(LEARN_DB_GET_INFO,NULL, NULL,false);
  2268. uTimerStart(&waitTimer, 5000);
  2269. break;
  2270. case 1://等待获取结果
  2271. ret=getHttpLearnStatus();
  2272. if(uTimerExpired(&waitTimer) || ret !=1){
  2273. MSG_INFO(1, "Wait user account:%d", ret);
  2274. if(ret==0) step=2;
  2275. else step=0;//重新获取
  2276. }
  2277. break;
  2278. case 2://发起参数获取请求
  2279. step=3;
  2280. learnUpdateStatus(LEARN_GET_PARAM);
  2281. learnTaskUpdate(LEARN_GET_PARAM,NULL, NULL,false);
  2282. uTimerStart(&waitTimer, 5000);
  2283. break;
  2284. case 3://等待获取结果
  2285. ret=getHttpLearnStatus();
  2286. if(uTimerExpired(&waitTimer) || ret !=1){
  2287. MSG_INFO(1, "Wait param:%d", ret);
  2288. if(ret==0) step=4;//停止
  2289. else step=2;//重新获取
  2290. }
  2291. break;
  2292. case 4://IDLE
  2293. break;
  2294. }
  2295. }
  2296. void uiShowLearnGroup(char type){
  2297. static unsigned int gid=0xffffffff;
  2298. if(false==isUserInfoReady()) return;
  2299. if(type) goto S_NOW;
  2300. if(gid == getLearnGid()) return;
  2301. gid=getLearnGid();
  2302. S_NOW:
  2303. uiShowLearnGName(getLearnGName());
  2304. }
  2305. static bool sdMountStatus=false;
  2306. void sdUnmount(void){
  2307. sdCardCtl(false);
  2308. sdMountStatus=false;
  2309. }
  2310. void sdMount(void){
  2311. if(sdMountStatus==false){
  2312. sdCardCtl(true);
  2313. sdMountStatus=true;
  2314. }
  2315. }
  2316. bool sdCardTest(void){
  2317. const char *table="sdTest.txt";
  2318. char file[50];
  2319. int fd=NULL;
  2320. bool ret=false;
  2321. sdCardCtl(true);
  2322. snprintf(file, sizeof(file), "%s/%s",SD_TOKEN,table);
  2323. fd = nwy_sdk_fopen(file, NWY_CREAT | NWY_RDWR|NWY_TRUNC);//清0式
  2324. if(fd<0){
  2325. MSG_WARN(1, "fopen to write failed:%s",table);
  2326. goto SD_END;
  2327. }
  2328. nwy_sdk_fwrite(fd, table, strlen(table));
  2329. nwy_sdk_fclose(fd);
  2330. fd = nwy_sdk_fopen(file, NWY_RDONLY);
  2331. if(fd < 0){
  2332. MSG_WARN(1, "fopen to read failed:%s",table);
  2333. goto SD_END;
  2334. }
  2335. memset(file, 0, sizeof(file));
  2336. nwy_sdk_fread(fd, file, strlen(table));
  2337. if(0==memcmp(file, table, strlen(file))) ret=true;
  2338. SD_END:
  2339. if(fd != NULL) nwy_sdk_fclose(fd);
  2340. sdCardCtl(false);
  2341. return ret;
  2342. }