js to ts conversion...

This commit is contained in:
DJ2LS 2023-10-20 19:02:40 +02:00
parent 6c07eb596f
commit a7eada0649
5 changed files with 171 additions and 157 deletions

View file

@ -7,6 +7,7 @@
"scripts": { "scripts": {
"start": "git pull && npm i && vite", "start": "git pull && npm i && vite",
"dev": "vite", "dev": "vite",
"check" : "vue-tsc --noEmit",
"build": "vue-tsc --noEmit && vite build && electron-builder", "build": "vue-tsc --noEmit && vite build && electron-builder",
"preview": "vite preview", "preview": "vite preview",
"lint": "eslint --ext .js,.vue src", "lint": "eslint --ext .js,.vue src",

View file

@ -23,7 +23,6 @@ import chat_conversations_entry from "./chat_conversations_entry.vue";
function chatSelected(callsign) { function chatSelected(callsign) {
chat.selectedCallsign = callsign.toUpperCase(); chat.selectedCallsign = callsign.toUpperCase();
// scroll message container to bottom // scroll message container to bottom
var messageBody = document.getElementById("message-container"); var messageBody = document.getElementById("message-container");
if (messageBody != null ) { if (messageBody != null ) {
@ -33,6 +32,8 @@ function chatSelected(callsign) {
if (getNewMessagesByDXCallsign(callsign)[1] > 0){ if (getNewMessagesByDXCallsign(callsign)[1] > 0){
let messageArray = getNewMessagesByDXCallsign(callsign)[2] let messageArray = getNewMessagesByDXCallsign(callsign)[2]
console.log(messageArray)
for (const key in messageArray){ for (const key in messageArray){
resetIsNewMessage(messageArray[key].uuid, false) resetIsNewMessage(messageArray[key].uuid, false)
} }
@ -70,7 +71,6 @@ function chatSelected(callsign) {
<div class="row"> <div class="row">
<div class="col-9">{{ item }} <div class="col-9">{{ item }}
<span class="badge rounded-pill bg-danger" v-if="getNewMessagesByDXCallsign(item)[1] > 0"> <span class="badge rounded-pill bg-danger" v-if="getNewMessagesByDXCallsign(item)[1] > 0">
{{getNewMessagesByDXCallsign(item)[1]}} new messages {{getNewMessagesByDXCallsign(item)[1]}} new messages
</span> </span>

View file

@ -159,7 +159,6 @@ alert("not yet implemented")
aria-label=".form-select-sm" aria-label=".form-select-sm"
id="hamlib_deviceport" id="hamlib_deviceport"
style="width: 7rem" style="width: 7rem"
@change="saveSettings"
v-html="settings.getSerialDevices()" v-html="settings.getSerialDevices()"
> >

View file

@ -11,6 +11,9 @@ setActivePinia(pinia);
import { useChatStore } from "../store/chatStore.js"; import { useChatStore } from "../store/chatStore.js";
const chat = useChatStore(pinia); const chat = useChatStore(pinia);
import { useStateStore } from "../store/stateStore.js";
const state = useStateStore(pinia);
import { sendMessage } from "./sock.js"; import { sendMessage } from "./sock.js";
import { displayToast } from "./popupHandler.js"; import { displayToast } from "./popupHandler.js";
@ -22,6 +25,52 @@ import {btoa_FD} from "./freedata.js"
// split character // split character
const split_char = "0;1;"; const split_char = "0;1;";
// define default message object
interface Attachment {
content_type: string;
data: string;
}
interface messageDefaultObject {
command: string;
hmac_signed: boolean;
percent: number;
bytesperminute: number;
is_new: boolean;
_id: string;
timestamp: number;
dxcallsign: string;
dxgrid: string;
msg: string;
checksum: string;
type: string;
status: string;
attempt: number;
uuid: string;
duration: number;
nacks: number;
speed_list: string;
broadcast_sender?: string; // optional for broadcasts
_attachments: {
[filename: string]: Attachment;
};
}
interface beaconDefaultObject{
command: string;
is_new: boolean;
_id: string;
timestamp: number;
dxcallsign: string;
dxgrid: string;
type: string;
status: string;
uuid: string;
snr: string;
}
// ---- MessageDB // ---- MessageDB
try { try {
var PouchDB = require("pouchdb"); var PouchDB = require("pouchdb");
@ -46,25 +95,22 @@ if(typeof process.env["APPDATA"] !== "undefined"){
console.log(appDataFolder) console.log(appDataFolder)
} else { } else {
let appDataFolder: string;
switch (process.platform) { switch (process.platform) {
case "darwin": case "darwin":
var appDataFolder = process.env["HOME"] + "/Library/Application Support"; appDataFolder = process.env["HOME"] + "/Library/Application Support";
console.log(appDataFolder) console.log(appDataFolder);
break; break;
case "linux": case "linux":
var appDataFolder = process.env["HOME"] + "/.config"; appDataFolder = process.env["HOME"] + "/.config";
console.log(appDataFolder) console.log(appDataFolder);
break; break;
case "linux2": case "win32":
var appDataFolder = "undefined"; appDataFolder = "undefined";
break;
case "windows":
var appDataFolder = "undefined";
break; break;
default: default:
var appDataFolder = "undefined"; appDataFolder = "undefined";
break; break;
} }
} }
@ -90,15 +136,11 @@ export function newBroadcast(broadcastChannel, chatmessage) {
var mode = ""; var mode = "";
var frames = ""; var frames = "";
var data = ""; var data = "";
if (typeof chatFile !== "undefined") {
var file = chatFile;
var filetype = chatFileType;
var filename = chatFileName;
} else {
var file = ""; var file = "";
var filetype = "text"; var filetype = "text";
var filename = ""; var filename = "";
}
var file_checksum = ""; //crc32(file).toString(16).toUpperCase(); var file_checksum = ""; //crc32(file).toString(16).toUpperCase();
var checksum = ""; var checksum = "";
var message_type = "broadcast_transmit"; var message_type = "broadcast_transmit";
@ -116,34 +158,33 @@ export function newBroadcast(broadcastChannel, chatmessage) {
uuid = uuid.slice(-4); uuid = uuid.slice(-4);
let newChatObj = new Object(); let newChatObj: messageDefaultObject = {
command: "broadcast",
newChatObj.command = "broadcast"; hmac_signed: false,
newChatObj.hmac_signed = false; percent: 0,
newChatObj.percent = 0; bytesperminute: 0, // You need to assign a value here
newChatObj.bytesperminute; is_new: false,
newChatObj.is_new = false; _id: uuid,
newChatObj._id = uuid; timestamp: timestamp,
newChatObj.timestamp = timestamp; dxcallsign: broadcastChannel,
newChatObj.dxcallsign = dxcallsign; dxgrid: "null",
newChatObj.dxgrid = "null"; msg: chatmessage,
newChatObj.msg = chatmessage; checksum: file_checksum,
newChatObj.checksum = file_checksum; type: message_type,
newChatObj.type = message_type; status: "transmitting",
newChatObj.status = "transmitting"; attempt: 1,
newChatObj.attempt = 1; uuid: uuid,
newChatObj.uuid = uuid; duration: 0,
newChatObj.duration = 0; nacks: 0,
newChatObj.nacks = 0; speed_list: "null",
newChatObj.speed_list = "null"; _attachments: {
newChatObj._attachments = {
[filename]: { [filename]: {
content_type: filetype, content_type: filetype,
data: btoa_FD(file), data: btoa_FD(file),
}, }
}; }
};
sendMessage(newChatObj) sendMessage(newChatObj)
@ -162,14 +203,17 @@ export function newMessage(
var mode = ""; var mode = "";
var frames = ""; var frames = "";
var data = ""; var data = "";
if (typeof chatFile !== "undefined") {
var file = chatFile;
var filetype = chatFileType;
var filename = chatFileName;
} else {
var file = "";
var filetype = "text";
var filename = ""; var filename = "";
var filetype = "";
var file=""
if (typeof chatFile !== "undefined") {
file = chatFile;
filetype = chatFileType;
filename = chatFileName;
} else {
file = "";
filetype = "text";
filename = "";
} }
var file_checksum = ""; //crc32(file).toString(16).toUpperCase(); var file_checksum = ""; //crc32(file).toString(16).toUpperCase();
var checksum = ""; var checksum = "";
@ -188,32 +232,32 @@ export function newMessage(
uuid = uuid.slice(-8); uuid = uuid.slice(-8);
let newChatObj = new Object(); let newChatObj: messageDefaultObject = {
command: "msg",
newChatObj.command = "msg"; hmac_signed: false,
newChatObj.hmac_signed = false; percent: 0,
newChatObj.percent = 0; bytesperminute: 0, // You need to assign a value here
newChatObj.bytesperminute; is_new: false,
newChatObj.is_new = false; _id: uuid,
newChatObj._id = uuid; timestamp: timestamp,
newChatObj.timestamp = timestamp; dxcallsign: dxcallsign,
newChatObj.dxcallsign = dxcallsign; dxgrid: "null",
newChatObj.dxgrid = "null"; msg: chatmessage,
newChatObj.msg = chatmessage; checksum: file_checksum,
newChatObj.checksum = file_checksum; type: message_type,
newChatObj.type = message_type; status: "transmitting",
newChatObj.status = "transmitting"; attempt: 1,
newChatObj.attempt = 1; uuid: uuid,
newChatObj.uuid = uuid; duration: 0,
newChatObj.duration = 0; nacks: 0,
newChatObj.nacks = 0; speed_list: "null",
newChatObj.speed_list = "null"; _attachments: {
newChatObj._attachments = {
[filename]: { [filename]: {
content_type: filetype, content_type: filetype,
data: btoa_FD(file), data: btoa_FD(file)
}, }
}; }
};
sendMessage(newChatObj); sendMessage(newChatObj);
addObjToDatabase(newChatObj); addObjToDatabase(newChatObj);
@ -423,7 +467,7 @@ function getFromUnsortedChatListByUUID(uuid){
return false; return false;
} }
export function getNewMessagesByDXCallsign(dxcallsign){ export function getNewMessagesByDXCallsign(dxcallsign): [number, number, any]{
let new_counter = 0 let new_counter = 0
let total_counter = 0 let total_counter = 0
let item_array = [] let item_array = []
@ -641,6 +685,7 @@ function createChatIndex() {
export function deleteChatByCallsign(callsign) { export function deleteChatByCallsign(callsign) {
chat.callsign_list.delete(callsign); chat.callsign_list.delete(callsign);
// @ts-expect-error
delete chat.unsorted_chat_list.callsign; delete chat.unsorted_chat_list.callsign;
delete chat.sorted_chat_list.callsign; delete chat.sorted_chat_list.callsign;
@ -696,17 +741,22 @@ export function newBeaconReceived(obj) {
"mycallsign": "DJ2LS-0" "mycallsign": "DJ2LS-0"
} }
*/ */
let newChatObj = new Object(); let newChatObj: beaconDefaultObject = {
command: "beacon",
is_new: false,
_id: obj["uuid"],
timestamp: obj["timestamp"],
dxcallsign: obj["dxcallsign"],
dxgrid: obj["dxgrid"],
type: "beacon",
status: obj["beacon"],
uuid: obj["uuid"],
snr: obj["snr"], // adding the new field
};
newChatObj.command = "beacon";
newChatObj._id = obj["uuid"];
newChatObj.uuid = obj["uuid"];
newChatObj.timestamp = obj["timestamp"];
newChatObj.dxcallsign = obj["dxcallsign"];
newChatObj.dxgrid = obj["dxgrid"];
newChatObj.type = "beacon";
newChatObj.status = obj["beacon"];
newChatObj.snr = obj["snr"];
addObjToDatabase(newChatObj); addObjToDatabase(newChatObj);
@ -813,35 +863,33 @@ export function newMessageReceived(message, protocol) {
*/ */
console.log(protocol); console.log(protocol);
let newChatObj = new Object(); let newChatObj: messageDefaultObject = {
command: "msg",
newChatObj.command = "msg"; hmac_signed: protocol["hmac_signed"],
newChatObj.hmac_signed = protocol["hmac_signed"]; percent: 100,
newChatObj.percent = 100; bytesperminute: protocol["bytesperminute"],
newChatObj.bytesperminute = protocol["bytesperminute"]; is_new: true,
newChatObj.is_new = true; _id: message[3],
newChatObj._id = message[3]; timestamp: message[4],
newChatObj.timestamp = message[4]; dxcallsign: protocol["dxcallsign"],
newChatObj.dxcallsign = protocol["dxcallsign"]; dxgrid: protocol["dxgrid"],
newChatObj.dxgrid = protocol["dxgrid"]; msg: message[5],
newChatObj.msg = message[5]; checksum: message[2],
newChatObj.checksum = message[2]; type: protocol["status"],
//newChatObj.type = message[1]; status: protocol["status"],
newChatObj.type = protocol["status"]; attempt: 1,
newChatObj.status = protocol["status"]; uuid: message[3],
newChatObj.attempt = 1; duration: protocol["duration"],
newChatObj.uuid = message[3]; nacks: protocol["nacks"],
newChatObj.duration = protocol["duration"]; speed_list: protocol["speed_list"],
newChatObj.nacks = protocol["nacks"]; _attachments: {
newChatObj.speed_list = protocol["speed_list"];
newChatObj._attachments = {
[message[6]]: { [message[6]]: {
content_type: message[7], content_type: message[7],
data: btoa_FD(message[8]), data: btoa_FD(message[8])
}, }
}; }
};
// some tweaks for broadcasts // some tweaks for broadcasts
if (protocol.fec == "broadcast") { if (protocol.fec == "broadcast") {
@ -874,37 +922,3 @@ console.log(data)
} }
// CRC CHECKSUMS
// https://stackoverflow.com/a/50579690
// crc32 calculation
//console.log(crc32('abc'));
//var crc32=function(r){for(var a,o=[],c=0;c<256;c++){a=c;for(var f=0;f<8;f++)a=1&a?3988292384^a>>>1:a>>>1;o[c]=a}for(var n=-1,t=0;t<r.length;t++)n=n>>>8^o[255&(n^r.charCodeAt(t))];return(-1^n)>>>0};
//console.log(crc32('abc').toString(16).toUpperCase()); // hex
var makeCRCTable = function () {
var c;
var crcTable = [];
for (var n = 0; n < 256; n++) {
c = n;
for (var k = 0; k < 8; k++) {
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
}
crcTable[n] = c;
}
return crcTable;
};
var crc32 = function (str) {
var crcTable = window.crcTable || (window.crcTable = makeCRCTable());
var crc = 0 ^ -1;
for (var i = 0; i < str.length; i++) {
crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xff];
}
return (crc ^ -1) >>> 0;
};

View file

@ -13,7 +13,7 @@ import {
updateTransmissionStatus, updateTransmissionStatus,
setStateSuccess, setStateSuccess,
setStateFailed, setStateFailed,
} from "./chatHandler.js"; } from "./chatHandler.ts";
import { displayToast } from "./popupHandler.js"; import { displayToast } from "./popupHandler.js";
// ----------------- init pinia stores ------------- // ----------------- init pinia stores -------------