forked from wavelog/WaveLogGate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
210 lines (176 loc) · 6.05 KB
/
renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process because
// `nodeIntegration` is turned off. Use `preload.js` to
// selectively enable features needed in the rendering
// process.
// Shorthand for document.querySelector.
var cfg={};
const {ipcRenderer} = require('electron')
const bt_save=select("#save");
const bt_quit=select("#quit");
const bt_test=select("#test");
var oldCat={ vfo: 0, mode: "SSB" };
$(document).ready(function() {
cfg=ipcRenderer.sendSync("get_config", '');
$("#wavelog_url").val(cfg.wavelog_url);
$("#wavelog_key").val(cfg.wavelog_key);
$("#wavelog_id").val(cfg.wavelog_id);
$("#wavelog_radioname").val(cfg.wavelog_radioname);
$("#flrig_host").val(cfg.flrig_host);
$("#flrig_port").val(cfg.flrig_port);
$("#flrig_ena").prop("checked", cfg.flrig_ena);
$("#wavelog_pmode").prop("checked", cfg.wavelog_pmode);
bt_save.addEventListener('click', () => {
cfg.wavelog_url=$("#wavelog_url").val().trim();
cfg.wavelog_key=$("#wavelog_key").val().trim();
cfg.wavelog_id=$("#wavelog_id").val().trim();
cfg.wavelog_radioname=$("#wavelog_radioname").val().trim();
cfg.flrig_host=$("#flrig_host").val().trim();
cfg.flrig_port=$("#flrig_port").val().trim();
cfg.flrig_ena=$("#flrig_ena").is(':checked');
cfg.wavelog_pmode=$("#wavelog_pmode").is(':checked');
x=ipcRenderer.sendSync("set_config", cfg);
console.log(x);
});
bt_quit.addEventListener('click', () => {
x=ipcRenderer.sendSync("quit", '');
});
bt_test.addEventListener('click', () => {
cfg.wavelog_url=$("#wavelog_url").val().trim();
cfg.wavelog_key=$("#wavelog_key").val().trim();
cfg.wavelog_id=$("#wavelog_id").val().trim();
cfg.wavelog_radioname=$("#wavelog_radioname").val().trim();
x=(ipcRenderer.sendSync("test", cfg));
if (x.payload.status == 'created') {
$("#test").removeClass('btn-primary');
$("#test").removeClass('btn-danger');
$("#test").addClass('btn-success');
$("#msg2").hide();
$("#msg2").html("");
} else {
$("#test").removeClass('btn-primary');
$("#test").removeClass('btn-success');
$("#test").addClass('btn-danger');
$("#msg2").show();
$("#msg2").html("Test failed. Reason: "+x.payload.reason);
}
console.log(x);
});
getsettrx();
$("#flrig_ena").on( "click",function() {
getsettrx();
});
setInterval(updateUtcTime, 1000);
window.onload = updateUtcTime;
$("#config-tab").on("click",function() {
obj={};
obj.width=420;
obj.height=550;
obj.ani=false;
resizeme(obj);
});
$("#status-tab").on("click",function() {
obj={};
obj.width=420;
obj.height=250;
obj.ani=false;
resizeme(obj);
});
});
function resizeme(size) {
x=(ipcRenderer.sendSync("resize", size))
return x;
}
function select(selector) {
return document.querySelector(selector);
}
window.TX_API.onUpdateMsg((value) => {
$("#msg").html(value);
$("#msg2").html("");
});
window.TX_API.onUpdateTX((value) => {
if (value.created) {
$("#log").html('<div class="alert alert-success" role="alert">'+value.qsos[0].TIME_ON+" "+value.qsos[0].CALL+" ("+value.qsos[0].GRIDSQUARE+") on "+value.qsos[0].BAND+" (R:"+value.qsos[0].RST_RCVD+" / S:"+value.qsos[0].RST_SENT+') - OK</div>');
} else {
$("#log").html('<div class="alert alert-danger" role="alert">'+value.qsos[0].TIME_ON+" "+value.qsos[0].CALL+" ("+value.qsos[0].GRIDSQUARE+") on "+value.qsos[0].BAND+" (R:"+value.qsos[0].RST_RCVD+" / S:"+value.qsos[0].RST_SENT+') - Error<br/>Reason: '+value.fail.payload.reason+'</div>');
}
})
async function get_trx() {
let currentCat={};
currentCat.vfo=await getInfo('rig.get_vfo');
currentCat.mode=await getInfo('rig.get_mode');
$("#current_trx").html((currentCat.vfo/(1000*1000))+" MHz / "+currentCat.mode);
if (!(isDeepEqual(oldCat,currentCat))) {
// console.log(currentCat);
console.log(await informWavelog(currentCat));
}
oldCat=currentCat;
return currentCat;
}
async function getInfo(which) {
const response = await fetch(
"http://"+$("#flrig_host").val()+':'+$("#flrig_port").val(),
{
method: 'POST',
// mode: 'no-cors',
headers: {
'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
},
body: '<?xml version="1.0"?><methodCall><methodName>'+which+'</methodName></methodCall>'
});
const data = await response.text();
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(data, "text/xml");
var qrgplain = xmlDoc.getElementsByTagName("value")[0].textContent;
return qrgplain;
}
async function getsettrx() {
if ($("#flrig_ena").is(':checked')) {
x=await get_trx();
setTimeout(() => {
getsettrx();
}, 1000);
}
}
const isDeepEqual = (object1, object2) => {
const objKeys1 = Object.keys(object1);
const objKeys2 = Object.keys(object2);
if (objKeys1.length !== objKeys2.length) return false;
for (var key of objKeys1) {
const value1 = object1[key];
const value2 = object2[key];
const isObjects = isObject(value1) && isObject(value2);
if ((isObjects && !isDeepEqual(value1, value2)) ||
(!isObjects && value1 !== value2)
) {
return false;
}
}
return true;
};
const isObject = (object) => {
return object != null && typeof object === "object";
};
async function informWavelog(CAT) {
let data={ radio: "WLGate", key: cfg.wavelog_key, radio: cfg.wavelog_radioname, frequency: (CAT.vfo), mode: CAT.mode };
let x=await fetch(cfg.wavelog_url + '/api/radio', {
method: 'POST',
rejectUnauthorized: false,
headers: {
Accept: 'application.json',
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
});
return x;
}
function updateUtcTime() {
const now = new Date();
const hours = ('0' + now.getUTCHours()).slice(-2);
const minutes = ('0' + now.getUTCMinutes()).slice(-2);
const seconds = ('0' + now.getUTCSeconds()).slice(-2);
const formattedTime = `${hours}:${minutes}:${seconds}z`;
document.getElementById('utc').innerHTML = formattedTime;
}