1- // Oppdatert dekoder for det nye komprimerte formatet
1+ // Updated decoder for the new compressed format
22function decodeMessage ( encodedMessage ) {
33 try {
44 if ( ! encodedMessage || encodedMessage . trim ( ) === "" ) {
@@ -12,7 +12,14 @@ function decodeMessage(encodedMessage) {
1212
1313 const base36ToInt = ( value ) => parseInt ( value , 36 ) ;
1414
15- return entries . map ( ( entry ) => {
15+ // First entry is the date
16+ const dateCode = entries . shift ( ) ;
17+ const year = Math . floor ( base36ToInt ( dateCode ) / 10000 ) + 2000 ;
18+ const month = Math . floor ( ( base36ToInt ( dateCode ) % 10000 ) / 100 ) ;
19+ const day = base36ToInt ( dateCode ) % 100 ;
20+ const decodedDate = `${ day . toString ( ) . padStart ( 2 , "0" ) } .${ month . toString ( ) . padStart ( 2 , "0" ) } .${ year } ` ;
21+
22+ const weatherData = entries . map ( ( entry ) => {
1623 if ( entry . length !== 10 ) {
1724 throw new Error ( `Invalid entry length for: ${ entry } ` ) ;
1825 }
@@ -30,24 +37,25 @@ function decodeMessage(encodedMessage) {
3037 const gust = base36ToInt ( gustBase36 ) ;
3138
3239 const cloudBase36 = entry . slice ( 6 , 7 ) ;
33- const cloud = base36ToInt ( cloudBase36 ) * 10 ;
40+ const cloud = base36ToInt ( cloudBase36 ) * 5 ; // Adjusted to 5% intervals
3441
35- const precipBase36 = entry . slice ( 7 , 8 ) ;
36- const precip = base36ToInt ( precipBase36 ) ;
42+ const precipBase36 = entry . slice ( 7 , 9 ) ; // Two-character precipitation encoding
43+ const precip = base36ToInt ( precipBase36 ) / 10 ; // Adjusted to include one decimal point
3744
38- const directionBase36 = entry . slice ( 8 , 9 ) ;
45+ const directionBase36 = entry . slice ( 9 , 10 ) ;
3946 const direction = directions [ base36ToInt ( directionBase36 ) % 8 ] ;
4047
4148 return {
4249 time : `${ time } :00` ,
4350 temp : `${ temp } °C` ,
44- precip : `${ precip } mm` ,
45- wind : `${ wind } m/s` ,
46- gust : `${ gust } m/s` ,
51+ precip : `${ precip . toFixed ( 1 ) } mm` , // Precipitation with one decimal point
52+ wind : `${ wind } m/s (${ gust } m/s)` ,
4753 direction : direction ,
4854 cloud : `${ cloud } %` ,
4955 } ;
5056 } ) ;
57+
58+ return { date : decodedDate , data : weatherData } ;
5159 } catch ( error ) {
5260 console . error ( "Error decoding message:" , error ) ;
5361 throw new Error ( "Failed to decode the message. Please check the input." ) ;
@@ -63,12 +71,16 @@ document.getElementById("decodeButton").addEventListener("click", () => {
6371 console . log ( "Full message to decode:" , fullMessage ) ;
6472
6573 try {
66- const decodedData = decodeMessage ( fullMessage ) ;
74+ const decoded = decodeMessage ( fullMessage ) ;
75+
76+ // Update date in the table
77+ document . getElementById ( "weatherDate" ) . textContent = `Dato: ${ decoded . date } ` ;
6778
79+ // Update weather data in the table
6880 const tableBody = document . getElementById ( "weatherTable" ) . querySelector ( "tbody" ) ;
6981 tableBody . innerHTML = "" ;
7082
71- decodedData . forEach ( ( data ) => {
83+ decoded . data . forEach ( ( data ) => {
7284 const row = document . createElement ( "tr" ) ;
7385
7486 Object . values ( data ) . forEach ( ( value ) => {
0 commit comments