Last active
March 19, 2020 13:41
-
-
Save Hacksore/4649b21bb6928396301a689d4016b33c to your computer and use it in GitHub Desktop.
bluelinky nodejs
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
const express = require('express'); | |
const BlueLinky = require('bluelinky'); | |
const bodyParser = require('body-parser'); | |
const config = { | |
validation_key: 'something you make', | |
username: 'username', | |
password: 'password', | |
region: 'EU', | |
vin: 'vin', | |
pin: 'pin', | |
}; | |
const app = express(); | |
app.use(bodyParser.json()); | |
let vehicle; | |
const middleWare = async (req, res, next) => { | |
const ip = req.connection.remoteAddress; | |
console.log(req.path, ip); | |
if (req.body.VALIDATION_KEY !== config.validation_key) { | |
console.log('Bad key used by: ' + ip); | |
return res.send({ error: 'bad key' }); | |
} | |
const client = new BlueLinky({ | |
username: config.username, | |
password: config.password, | |
pin: config.pin, | |
region: config.region | |
}); | |
client.on('ready', () => { | |
vehicle = client.getVehicle(config.vin); | |
return next(); | |
}); | |
}; | |
app.use(middleWare); | |
app.post('/start', async (req, res) => { | |
let response; | |
try { | |
response = await vehicle.start({ | |
airCtrl: true, | |
igniOnDuration: 10, | |
airTempvalue: 60 | |
}); | |
} catch (e) { | |
response = { | |
error: e.message | |
}; | |
} | |
res.send(response); | |
}); | |
app.post('/lock', async (req, res) => { | |
let response; | |
try { | |
response = await vehicle.lock(); | |
} catch (e) { | |
console.log(e); | |
response = { | |
error: e.message | |
}; | |
} | |
res.send(response); | |
}); | |
app.post('/status', async (req, res) => { | |
let response; | |
try { | |
response = await vehicle.status(); | |
} catch (e) { | |
console.log(e); | |
response = { | |
error: e.message | |
}; | |
} | |
res.send(response); | |
}); | |
app.listen(8080, '0.0.0.0'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment