update sms code
This commit is contained in:
@ -335,70 +335,61 @@ async function commitMessage(now, logDevice) {
|
||||
}
|
||||
|
||||
// check speed limit, if speed > vhc.speed_limit, send sms
|
||||
if (vhc.length > 0 && vhc[0].speed_limit > 0 && logDevice.speed > vhc[0].speed_limit) {
|
||||
// get user
|
||||
const queryUsers = `select phone from t_users where status_sms = 1`
|
||||
const users = await db.query(queryUsers, [])
|
||||
if (users && users.length > 0) {
|
||||
const nopol = (vhc[0].nopol1 || "") + (vhc[0].nopol2 || "") + (vhc[0].nopol3 || "")
|
||||
let address = ""
|
||||
if (logDevice.latitude && logDevice.longitude) {
|
||||
try {
|
||||
const sameAddr = await GpsTracksModels.select2Address(logDevice.latitude, logDevice.longitude)
|
||||
if (sameAddr.length > 0) {
|
||||
address = decodeURIComponent(sameAddr[0].fulladdress)
|
||||
} else {
|
||||
const axInstance = axios.create({
|
||||
proxy: process.env.PROXY_URL ? {
|
||||
host: process.env.PROXY_URL,
|
||||
port: Number(process.env.PROXY_PORT),
|
||||
} : false,
|
||||
timeout: 15_000,
|
||||
})
|
||||
const urlBase = request.osm_reverse_geo.urlFull
|
||||
const params = new url.URLSearchParams({
|
||||
lat: logDevice.latitude,
|
||||
lon: logDevice.longitude,
|
||||
format: "geojson",
|
||||
})
|
||||
const resp = await axInstance.get(`${urlBase}?${params.toString()}`, {
|
||||
headers: { "User-Agent": `movana-fleet-management-overspeed` },
|
||||
})
|
||||
if (resp.status === 200 && resp.data && resp.data.features && resp.data.features.length > 0) {
|
||||
address = resp.data.features[0].properties.display_name
|
||||
}
|
||||
}
|
||||
} catch (addrErr) {
|
||||
console.error("Error fetching address for overspeed alert:", addrErr.message)
|
||||
}
|
||||
}
|
||||
const time = moment.unix(logDevice.crt_d + TIMEFIX).format('DD MMM YYYY HH:mm:ss')
|
||||
const rawMsg = `${nopol} | OVERSPEED | ${time} | ${logDevice.speed} km/h | ${address}`
|
||||
for (let user of users) {
|
||||
if (user.phone) {
|
||||
try {
|
||||
await axios.get(`http://192.168.40.2:8181/mobicents/sendSms`, {
|
||||
params: {
|
||||
to: `670${user.phone}`,
|
||||
msg: rawMsg,
|
||||
sender: "Overspeed FROTA",
|
||||
},
|
||||
})
|
||||
if (Array.isArray(vhc) && vhc.length > 0 && vhc[0].speed_limit > 0 && logDevice.speed > vhc[0].speed_limit) {
|
||||
|
||||
// insert logs
|
||||
await db.query(
|
||||
`insert into sms_notif set to_no = ?, message = ?`,
|
||||
[user.phone, rawMsg],
|
||||
)
|
||||
} catch (err) {
|
||||
console.error(
|
||||
"Error sending SMS : ",
|
||||
err.response ? JSON.stringify(err.response.data) : err.message,
|
||||
)
|
||||
const users = await db.query(`SELECT phone FROM t_users WHERE status_sms = 1`, [])
|
||||
if (!users?.length) return
|
||||
|
||||
const nopol = (vhc[0].nopol1 ?? "") + (vhc[0].nopol2 ?? "") + (vhc[0].nopol3 ?? "")
|
||||
let address = ""
|
||||
|
||||
if (logDevice.latitude && logDevice.longitude) {
|
||||
try {
|
||||
const cached = await GpsTracksModels.select2Address(logDevice.latitude, logDevice.longitude)
|
||||
if (cached.length > 0) {
|
||||
address = decodeURIComponent(cached[0].fulladdress)
|
||||
} else {
|
||||
const axInstance = axios.create({
|
||||
proxy: process.env.PROXY_URL ? { host: process.env.PROXY_URL, port: Number(process.env.PROXY_PORT) } : false,
|
||||
timeout: 15_000,
|
||||
})
|
||||
const params = new url.URLSearchParams({ lat: logDevice.latitude, lon: logDevice.longitude, format: "geojson" })
|
||||
const resp = await axInstance.get(`${request.osm_reverse_geo.urlFull}?${params}`, {
|
||||
headers: { "User-Agent": "movana-fleet-management-overspeed" },
|
||||
})
|
||||
if (resp.status === 200 && resp.data?.features?.length > 0) {
|
||||
address = resp.data.features[0].properties.display_name
|
||||
}
|
||||
}
|
||||
} catch (addrErr) {
|
||||
console.error("Error fetching address for overspeed alert:", addrErr.message)
|
||||
}
|
||||
}
|
||||
|
||||
const time = moment.unix(logDevice.crt_d + TIMEFIX).format("DD MMM YYYY HH:mm:ss")
|
||||
const rawMsg = `${nopol} | OVERSPEED | ${time} | ${logDevice.speed} km/h | ${address}`.trim()
|
||||
const countryCode = process.env.SMS_COUNTRY_CODE ?? "670"
|
||||
const smsHost = process.env.SMS_HOST ?? "http://192.168.40.2:8181"
|
||||
|
||||
await Promise.allSettled(
|
||||
users
|
||||
.filter(u => u.phone)
|
||||
.map(async (user) => {
|
||||
try {
|
||||
await axios.get(`${smsHost}/mobicents/sendSms`, {
|
||||
params: { to: `${countryCode}${user.phone}`, msg: rawMsg, sender: "Overspeed FROTA" },
|
||||
})
|
||||
} catch (smsErr) {
|
||||
console.error("Error sending SMS:", smsErr.response ? JSON.stringify(smsErr.response.data) : smsErr.message)
|
||||
}
|
||||
|
||||
try {
|
||||
await db.query(`INSERT INTO sms_notif SET to_no = ?, message = ?`, [user.phone, rawMsg])
|
||||
} catch (dbErr) {
|
||||
console.error("Error logging SMS to DB:", dbErr.message)
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
|
||||
Reference in New Issue
Block a user