Compare commits

30 Commits

Author SHA256 Message Date
c0ac71ebba fix: edit wrong .gitignore :D 2025-03-22 10:22:19 +01:00
10e8e14cd6 feat: add docker compose to gitignore 2025-03-22 10:20:42 +01:00
a9c8525e6e Merge pull request 'Add timestamps of locations providers' (#18) from feat/timestamps into main
Reviewed-on: #18
Reviewed-by: Philipp Schweizer <Philipp.schw@directbox.com>
2025-02-14 21:22:57 +01:00
39c07fcef0 refactor: only search once for wifiMessage 2025-02-14 21:06:47 +01:00
52d521a6ad rename var 2025-02-08 21:24:40 +01:00
d3848ac1aa Merge branch 'main' into feat/timestamps 2025-02-08 20:54:50 +01:00
93f0c71a6c set wifi timestamp 2025-02-08 20:54:06 +01:00
5e4fd59148 refactor to use either timestamp when one is undefined 2025-02-02 20:10:29 +01:00
62a2dc2c4a fix gnss timestamp 2025-01-29 23:03:48 +01:00
59dc57a618 set gnss timestamp in location 2025-01-28 22:12:48 +01:00
452589d11d add timestamps in DB model 2025-01-28 21:55:54 +01:00
d04bdb3ac1 Merge pull request 'Add Prometheus metrics endpoint' (#14) from feat/prometheus into main
Reviewed-on: #14
Reviewed-by: Philipp Schweizer <Philipp.schw@directbox.com>
2025-01-26 10:03:28 +01:00
a745aaf9d0 add timestamps to DB model 2025-01-25 21:51:42 +01:00
51112b5870 update readme 2025-01-25 19:14:18 +01:00
262b4718fc Merge branch 'main' into feat/prometheus 2025-01-25 19:10:09 +01:00
4d0e84b84a move metrics definition to service 2025-01-25 19:08:30 +01:00
f969b0a4c0 use Promise.all 2025-01-25 18:50:25 +01:00
49fe1b4ce3 README.md aktualisiert 2025-01-19 13:35:53 +01:00
34167c4d99 export metric for gnss locations 2025-01-18 19:36:44 +01:00
79580e16b7 Merge branch 'main' into feat/prometheus 2025-01-18 19:01:11 +01:00
4e5b1bfad0 Merge pull request 'Add docs and setup files' (#15) from doc/tracker-integration into main
Reviewed-on: #15
2025-01-18 19:00:17 +01:00
f03a98aadd improve readme 2025-01-18 18:54:15 +01:00
2ae6d49b72 add systemd and improve env.template 2025-01-18 18:48:43 +01:00
de15b67115 add payloadformatter 2025-01-18 18:07:55 +01:00
abf6b9af82 add metric TTN Uplinks 2025-01-16 21:50:00 +01:00
85e3509731 embarrassing 2025-01-15 17:32:31 +01:00
8a4eadefcb use db wifi_location as metric source 2025-01-14 21:03:44 +01:00
e8154d1e13 wigle api request counter 2025-01-11 23:38:44 +01:00
9b00853d5a use prom-client 2025-01-11 22:59:07 +01:00
574a63b2a3 add metrics api 2025-01-11 21:59:11 +01:00
19 changed files with 1273 additions and 32 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
docker-compose.yml

View File

@ -1,25 +1,96 @@
# LocationHub
TODO
**Self-hosted backend for LoRaWAN-based location tracking, supporting multiple location providers such as GNSS, Gateway-Triangulation, WiFi-Triangulation, and BLE-Triangulation.**
We recommend using [The Things Network](https://www.thethingsnetwork.org/) (TTN) as middleware for relaying LoRaWAN payloads.
---
## Table of Contents
1. [Setup](#setup)
- [Prerequisites](#prerequisites)
- [Database](#database)
- [Configuration](#configuration)
2. [TTN Integration](#ttn-integration)
3. [Prometheus Metrics](#prometheus-metrics)
4. [Add a Location Tracker](#add-a-location-tracker)
- [Onboard SenseCAP T1000-B](#onboard-sensecap-t1000-b)
- [Register SenseCAP T1000-B](#register-sensecap-t1000-b)
5. [Testing](#testing)
- [Testing Webhook](#testing-webhook)
- [Emulating Wigle API](#emulating-wigle-api)
---
## Setup
### Prerequisites
- Node.js >= 22.11.0
- Maria DB >= 11.6.2
Ensure the following dependencies are installed:
- **Node.js** >= 22.11.0
- **MariaDB** >= 11.6.2
- A web server, such as **nginx**
---
### Database
**Change name of database and credentials as you like!**
**Customize the database name and credentials to your preference.**
Follow these steps to set up the database:
- Create new database: `CREATE DATABASE dev_locationhub;`
- Create new user for database: `GRANT ALL PRIVILEGES ON dev_locationhub.* TO 'dbuser'@'localhost' IDENTIFIED BY '1234';`
- Import tables: `/usr/bin/mariadb -u dbuser -p1234 dev_locationhub < server/sql/tables.sql`
### Configuration
- Copy the .env.template file and rename it to .env:
- Use a strong token/secret for the webhook.
- Add your [Wigle](http://wigle.net) API token to translate MAC addresses to coordinates
- Use [systemd](server/scripts/locationhub.service) to start the server.
### TTN Integration
Create new Webhook for application. Set base url and enable "Uplink message" to api `/api/ttn/webhook`.
Create new Webhook for your application. Set base url to `https://your.domain.tld` and enable "Uplink message" to api `/api/ttn/webhook`.
Add a addidtional header:
- Type: `authorization`
- Value: `Bearer your-very-secure-token`
- Value: `Bearer your-very-secure-token-from-the-env-file`
### Prometheus Metrics
Use `https://your.domain.tld/api/metrics` to retrieve useful insides for monitoring.
## Add a Location Tracker
We use the [SenseCAP T1000-B](https://www.seeedstudio.com/SenseCAP-Card-Tracker-T1000-B-p-5698.html) from seeedstudio because of the fair price and multiple location providers. However, you can use any LoRaWAN-enabled tracker that is compatible with TTN and supports the required payload fields.
### Onboard SenseCAP T1000-B
1. Download and install the App [SenseCraft](https://play.google.com/store/apps/details?id=cc.seeed.sensecapmate)
2. Skip the user account at startup with `Skip` in the upper right corner
3. Select the `Tracker T1000`
4. Connect to your SenseCAP T1000-B
5. In the `Settings` tab select `The Things Network` as Platform under `LoRa`
6. Copy `Device EUI`, `AppEUI` and `AppKey`
7. Save LoRa settings
8. Under `Geolocation` select Geolocation Strategy as `GNSS+Wi-Fi`
9. Set `GNSS Max Scan Time (s)` to 120
10. Save Geolocation settings
11. Exit App with return arrow to trigger re-init of SenseCAP T1000-B
### Register SenseCAP T1000-B
1. Open your Application in TTN and navigate to `End devices`
2. Click `Register end device` in the upper right corner
3. Set input method to `Enter end device specifics manually`
- Frequency plan: `Europe 863-870 MHz (SF9 for RX2 - recommended)`
- LoRaWAN version: `LoRaWAN Specification 1.0.4`
- Regional Parameters version: `RP002 Regional Parameters 1.0.3`
4. Place as `JoinEUI` the `AppEUI`
5. Place the `DevEUI`
6. Place the `AppKey`
7. Set a name for end device
8. Click `Register end device`
9. Check if new end device joins the TTN
10. Place the [Uplink Payload Formatter](TTN/sensecap_payload_formater.js) as `Custom Javascript formatter` for new end device
## Testing
### Testing Webhook
- To test the webhook use the python script `ttn-webhook-dummy.py` to send prerecorded TTN Uplinks.
- To test the script you can use `while true; do echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nSuccess"; nc -l -p 8080 -q 1; done`
- To test the webhook use the python script [ttn-webhook-dummy.py](server/scripts/ttn-webhook-dummy.py) to send prerecorded TTN Uplinks.
- To test the script you can use `while true; do echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nSuccess"; nc -l -p 8080 -q 1; done`
### Emulating Wigle API
- To emulate the Wigle API use the python script [wigle-dummy.py](server/scripts/wigle-dummy.py) to translate MAC addresses to coordinates.
Happy tracking! 🎉

View File

@ -0,0 +1,911 @@
function decodeUplink (input) {
const bytes = input['bytes']
const fport = parseInt(input['fPort'])
const bytesString = bytes2HexString(bytes)
const originMessage = bytesString.toLocaleUpperCase()
const decoded = {
valid: true,
err: 0,
payload: bytesString,
messages: []
}
if (fport === 199 || fport === 192) {
decoded.messages.push({fport: fport, payload: bytesString})
return { data: decoded }
}
let measurement = messageAnalyzed(originMessage)
if (measurement.length === 0) {
decoded.valid = false
return { data: decoded }
}
for (let message of measurement) {
if (message.length === 0) {
continue
}
let elements = []
for (let element of message) {
if (element.errorCode) {
decoded.err = element.errorCode
decoded.errMessage = element.error
} else {
elements.push(element)
}
}
if (elements.length > 0) {
decoded.messages.push(elements)
}
}
// decoded.messages = measurement
return { data: decoded }
}
function messageAnalyzed (messageValue) {
try {
let frames = unpack(messageValue)
let measurementResultArray = []
for (let i = 0; i < frames.length; i++) {
let item = frames[i]
let dataId = item.dataId
let dataValue = item.dataValue
let measurementArray = deserialize(dataId, dataValue)
measurementResultArray.push(measurementArray)
}
return measurementResultArray
} catch (e) {
return e.toString()
}
}
function unpack (messageValue) {
let frameArray = []
for (let i = 0; i < messageValue.length; i++) {
let remainMessage = messageValue
let dataId = remainMessage.substring(0, 2).toUpperCase()
let dataValue
let dataObj = {}
let packageLen
switch (dataId) {
case '01':
dataValue = remainMessage.substring(2, 94)
messageValue = remainMessage.substring(94)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '02':
dataValue = remainMessage.substring(2, 32)
messageValue = remainMessage.substring(32)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '03':
dataValue = remainMessage.substring(2, 64)
messageValue = remainMessage.substring(64)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '04':
dataValue = remainMessage.substring(2, 20)
messageValue = remainMessage.substring(20)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '05':
dataValue = remainMessage.substring(2, 10)
messageValue = remainMessage.substring(10)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '06':
dataValue = remainMessage.substring(2, 44)
messageValue = remainMessage.substring(44)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '07':
dataValue = remainMessage.substring(2, 84)
messageValue = remainMessage.substring(84)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '08':
dataValue = remainMessage.substring(2, 70)
messageValue = remainMessage.substring(70)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '09':
dataValue = remainMessage.substring(2, 36)
messageValue = remainMessage.substring(36)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '0A':
dataValue = remainMessage.substring(2, 76)
messageValue = remainMessage.substring(76)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '0B':
dataValue = remainMessage.substring(2, 62)
messageValue = remainMessage.substring(62)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '0C':
break
case '0D':
dataValue = remainMessage.substring(2, 10)
messageValue = remainMessage.substring(10)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '0E':
packageLen = getInt(remainMessage.substring(8, 10)) * 2 + 10
dataValue = remainMessage.substring(2, 8) + remainMessage.substring(10, packageLen)
messageValue = remainMessage.substring(packageLen)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '0F':
dataValue = remainMessage.substring(2, 34)
messageValue = remainMessage.substring(34)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '10':
dataValue = remainMessage.substring(2, 26)
messageValue = remainMessage.substring(26)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '11':
dataValue = remainMessage.substring(2, 28)
messageValue = remainMessage.substring(28)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '1A':
dataValue = remainMessage.substring(2, 56)
messageValue = remainMessage.substring(56)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '1B':
dataValue = remainMessage.substring(2, 96)
messageValue = remainMessage.substring(96)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '1C':
dataValue = remainMessage.substring(2, 82)
messageValue = remainMessage.substring(82)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
case '1D':
dataValue = remainMessage.substring(2, 40)
messageValue = remainMessage.substring(40)
dataObj = {
'dataId': dataId, 'dataValue': dataValue
}
break
default:
return frameArray
}
if (dataValue.length < 2) {
break
}
frameArray.push(dataObj)
}
return frameArray
}
function deserialize (dataId, dataValue) {
let measurementArray = []
let eventList = []
let measurement = {}
let collectTime = 0
let groupId = 0
let shardFlag = {}
let payload = ''
let motionId = ''
switch (dataId) {
case '01':
measurementArray = getUpShortInfo(dataValue)
measurementArray.push(...getMotionSetting(dataValue.substring(30, 40)))
measurementArray.push(...getStaticSetting(dataValue.substring(40, 46)))
measurementArray.push(...getShockSetting(dataValue.substring(46, 52)))
measurementArray.push(...getTempSetting(dataValue.substring(52, 72)))
measurementArray.push(...getLightSetting(dataValue.substring(72, 92)))
break
case '02':
measurementArray = getUpShortInfo(dataValue)
break
case '03':
measurementArray.push(...getMotionSetting(dataValue.substring(0, 10)))
measurementArray.push(...getStaticSetting(dataValue.substring(10, 16)))
measurementArray.push(...getShockSetting(dataValue.substring(16, 22)))
measurementArray.push(...getTempSetting(dataValue.substring(22, 42)))
measurementArray.push(...getLightSetting(dataValue.substring(42, 62)))
break
case '04':
let interval = 0
let workMode = getInt(dataValue.substring(0, 2))
let heartbeatInterval = getMinsByMin(dataValue.substring(4, 8))
let periodicInterval = getMinsByMin(dataValue.substring(8, 12))
let eventInterval = getMinsByMin(dataValue.substring(12, 16))
switch (workMode) {
case 0:
interval = heartbeatInterval
break
case 1:
interval = periodicInterval
break
case 2:
interval = eventInterval
break
}
measurementArray = [
{measurementId: '3940', type: 'Work Mode', measurementValue: workMode},
{measurementId: '3942', type: 'Heartbeat Interval', measurementValue: heartbeatInterval},
{measurementId: '3943', type: 'Periodic Interval', measurementValue: periodicInterval},
{measurementId: '3944', type: 'Event Interval', measurementValue: eventInterval},
{measurementId: '3941', type: 'SOS Mode', measurementValue: getSOSMode(dataValue.substring(16, 18))},
{measurementId: '3900', type: 'Uplink Interval', measurementValue: interval}
]
break;
case '05':
measurementArray = [
{measurementId: '3000', type: 'Battery', measurementValue: getBattery(dataValue.substring(0, 2))},
{measurementId: '3940', type: 'Work Mode', measurementValue: getWorkingMode(dataValue.substring(2, 4))},
{measurementId: '3965', type: 'Positioning Strategy', measurementValue: getPositioningStrategy(dataValue.substring(4, 6))},
{measurementId: '3941', type: 'SOS Mode', measurementValue: getSOSMode(dataValue.substring(6, 8))}
]
break
case '06':
collectTime = getUTCTimestamp(dataValue.substring(8, 16))
motionId = getMotionId(dataValue.substring(6, 8))
measurementArray = [
{measurementId: '4200', timestamp: collectTime, motionId: motionId, type: 'Event Status', measurementValue: getEventStatus(dataValue.substring(0, 6))},
{measurementId: '4197', timestamp: collectTime, motionId: motionId, type: 'Longitude', measurementValue: parseFloat(getSensorValue(dataValue.substring(16, 24), 1000000))},
{measurementId: '4198', timestamp: collectTime, motionId: motionId, type: 'Latitude', measurementValue: parseFloat(getSensorValue(dataValue.substring(24, 32), 1000000))},
{measurementId: '4097', timestamp: collectTime, motionId: motionId, type: 'Air Temperature', measurementValue: getSensorValue(dataValue.substring(32, 36), 10)},
{measurementId: '4199', timestamp: collectTime, motionId: motionId, type: 'Light', measurementValue: getSensorValue(dataValue.substring(36, 40))},
{measurementId: '3000', timestamp: collectTime, motionId: motionId, type: 'Battery', measurementValue: getBattery(dataValue.substring(40, 42))}
]
break
case '07':
eventList = getEventStatus(dataValue.substring(0, 6))
collectTime = getUTCTimestamp(dataValue.substring(8, 16))
motionId = getMotionId(dataValue.substring(6, 8))
measurementArray = [
{measurementId: '4200', timestamp: collectTime, motionId: motionId, type: 'Event Status', measurementValue: getEventStatus(dataValue.substring(0, 6))},
{measurementId: '5001', timestamp: collectTime, motionId: motionId, type: 'Wi-Fi Scan', measurementValue: getMacAndRssiObj(dataValue.substring(16, 72))},
{measurementId: '4097', timestamp: collectTime, motionId: motionId, type: 'Air Temperature', measurementValue: getSensorValue(dataValue.substring(72, 76), 10)},
{measurementId: '4199', timestamp: collectTime, motionId: motionId, type: 'Light', measurementValue: getSensorValue(dataValue.substring(76, 80))},
{measurementId: '3000', timestamp: collectTime, motionId: motionId, type: 'Battery', measurementValue: getBattery(dataValue.substring(80, 82))}
]
break
case '08':
collectTime = getUTCTimestamp(dataValue.substring(8, 16))
motionId = getMotionId(dataValue.substring(6, 8))
measurementArray = [
{measurementId: '4200', timestamp: collectTime, motionId: motionId, type: 'Event Status', measurementValue: getEventStatus(dataValue.substring(0, 6))},
{measurementId: '5002', timestamp: collectTime, motionId: motionId, type: 'BLE Scan', measurementValue: getMacAndRssiObj(dataValue.substring(16, 58))},
{measurementId: '4097', timestamp: collectTime, motionId: motionId, type: 'Air Temperature', measurementValue: getSensorValue(dataValue.substring(58, 62), 10)},
{measurementId: '4199', timestamp: collectTime, motionId: motionId, type: 'Light', measurementValue: getSensorValue(dataValue.substring(62, 66))},
{measurementId: '3000', timestamp: collectTime, motionId: motionId, type: 'Battery', measurementValue: getBattery(dataValue.substring(66, 68))}
]
break
case '09':
collectTime = getUTCTimestamp(dataValue.substring(8, 16))
motionId = getMotionId(dataValue.substring(6, 8))
measurementArray = [
{measurementId: '4200', timestamp: collectTime, motionId: motionId, type: 'Event Status', measurementValue: getEventStatus(dataValue.substring(0, 6))},
{measurementId: '4197', timestamp: collectTime, motionId: motionId, type: 'Longitude', measurementValue: parseFloat(getSensorValue(dataValue.substring(16, 24), 1000000))},
{measurementId: '4198', timestamp: collectTime, motionId: motionId, type: 'Latitude', measurementValue: parseFloat(getSensorValue(dataValue.substring(24, 32), 1000000))},
{measurementId: '3000', timestamp: collectTime, motionId: motionId, type: 'Battery', measurementValue: getBattery(dataValue.substring(32, 34))}
]
break
case '0A':
collectTime = getUTCTimestamp(dataValue.substring(8, 16))
motionId = getMotionId(dataValue.substring(6, 8))
measurementArray = [
{measurementId: '4200', timestamp: collectTime, motionId, type: 'Event Status', measurementValue: getEventStatus(dataValue.substring(0, 6))},
{measurementId: '5001', timestamp: collectTime, motionId, type: 'Wi-Fi Scan', measurementValue: getMacAndRssiObj(dataValue.substring(16, 72))},
{measurementId: '3000', timestamp: collectTime, motionId, type: 'Battery', measurementValue: getBattery(dataValue.substring(72, 74))}
]
break
case '0B':
collectTime = getUTCTimestamp(dataValue.substring(8, 16))
motionId = getMotionId(dataValue.substring(6, 8))
measurementArray = [
{measurementId: '4200', timestamp: collectTime, motionId, type: 'Event Status', measurementValue: getEventStatus(dataValue.substring(0, 6))},
{measurementId: '5002', timestamp: collectTime, motionId, type: 'BLE Scan', measurementValue: getMacAndRssiObj(dataValue.substring(16, 58))},
{measurementId: '3000', timestamp: collectTime, motionId, type: 'Battery', measurementValue: getBattery(dataValue.substring(58, 60))},
]
break
case '0D':
let errorCode = getInt(dataValue)
let error = ''
switch (errorCode) {
case 1:
error = 'FAILED TO OBTAIN THE UTC TIMESTAMP'
break
case 2:
error = 'ALMANAC TOO OLD'
break
case 3:
error = 'DOPPLER ERROR'
break
}
measurementArray.push({errorCode, error})
break
case '0E':
shardFlag = getShardFlag(dataValue.substring(0, 2))
groupId = getInt(dataValue.substring(2, 6))
payload = dataValue.substring(6)
measurement = {
measurementId: '6152',
groupId: groupId,
index: shardFlag.index,
count: shardFlag.count,
type: 'gnss-ng payload',
measurementValue: payload
}
measurementArray.push(measurement)
break
case '0F':
collectTime = getUTCTimestamp(dataValue.substring(8, 16))
shardFlag = getShardFlag(dataValue.substring(26, 28))
groupId = getInt(dataValue.substring(28, 32))
motionId = getMotionId(dataValue.substring(6, 8))
measurementArray.push({
measurementId: '4200',
timestamp: collectTime,
motionId,
groupId: groupId,
index: shardFlag.index,
count: shardFlag.count,
type: 'Event Status',
measurementValue: getEventStatus(dataValue.substring(0, 6))
})
measurementArray.push({
measurementId: '4097',
timestamp: collectTime,
motionId,
groupId: groupId,
index: shardFlag.index,
count: shardFlag.count,
type: 'Air Temperature',
measurementValue: getSensorValue(dataValue.substring(16, 20), 10)
})
measurementArray.push({
measurementId: '4199',
timestamp: collectTime,
motionId,
groupId: groupId,
index: shardFlag.index,
count: shardFlag.count,
type: 'Light',
measurementValue: getSensorValue(dataValue.substring(20, 24))
})
measurementArray.push({
measurementId: '3000',
timestamp: collectTime,
motionId,
groupId: groupId,
index: shardFlag.index,
count: shardFlag.count,
type: 'Battery',
measurementValue: getBattery(dataValue.substring(24, 26))
})
break
case '10':
collectTime = getUTCTimestamp(dataValue.substring(8, 16))
shardFlag = getShardFlag(dataValue.substring(18, 20))
groupId = getInt(dataValue.substring(20, 24))
motionId = getMotionId(dataValue.substring(6, 8))
measurementArray.push({
measurementId: '4200',
timestamp: collectTime,
motionId,
groupId: groupId,
index: shardFlag.index,
count: shardFlag.count,
type: 'Event Status',
measurementValue: getEventStatus(dataValue.substring(0, 6))
})
measurementArray.push({
measurementId: '3000',
timestamp: collectTime,
motionId,
groupId: groupId,
index: shardFlag.index,
count: shardFlag.count,
type: 'Battery',
measurementValue: getBattery(dataValue.substring(16, 18))
})
break
case '11':
collectTime = getUTCTimestamp(dataValue.substring(8, 16))
measurementArray.push({
measurementId: '3576',
timestamp: collectTime,
type: 'Positioning Status',
measurementValue: getPositingStatus(dataValue.substring(0, 2))
})
measurementArray.push({
timestamp: collectTime,
measurementId: '4200',
type: 'Event Status',
measurementValue: getEventStatus(dataValue.substring(2, 8))
})
if (!isNaN(parseFloat(getSensorValue(dataValue.substring(16, 20), 10)))) {
measurementArray.push({
timestamp: collectTime,
measurementId: '4097',
type: 'Air Temperature',
measurementValue: getSensorValue(dataValue.substring(16, 20), 10)
})
}
if (!isNaN(parseFloat(getSensorValue(dataValue.substring(20, 24))))) {
measurementArray.push({
timestamp: collectTime,
measurementId: '4199',
type: 'Light',
measurementValue: getSensorValue(dataValue.substring(20, 24))
})
}
measurementArray.push({
timestamp: collectTime,
measurementId: '3000',
type: 'Battery',
measurementValue: getBattery(dataValue.substring(24, 26))
})
break
case '1A':
collectTime = getUTCTimestamp(dataValue.substring(8, 16))
motionId = getMotionId(dataValue.substring(6, 8))
measurementArray = [
{measurementId: '4200', timestamp: collectTime, motionId, type: 'Event Status', measurementValue: getEventStatus(dataValue.substring(0, 6))},
{measurementId: '4197', timestamp: collectTime, motionId, type: 'Longitude', measurementValue: parseFloat(getSensorValue(dataValue.substring(16, 24), 1000000))},
{measurementId: '4198', timestamp: collectTime, motionId, type: 'Latitude', measurementValue: parseFloat(getSensorValue(dataValue.substring(24, 32), 1000000))},
{measurementId: '4097', timestamp: collectTime, motionId, type: 'Air Temperature', measurementValue: getSensorValue(dataValue.substring(32, 36), 10)},
{measurementId: '4199', timestamp: collectTime, motionId, type: 'Light', measurementValue: getSensorValue(dataValue.substring(36, 40))},
{measurementId: '4210', timestamp: collectTime, motionId, type: 'AccelerometerX', measurementValue: getSensorValue(dataValue.substring(40, 44))},
{measurementId: '4211', timestamp: collectTime, motionId, type: 'AccelerometerY', measurementValue: getSensorValue(dataValue.substring(44, 48))},
{measurementId: '4212', timestamp: collectTime, motionId, type: 'AccelerometerZ', measurementValue: getSensorValue(dataValue.substring(48, 52))},
{measurementId: '3000', timestamp: collectTime, motionId, type: 'Battery', measurementValue: getBattery(dataValue.substring(52, 54))},
]
break
// WIFI定位数据+sensor+三轴+电量
case '1B':
collectTime = getUTCTimestamp(dataValue.substring(8, 16))
motionId = getMotionId(dataValue.substring(6, 8))
measurementArray = [
{measurementId: '4200', timestamp: collectTime, motionId, type: 'Event Status', measurementValue: getEventStatus(dataValue.substring(0, 6))},
{measurementId: '5001', timestamp: collectTime, motionId, type: 'Wi-Fi Scan', measurementValue: getMacAndRssiObj(dataValue.substring(16, 72))},
{measurementId: '4097', timestamp: collectTime, motionId, type: 'Air Temperature', measurementValue: getSensorValue(dataValue.substring(72, 76), 10)},
{measurementId: '4199', timestamp: collectTime, motionId, type: 'Light', measurementValue: getSensorValue(dataValue.substring(76, 80))},
{measurementId: '4210', timestamp: collectTime, motionId, type: 'AccelerometerX', measurementValue: getSensorValue(dataValue.substring(80, 84))},
{measurementId: '4211', timestamp: collectTime, motionId, type: 'AccelerometerY', measurementValue: getSensorValue(dataValue.substring(84, 88))},
{measurementId: '4212', timestamp: collectTime, motionId, type: 'AccelerometerZ', measurementValue: getSensorValue(dataValue.substring(88, 92))},
{measurementId: '3000', timestamp: collectTime, motionId, type: 'Battery', measurementValue: getBattery(dataValue.substring(92, 94))}
]
break
// BLE定位数据+sensor+三轴+电量
case '1C':
collectTime = getUTCTimestamp(dataValue.substring(8, 16))
motionId = getMotionId(dataValue.substring(6, 8))
measurementArray = [
{measurementId: '4200', timestamp: collectTime, motionId, type: 'Event Status', measurementValue: getEventStatus(dataValue.substring(0, 6))},
{measurementId: '5002', timestamp: collectTime, motionId, type: 'BLE Scan', measurementValue: getMacAndRssiObj(dataValue.substring(16, 58))},
{measurementId: '4097', timestamp: collectTime, motionId, type: 'Air Temperature', measurementValue: getSensorValue(dataValue.substring(58, 62), 10)},
{measurementId: '4199', timestamp: collectTime, motionId, type: 'Light', measurementValue: getSensorValue(dataValue.substring(62, 66))},
{measurementId: '4210', timestamp: collectTime, motionId, type: 'AccelerometerX', measurementValue: getSensorValue(dataValue.substring(66, 70))},
{measurementId: '4211', timestamp: collectTime, motionId, type: 'AccelerometerY', measurementValue: getSensorValue(dataValue.substring(70, 74))},
{measurementId: '4212', timestamp: collectTime, motionId, type: 'AccelerometerZ', measurementValue: getSensorValue(dataValue.substring(74, 78))},
{measurementId: '3000', timestamp: collectTime, motionId, type: 'Battery', measurementValue: getBattery(dataValue.substring(78, 80))}
]
break
// 定位状态 + sensor+三轴数据上报
case '1D':
collectTime = getUTCTimestamp(dataValue.substring(8, 16))
measurementArray.push({
measurementId: '3576',
timestamp: collectTime,
type: 'Positioning Status',
measurementValue: getPositingStatus(dataValue.substring(0, 2))
})
measurementArray.push({
timestamp: collectTime,
measurementId: '4200',
type: 'Event Status',
measurementValue: getEventStatus(dataValue.substring(2, 8))
})
if (!isNaN(parseFloat(getSensorValue(dataValue.substring(16, 20), 10)))) {
measurementArray.push({
timestamp: collectTime,
measurementId: '4097',
type: 'Air Temperature',
measurementValue: getSensorValue(dataValue.substring(16, 20), 10)
})
}
if (!isNaN(parseFloat(getSensorValue(dataValue.substring(20, 24))))) {
measurementArray.push({
timestamp: collectTime,
measurementId: '4199',
type: 'Light',
measurementValue: getSensorValue(dataValue.substring(20, 24))
})
}
measurementArray.push({
timestamp: collectTime,
measurementId: '4210',
type: 'AccelerometerX',
measurementValue: getSensorValue(dataValue.substring(24, 28))
})
measurementArray.push({
timestamp: collectTime,
measurementId: '4211',
type: 'AccelerometerY',
measurementValue: getSensorValue(dataValue.substring(28, 32))
})
measurementArray.push({
timestamp: collectTime,
measurementId: '4212',
type: 'AccelerometerZ',
measurementValue: getSensorValue(dataValue.substring(32, 36))
})
measurementArray.push({
timestamp: collectTime,
measurementId: '3000',
type: 'Battery',
measurementValue: getBattery(dataValue.substring(36, 38))
})
break
}
return measurementArray
}
function getMotionId (str) {
return getInt(str)
}
function getPositingStatus (str) {
let status = getInt(str)
switch (status) {
case 0:
return {id:status, statusName:"Positioning successful."}
case 1:
return {id:status, statusName:"The GNSS scan timed out and failed to obtain the location."}
case 2:
return {id:status, statusName:"The Wi-Fi scan timed out and failed to obtain the location."}
case 3:
return {id:status, statusName:"The Wi-Fi + GNSS scan timed out and failed to obtain the location."}
case 4:
return {id:status, statusName:"The GNSS + Wi-Fi scan timed out and failed to obtain the location."}
case 5:
return {id:status, statusName:"The Bluetooth scan timed out and failed to obtain the location."}
case 6:
return {id:status, statusName:"The Bluetooth + Wi-Fi scan timed out and failed to obtain the location."}
case 7:
return {id:status, statusName:"The Bluetooth + GNSS scan timed out and failed to obtain the location."}
case 8:
return {id:status, statusName:"The Bluetooth + Wi-Fi + GNSS scan timed out and failed to obtain the location."}
case 9:
return {id:status, statusName:"Location Server failed to parse the GNSS location."}
case 10:
return {id:status, statusName:"Location Server failed to parse the Wi-Fi location."}
case 11:
return {id:status, statusName:"Location Server failed to parse the Bluetooth location."}
case 12:
return {id:status, statusName:"Failed to parse the GNSS location due to the poor accuracy."}
case 13:
return {id:status, statusName:"Time synchronization failed."}
case 14:
return {id:status, statusName:"Failed to obtain location due to the old Almanac."}
}
return getInt(str)
}
function getUpShortInfo (messageValue) {
return [
{
measurementId: '3000', type: 'Battery', measurementValue: getBattery(messageValue.substring(0, 2))
}, {
measurementId: '3502', type: 'Firmware Version', measurementValue: getSoftVersion(messageValue.substring(2, 6))
}, {
measurementId: '3001', type: 'Hardware Version', measurementValue: getHardVersion(messageValue.substring(6, 10))
}, {
measurementId: '3940', type: 'Work Mode', measurementValue: getWorkingMode(messageValue.substring(10, 12))
}, {
measurementId: '3965', type: 'Positioning Strategy', measurementValue: getPositioningStrategy(messageValue.substring(12, 14))
}, {
measurementId: '3942', type: 'Heartbeat Interval', measurementValue: getMinsByMin(messageValue.substring(14, 18))
}, {
measurementId: '3943', type: 'Periodic Interval', measurementValue: getMinsByMin(messageValue.substring(18, 22))
}, {
measurementId: '3944', type: 'Event Interval', measurementValue: getMinsByMin(messageValue.substring(22, 26))
}, {
measurementId: '3945', type: 'Sensor Enable', measurementValue: getInt(messageValue.substring(26, 28))
}, {
measurementId: '3941', type: 'SOS Mode', measurementValue: getSOSMode(messageValue.substring(28, 30))
}
]
}
function getMotionSetting (str) {
return [
{measurementId: '3946', type: 'Motion Enable', measurementValue: getInt(str.substring(0, 2))},
{measurementId: '3947', type: 'Any Motion Threshold', measurementValue: getSensorValue(str.substring(2, 6), 1)},
{measurementId: '3948', type: 'Motion Start Interval', measurementValue: getMinsByMin(str.substring(6, 10))},
]
}
function getStaticSetting (str) {
return [
{measurementId: '3949', type: 'Static Enable', measurementValue: getInt(str.substring(0, 2))},
{measurementId: '3950', type: 'Device Static Timeout', measurementValue: getMinsByMin(str.substring(2, 6))}
]
}
function getShockSetting (str) {
return [
{measurementId: '3951', type: 'Shock Enable', measurementValue: getInt(str.substring(0, 2))},
{measurementId: '3952', type: 'Shock Threshold', measurementValue: getInt(str.substring(2, 6))}
]
}
function getTempSetting (str) {
return [
{measurementId: '3953', type: 'Temp Enable', measurementValue: getInt(str.substring(0, 2))},
{measurementId: '3954', type: 'Event Temp Interval', measurementValue: getMinsByMin(str.substring(2, 6))},
{measurementId: '3955', type: 'Event Temp Sample Interval', measurementValue: getSecondsByInt(str.substring(6, 10))},
{measurementId: '3956', type: 'Temp ThMax', measurementValue: getSensorValue(str.substring(10, 14), 10)},
{measurementId: '3957', type: 'Temp ThMin', measurementValue: getSensorValue(str.substring(14, 18), 10)},
{measurementId: '3958', type: 'Temp Warning Type', measurementValue: getInt(str.substring(18, 20))}
]
}
function getLightSetting (str) {
return [
{measurementId: '3959', type: 'Light Enable', measurementValue: getInt(str.substring(0, 2))},
{measurementId: '3960', type: 'Event Light Interval', measurementValue: getMinsByMin(str.substring(2, 6))},
{measurementId: '3961', type: 'Event Light Sample Interval', measurementValue: getSecondsByInt(str.substring(6, 10))},
{measurementId: '3962', type: 'Light ThMax', measurementValue: getSensorValue(str.substring(10, 14), 10)},
{measurementId: '3963', type: 'Light ThMin', measurementValue: getSensorValue(str.substring(14, 18), 10)},
{measurementId: '3964', type: 'Light Warning Type', measurementValue: getInt(str.substring(18, 20))}
]
}
function getShardFlag (str) {
let bitStr = getByteArray(str)
return {
count: parseInt(bitStr.substring(0, 4), 2),
index: parseInt(bitStr.substring(4), 2)
}
}
function getBattery (batteryStr) {
return loraWANV2DataFormat(batteryStr)
}
function getSoftVersion (softVersion) {
return `${loraWANV2DataFormat(softVersion.substring(0, 2))}.${loraWANV2DataFormat(softVersion.substring(2, 4))}`
}
function getHardVersion (hardVersion) {
return `${loraWANV2DataFormat(hardVersion.substring(0, 2))}.${loraWANV2DataFormat(hardVersion.substring(2, 4))}`
}
function getSecondsByInt (str) {
return getInt(str)
}
function getMinsByMin (str) {
return getInt(str)
}
function getSensorValue (str, dig) {
if (str === '8000') {
return null
} else {
return loraWANV2DataFormat(str, dig)
}
}
function bytes2HexString (arrBytes) {
var str = ''
for (var i = 0; i < arrBytes.length; i++) {
var tmp
var num = arrBytes[i]
if (num < 0) {
tmp = (255 + num + 1).toString(16)
} else {
tmp = num.toString(16)
}
if (tmp.length === 1) {
tmp = '0' + tmp
}
str += tmp
}
return str
}
function loraWANV2DataFormat (str, divisor = 1) {
let strReverse = bigEndianTransform(str)
let str2 = toBinary(strReverse)
if (str2.substring(0, 1) === '1') {
let arr = str2.split('')
let reverseArr = arr.map((item) => {
if (parseInt(item) === 1) {
return 0
} else {
return 1
}
})
str2 = parseInt(reverseArr.join(''), 2) + 1
return parseFloat('-' + str2 / divisor)
}
return parseInt(str2, 2) / divisor
}
function bigEndianTransform (data) {
let dataArray = []
for (let i = 0; i < data.length; i += 2) {
dataArray.push(data.substring(i, i + 2))
}
return dataArray
}
function toBinary (arr) {
let binaryData = arr.map((item) => {
let data = parseInt(item, 16)
.toString(2)
let dataLength = data.length
if (data.length !== 8) {
for (let i = 0; i < 8 - dataLength; i++) {
data = `0` + data
}
}
return data
})
return binaryData.toString().replace(/,/g, '')
}
function getSOSMode (str) {
return loraWANV2DataFormat(str)
}
function getMacAndRssiObj (pair) {
let pairs = []
if (pair.length % 14 === 0) {
for (let i = 0; i < pair.length; i += 14) {
let mac = getMacAddress(pair.substring(i, i + 12))
if (mac) {
let rssi = getInt8RSSI(pair.substring(i + 12, i + 14))
pairs.push({mac: mac, rssi: rssi})
} else {
continue
}
}
}
return pairs
}
function getMacAddress (str) {
if (str.toLowerCase() === 'ffffffffffff') {
return null
}
let macArr = []
for (let i = 1; i < str.length; i++) {
if (i % 2 === 1) {
macArr.push(str.substring(i - 1, i + 1))
}
}
let mac = ''
for (let i = 0; i < macArr.length; i++) {
mac = mac + macArr[i]
if (i < macArr.length - 1) {
mac = mac + ':'
}
}
return mac
}
function getInt8RSSI (str) {
return loraWANV2DataFormat(str)
}
function getInt (str) {
return parseInt(str, 16)
}
function getEventStatus (str) {
// return getInt(str)
let bitStr = getByteArray(str)
let bitArr = []
for (let i = 0; i < bitStr.length; i++) {
bitArr[i] = bitStr.substring(i, i + 1)
}
bitArr = bitArr.reverse()
let event = []
for (let i = 0; i < bitArr.length; i++) {
if (bitArr[i] !== '1') {
continue
}
switch (i){
case 0:
event.push({id:1, eventName:"Start moving event."})
break
case 1:
event.push({id:2, eventName:"End movement event."})
break
case 2:
event.push({id:3, eventName:"Motionless event."})
break
case 3:
event.push({id:4, eventName:"Shock event."})
break
case 4:
event.push({id:5, eventName:"Temperature event."})
break
case 5:
event.push({id:6, eventName:"Light event."})
break
case 6:
event.push({id:7, eventName:"SOS event."})
break
case 7:
event.push({id:8, eventName:"Press once event."})
break
}
}
return event
}
function getByteArray (str) {
let bytes = []
for (let i = 0; i < str.length; i += 2) {
bytes.push(str.substring(i, i + 2))
}
return toBinary(bytes)
}
function getWorkingMode (workingMode) {
return getInt(workingMode)
}
function getPositioningStrategy (strategy) {
return getInt(strategy)
}
function getUTCTimestamp(str){
return parseInt(loraWANV2PositiveDataFormat(str)) * 1000
}
function loraWANV2PositiveDataFormat (str, divisor = 1) {
let strReverse = bigEndianTransform(str)
let str2 = toBinary(strReverse)
return parseInt(str2, 2) / divisor
}

View File

@ -1,10 +1,18 @@
# Database
DB_NAME=""
DB_USER=""
DB_PASSWORD=""
DB_HOST=""
DB_DIALECT=""
DB_PORT=""
DB_HOST="localhost"
DB_DIALECT="mariadb"
DB_PORT="3306"
# Server
PORT="3000" #Port the server runs on, match with your reverse proxy config
# TTN Webhook
WEBHOOK_TOKEN="" #Token that is placed a the TTN Webhook auth
# Wigle API
WIGLE_TOKEN="" # Go to account and generate token "Encoded for use"
WIGLE_BASE_URL="https://api.wigle.net"
WIGLE_NETWORK_SEARCH="/api/v2/network/search"

5
server/.gitignore vendored
View File

@ -308,7 +308,4 @@ cython_debug/
# Built Visual Studio Code Extensions
*.vsix
config.py
#docker
docker-compose.yml
config.py

View File

@ -14,6 +14,7 @@
"express": "^4.21.2",
"http-status-codes": "^2.3.0",
"mariadb": "^3.4.0",
"prom-client": "^15.1.3",
"reflect-metadata": "^0.2.2",
"sequelize": "^6.37.5",
"swagger-jsdoc": "^6.2.8",
@ -120,6 +121,15 @@
"integrity": "sha512-4JQNk+3mVzK3xh2rqd6RB4J46qUR19azEHBneZyTZM+c456qOrbbM/5xcR8huNCCcbVt7+UmizG6GuUvPvKUYg==",
"license": "MIT"
},
"node_modules/@opentelemetry/api": {
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/@opentelemetry/api/-/api-1.9.0.tgz",
"integrity": "sha512-3giAOQvZiH5F9bMlMiv8+GSPMeqg0dbaeo58/0SlA9sxSqZhnUtxzX9/2FzyhS9sWQf5S0GJE0AKBrFqjpeYcg==",
"license": "Apache-2.0",
"engines": {
"node": ">=8.0.0"
}
},
"node_modules/@scarf/scarf": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/@scarf/scarf/-/scarf-1.4.0.tgz",
@ -386,6 +396,12 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/bintrees": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/bintrees/-/bintrees-1.0.2.tgz",
"integrity": "sha512-VOMgTMwjAaUG580SXn3LacVgjurrbMme7ZZNYGSSV7mmtY6QQRh0Eg3pwIcntQ77DErK1L0NxkbetjcoXzVwKw==",
"license": "MIT"
},
"node_modules/body-parser": {
"version": "1.20.3",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.3.tgz",
@ -1434,6 +1450,19 @@
"url": "https://github.com/sponsors/jonschlinkert"
}
},
"node_modules/prom-client": {
"version": "15.1.3",
"resolved": "https://registry.npmjs.org/prom-client/-/prom-client-15.1.3.tgz",
"integrity": "sha512-6ZiOBfCywsD4k1BN9IX0uZhF+tJkV8q8llP64G5Hajs4JOeVLPCwpPVcpXy3BwYiUGgyJzsJJQeOIv7+hDSq8g==",
"license": "Apache-2.0",
"dependencies": {
"@opentelemetry/api": "^1.4.0",
"tdigest": "^0.1.1"
},
"engines": {
"node": "^16 || ^18 || >=20"
}
},
"node_modules/proxy-addr": {
"version": "2.0.7",
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
@ -1873,6 +1902,15 @@
"express": ">=4.0.0 || >=5.0.0-beta"
}
},
"node_modules/tdigest": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/tdigest/-/tdigest-0.1.2.tgz",
"integrity": "sha512-+G0LLgjjo9BZX2MfdvPfH+MKLCrxlXSYec5DaPYP1fe6Iyhf0/fSmJ0bFiZ1F8BT6cGXl2LpltQptzjXKWEkKA==",
"license": "MIT",
"dependencies": {
"bintrees": "1.0.2"
}
},
"node_modules/to-regex-range": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz",

View File

@ -24,6 +24,7 @@
"express": "^4.21.2",
"http-status-codes": "^2.3.0",
"mariadb": "^3.4.0",
"prom-client": "^15.1.3",
"reflect-metadata": "^0.2.2",
"sequelize": "^6.37.5",
"swagger-jsdoc": "^6.2.8",

View File

@ -0,0 +1,22 @@
[Unit]
Description=LocationHub
After=network.target systemd-networkd-wait-online.service mysqld.service
[Service]
Type=simple
User=locationhub
WorkingDirectory=/home/locationhub/git/LocationHub/server/
ExecStart=/usr/bin/npm run dev
Restart=on-failure
StandardOutput=append:/var/log/LocationHub.log
StandardError=append:/var/log/LocationHub.log
[Install]
WantedBy=multi-user.target
```
Activate Systemd Job
```
systemctl daemon-reload
systemctl enable locationhub.service
systemctl start locationhub.service
systemctl status locationhub.service

View File

@ -5,7 +5,7 @@ CREATE TABLE IF NOT EXISTS lp_ttn_end_device_uplinks (
dev_eui VARCHAR(255),
join_eui VARCHAR(255),
dev_addr VARCHAR(255),
received_at_utc DATE,
received_at_utc DATE NOT NULL,
battery NUMERIC,
created_at_utc TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at_utc TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
@ -16,6 +16,7 @@ CREATE TABLE IF NOT EXISTS wifi_scan (
lp_ttn_end_device_uplinks_id UUID,
mac VARCHAR(255),
rssi NUMERIC,
scanned_at_utc TIMESTAMP NOT NULL,
created_at_utc TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at_utc TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (lp_ttn_end_device_uplinks_id) REFERENCES lp_ttn_end_device_uplinks(lp_ttn_end_device_uplinks_id)
@ -63,6 +64,7 @@ CREATE TABLE IF NOT EXISTS location (
gnss_longitude DOUBLE,
ttn_gw_latitude DOUBLE,
ttn_gw_longitude DOUBLE,
gnss_location_at_utc TIMESTAMP,
created_at_utc TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at_utc TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (lp_ttn_end_device_uplinks_id) REFERENCES lp_ttn_end_device_uplinks(lp_ttn_end_device_uplinks_id)

View File

@ -0,0 +1,63 @@
import express, { Request, Response } from "express";
import { container } from "tsyringe";
import { Counter, Gauge, collectDefaultMetrics, register } from "prom-client";
import { LocationService } from "../services/locationService";
import { WifiLocationService } from "../services/wifiLocationService";
import { TtnGatewayReceptionService } from "../services/ttnGatewayReceptionService";
import { MetricsService } from "../services/metricsService";
const router = express.Router();
const locationService = container.resolve(LocationService);
const wifiLocationService = container.resolve(WifiLocationService);
const ttnGatewayReceptionService = container.resolve(TtnGatewayReceptionService);
const metricsService = container.resolve(MetricsService);
const requestCounter = metricsService.getRequestCounter();
const locationsTotal = metricsService.getLocationsTotal();
const gnssLocationsTotal = metricsService.getGnssLocationsTotal()
const wifiLocationTotal = metricsService.getWifiLocationTotal();
const wifiLocationNotResolvable = metricsService.getWifiLocationResolvable();
const wifiLocationRequestLimitExceeded = metricsService.getLWifiLocationRequestLimitExceeded();
const ttnGatewayReceptions = metricsService.getTnnGatewayReceptions();
// Define the metrics endpoint
router.get("/", async (req: Request, res: Response) => {
try {
const [
allLocations,
gnssLocations,
allWifiLocations,
wifiLocationsNotResolvable,
wifiLocationsRequestLimitExceeded,
allTtnGatewayReceptions
] = await Promise.all([
locationService.getAllLocations(),
locationService.getAllGnssLocations(),
wifiLocationService.getAllWifiLocations(),
wifiLocationService.getAllWifiLocationsByNotResolvable(),
wifiLocationService.getAllWifiLocationsByRequestLimitExceeded(),
ttnGatewayReceptionService.getAllGatewayReceptions()
]);
locationsTotal.set(allLocations.length);
gnssLocationsTotal.set(gnssLocations.length);
wifiLocationTotal.set(allWifiLocations.length);
wifiLocationNotResolvable.set(wifiLocationsNotResolvable.length);
wifiLocationRequestLimitExceeded.set(wifiLocationsRequestLimitExceeded.length);
ttnGatewayReceptions.set(allTtnGatewayReceptions.length);
// Increment the counter with labels
requestCounter.inc({ method: req.method, route: req.route.path, status: 200 });
// Expose metrics in Prometheus format
res.set("Content-Type", register.contentType);
res.send(await register.metrics());
} catch (error) {
// Increment the counter for errors
requestCounter.inc({ method: req.method, route: req.route.path, status: 500 });
console.error("Error running metrics endpoint:", error);
res.status(500).json({ error: "Error running metrics endpoint" });
}
});
export default router;

View File

@ -42,23 +42,38 @@ router.post(
)?.measurementValue,
});
const messageData = message.uplink_message.decoded_payload?.messages[0];
const latitudeData = messageData?.find((e) => e.type === "Latitude");
const longitudeData = messageData?.find((e) => e.type === "Longitude");
const gnnsLocation = {
latitude: message.uplink_message.decoded_payload?.messages[0].find(
(e) => e.type === "Latitude"
)?.measurementValue,
longitude: message.uplink_message.decoded_payload?.messages[0].find(
(e) => e.type === "Longitude"
)?.measurementValue,
latitude: latitudeData?.measurementValue,
longitude: longitudeData?.measurementValue,
};
const gnssTimestamp = {
timestamp: latitudeData?.timestamp
? new Date(latitudeData.timestamp)
: longitudeData?.timestamp
? new Date(longitudeData.timestamp)
: undefined,
};
const wifiMessage =
message.uplink_message.decoded_payload?.messages[0].find(
(e) => e.type === "Wi-Fi Scan"
);
const wifiScans =
message.uplink_message.decoded_payload?.messages[0]
.find((e) => e.type === "Wi-Fi Scan")
?.measurementValue?.map((w) => ({
lp_ttn_end_device_uplinks_id,
mac: w.mac,
rssi: w.rssi,
})) ?? [];
wifiMessage?.measurementValue?.map((w) => ({
lp_ttn_end_device_uplinks_id,
mac: w.mac,
rssi: w.rssi,
scanned_at_utc: wifiMessage?.timestamp
? new Date(wifiMessage.timestamp)
: undefined,
})) ?? [];
const ttnGatewayReceptions = message.uplink_message.rx_metadata.map(
(g) => ({
@ -98,6 +113,7 @@ router.post(
longitude: gnnsLocation.longitude,
}
: undefined,
gnss_timestamp: gnssTimestamp.timestamp,
});
};
createDatabaseEntries().then();

View File

@ -10,6 +10,7 @@ import ttnGatewayReceptionRoutes from "./controller/ttnGatewayReceptionControlle
import wifiLocationRoutes from "./controller/wifiLocationController";
import wifiLocationHistoryRoutes from "./controller/wifiLocationHistoryController";
import wifiScanRoutes from "./controller/wifiScanController";
import metricsRoutes from "./controller/metricsController";
dotenv.config();
@ -26,6 +27,7 @@ app.use("/api/wifi-location", wifiLocationRoutes);
app.use("/api/wifi-scans", wifiScanRoutes);
app.use("/api/locations", locationRoutes);
app.use("/api/ttn", ttnRoutes);
app.use("/api/metrics", metricsRoutes);
app.listen(PORT, () => {
console.log(`🚀 Server runs here: http://localhost:${PORT}`);

View File

@ -10,6 +10,7 @@ export class Location extends Model {
public gnss_longitude!: number;
public ttn_gw_latitude!: number;
public ttn_gw_longitude!: number;
public gnss_location_at_utc!: Date;
public created_at_utc!: Date;
public updated_at_utc!: Date;
}
@ -42,6 +43,9 @@ Location.init(
ttn_gw_longitude: {
type: DataTypes.NUMBER,
},
gnss_location_at_utc: {
type: DataTypes.DATE,
},
created_at_utc: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,

View File

@ -6,6 +6,7 @@ export class WifiScan extends Model {
public wifi_scan_id!: string;
public mac!: string;
public rssi!: number;
public scanned_at_utc!: Date;
public created_at_utc!: Date;
public updated_at_utc!: Date;
}
@ -30,6 +31,11 @@ WifiScan.init(
type: DataTypes.NUMBER,
allowNull: false,
},
scanned_at_utc: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false,
},
created_at_utc: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,

View File

@ -1,10 +1,11 @@
import { Attributes, FindOptions } from "sequelize";
import { injectable } from "tsyringe";
import { Location } from "../models/location";
@injectable()
export class LocationRepository {
public async findAll() {
return await Location.findAll();
public async findAll(options?: FindOptions<Attributes<Location>>) {
return await Location.findAll(options);
}
public async findById(id: string) {

View File

@ -1,4 +1,5 @@
import { inject, injectable } from "tsyringe";
import { Op } from 'sequelize';
import { Location } from "../models/location";
import { LocationRepository } from "../repositories/locationRepository";
import { WifiLocationService } from "./wifiLocationService";
@ -8,6 +9,7 @@ interface CreateLocationParams {
wifi?: Coordinates;
gnss?: Coordinates;
ttn_gw?: Coordinates;
gnss_timestamp?: Date;
}
interface CreateLocationTriangulationParams {
@ -18,6 +20,7 @@ interface CreateLocationTriangulationParams {
}[];
ttn_gw: LocationSignal[];
gnss?: Coordinates;
gnss_timestamp?: Date;
}
interface LocationSignal extends Coordinates {
@ -52,6 +55,15 @@ export class LocationService {
return this.repository.findAll();
}
public async getAllGnssLocations() {
return this.repository.findAll({
where: {
gnss_latitude: { [Op.ne]: null },
gnss_longitude: { [Op.ne]: null }
}
});
}
public async getLocationById(id: string) {
return this.repository.findById(id);
}
@ -65,6 +77,7 @@ export class LocationService {
ttn_gw_longitude: data.ttn_gw?.longitude,
gnss_latitude: data.gnss?.latitude,
gnss_longitude: data.gnss?.longitude,
gnss_location_at_utc: data.gnss_timestamp,
});
}
@ -81,6 +94,7 @@ export class LocationService {
wifi: wifi_location,
ttn_gw: gateway_location,
gnss: data.gnss,
gnss_timestamp: data.gnss_timestamp,
});
}

View File

@ -0,0 +1,70 @@
import {injectable } from "tsyringe";
import { Counter, Gauge, collectDefaultMetrics} from "prom-client";
// Collect default system metrics (e.g., CPU, memory usage)
const prefix = 'locationhub_';
collectDefaultMetrics({ prefix });
@injectable()
export class MetricsService {
constructor(
) { }
public getRequestCounter() {
const requestCounter = new Counter({
name: `${prefix}http_requests_total`,
help: "Total number of HTTP requests",
labelNames: ["method", "route", "status"],
});
return requestCounter;
}
public getLocationsTotal() {
const locationsTotal = new Gauge({
name: `${prefix}locations_total`,
help: "Total number of location entries in database",
labelNames: ["database"],
});
return locationsTotal;
}
public getGnssLocationsTotal() {
const gnssLocationsTotal = new Gauge({
name: `${prefix}gnss_locations_total`,
help: "Total number of location entries with GNSS in database",
labelNames: ["database"],
});
return gnssLocationsTotal;
}
public getWifiLocationTotal() {
const wifiLocationTotal = new Gauge({
name: `${prefix}wifi_locations_total`,
help: "Total number of wifi location entries in database",
labelNames: ["database"],
});
return wifiLocationTotal;
}
public getWifiLocationResolvable() {
const wifiLocationNotResolvable = new Gauge({
name: `${prefix}wifi_locations_not_resolvable`,
help: "Unresolved number of wifi location entries in database",
labelNames: ["database"],
});
return wifiLocationNotResolvable;
}
public getLWifiLocationRequestLimitExceeded() {
const wifiLocationRequestLimitExceeded = new Gauge({
name: `${prefix}wifi_locations_request_limit_exceeded`,
help: "Unresolved number of wifi location because request limit exceeded entries in database",
labelNames: ["database"],
});
return wifiLocationRequestLimitExceeded;
}
public getTnnGatewayReceptions() {
const ttnGatewayReceptions = new Gauge({
name: `${prefix}ttn_gateway_receptions`,
help: "Total number of TTN Gateway receptions entries in database",
labelNames: ["database"],
});
return ttnGatewayReceptions;
}
}

View File

@ -29,6 +29,18 @@ export class WifiLocationService {
});
}
public async getAllWifiLocationsByNotResolvable() {
return this.repository.findAll({
where: { location_not_resolvable: true },
});
}
public async getAllWifiLocationsByRequestLimitExceeded() {
return this.repository.findAll({
where: { request_limit_exceeded: true },
});
}
public async getWifiLocationByMac(mac: string) {
let wifiLocation = await this.repository.findById(mac);

View File

@ -5,12 +5,14 @@ interface CreateWifiScanParams {
lp_ttn_end_device_uplinks_id: string;
mac: string;
rssi: number;
scanned_at_utc?: Date;
}
interface UpdateWifiScanParams {
wifi_scan_id: string;
mac?: string;
rssi?: number;
scanned_at_utc?: Date;
}
@injectable()