Skip to content

Commit

Permalink
include new log file
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar-Raygoza committed Jun 5, 2020
1 parent 0351f56 commit 204de63
Showing 1 changed file with 89 additions and 40 deletions.
129 changes: 89 additions & 40 deletions app/renderer/modules/ports.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,33 @@ import SerialPort from "serialport";
import Readline from "@serialport/parser-readline";
import axios from "axios";

/**
*/
import { userInfo } from "os";
import fs from "fs";

let logFileName;
if (process.platform == "darwin") {
console.log(userInfo().username);
logFileName = `/Users/${userInfo().username}/Documents/ec-catwan-monitor`;
} else if (process.platform == "win32") {
console.log(userInfo().username);
logFileName = `C:\\Users\\${
userInfo().username
}\\Documents\\ec-catwan-monitor`;
} else if (process.platform == "linux") {
console.log(userInfo().username);
logFileName = `/home/ec-catwan-monitor`;
}

fs.mkdir(`${logFileName}`, { recursive: true }, function(err) {
if (err) {
return console.error(err);
}
console.log("Directory created successfully!");
});
console.log(logFileName);

const parser = new Readline();

let _state = store.getState();
Expand All @@ -15,7 +42,7 @@ export const getters = {
console.log("LIST_PORTS()");
console.log(_state.activePort);
let all_ports = [];
await SerialPort.list().then(ports => {
await SerialPort.list().then((ports) => {
ports.forEach(function(port) {
console.log(port.path);
console.log(port.pnpId);
Expand All @@ -32,7 +59,7 @@ export const getters = {
sp.pipe(parser);
console.log(sp);
return sp;
}
},
};

export const actions = {
Expand All @@ -48,65 +75,87 @@ export const actions = {
let __url = url;
let __urlPort = urlPort;
console.table("ENTRY IN FUNCTION");

let timeDate = "init";
let log;

sp.on("open", function(err) {
console.log("open port!");

parser.on("data", function(data) {
try {
let obj = [
{
type: "uplink",
payload: {
adr: false,
applicationID: "1",
applicationName: "Relay",
data: "AXQs7AKABAMDgAQDA4MAAdyQBGcA3A==",
devEUI: "0000000000000000",
deviceName: "Relay1",
fCnt: 1,
fPort: 1,
object: {
d1: data.charAt(0),
d2: data.charAt(1),
d3: data.charAt(2),
},
rxInfo: [
{
gatewayID: "USBStick",
loRaSNR: 2,
location: {
altitude: 0,
latitude: 0,
longitude: 0,
},
name: "USBStick",
rssi: -108,
},
],
txInfo: {
dr: 3,
frequency: 90200000,
},
},
},
];

let date = new Date();

log = JSON.stringify(obj) + "\n";

console.log("Send data port!");
store.dispatch(getDataPort(data));

if (__url != undefined && __urlPort != undefined) {
axios
.post(`${__url}:${__urlPort}`, [
{
type: "uplink",
payload: {
adr: false,
applicationID: "1",
applicationName: "Relay",
data: "AXQs7AKABAMDgAQDA4MAAdyQBGcA3A==",
devEUI: "0000000000000000",
deviceName: "Relay1",
fCnt: 1,
fPort: 1,
object: {
d1: data.charAt(0),
d2: data.charAt(1),
d3: data.charAt(2)
},
rxInfo: [
{
gatewayID: "USBStick",
loRaSNR: 2,
location: {
altitude: 0,
latitude: 0,
longitude: 0
},
name: "USBStick",
rssi: -108
}
],
txInfo: {
dr: 3,
frequency: 90200000
}
}
}
])
.post(`${__url}:${__urlPort}`, obj)
.then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});
}
fs.appendFile(
`${logFileName}/log.txt`,
`${timeDate}, Url: ${__url}:${__urlPort},
data: ${log}`,
function(error) {
if (error) throw error; // Handle the error just in case
}
);

timeDate = `${date.getDate()}/${date.getMonth()}/${date.getFullYear()}, hr: ${date.getHours()} min: ${date.getMinutes()}`;
} catch (e) {
console.log(e.message);
}
});
});
}
}
},
};

//store.subscribe(connectToSerialPort);
Expand Down

0 comments on commit 204de63

Please sign in to comment.