const path = require('path')
const {
ipcRenderer
} = require('electron')
const { v4: uuidv4 } = require('uuid');
// https://stackoverflow.com/a/26227660
var appDataFolder = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Application Support' : process.env.HOME + "/.config")
var configFolder = path.join(appDataFolder, "FreeDATA");
var configPath = path.join(configFolder, 'config.json')
const config = require(configPath);
// set date format
const dateFormat = new Intl.DateTimeFormat('en-GB', {
timeStyle: 'long',
dateStyle: 'full'
});
// split character
const split_char = '\0;'
var chatDB = path.join(configFolder, 'chatDB')
// ---- MessageDB
var PouchDB = require('pouchdb');
var db = new PouchDB(chatDB);
// get all messages from database
//var messages = db.get("messages").value()
// get all dxcallsigns in database
var dxcallsigns = new Set();
db.allDocs({
include_docs: true,
attachments: true
}).then(function (result) {
// handle result
// get all dxcallsigns and append to list
result.rows.forEach(function(item) {
update_chat(item.doc)
});
}).catch(function (err) {
console.log(err);
});
// WINDOW LISTENER
window.addEventListener('DOMContentLoaded', () => {
// SEND MSG
document.getElementById("sendMessage").addEventListener("click", () => {
var dxcallsign = document.getElementById('chatModuleDxCall').value
dxcallsign = dxcallsign.toUpperCase()
message = document.getElementById('chatModuleMessage').value
console.log(dxcallsign)
let Data = {
command: "send_message",
dxcallsign : dxcallsign,
mode : 255,
frames : 1,
data : message,
checksum : '123'
};
ipcRenderer.send('run-tnc-command', Data);
var uuid = uuidv4();
db.post({
_id: uuid,
timestamp: Math.floor(Date.now() / 1000),
dxcallsign: dxcallsign,
dxgrid: 'NULL',
msg: message,
checksum: 'NULL',
type: "transmit"
}).then(function (response) {
// handle response
console.log("new database entry")
console.log(response)
}).catch(function (err) {
console.log(err);
});
db.get(uuid).then(function (doc) {
// handle doc
update_chat(doc)
}).catch(function (err) {
console.log(err);
});
// scroll to bottom
var element = document.getElementById("message-container");
element.scrollTo(0,element.scrollHeight);
// clear input
document.getElementById('chatModuleMessage').value = ''
})
});
ipcRenderer.on('action-new-msg-received', (event, arg) => {
console.log(arg.data)
var new_msg = arg.data
new_msg.forEach(function(item) {
console.log(item)
//for (i = 0; i < arg.data.length; i++) {
let obj = new Object();
var encoded_data = atob(item.data);
var splitted_data = encoded_data.split(split_char)
//obj.uuid = item.uuid;
item.checksum = splitted_data[2]
item.msg = splitted_data[3]
//obj.dxcallsign = item.dxcallsign;
//obj.dxgrid = item.dxgrid;
//obj.timestamp = item.timestamp;
// check if message not exists in database.
// this might cause big cpu load of file is getting too big
/*
if(!JSON.stringify(db.get("messages")).includes(item.uuid)){
console.log("new message: " + item);
db.get("messages").push(item).save();
}
*/
db.put({
_id: item.uuid,
timestamp: item.timestamp,
dxcallsign: item.dxcallsign,
dxgrid: item.dxgrid,
msg: item.msg,
checksum: item.checksum,
type: "received"
}).then(function (response) {
// handle response
console.log("new database entry")
console.log(response)
}).catch(function (err) {
console.log(err);
});
db.get(item.uuid).then(function (doc) {
// handle doc
// timestamp
update_chat(doc)
}).catch(function (err) {
console.log(err);
});
console.log("...................................")
return
});
});
// Update chat list
update_chat = function(obj) {
var dxcallsign = obj.dxcallsign;
// CALLSIGN LIST
if(!(document.getElementById('chat-' + dxcallsign + '-list'))){
var new_callsign = `
${dxcallsign}
${timestamp}
${obj.msg}
${timestamp}
${obj.msg}