diff --git a/controllers/ListenController.js b/controllers/ListenController.js index 91a8993..03fec26 100644 --- a/controllers/ListenController.js +++ b/controllers/ListenController.js @@ -368,8 +368,8 @@ async function commitMessage(now, logDevice) { } const time = moment.unix(logDevice.crt_d + TIMEFIX).format("DD MMM YYYY HH:mm:ss") - address = address.substring(0, 60) - const rawMsg = `${nopol} | OVERSPEED | ${time} | ${logDevice.speed} km/h | ${address}`.trim() + address = sanitizeForGsm(address) + const rawMsg = `${nopol} | OVERSPEED | ${time} | ${logDevice.speed} km/h | ${address}`.substring(0, 150).trim() // const rawMsg = `${nopol} | OVERSPEED | ${time} | ${logDevice.speed} km/h`.trim() const countryCode = process.env.SMS_COUNTRY_CODE ?? "670" const smsHost = process.env.SMS_HOST ?? "http://192.168.40.2:8181" @@ -839,6 +839,29 @@ class ListenController { } } +function sanitizeForGsm(text) { + // 1. Map symbols and ligatures that do not normalize cleanly + const customMap = { + '€': 'EUR', '£': 'GBP', '¥': 'JPY', + 'ß': 'ss', 'æ': 'ae', 'Æ': 'AE', 'œ': 'oe', 'Œ': 'OE', + '“': '"', '”': '"', '‘': "'", '’': "'", '…': '...', + '–': '-', '—': '-' + }; + + // Replace custom mapped characters + let mappedText = text.replace( + /[€£¥ßæÆœŒ“”‘’…–—]/g, + match => customMap[match] + ); + + // 2. Normalize and strip diacritics + // .normalize('NFD') splits characters like 'é' into 'e' + '´' + // The regex /[\u0300-\u036f]/g matches and removes those detached accent marks + let cleanText = mappedText.normalize('NFD').replace(/[\u0300-\u036f]/g, ''); + + return cleanText; +} + const object = new ListenController() module.exports = object