TVehicleOnlineInfoService.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class TVehicleOnlineInfoService extends Service {
  4. async online({ vin }) {
  5. const { ctx } = this;
  6. let result;
  7. if (vin) {
  8. result = await ctx.model.TVehicleOnlineInfoModel.find(
  9. { _id: vin, online_status: 1, longitude: { $exists: true }, latitude: { $exists: true } },
  10. { longitude: 1, latitude: 1 });
  11. } else {
  12. result = await ctx.model.TVehicleOnlineInfoModel.find(
  13. { online_status: 1, longitude: { $exists: true }, latitude: { $exists: true } },
  14. { longitude: 1, latitude: 1 })
  15. .batchSize(100000);
  16. }
  17. return result;
  18. }
  19. async onlineCount() {
  20. const { ctx } = this;
  21. const result = await ctx.model.TVehicleOnlineInfoModel.find(
  22. { online_status: 1 })
  23. .countDocuments();
  24. const offlineCount = await ctx.model.TVehicleOnlineInfoModel.find(
  25. { online_status: 0 })
  26. .countDocuments();
  27. const saledCount = await ctx.model.TVehicleRecordModel.find(
  28. { is_saled_car: 1, user_id: { $ne: null } })
  29. .countDocuments();
  30. return { count: result, offlineCount, saledCount };
  31. }
  32. async offline({ vin }) {
  33. const { ctx } = this;
  34. let result;
  35. if (vin) {
  36. result = await ctx.model.TVehicleOnlineInfoModel.find(
  37. { _id: vin, online_status: 0, longitude: { $exists: true }, latitude: { $exists: true } },
  38. { longitude: 1, latitude: 1 });
  39. } else {
  40. result = await ctx.model.TVehicleOnlineInfoModel.find(
  41. { online_status: 0, longitude: { $exists: true }, latitude: { $exists: true } },
  42. { longitude: 1, latitude: 1 })
  43. .batchSize(100000);
  44. }
  45. return result;
  46. }
  47. async liveStatus({ vin, longitude, latitude }) {
  48. const { service } = this;
  49. let online_status = await service.redisService.hget(vin, 'ONLINE_STATUS')[1];
  50. if (online_status == undefined) {
  51. online_status = -1;
  52. }
  53. const address = await service.redisService.getAddress(longitude, latitude);
  54. const carArr = [];
  55. const running_status = await service.redisService.hget(vin, 'RUNNING_STATUS')[1];
  56. switch (running_status) {
  57. case '0':
  58. carArr.push('下电');
  59. break;
  60. case '1':
  61. carArr.push('上电');
  62. break;
  63. case '2':
  64. carArr.push('怠速');
  65. break;
  66. case '3':
  67. carArr.push('行驶');
  68. break;
  69. default:
  70. break;
  71. }
  72. const network_status = await service.redisService.hget(vin, 'NETWORK_STATUS')[1];
  73. if (network_status == 1) {
  74. carArr.push('网络异常');
  75. }
  76. const failure_status = await service.redisService.hget(vin, 'FAILURE_STATUS')[1];
  77. if (failure_status == 1) {
  78. carArr.push('整车故障');
  79. }
  80. const car = carArr.join(',');
  81. // const electric_status = await service.redisService.hget(vin, 'ELECTRIC_STATUS')[1];
  82. const behaviorArr = [];
  83. const dsm_status = await service.redisService.hget(vin, 'ANALYSIS_DSM_STATUS');
  84. if (dsm_status == 1) {
  85. behaviorArr.push('长时间行驶');
  86. }
  87. const speeding_status = await service.redisService.hget(vin, 'ANALYSIS_SPEEDING_STATUS');
  88. if (speeding_status == 1) {
  89. behaviorArr.push('超速');
  90. }
  91. const un_tie_sb_status = await service.redisService.hget(vin, 'ANALYSIS_UN_TIE_SB_STATUS');
  92. if (un_tie_sb_status == 1) {
  93. behaviorArr.push('未系安全带');
  94. }
  95. const leave_sw_status = await service.redisService.hget(vin, 'ANALYSIS_LEAVE_SW_STATUS');
  96. if (leave_sw_status == 1) {
  97. behaviorArr.push('双手离开方向盘时间过长');
  98. }
  99. const acceleration_status = await service.redisService.hget(vin, 'ANALYSIS_ACCELERATION_STATUS');
  100. if (acceleration_status == 1) {
  101. behaviorArr.push('急加速');
  102. }
  103. const decelerate_status = await service.redisService.hget(vin, 'ANALYSIS_DECELERATE_STATUS');
  104. if (decelerate_status == 1) {
  105. behaviorArr.push('急减速');
  106. }
  107. const turn_status = await service.redisService.hget(vin, 'ANALYSIS_TURN_STATUS');
  108. if (turn_status == 1) {
  109. behaviorArr.push('急转弯');
  110. }
  111. const clu_status = await service.redisService.hget(vin, 'ANALYSIS_CLU_STATUS');
  112. if (clu_status == 1) {
  113. behaviorArr.push('急换道');
  114. }
  115. const behavior = behaviorArr.join(',');
  116. const dangerArr = [];
  117. const safety_air_bag_status = await service.redisService.hget(vin, 'ANALYSIS_SAFETY_AIR_BAG_STATUS');
  118. const fault_status = await service.redisService.hget(vin, 'ANALYSIS_FAULT_STATUS');
  119. const lka_status = await service.redisService.hget(vin, 'ANALYSIS_LKA_STATUS');
  120. const ldw_status = await service.redisService.hget(vin, 'ANALYSIS_LDW_STATUS');
  121. const bsd_status = await service.redisService.hget(vin, 'ANALYSIS_BSD_STATUS');
  122. const fcw_status = await service.redisService.hget(vin, 'ANALYSIS_FCW_STATUS');
  123. if (lka_status == 1 || ldw_status == 1 || bsd_status == 1 || fcw_status == 1) {
  124. dangerArr.push('ADAS危险事件报警');
  125. }
  126. if (safety_air_bag_status == 1) {
  127. dangerArr.push('安全气囊弹出');
  128. }
  129. if (fault_status == 1) {
  130. dangerArr.push('带故障行驶');
  131. }
  132. const danger = dangerArr.join(',');
  133. const v_parking = await service.redisService.hget(vin, '_S0210');// 第一个数值 [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 ]
  134. const acc = await service.redisService.hget(vin, '_S0508');
  135. let idriving_status = '手动驾驶状态';
  136. const v_dict = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 ];
  137. if ((acc && acc.length > 0) || (v_parking && v_parking.length > 0 && v_dict.indexOf(v_parking[1]) != -1)) {
  138. idriving_status = '自动驾驶状态';
  139. }
  140. const traffic = await service.redisService.hget(vin, '_S0558')[2];// 第二个数值 0:无效;1:严重拥堵;2:中度拥堵;3:轻度拥堵;4:畅通拥堵;5:高速行驶
  141. let event0818 = {};
  142. const eventResult = await service.redisService.hget(vin, 'EVENT0818');
  143. if (eventResult) {
  144. event0818 = JSON.parse(eventResult);
  145. }
  146. const set = new Set();
  147. Object.keys(event0818).forEach(item => {
  148. if (this.ctx.helper.faultDict[item] && event0818[item][1] == 1) {
  149. set.add(this.ctx.helper.faultDict[item]);
  150. }
  151. });
  152. const event = Array.from(set).join(',');
  153. // 取event里_S0100 - _S0169 的对应值 0基本是正常,具体看文档 online_status有0的可能吗
  154. return { online_status, car, behavior: behavior || '无', danger: danger || '无',
  155. address, idriving_status, traffic: traffic || '-1', event: event || '无' };
  156. }
  157. async travel(param) {
  158. const { service } = this;
  159. const result = await service.otherService.travel(param);
  160. const array = result.filter(item => {
  161. const gpsInfo = JSON.parse(item.gpsInfo);
  162. return gpsInfo.length == 9 && gpsInfo[5] != 0 && gpsInfo[7] != 0 && gpsInfo[8] == 1 && gpsInfo[0] == 0
  163. && (gpsInfo[6] == 0 || gpsInfo[6] == 1) && (gpsInfo[8] == 0 || gpsInfo[8] == 1);
  164. }).map(item => {
  165. const gpsInfo = JSON.parse(item.gpsInfo);
  166. return {
  167. latitude: gpsInfo[5],
  168. longitude: gpsInfo[7],
  169. ag: gpsInfo[1],
  170. sp: 10,
  171. time: this.ctx.helper.parseS(gpsInfo[2]) || 0,
  172. };
  173. }).reverse();
  174. const number = parseInt(array.length / 250) || 1;
  175. let firstTime = 0;
  176. return array.filter((item, index) => index % number == 0).map((item, index) => {
  177. if (index == 0) {
  178. item.tm = item.time;
  179. } else {
  180. item.tm = (item.time - firstTime > 299 ? 299 : item.time - firstTime);
  181. }
  182. firstTime = item.time;
  183. return item;
  184. });
  185. }
  186. }
  187. module.exports = TVehicleOnlineInfoService;