Nodejs Files

These are the nodejs files required for the node-red flow to work.
dj_add.js
module.exports = msg => {
//Obtaining the libraries needed
yaml = require('js-yaml');
fs = require('fs');
//Get document or output error
try {
var doc = yaml.safeLoad(fs.readFileSync('/home/pi/Desktop/dj_topic.yml', 'utf8'));
console.log(doc.Options.Insert.Youtube_Search);
} catch (e) {
console.log(e);
}
function endOfArr(array) {
var count=0;
for (count; array[count]!=null;count++) {
;
}
return count;
};
var num = endOfArr(doc.Options.Insert.Youtube_Search);
doc.Options.Insert.Youtube_Search[num] = {
Name: msg.payload.Name,
Hour: msg.payload.Hour,
Min: msg.payload.Min
}
var revDoc = yaml.safeDump(doc);
fs.writeFileSync('/home/pi/Desktop/dj_topic.yml',revDoc);
return revDoc;
}
dj_delete.js
module.exports = msg => {
//Obtaining the libraries needed
yaml = require('js-yaml');
fs = require('fs');
//Get document or output error
try {
var doc = yaml.safeLoad(fs.readFileSync('/home/pi/Desktop/dj_topic.yml', 'utf8'));
console.log(doc.Options.Insert.Youtube_Search);
} catch (e) {
console.log(e);
}
var name = msg.payload.Name;
function findNode(array) {
var count = 0;
while (array[count]!=null && array[count].Name!=name) {
count++;
}
return count;
}
var num = findNode(doc.Options.Insert.Youtube_Search);
doc.Options.Insert.Youtube_Search.splice(num,1);
var revDoc = yaml.safeDump(doc);
fs.writeFileSync('/home/pi/Desktop/dj_topic.yml',revDoc);
return revDoc;
}
dj_list.js
module.exports = msg => {
//Obtaining the libraries needed
yaml = require('js-yaml');
fs = require('fs');
//Get document or output error
try {
var doc = yaml.safeLoad(fs.readFileSync('/home/pi/Desktop/dj_topic.yml', 'utf8'));
// console.log(doc.Options.Insert.Youtube_Search);
} catch (e) {
console.log(e);
}
var temp = [];
function convTime (array) {
var count =0;
while (array[count]!=null && array[count].Name!=null) {
var tempHour = array[count].Hour;
var tempMin = array[count].Min;
if (tempMin<10) {
tempMin = "0"+tempMin;
}
if (tempHour<10) {
tempHour = "0"+tempHour;
}
var tempTime = tempHour+":"+tempMin;
temp.push(tempTime);
count++
}
return null;
}
function addName (arraySource, arrayDest) {
var count=0;
while (arrayDest[count]!=null && arraySource[count].Name!=null) {
arrayDest[count] = arraySource[count].Name+" @ "+arrayDest[count];
count++;
}
return null;
}
convTime(doc.Options.Insert.Youtube_Search);
addName(doc.Options.Insert.Youtube_Search,temp);
var str="";
function makeStr (array) {
var count=0;
while (array[count]!=null) {
str += array[count];
str += "\n";
count++;
}
}
makeStr(temp);
console.log(str);
msg.payload = str;
return msg;
}
dj_topic.yaml
Name: SmartScreen
Description: Device used to display images or videos at your convenience.
Options:
Insert:
Images: null
Video_Uploads: null
Youtube_Search:
- Name: Sugar
Hour: '12'
Min: '13'
Delete:
Images: null
Videos: null
Search:
Images: null
Videos: null
Set:
File Name: null