123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- 'use strict';
- const Service = require('egg').Service;
- class TVehicleOnlineInfoService extends Service {
- async online({ vin }) {
- const { ctx } = this;
- let result;
- if (vin) {
- result = await ctx.model.TVehicleOnlineInfoModel.find(
- { _id: vin, online_status: 1, longitude: { $exists: true }, latitude: { $exists: true } },
- { longitude: 1, latitude: 1 });
- } else {
- result = await ctx.model.TVehicleOnlineInfoModel.find(
- { online_status: 1, longitude: { $exists: true }, latitude: { $exists: true } },
- { longitude: 1, latitude: 1 })
- .batchSize(100000);
- }
- return result;
- }
- async onlineCount() {
- const { ctx } = this;
- const result = await ctx.model.TVehicleOnlineInfoModel.find(
- { online_status: 1 })
- .countDocuments();
- const offlineCount = await ctx.model.TVehicleOnlineInfoModel.find(
- { online_status: 0 })
- .countDocuments();
- const saledCount = await ctx.model.TVehicleRecordModel.find(
- { is_saled_car: 1, user_id: { $ne: null } })
- .countDocuments();
- return { count: result, offlineCount, saledCount };
- }
- async offline({ vin }) {
- const { ctx } = this;
- let result;
- if (vin) {
- result = await ctx.model.TVehicleOnlineInfoModel.find(
- { _id: vin, online_status: 0, longitude: { $exists: true }, latitude: { $exists: true } },
- { longitude: 1, latitude: 1 });
- } else {
- result = await ctx.model.TVehicleOnlineInfoModel.find(
- { online_status: 0, longitude: { $exists: true }, latitude: { $exists: true } },
- { longitude: 1, latitude: 1 })
- .batchSize(100000);
- }
- return result;
- }
- async liveStatus({ vin, longitude, latitude }) {
- const { service } = this;
- let online_status = await service.redisService.hget(vin, 'ONLINE_STATUS')[1];
- if (online_status == undefined) {
- online_status = -1;
- }
- const address = await service.redisService.getAddress(longitude, latitude);
- const carArr = [];
- const running_status = await service.redisService.hget(vin, 'RUNNING_STATUS')[1];
- switch (running_status) {
- case '0':
- carArr.push('下电');
- break;
- case '1':
- carArr.push('上电');
- break;
- case '2':
- carArr.push('怠速');
- break;
- case '3':
- carArr.push('行驶');
- break;
- default:
- break;
- }
- const network_status = await service.redisService.hget(vin, 'NETWORK_STATUS')[1];
- if (network_status == 1) {
- carArr.push('网络异常');
- }
- const failure_status = await service.redisService.hget(vin, 'FAILURE_STATUS')[1];
- if (failure_status == 1) {
- carArr.push('整车故障');
- }
- const car = carArr.join(',');
- // const electric_status = await service.redisService.hget(vin, 'ELECTRIC_STATUS')[1];
- const behaviorArr = [];
- const dsm_status = await service.redisService.hget(vin, 'ANALYSIS_DSM_STATUS');
- if (dsm_status == 1) {
- behaviorArr.push('长时间行驶');
- }
- const speeding_status = await service.redisService.hget(vin, 'ANALYSIS_SPEEDING_STATUS');
- if (speeding_status == 1) {
- behaviorArr.push('超速');
- }
- const un_tie_sb_status = await service.redisService.hget(vin, 'ANALYSIS_UN_TIE_SB_STATUS');
- if (un_tie_sb_status == 1) {
- behaviorArr.push('未系安全带');
- }
- const leave_sw_status = await service.redisService.hget(vin, 'ANALYSIS_LEAVE_SW_STATUS');
- if (leave_sw_status == 1) {
- behaviorArr.push('双手离开方向盘时间过长');
- }
- const acceleration_status = await service.redisService.hget(vin, 'ANALYSIS_ACCELERATION_STATUS');
- if (acceleration_status == 1) {
- behaviorArr.push('急加速');
- }
- const decelerate_status = await service.redisService.hget(vin, 'ANALYSIS_DECELERATE_STATUS');
- if (decelerate_status == 1) {
- behaviorArr.push('急减速');
- }
- const turn_status = await service.redisService.hget(vin, 'ANALYSIS_TURN_STATUS');
- if (turn_status == 1) {
- behaviorArr.push('急转弯');
- }
- const clu_status = await service.redisService.hget(vin, 'ANALYSIS_CLU_STATUS');
- if (clu_status == 1) {
- behaviorArr.push('急换道');
- }
- const behavior = behaviorArr.join(',');
- const dangerArr = [];
- const safety_air_bag_status = await service.redisService.hget(vin, 'ANALYSIS_SAFETY_AIR_BAG_STATUS');
- const fault_status = await service.redisService.hget(vin, 'ANALYSIS_FAULT_STATUS');
- const lka_status = await service.redisService.hget(vin, 'ANALYSIS_LKA_STATUS');
- const ldw_status = await service.redisService.hget(vin, 'ANALYSIS_LDW_STATUS');
- const bsd_status = await service.redisService.hget(vin, 'ANALYSIS_BSD_STATUS');
- const fcw_status = await service.redisService.hget(vin, 'ANALYSIS_FCW_STATUS');
- if (lka_status == 1 || ldw_status == 1 || bsd_status == 1 || fcw_status == 1) {
- dangerArr.push('ADAS危险事件报警');
- }
- if (safety_air_bag_status == 1) {
- dangerArr.push('安全气囊弹出');
- }
- if (fault_status == 1) {
- dangerArr.push('带故障行驶');
- }
- const danger = dangerArr.join(',');
- 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 ]
- const acc = await service.redisService.hget(vin, '_S0508');
- let idriving_status = '手动驾驶状态';
- const v_dict = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 ];
- if ((acc && acc.length > 0) || (v_parking && v_parking.length > 0 && v_dict.indexOf(v_parking[1]) != -1)) {
- idriving_status = '自动驾驶状态';
- }
- const traffic = await service.redisService.hget(vin, '_S0558')[2];// 第二个数值 0:无效;1:严重拥堵;2:中度拥堵;3:轻度拥堵;4:畅通拥堵;5:高速行驶
- let event0818 = {};
- const eventResult = await service.redisService.hget(vin, 'EVENT0818');
- if (eventResult) {
- event0818 = JSON.parse(eventResult);
- }
- const set = new Set();
- Object.keys(event0818).forEach(item => {
- if (this.ctx.helper.faultDict[item] && event0818[item][1] == 1) {
- set.add(this.ctx.helper.faultDict[item]);
- }
- });
- const event = Array.from(set).join(',');
- // 取event里_S0100 - _S0169 的对应值 0基本是正常,具体看文档 online_status有0的可能吗
- return { online_status, car, behavior: behavior || '无', danger: danger || '无',
- address, idriving_status, traffic: traffic || '-1', event: event || '无' };
- }
- async travel(param) {
- const { service } = this;
- const result = await service.otherService.travel(param);
- const array = result.filter(item => {
- const gpsInfo = JSON.parse(item.gpsInfo);
- return gpsInfo.length == 9 && gpsInfo[5] != 0 && gpsInfo[7] != 0 && gpsInfo[8] == 1 && gpsInfo[0] == 0
- && (gpsInfo[6] == 0 || gpsInfo[6] == 1) && (gpsInfo[8] == 0 || gpsInfo[8] == 1);
- }).map(item => {
- const gpsInfo = JSON.parse(item.gpsInfo);
- return {
- latitude: gpsInfo[5],
- longitude: gpsInfo[7],
- ag: gpsInfo[1],
- sp: 10,
- time: this.ctx.helper.parseS(gpsInfo[2]) || 0,
- };
- }).reverse();
- const number = parseInt(array.length / 250) || 1;
- let firstTime = 0;
- return array.filter((item, index) => index % number == 0).map((item, index) => {
- if (index == 0) {
- item.tm = item.time;
- } else {
- item.tm = (item.time - firstTime > 299 ? 299 : item.time - firstTime);
- }
- firstTime = item.time;
- return item;
- });
- }
- }
- module.exports = TVehicleOnlineInfoService;
|