Commit 0d3e157c authored by Nicolai Grymer's avatar Nicolai Grymer 🐤
Browse files

Adding functional script

parent 34cc4c37
/node_modules
package-lock.json
settings.txt
'use strict';
const fs = require('fs');
const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');
const axios = require('axios');
let errorState = false;
readFile();
function readFile() {
fs.readFile('./settings.txt', 'utf8' , (err, data) => {
if (err) {
if(err.code==='ENOENT'){
setupFile();
return;
}
console.error(err);
return
}
log("Opening on port: " + JSON.parse(data).port);
start(JSON.parse(data).port);
});
}
function setupFile() {
log('-------------');
log(`Port can be found in Arduino IDE -> Tools -> Port`)
log(`can also be found in down-right corner of Arduino IDE`)
log('-------------');
log(`Avaliable ports`)
SerialPort.list().then((list) => {list.forEach(obj => process.stdout.write(obj.path + ", "))
console.log();});
askForInfo('Please enter the name of the port used for Arduino:', (data) => {
if(data === 'exit\n'){
process.exit();
} else {
const path = data.replace(/(\r\n|\n|\r)/gm, "");
log('Port set to: ' + path);
writeFile('settings.txt', {port: path}, readFile);
}
});
}
function writeFile(filename, data, callback){
fs.writeFile(filename, JSON.stringify(data), (err) => {
if (err) throw err;
log(`File has been created`);
log('-------------\n');
callback();
});
}
function askForInfo(line, callback){
setTimeout(() => {
var standard_input = process.stdin;
standard_input.setEncoding('utf-8');
log('-------------');
log(line, true);
standard_input.once('data', callback);
},500);
}
function errMessage(err){
console.log('\x1b[31m', '#####################################');
console.log('\x1b[31m', err);
console.log('\x1b[31m', '#####################################', '\x1b[0m');
}
function errOneLine(err){
console.log('\x1b[31m', err, '\x1b[0m');
}
function errBar(){
console.log('\x1b[31m', '#####################################', '\x1b[0m');
}
function portError(err){
const errCode = String(err);
if(errCode.includes('File not found')){
errMessage('Could not open Serial Port!');
setupFile();
} else if(errCode.includes('Access denied')){
errMessage('Serial Port is busy. Close Arduino Terminal or wait for Arduino to finish uploading');
askForInfo('Hit Enter to try again',readFile);
} else {
log(err);
}
}
function start(path){
const port = new SerialPort(path , { baudRate: 115200 });
port.on('error', portError);
const parser = new Readline();
port.pipe(parser);
parser.on('data', line => {
if(line.substring(0,7) === 'http://'){
const input = line.split('|');
const data = JSON.parse(input[1]);
if(data.hue !== undefined){
if(data.hue < 0){
data.hue += 65536;
}
}
sendData(input[0], data);
if(!errorState)
log(`> ${input[0]}::${JSON.stringify(data)}`);
} else {
console.log('\x1b[36m', `> ${line}`, '\x1b[0m');
}
});
}
function sendData(url, data){
if(!errorState){
axios.put(url, data, {timeout: 4000})
.then(function (response) {
if(JSON.parse(response)[0].success === undefined){
errMessage('Connected to the bridge, but did not find bulb! Please check');
ErrOneLine('>>> You use the right bulb ID!');
ErrOneLine('>>> Your bulb has power');
errBar();
triggerErrorState();
} else {
console.log('\x1b[32m', '--- SUCCESSFUL TRANSFER ---', '\x1b[0m');
}
})
.catch(function (error) {
if(!errorState) {
if(error.code === 'ECONNABORTED'){
errMessage('Could not reach bridge. Check the following to fix:');
errOneLine('>>> Join same network as the bridge!');
errOneLine('>>> Turn on the bridge');
errOneLine('>>> Ensure you use the right IP adresse for the bridge');
errBar();
} else {
errOneLine(JSON.stringify(error));
errOneLine('--- SOMETHING WENT WRONG ---');
}
triggerErrorState();
}
});
}
}
function triggerErrorState(){
errorState = true;
clearTimeout(triggerErrorState.timeout);
triggerErrorState.timeout = setTimeout(() => {errorState = false;},2000);
}
function log(str, bool = false){
console.log(bool ? '\x1b[1m' : '\x1b[0m',str);
}
\ No newline at end of file
File added
echo DOWNLOADING AND INSTALLING NECCESSARY FILES
npm i
echo FINISHED
echo Press enter to exit
read varname
\ No newline at end of file
node NodeHue.js
echo Press enter to exit
read varname
\ No newline at end of file
{
"name": "arduino-hue-project",
"version": "1.0.0",
"description": "Ships data to hue from Arduino",
"main": "./main.js",
"repository": {
"type": "git",
"url": "https://gitlab.au.dk/au648662/arduinohue20.git"
},
"dependencies": {
"axios": "0.20.0",
"serialport": "9.0.1"
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment