xinbiao.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. #include "includes.h"
  2. #include "math.h"
  3. SUT_XINBIAO_PAGE sut_xinbiaopage[XINBIAO_NUM];
  4. SUT_XINBIAO_PAGE New_xinbiaopage;
  5. SUT_XINBIAO_INFO sut_xinbiao_info;
  6. USERARR UserArr[MAX_USER]; //临时存储当前所有成员 //每次 86 都需要更新
  7. LEADER leader[XINBIAO_NUM]; //根据 UserArr 每次更新 进表格 存入文件系统
  8. void PushXinbiao(SUT_XINBIAO_PAGE*NewXinbiao);
  9. /*
  10. 获得新的信标页
  11. callID //是, 更新页
  12. //否,创建新页
  13. */
  14. //+POC=8C00%08x%02x;%d;%d;%d;%d;%d;\r\n"
  15. //+POC:8C00 934C0008 00 3B
  16. void Recv_PackNewxinbiaopage(char*msg)
  17. {
  18. char i,j=0,z=0;
  19. char start=0;
  20. char *Pmsg=0;
  21. char data[500]={0};
  22. msg[17]=0;
  23. //处理完 清空
  24. memset(&New_xinbiaopage,0,sizeof(New_xinbiaopage));
  25. New_xinbiaopage.xb_CallID=htoi(msg+9);
  26. MSG_INFO(1,"xb_CallID===%x",New_xinbiaopage.xb_CallID);
  27. //find user name
  28. for(i=0;i<MAX_USER;i++){
  29. if(UserArr[i].id==New_xinbiaopage.xb_CallID){
  30. strcpy(New_xinbiaopage.xb_CallName,UserArr[i].UserName);
  31. }
  32. }
  33. MSG_INFO(1,"New_xinbiaopage.xb_CallName===%s",New_xinbiaopage.xb_CallName);
  34. StrAsciiToHex(msg+19,data);
  35. Pmsg=data;
  36. MSG_INFO(1,"Pmsg===%s",data);
  37. z=strlen(Pmsg);
  38. for(i=0;i<z;i++){
  39. if(Pmsg[i]==';'){
  40. Pmsg[i]=0;
  41. switch(j){
  42. case 0:
  43. strcpy(New_xinbiaopage.xb_Firm_CallId,Pmsg+start);
  44. MSG_INFO(1,"step%02d=====%s",j,New_xinbiaopage.xb_Firm_CallId);
  45. break;
  46. case 1:
  47. New_xinbiaopage.xb_longitue=atoi(Pmsg+start);
  48. MSG_INFO(1,"step%02d=====%d",j,New_xinbiaopage.xb_longitue);
  49. break;
  50. case 2:
  51. New_xinbiaopage.xb_latitue=atoi(Pmsg+start);
  52. MSG_INFO(1,"step%02d=====%d",j,New_xinbiaopage.xb_latitue);
  53. break;
  54. case 3:
  55. New_xinbiaopage.xb_heading=atoi(Pmsg+start);
  56. MSG_INFO(1,"step%02d=====%d",j,New_xinbiaopage.xb_heading);//航向
  57. break;
  58. case 4:
  59. New_xinbiaopage.xb_headerSpeed=atoi(Pmsg+start);
  60. MSG_INFO(1,"step%02d=====%d",j,New_xinbiaopage.xb_headerSpeed);//航速
  61. break;
  62. case 5:
  63. New_xinbiaopage.xb_elevation=atoi(Pmsg+start);
  64. MSG_INFO(1,"step%02d=====%d",j,New_xinbiaopage.xb_elevation);//海拔
  65. break;
  66. }
  67. i++;
  68. start=i;
  69. j++;
  70. }
  71. }
  72. PushXinbiao(&New_xinbiaopage);
  73. sutPocStatus.newxinbiaoflag=1;
  74. sutPocStatus.newxinbiaoStanby=1;
  75. if(newPara.Xinbiao_LiveUpdate==1){
  76. if(sutUIstatus.Status==UIS_STANDBY||sutUIstatus.Status==UIS_CALL_WINDOW)uISetNextStatus(UIS_MENU_ONLINE_XINBIAO);
  77. }
  78. }
  79. const double EARTH_RADIUS = 6378.137;
  80. #define PI 3.141592
  81. #define RIGHT_ANGLE 90.0
  82. static double Deg2Rad(double degree){
  83. return degree * PI / (RIGHT_ANGLE * 2);
  84. }
  85. double GetDirectDistance(double srcLon, double srcLat, double destLon, double destLat){
  86. double radSrcLat = Deg2Rad(srcLat);
  87. double radDestLat = Deg2Rad(destLat);
  88. double a = radSrcLat - radDestLat;
  89. double b = Deg2Rad(srcLon) - Deg2Rad(destLon);
  90. double DirectDistance = 2 * asin(sqrt(pow(sin(a/2),2) +cos(radSrcLat)*cos(radDestLat)*pow(sin(b/2),2)));
  91. DirectDistance = DirectDistance * EARTH_RADIUS;
  92. DirectDistance = round(DirectDistance * 10000) / 10000;
  93. return DirectDistance;
  94. }
  95. void CalcDistance(SUT_XINBIAO_PAGE*NewXinbiao)
  96. {
  97. double srcLon=sutGpsInfo.longitue;
  98. double srcLat=sutGpsInfo.latitue;
  99. double destLon=NewXinbiao->xb_longitue;
  100. double destLat=NewXinbiao->xb_latitue;
  101. srcLon/=1000000;
  102. srcLat/=1000000;
  103. destLon/=1000000;
  104. destLat/=1000000;
  105. MSG_INFO(1,"srcLon==%lf,srcLat====%lf,destLon====%lf,destLat===%lf",srcLon,srcLat,destLon,destLat);
  106. NewXinbiao->xb_distance=GetDirectDistance(srcLon,srcLat,destLon,destLat);
  107. MSG_INFO(1,"xb_distance===%lf",NewXinbiao->xb_distance);
  108. }
  109. //根据角度计算弧度
  110. double rad(double d)
  111. {
  112. //const double PI = 3.1415926535898;
  113. double num=0;
  114. num=d * PI/180.0;
  115. MSG_INFO(1,"num===%lf",num);
  116. return num;
  117. }
  118. //根据弧度计算角度
  119. double Angle(double r)
  120. {
  121. // const double PI = 3.1415926535898;
  122. return r * 180/PI;
  123. }
  124. //计算方位角
  125. double GetBearing(double lat1, double lng1, double lat2, double lng2)
  126. {
  127. double d = 0;
  128. double radLat1 = rad(lat1);//根据角度计算弧度
  129. double radLat2 = rad(lat2);
  130. double radLng1 = rad(lng1);
  131. double radLng2 = rad(lng2);
  132. d = sin(radLat1)*sin(radLat2)+cos(radLat1)*cos(radLat2)*cos(radLng2-radLng1);
  133. d = sqrt(1-d*d);
  134. d = cos(radLat2)*sin(radLng2-radLng1)/d;
  135. d = Angle(asin(d));
  136. return d;//返回方位角
  137. }
  138. void PushXinbiao(SUT_XINBIAO_PAGE*NewXinbiao)
  139. {
  140. char i=0;
  141. double srcLon=sutGpsInfo.longitue;
  142. double srcLat=sutGpsInfo.latitue;
  143. double destLon=NewXinbiao->xb_longitue;
  144. double destLat=NewXinbiao->xb_latitue;
  145. srcLat/=1000000;
  146. srcLon/=1000000;
  147. destLat/=1000000;
  148. destLon/=1000000;
  149. NewXinbiao->xb_dir=GetBearing(srcLat,srcLon,destLat,destLon);
  150. MSG_INFO(1,"xb_dir=======%d",NewXinbiao->xb_dir);
  151. CalcDistance(NewXinbiao);
  152. for(i=0;i<sut_xinbiao_info.Max_id;i++){
  153. if(NewXinbiao->xb_CallID==sut_xinbiaopage[i].xb_CallID){
  154. NewXinbiao->xb_id=sut_xinbiaopage[i].xb_id;
  155. memcpy(&sut_xinbiaopage[i],NewXinbiao,sizeof(SUT_XINBIAO_PAGE));
  156. return;
  157. }
  158. }
  159. //否,创建新页
  160. sut_xinbiao_info.Max_id++;
  161. NewXinbiao->xb_id=sut_xinbiao_info.Max_id;
  162. memcpy(&sut_xinbiaopage[i],NewXinbiao,sizeof(SUT_XINBIAO_PAGE));
  163. //
  164. }
  165. //
  166. /*
  167. 信标协议 cmd 00 通用
  168. 0a 摇晕 只收不发
  169. a0 解除摇晕
  170. 0b 摇毙 不收不发
  171. b0 解除摇毙
  172. 0c 强制打开GPS
  173. */
  174. //AT+POC=150000 000be4e300
  175. void XinbiaoPackSend(char cmd)
  176. {
  177. char temp[100]={0};
  178. char buf[50];
  179. /*
  180. snprintf(buf,sizeof(buf),"AT+POC=150000%08x%02x",sutPocStatus.LocalGroup.ID);
  181. msgAtSend(buf);
  182. snprintf(buf,sizeof(buf),"%d;%d;%d;%d;%d;",cmd,sutGpsInfo.longitue\
  183. ,sutGpsInfo.latitue,sutGpsInfo.aspect,sutGpsInfo.speed,sutGpsInfo.hight);
  184. */
  185. snprintf(buf,sizeof(buf),"AT+POC=150000%08x%02x",sutPocStatus.LocalGroup.ID,cmd);
  186. msgAtSend(buf);
  187. snprintf(buf,sizeof(buf),"%s;%d;%d;%d;%d;%d;",sutApp.UserInfo.Firm_CallID,sutGpsInfo.longitue\
  188. ,sutGpsInfo.latitue,sutGpsInfo.aspect,sutGpsInfo.speed,sutGpsInfo.hight);
  189. AscStrTurnHexStr(buf,temp);
  190. msgAtSend(temp);
  191. msgAtSend("\r\n");
  192. MSG_INFO(1,temp);
  193. }
  194. //定时发送信标
  195. void Xinbiao_handler(unsigned int interval){
  196. static unsigned short cnt=0;
  197. //char Packdata[100]={0};
  198. if(sutApp.gtMode!=0) return;
  199. if(newPara.Xinbiao_SendMode==0)return;
  200. if(newPara.gpsEnable==0)return;
  201. if(++cnt<((newPara.Xinbiao_Time+1)*5*1000/interval) && gpsCheckCnt==0) return;
  202. cnt=0;
  203. gpsCheckCnt=0;
  204. if(sutGpsInfo.isGpsValid){
  205. XinbiaoPackSend(NULLCMD);
  206. }
  207. }
  208. void ReadXbFile()
  209. {
  210. int fd,i=0;
  211. fd=LSAPI_FS_Open(XINBIAO_FILE, LSAPI_FS_O_RDWR,0);
  212. if(fd<0){
  213. MSG_ERR(1,"%s open read failed", XINBIAO_FILE);
  214. return;
  215. }
  216. LSAPI_FS_Seek(fd, 0L, LSAPI_FS_SEEK_SET);
  217. LSAPI_FS_Read(fd, (unsigned char *)&sut_xinbiaopage, sizeof(SUT_XINBIAO_PAGE)*XINBIAO_NUM);
  218. for(i=0;i<XINBIAO_NUM;i++){
  219. if(sut_xinbiaopage[i].xb_id==0){
  220. if(i!=0)sut_xinbiao_info.Max_id=sut_xinbiaopage[i-1].xb_id;
  221. break;
  222. }
  223. }
  224. }
  225. void WriteXbFile()
  226. {
  227. int fd;
  228. MSG_INFO(1,"%s save start",XINBIAO_FILE);
  229. fd=LSAPI_FS_Open(XINBIAO_FILE, LSAPI_FS_O_RDWR,0x0);
  230. if(fd<0){
  231. MSG_ERR(1,"%s open failed",XINBIAO_FILE);
  232. return;
  233. }
  234. LSAPI_FS_Seek(fd, 0L, LSAPI_FS_SEEK_SET);
  235. LSAPI_FS_Write(fd, (unsigned char *)&sut_xinbiaopage, sizeof(SUT_XINBIAO_PAGE)*XINBIAO_NUM);
  236. LSAPI_FS_Close(fd);
  237. MSG_INFO(1,"%s save done", XINBIAO_FILE);
  238. }
  239. /*******************************************************************************************
  240. 下面为 首长功能
  241. *******************************************************************************************/
  242. signed char Record_Leader_CurIndex(){
  243. unsigned char i;
  244. for(i=0;i<MAX_USER;i++){
  245. if(leader[i].id==0)return i;//找到空白
  246. }
  247. return -1;
  248. }
  249. char Record_Leader_Scanf(int id){
  250. unsigned char i;
  251. for(i=0;i<MAX_USER;i++){
  252. if(leader[i].id==id)break;
  253. if(leader[i].id==0)break;//找到空白
  254. }
  255. if(i==MAX_USER)i=0; //覆盖第一条
  256. return i;
  257. }
  258. void Record_Leader_Status(char type){
  259. unsigned char i;
  260. unsigned char index;
  261. for(i=1;i<MAX_USER;i++){
  262. index=Record_Leader_Scanf(UserArr[i-1].id);
  263. leader[index].id=UserArr[i-1].id;
  264. if(type==0){//摇匀
  265. if(sut_check_box.checkstatus[i]){
  266. leader[index].status|=0x01;
  267. }
  268. else {
  269. leader[index].status &=~(1<<0);
  270. }
  271. }else if(type==1){
  272. if(sut_check_box.checkstatus[i]){
  273. leader[index].status|=0x02;
  274. }
  275. else {
  276. leader[index].status &=~(1<<1);
  277. }
  278. }else if(type==2){
  279. if(sut_check_box.checkstatus[i]){
  280. leader[index].status|=0x04;
  281. }
  282. else {
  283. leader[index].status &=~(1<<2);
  284. }
  285. }
  286. }
  287. }
  288. // ID cmd ;
  289. //+POC:8C00 934C0008 00 3B
  290. //+POC:8C00 72F4000A A0 72F40009 72F40003 72F40002 72F4000872F4000472F4000772F4000672F4000172F40005
  291. void LeaderRecv_handler(char*msg,char type)
  292. {
  293. char i,len=0;
  294. char start=0;
  295. char *Pmsg=0;
  296. int id=0;
  297. char data[250];
  298. char buf[9]={0};
  299. Pmsg=msg+19;
  300. len =strlen(Pmsg);
  301. for(i=0;i<len;i+=8){
  302. memcpy(buf,Pmsg+start,8);
  303. id=htoi(buf);
  304. MSG_INFO(1,"id======%08x",id);
  305. MSG_INFO(1,"LocalUser.ID======%08x",sutPocStatus.LocalUser.ID);
  306. if(id==sutPocStatus.LocalUser.ID){
  307. sutPocStatus.LeaderCmd|=type;
  308. MSG_INFO(1,"sutPocStatus.LeaderCmd===%d",sutPocStatus.LeaderCmd);
  309. if(type==0x04){
  310. newPara.gpsEnable=1;
  311. sysIniSave("location");
  312. GpsTimeUpdate();
  313. }
  314. if(sutPocStatus.spk)msgAtSend("AT+POC=0C0000\r\n");//说话状态释放话语权
  315. break;
  316. }
  317. start=i;
  318. }
  319. if(i==len){
  320. if(type==1&&(sutPocStatus.LeaderCmd&0x01)){
  321. sutPocStatus.LeaderCmd &=~(1<<0);
  322. }else if(type==2&&(sutPocStatus.LeaderCmd&0x02)){
  323. sutPocStatus.LeaderCmd &=~(1<<1);
  324. }else if(type==4&&(sutPocStatus.LeaderCmd&0x04)){
  325. sutPocStatus.LeaderCmd &=~(1<<2);
  326. MSG_INFO(1,"jiechu GPS");
  327. }
  328. }
  329. }
  330. void LeaderPackSend(char cmd,char type)
  331. {
  332. char temp[200]={0};
  333. char buf[10];
  334. int i,j=0;
  335. int max;
  336. max=Record_Leader_CurIndex();
  337. if(max==-1)return;
  338. snprintf(temp,sizeof(temp),"AT+POC=150000%08x%02x",sutPocStatus.LocalGroup.ID,cmd);
  339. for(i=0;i<max;i++){
  340. //if(leader[i].status=0){
  341. if((type==0&&(leader[i].status&0x01))||(type==1&&(leader[i].status&0x02))||(type==2&&(leader[i].status&0x04))){
  342. snprintf(buf,sizeof(buf),"%08x",leader[i].id);
  343. strcat(temp,buf);
  344. j=1;
  345. }
  346. }
  347. if(j==0){
  348. if(cmd==YAOYUN)strcat(temp,DISABLE_YAOYUN);
  349. else if(cmd==YAOBI)strcat(temp,DISABLE_YAOBI);
  350. else if(cmd==GPSALLON)strcat(temp,DISABLE_GPS);
  351. }
  352. msgAtSend(temp);
  353. msgAtSend("\r\n");
  354. MSG_INFO(1,temp);
  355. }
  356. //LEADER leader[XINBIAO_NUM];
  357. void ReadLeaderFile()
  358. {
  359. int fd,i=0;
  360. fd=LSAPI_FS_Open(LEADER_FILE, LSAPI_FS_O_RDWR,0);
  361. if(fd<0){
  362. MSG_ERR(1,"%s open read failed", LEADER_FILE);
  363. return;
  364. }
  365. LSAPI_FS_Seek(fd, 0L, LSAPI_FS_SEEK_SET);
  366. LSAPI_FS_Read(fd, (unsigned char *)&leader, sizeof(LEADER)*XINBIAO_NUM);
  367. }
  368. void WriteLeaderFile()
  369. {
  370. int fd;
  371. MSG_INFO(1,"%s save start",LEADER_FILE);
  372. fd=LSAPI_FS_Open(LEADER_FILE, LSAPI_FS_O_RDWR,0x0);
  373. if(fd<0){
  374. MSG_ERR(1,"%s open failed",LEADER_FILE);
  375. return;
  376. }
  377. LSAPI_FS_Seek(fd, 0L, LSAPI_FS_SEEK_SET);
  378. LSAPI_FS_Write(fd, (unsigned char *)&leader, sizeof(LEADER)*XINBIAO_NUM);
  379. LSAPI_FS_Close(fd);
  380. MSG_INFO(1,"%s save done", LEADER_FILE);
  381. }