voici ce que je voudrais obtenir:
forecast:
- dt: 1626598800000
date_heure: 18/7/2021 11:0:0
vent_rafales: 18
vent: 7
vent_dir: 13
vent_card: NNE
vent_beaufort: 2 Légère brise
- dt: 1626609600000
date_heure: 18/7/2021 14:0:0
vent_rafales: 8
vent: 4
vent_dir: 281
vent_card: W
vent_beaufort: 1 Très légère brise
mais avec ce code :
var fcdata = {};
fcdata.payload = {
ts: {
}
}
for (i = 0; i < msg.payload.ts.length; i++) {
fcdata.payload.ts[i] = msg.payload.ts[i] ;
fcdata.payload.ts[i] = fcdata.payload.ts[i] + ', date_heure: ' + dateheure(msg.payload.ts[i]);
fcdata.payload.ts[i] = fcdata.payload.ts[i] + ', vent_rafales: ' + ms2kmh(msg.payload.gust_surface[i]) ;
fcdata.payload.ts[i] = fcdata.payload.ts[i] + ', vent: ' + ms2kmh(vent(msg.payload.wind_u_surface[i],msg.payload.wind_v_surface[i])) ;
fcdata.payload.ts[i] = fcdata.payload.ts[i] + ', vent_dir: ' + vent_dir(msg.payload.wind_u_surface[i],msg.payload.wind_v_surface[i]) ;
fcdata.payload.ts[i] = fcdata.payload.ts[i] + ', vent_card: ' + deg2card(vent_dir(msg.payload.wind_u_surface[i],msg.payload.wind_v_surface[i])) ;
fcdata.payload.ts[i] = fcdata.payload.ts[i] + ', vent_beaufort: ' + beaufort(vent(msg.payload.wind_u_surface[i],msg.payload.wind_v_surface[i])) ;
}
/* calcul force du vent en m/s en fonction des valeurs u et v meteo */
function vent(u,v) {
return Math.round(Math.sqrt(Math.abs(u)* Math.abs(u) + Math.abs(v)* Math.abs(v)));
}
/* calcul direction du vent en fonction des valeurs u et v meteo */
function vent_dir(u,v) {
wind= Math.sqrt(Math.abs(u)* Math.abs(u) + Math.abs(v)* Math.abs(v));
wind_dir_trig_to = Math.atan2(u/wind, v/wind);
wind_dir_trig_to_degrees = wind_dir_trig_to * 180/Math.PI;
wind_dir_trig_from_degrees = wind_dir_trig_to_degrees + 180;
return Math.round(wind_dir_trig_from_degrees);
}
/* changement unité m/s en km/h */
function ms2kmh(speed) {
return Math.round(speed * 3.6);
}
/* recherche date heure en fonction d'un timestamp */
function dateheure(ts) {
var dt = new Date(ts);
m = dt.getMonth() + 1;
var dateheure = dt.getDate() + '/' + m + '/' + dt.getFullYear() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getMilliseconds();
return dateheure;
}
/* conversion degré en texte */
function deg2card(deg) {
if (deg>11.25 && deg<=33.75){
return "NNE";
}else if (deg>33.75 && deg<56.25){
return "NE";
}else if (deg>56.25 && deg<78.75){
return "ENE";
}else if (deg>78.75 && deg<101.25){
return "E";
}else if (deg>101.25 && deg<123.75){
return "ESE";
}else if (deg>123.75 && deg<146.25){
return "SE";
}else if (deg>146.25 && deg<168.75){
return "SSE";
}else if (deg>168.75 && deg<191.25){
return "S";
}else if (deg>191.25 && deg<213.75){
return "SSW";
}else if (deg>213.75 && deg<236.25){
return "SW";
}else if (deg>236.25 && deg<258.75){
return "WSW";
}else if (deg>258.75 && deg<281.25){
return "W";
}else if (deg>281.25 && deg<303.75){
return "WNW";
}else if (deg>303.75 && deg<326.25){
return "Nw";
}else if (deg>326.25 && deg<348.75){
return "NNW";
}else{
return "N";
}
}
/* conversion force du vent en Beaufort*/
function beaufort(speed) {
if (speed>0.5 && speed<=1.5){
return "1 Très légère brise";
}else if (speed>1.5 && speed<=3.3){
return "2 Légère brise";
}else if (speed>3.3 && speed<=5.5){
return "3 Petite brise";
}else if (speed>5.5 && speed<=7.9){
return "4 Jolie brise";
}else if (speed>7.9 && speed<=10.7){
return "5 Bonne brise";
}else if (speed>10.7 && speed<=13.8){
return "6 Vent frais";
}else if (speed>13.8 && speed<=17.1){
return "7 Grand frais";
}else if (speed>17.1 && speed<=20.7){
return "8 Coup de vent";
}else if (speed>20.7 && speed<=24.4){
return "9 Fort coup de vent";
}else if (speed>24.4 && speed<=28.4){
return "10 Tempête";
}else if (speed>28.4 && speed<=32.6){
return "11 Violente tempête";
}else if (speed>32.6){
return "12 Ouragan";
}else{
return "0 Calme";
}
}
return fcdata;
j’obtiens :
forecast:
ts:
'0': >-
1626598800000, date_heure: 18/7/2021 11:0:0, vent_rafales: 18, vent: 7,
vent_dir: 13, vent_card: NNE, vent_beaufort: 2 Légère brise
'1': >-
1626609600000, date_heure: 18/7/2021 14:0:0, vent_rafales: 8, vent: 4,
vent_dir: 281, vent_card: W, vent_beaufort: 1 Très légère brise
'2': >-
1626620400000, date_heure: 18/7/2021 17:0:0, vent_rafales: 40, vent: 18,
vent_dir: 276, vent_card: W, vent_beaufort: 3 Petite brise
c’est pas bon , comment je peux remettre ca en cohérence ?