learnTask.c 71 KB

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