import api from '@/api'
import APIUrls from '@/config/simpliciti-apis.js'
import circuitService from '@/services/circuit-service'
function getNewState() {
return {
categoriesCircuit: [],
sous_categoriesCircuit: [],
// responseApi: {
// couleur: "#DC143C",
// linestring:
// "1.89254 47.90834,1.89249 47.90836,1.89202 47.90719,1.89173 47.90867,1.89178 47.90865,1.89249 47.90836,1.89339 47.90799,1.89339 47.90799,1.89342 47.90798,1.89338 47.90793,1.89338 47.90789,1.89344 47.90766,1.8935 47.90758,1.89356 47.90754,1.89356 47.90754,1.89358 47.90753,1.89391 47.90754,1.89422 47.90762,1.89422 47.90762,1.89492 47.90734,1.89716 47.90647,1.89825 47.90603,1.89246 47.9083,1.89249 47.90836,1.89181 47.90864,1.89109 47.90762,1.89104 47.90755,1.89088 47.90731,1.89056 47.90685,1.89016 47.90626,1.89335 47.9046,1.89339 47.90464,1.89243 47.90522,1.89201 47.90544,1.89201 47.90544,1.89194 47.90548,1.89138 47.90479,1.89124 47.90479,1.89135 47.90475,1.8919 47.90544,1.8919 47.90544,1.89194 47.90548,1.89132 47.90572,1.89091 47.90588,1.89014 47.90619,1.89241 47.90353,1.89245 47.90358,1.89126 47.90449,1.89126 47.90449,1.89119 47.90454,1.89088 47.90416,1.89065 47.90386,1.89065 47.90386,1.89063 47.90384,1.89216 47.90329,1.89216 47.90329,1.89223 47.90327,1.89243 47.90355,1.8903 47.90612,1.89012 47.9062,1.89056 47.90685,1.89088 47.90731,1.891 47.90749,1.89827 47.90602,1.89716 47.90647,1.89716 47.90647,1.89497 47.90732,1.89497 47.90732,1.89492 47.90734,1.89451 47.90686,1.89442 47.90675,1.89404 47.90631,1.89404 47.90631,1.89401 47.90627,1.89296 47.90669,1.89299 47.90668,1.89292 47.90671,1.89203 47.9071,1.89203 47.9071,1.89199 47.90712,1.89246 47.9083,1.89354 47.90749,1.89358 47.90753,1.89391 47.90754,1.89418 47.90761,1.89418 47.90761,1.89422 47.90762,1.89432 47.90776,1.89433 47.90788,1.89429 47.90795,1.89419 47.90803,1.89405 47.90809,1.89353 47.90801,1.89345 47.90799,1.89345 47.90799,1.89342 47.90798,1.89254 47.90834,1.89107 47.90753,1.89104 47.90755,1.89141 47.9081,1.89161 47.9084,1.89175 47.90861,1.89175 47.90861,1.89178 47.90865,1.89023 47.90925,1.89023 47.90925,1.89012 47.90929,1.88987 47.90874,1.88978 47.90866,1.88976 47.90866,1.88976 47.90866,1.8898250294304 47.908536367488,1.8898250294304 47.908536367488,1.88978 47.90866,1.88978 47.90866,1.88987 47.90874,1.89011 47.90927,1.89011 47.90927,1.89012 47.90929,1.89173 47.90867,1.89181 47.90864,1.89178 47.90865,1.89161 47.9084,1.89141 47.9081,1.89109 47.90762,1.89203 47.90543,1.89194 47.90548,1.89211 47.90569,1.89289 47.90668,1.8901 47.90621,1.89012 47.9062,1.88934 47.90538,1.88934 47.90538,1.8893 47.90533,1.88866 47.90547,1.88794 47.90568,1.88768 47.90574,1.88708 47.90586,1.88708 47.90586,1.88702 47.90587,1.88693 47.90564,1.88693 47.90564,1.887 47.90581,1.887 47.90581,1.88702 47.90587,1.88768 47.90574,1.88794 47.90568,1.88866 47.90547,1.88927 47.90534,1.88927 47.90534,1.8893 47.90533,1.89032 47.90512,1.89124 47.90479,1.89138 47.90479,1.89135 47.90475,1.89199 47.90453,1.89282 47.90423,1.89282 47.90423,1.89294 47.90419,1.89331 47.90456,1.89331 47.90456,1.89339 47.90464,1.89243 47.90522,1.89203 47.90543,1.89296 47.90669,1.89292 47.90671,1.89354 47.90749,1.89289 47.90668,1.89292 47.90671,1.89353 47.90747,1.89353 47.90747,1.89358 47.90753,1.89391 47.90754,1.89422 47.90762,1.89422 47.90762,1.89492 47.90734,1.89716 47.90647,1.89824 47.90603,1.89824 47.90603,1.8983 47.90601,1.89794 47.90579,1.89674 47.90509,1.89661 47.90517,1.8966 47.90518,1.89692 47.90573,1.89712 47.90635,1.89712 47.90635,1.89716 47.90647,1.89821 47.90605,1.89821 47.90605,1.8983 47.90601,1.89794 47.90579,1.89675 47.9051,1.89675 47.9051,1.89672 47.90508,1.8966 47.90518,1.89621 47.90538,1.89411 47.90623,1.89411 47.90623,1.89401 47.90627,1.89299 47.90668,1.89825 47.90603,1.8983 47.90601,1.89794 47.90579,1.89675 47.9051,1.89675 47.9051,1.89672 47.90508,1.89661 47.90517,1.89674 47.90509,1.89672 47.90508,1.89661 47.90501,1.89616 47.90472,1.89574 47.90453,1.89517 47.9042,1.89497 47.90408,1.89469 47.90392,1.89469 47.90392,1.89462 47.90388,1.89406 47.90422,1.8934 47.90463,1.8934 47.90463,1.89339 47.90464,1.89243 47.90522,1.89199 47.90545,1.89199 47.90545,1.89194 47.90548,1.89132 47.90572,1.89091 47.90588,1.89021 47.90616,1.89021 47.90616,1.89012 47.9062,1.88922 47.90658,1.88881 47.90675,1.88843 47.90692,1.88824 47.90701,1.8876 47.9073,1.88602 47.90805,1.88507 47.90846,1.88448 47.9087,1.88393 47.90892,1.88389 47.90893,1.88389 47.90893,1.88387 47.90894,1.88381 47.909,1.8834 47.90917,1.88254 47.90954,1.88193 47.9098,1.8819 47.90981,1.8819 47.90981,1.8819126973978 47.909915234534,1.8819126973978 47.909915234534,1.8815157304636 47.91008063922,1.8815157304636 47.91008063922,1.8814 47.90993,1.8814 47.90993,1.88161 47.90985,1.88165 47.90983,1.88165 47.90983,1.88247 47.90949,1.88247 47.90949,1.88251 47.90947,1.88254 47.90954,1.88272 47.90981,1.88281 47.91011,1.88249 47.91081,1.88238 47.91092,1.88226 47.91097,1.88151 47.91111,1.88151 47.91111,1.88141 47.91113,1.88122 47.91066,1.88104 47.91025,1.88104 47.91025,1.88104 47.91024,1.88101 47.9102,1.88095 47.91012,1.88131 47.90997,1.88161 47.90985,1.88247 47.90948,1.88247 47.90948,1.88251 47.90947,1.88335 47.90912,1.88384 47.90895,1.88384 47.90895,1.88387 47.90894,1.88393 47.90892,1.88448 47.9087,1.88507 47.90846,1.88602 47.90805,1.8876 47.9073,1.88824 47.90701,1.88843 47.90692,1.88881 47.90675,1.88922 47.90658,1.8901 47.90621,1.89016 47.90626,1.89012 47.9062,1.89091 47.90588,1.89132 47.90572,1.89194 47.90548,1.89243 47.90522,1.89339 47.90464,1.89406 47.90422,1.89458 47.9039,1.89458 47.9039,1.89462 47.90388,1.89451 47.90379,1.89386 47.90303,1.89384 47.90301,1.89384 47.90301,1.89349 47.90261,1.89349 47.90261,1.89346 47.90257,1.89299 47.90273,1.89214 47.903,1.89214 47.903,1.89209 47.90302,1.89223 47.90327,1.89245 47.90358,1.89254 47.9037,1.89294 47.90419,1.89335 47.9046,1.89243 47.90355,1.89245 47.90358,1.89252 47.90352,1.8927 47.90344,1.89333 47.90321,1.89375 47.90307,1.89375 47.90307,1.89386 47.90303,1.89349 47.90261,1.89349 47.90261,1.89346 47.90257,1.89299 47.90273,1.89221 47.90298,1.892 47.90301,1.89209 47.90302,1.89205 47.90295,1.89218 47.9022,1.89236 47.9014,1.89236 47.9014,1.89239 47.90128,1.8922 47.90126,1.89143 47.90118,1.89003 47.90104,1.88959 47.90099,1.88837 47.90085,1.88836 47.90087,1.88836 47.90087,1.88833 47.90095,1.88929 47.90213,1.88935 47.90221,1.88989 47.90287,1.88998 47.90299,1.89001 47.90302,1.88814 47.90303,1.88814 47.90303,1.88806 47.90303,1.88774 47.90224,1.88774 47.90225,1.88772 47.90219,1.88931 47.90221,1.88925 47.90209,1.8895924514263 47.901004281534,1.8895924514263 47.901004281534,1.8884 47.90085,1.8884 47.90085,1.88837 47.90085,1.88833 47.90095,1.88929 47.90213,1.88932 47.90217,1.88932 47.90217,1.88935 47.90221,1.88998 47.90298,1.88997 47.90297,1.89001 47.90302,1.8906 47.9038,1.8906 47.9038,1.89063 47.90384,1.89215 47.9033,1.89215 47.9033,1.89223 47.90327,1.89241 47.90353,1.88998 47.90298,1.89001 47.90302,1.89196 47.903,1.892 47.90301,1.88989 47.90287,1.89001 47.90302,1.89186 47.903,1.89186 47.903,1.89196 47.903,1.89209 47.90302,1.89205 47.90295,1.89217 47.90226,1.89217 47.90226,1.89218 47.9022,1.88938 47.90221,1.88938 47.90221,1.88935 47.90221,1.88997 47.90297,1.88931 47.90221,1.88935 47.90221,1.88998 47.90299,1.89221 47.90298,1.89209 47.90302,1.89223 47.90327,1.89245 47.90358,1.89254 47.9037,1.89294 47.90419,1.89333 47.90458,1.89333 47.90458,1.89339 47.90464,1.89243 47.90522,1.89194 47.90548,1.89132 47.90572,1.89091 47.90588,1.8903 47.90612,1.89014 47.90619,1.89012 47.9062,1.88932 47.90535,1.88932 47.90535,1.8893 47.90533,1.88877 47.90448,1.8885 47.904,1.88838 47.9038,1.88806 47.90303,1.88774 47.90225,1.88774 47.90224,1.88772 47.90219,1.88819 47.90121,1.88831 47.90099,1.88831 47.90099,1.88833 47.90095,1.88925 47.90209,1.89202 47.90719,1.89199 47.90712,1.89107 47.90753,1.891 47.90749,1.89104 47.90755,1.89141 47.9081,1.89161 47.9084,1.89177 47.90863,1.89177 47.90863,1.89178 47.90865,1.89015 47.90928,1.89015 47.90928,1.89012 47.90929,1.88987 47.90874,1.88978 47.90866,1.88972 47.90865,1.88972 47.90865,1.88966 47.90864,1.88957 47.90865,1.88882 47.9088,1.88835 47.90895,1.88811 47.909,1.8873 47.90914,1.8873 47.90914,1.88722 47.90916,1.88738 47.90944,1.88758 47.90984,1.88795 47.91061,1.88796 47.91064,1.88796 47.91064,1.88798 47.91067,1.88898 47.91045,1.8894 47.91036,1.89037 47.91011,1.89037 47.91011,1.8905 47.91008,1.89015 47.90936,1.89015 47.90936,1.89012 47.90929,1.88987 47.90874,1.88978 47.90866,1.88966 47.90864,1.88957 47.90865,1.88882 47.9088,1.88835 47.90895,1.88811 47.909,1.8873 47.90915,1.8873 47.90915,1.88722 47.90916,1.88657 47.90928,1.88639 47.90932,1.88639 47.90932,1.88635 47.90932",
// },
circuitId: null,
circuitConsumption: [],
}
}
/**
* Renvoie les
* @param steps
* @returns {*[]}
*/
function consoRange(steps) {
// Prendre des range à 10% de la moyenne
let conso = []
let dividende = 0
let diviseur = 1
for (let i = 0; i < steps.length; i++) {
conso[i] = steps[i].consommation_moyenne
dividende += parseInt(steps[i].consommation_moyenne * steps[i].distance)
diviseur += parseInt(steps[i].distance)
}
let moyenne = Math.floor((10 * dividende) / diviseur) / 10
let step = 1
if (moyenne != 0) {
step = Math.round(moyenne / 10)
}
let res = []
for (let j = 0; j < 5; j++) {
res[j] = Math.floor(moyenne + step * j)
}
return res
}
/**
* Get linestring color for given conso.
* @param conso
* @param panel
* @returns {string}
* @author FMA 2021-10-20 https://easyredmine.simpliciti.fr/issues/27249
*/
function pickColor(conso, panel) {
let res = '#82b021'
let colors = ['#82b021', '#c2da32', '#f3d81e', '#ff9d0a', '#ff3203']
let i = 0
if (conso <= panel[0]) {
res = colors[0]
} else {
for (const v of panel) {
if (conso > v) {
res = colors[i]
} else {
break
}
i++
}
}
return res
}
/**
* Format a linestring for LPolyline
* @param linestring
* @returns {*[]}
* @author FMA 2021-10-20 https://easyredmine.simpliciti.fr/issues/27249
*/
function formatLinestring(linestring, color) {
let res = []
let minmax = { latmin: 90, latmax: -90, lngmin: 180, lngmax: -180 }
let coords = []
let coordsInverse = linestring.split(',')
let arrayCoordsInverse = []
for (let i = 0; i < coordsInverse.length; i++) {
arrayCoordsInverse[i] = coordsInverse[i].split(' ')
coords.push({
lat: arrayCoordsInverse[i][1],
lng: arrayCoordsInverse[i][0],
})
}
for (let i = 0; i < coords.length - 2; i++) {
res.push({
color: color,
startLat: coords[i]['lat'],
endLat: coords[i + 1]['lat'],
startLng: coords[i]['lng'],
endLng: coords[i + 1]['lng'],
})
if (coords[i]['lat'] > minmax['latmax']) {
minmax['latmax'] = coords[i]['lat']
}
if (coords[i]['lat'] < minmax['latmin']) {
minmax['latmin'] = coords[i]['lat']
}
if (coords[i]['lng'] > minmax['lngmax']) {
minmax['lngmax'] = coords[i]['lng']
}
if (coords[i]['lng'] < minmax['lngmin']) {
minmax['lngmin'] = coords[i]['lng']
}
}
return { coords: res, minmax: minmax }
}
/**
* Group and compil multiples polylines in only one polyline
* @param state
* @returns {{minmax: {lngmin: number, lngmax: number, latmin: number, latmax: number}, coords: *[]}}
* @author FMA 2021-10-20 https://easyredmine.simpliciti.fr/issues/27249
*/
function compiledLineStrings(state) {
let color = ''
let consoRange1 = consoRange(state.circuitConsumption)
let allCoords = []
let allMinmax = { latmin: 90, latmax: -90, lngmin: 180, lngmax: -180 }
let formatedLineString = []
for (let i = 0; i < state.circuitConsumption.length; i++) {
color = pickColor(
state.circuitConsumption[i].consommation_moyenne,
consoRange1
)
formatedLineString[i] = formatLinestring(
state.circuitConsumption[i].linestring,
color
)
// compilation
for (let j = 0; j < formatedLineString[i].coords.length; j++) {
// raccordement des tronçons entre eux
if (j == 0 && typeof formatedLineString[i - 1] !== 'undefined') {
let x = formatedLineString[i - 1].coords.length - 1
let lastcoords = formatedLineString[i - 1].coords[x]
if (typeof lastcoords !== 'undefined') {
let raccordement = {
color: color,
startLat: lastcoords['endLat'],
endLat: formatedLineString[i].coords[j]['startLat'],
startLng: lastcoords['endLng'],
endLng: formatedLineString[i].coords[j]['startLng'],
}
allCoords.push(raccordement)
}
}
allCoords.push(formatedLineString[i].coords[j])
}
// calcul des minimum et maximum
if (formatedLineString[i].minmax.latmax > allMinmax['latmax']) {
allMinmax['latmax'] = formatedLineString[i].minmax.latmax
}
if (formatedLineString[i].minmax.latmin < allMinmax['latmin']) {
allMinmax['latmin'] = formatedLineString[i].minmax.latmin
}
if (formatedLineString[i].minmax.lngmax > allMinmax['lngmax']) {
allMinmax['lngmax'] = formatedLineString[i].minmax.lngmax
}
if (formatedLineString[i].minmax.lngmin < allMinmax['lngmin']) {
allMinmax['lngmin'] = formatedLineString[i].minmax.lngmin
}
}
return { coords: allCoords, minmax: allMinmax, rangeValues: consoRange1 }
}
export default {
state: getNewState(),
getters: {
getResponseApi(state) {
// FAKE DATA
return formatLinestring(
state.responseApi.linestring,
state.responseApi.couleur
)
},
getCircuitConsumption(state) {
if (state.circuitConsumption.error) {
return state.circuitConsumption
} else {
return compiledLineStrings(state)
}
},
getCategoriesCircuit(state) {
return state.categoriesCircuit
},
getSousCategoriesCircuit(state) {
return state.sous_categoriesCircuit
},
},
mutations: {
setCircuitId(state, infos) {
state.circuitId = infos
},
setCategoriesCircuit(state, infos = []) {
state.categoriesCircuit = infos
},
setSousCategoriesCircuit(state, infos = []) {
state.sous_categoriesCircuit = infos
},
setCircuitConsumption(state, infos) {
state.circuitConsumption = infos
},
},
actions: {
// async initializeCircuit({ state, commit }) {
async initializeCircuit({ commit }) {
let circuitCategories = await circuitService.fetchCircuitCategories(
circuitService.normalizeRoundCategoryToV2
)
let circuits = await circuitService.fetchCircuits((c) => {
return circuitService.normalizeRoundToV2(c, circuitCategories)
})
commit('setCategoriesCircuit', circuitCategories)
commit('setSousCategoriesCircuit', circuits)
},
/**
* Récupère les infos de consommation sur un circuit pour la cartographie.
* @param commit
* @param vehiculeId int
* @param driverId int
* @param dhd date heure début activité
* @param dhf date heure fin activité
* @returns {Promise<void>}
* @author FMA 20210809 https://easyredmine.simpliciti.fr/issues/26127
*/
async fetchCircuitsConsumption({ commit }, payload) {
let id = payload.vehicleId
let dhdeb = payload.dhd
let dhfin = payload.dhf
let distance = payload.distance
let url = `${APIUrls.APIV2_HISTORIQUE_CONSOMMATION_PAR_PORTION}?vehicule_id=${id}&dateheure_debut=${dhdeb}&dateheure_fin=${dhfin}&distance=${distance}`
let data = (await api.v2.get(url)).data
if (data.error) {
// Vue.$log.error({
// requestUrl: url,
// error: data.error,
// });
data = { error: data.error }
}
commit('setCircuitConsumption', data)
},
},
}
Source