-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Julien
committed
Apr 24, 2024
0 parents
commit d7bd48a
Showing
6 changed files
with
1,200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FROM node:21-alpine3.18 | ||
# Create app directory | ||
WORKDIR /usr/node/app | ||
COPY package*.json ./ | ||
RUN npm install | ||
COPY . . | ||
EXPOSE 8081 | ||
CMD [ "npm", "run", "start" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
services: | ||
teslamate: | ||
image: teslamate/teslamate:latest | ||
restart: always | ||
environment: | ||
- ENCRYPTION_KEY=secretkey #replace with a secure key to encrypt your Tesla API tokens | ||
- DATABASE_USER=teslamate | ||
- DATABASE_PASS=password #insert your secure database password! | ||
- DATABASE_NAME=teslamate | ||
- DATABASE_HOST=database | ||
- MQTT_HOST=mosquitto | ||
ports: | ||
- 4000:4000 | ||
volumes: | ||
- ./import:/opt/app/import | ||
cap_drop: | ||
- all | ||
database: | ||
image: postgres:15 | ||
restart: always | ||
environment: | ||
- POSTGRES_USER=teslamate | ||
- POSTGRES_PASSWORD=password #insert your secure database password! | ||
- POSTGRES_DB=teslamate | ||
volumes: | ||
- teslamate-db:/var/lib/postgresql/data | ||
|
||
grafana: | ||
image: teslamate/grafana:latest | ||
restart: always | ||
environment: | ||
- DATABASE_USER=teslamate | ||
- DATABASE_PASS=password #insert your secure database password! | ||
- DATABASE_NAME=teslamate | ||
- DATABASE_HOST=database | ||
ports: | ||
- 3000:3000 | ||
volumes: | ||
- teslamate-grafana-data:/var/lib/grafana | ||
|
||
mosquitto: | ||
image: eclipse-mosquitto:2 | ||
restart: always | ||
command: mosquitto -c /mosquitto-no-auth.conf | ||
# ports: | ||
# - 1883:1883 | ||
volumes: | ||
- mosquitto-conf:/mosquitto/config | ||
- mosquitto-data:/mosquitto/data | ||
|
||
volumes: | ||
teslamate-db: | ||
teslamate-grafana-data: | ||
mosquitto-conf: | ||
mosquitto-data: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
const WebSocket = require('ws'); | ||
const { PubSub } = require('@google-cloud/pubsub'); | ||
|
||
// Création du serveur WebSocket | ||
const wss = new WebSocket.Server({ port: 8081 }); | ||
|
||
// Spécifiez le chemin vers votre fichier key.json | ||
const credentials = process.env.CREDENTIALS; | ||
const projectId = 'telemetry-420222'; | ||
const subscriptionName = 'subTest'; | ||
|
||
// Créez une instance Pub/Sub en utilisant le fichier key.json | ||
const pubsub = new PubSub({ projectId, credentials }); | ||
|
||
let tags = {}; | ||
// Fonction pour envoyer un message à tous les clients WebSocket connectés | ||
function broadcastMessage(message) { | ||
try { | ||
const jsonData = JSON.parse(message); | ||
const associativeArray = {}; | ||
|
||
// Itérer sur les données pour extraire les clés et les valeurs | ||
jsonData.data.forEach(item => { | ||
// Vérifier si la valeur est un objet locationValue | ||
if (item.value.locationValue) { | ||
// Si oui, enregistrer l'objet locationValue directement | ||
associativeArray['Latitude'] = item.value.locationValue.latitude; | ||
associativeArray['Longitude'] = item.value.locationValue.longitude; | ||
} else { | ||
// Sinon, enregistrer la valeur stringValue | ||
associativeArray[item.key] = item.value.stringValue; | ||
} | ||
}); | ||
|
||
const r = { | ||
msg_type: "data:update", | ||
tag: jsonData.vin, | ||
value: [ | ||
new Date(jsonData.createdAt).getTime(), //Date.now(), // time | ||
associativeArray['VehicleSpeed'] ?? 0, // speed | ||
associativeArray['Odometer'], // odometer | ||
associativeArray['Soc'], // soc | ||
0, // elevation ? | ||
associativeArray['GpsHeading'] || 0, // est_heading ? | ||
associativeArray['Latitude'], // est_lat | ||
associativeArray['Longitude'], // est_lng | ||
(associativeArray['ACChargingPower'] != '0' || associativeArray['DCChargingPower'] != '0') ? 1 : 0, // power | ||
associativeArray['Gear'] ?? 0, // 0 shift_state | ||
associativeArray['RatedRange'], // range | ||
associativeArray['EstBatteryRange'], // est_range | ||
associativeArray['GpsHeading'] || 0 // heading | ||
].join(',') | ||
}; | ||
console.log(r); | ||
|
||
wss.clients.forEach(function each(client) { | ||
if (tags[r.tag] && client == tags[r.tag] && client.readyState === WebSocket.OPEN) { | ||
client.send(JSON.stringify(r)); | ||
} | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
// Écoute des connexions entrantes | ||
wss.on('connection', function connection(ws, req) { | ||
console.log('Nouvelle connexion établie.'); | ||
|
||
// Écouter le message contenant le nom du client | ||
ws.on('open', function incoming(open) { | ||
console.log('Open : %s', message); | ||
}); | ||
|
||
// Écouter le message contenant le nom du client | ||
ws.on('message', function incoming(message) { | ||
console.log('Data : %s', message); | ||
const js = JSON.parse(message); | ||
if(js.msg_type == 'data:subscribe_oauth') { | ||
tags[js.tag] = ws; | ||
} | ||
ws.send(JSON.stringify({ | ||
msg_type: "control:hello", | ||
connection_timeout: 30000 | ||
})); | ||
}); | ||
|
||
// Gérer la fermeture de la connexion | ||
ws.on('close', function close() { | ||
console.log('Connexion fermée.'); | ||
let keys = Object.keys(tags); | ||
for (i = 0; i < keys.length; i++) { | ||
if (this == tags[keys[i]]) { | ||
delete tags[keys[i]]; | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
// Écouter les messages Pub/Sub | ||
pubsub | ||
.subscription(subscriptionName) | ||
.on('message', (message) => { | ||
// Envoyer le message reçu à tous les clients WebSocket connectés | ||
broadcastMessage(message.data.toString('utf-8')); | ||
message.ack(); | ||
}); |
Oops, something went wrong.