Browse code

Inital commit of AUTO Uploader

Richard Cornwell (K9RCP) authored on 01/27/2022 11:20:25
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,220 @@
1
+<!DOCTYPE html>
2
+
3
+  <html lang="en">
4
+
5
+  <head>
6
+    <meta charset="UTF-8">
7
+    <title>Auto Upload SpotRep - K9RCP</title>
8
+<style>
9
+html { 
10
+  background: url('images/bg.jpg') no-repeat center center fixed; 
11
+  -webkit-background-size: cover;
12
+  -moz-background-size: cover;
13
+  -o-background-size: cover;
14
+  background-size: cover;
15
+}
16
+
17
+#status-bar {
18
+    position: absolute;
19
+    margin: 0 0 0 0;
20
+    left: 0px;
21
+    bottom: 0px;
22
+    z-index: 1;
23
+    padding: 0px;
24
+    width: 100%;
25
+	background-color: white;
26
+}
27
+#main{
28
+  -moz-border-radius:10px;
29
+  -webkit-border-radius:10px;
30
+  border-radius:10px;
31
+  background: #fff; /* fallback for browsers that don't understand rgba */
32
+  border: solid 10px #000; /* fallback for browsers that don't understand rgba */
33
+  background-color: rgba(255,255,255,0.8); /* slighly transparent white */
34
+  border-color: rgba(0,0,0,0.2); /*Very transparent black*/
35
+}
36
+</style>
37
+  </head>
38
+
39
+  <body>
40
+ 
41
+    <div id="main"><center>
42
+<table style="width:100%">
43
+  <tr>
44
+    <td>Callsign: <input type="text" id="callsign" onkeyup="updateCall()" name="name" required
45
+       minlength="3" maxlength="8" size="10" /><span id="file-status"></span></td>
46
+    <td><center><div id="status"></div></center></td>
47
+  </tr>
48
+  
49
+</table>
50
+<button onclick="gui.Shell.openExternal('http://k9rcp-server.local.mesh/spotrep/');">View Data</button>
51
+	</center>
52
+	</div>
53
+	<div id="status-bar"> Status: </div>
54
+	<script src="./md5.js"></script>
55
+    <script>
56
+	      var gui = require('nw.gui');
57
+        var win = gui.Window.get();
58
+        win.on('new-win-policy', function (frame, url, policy) {
59
+            policy.forceNewPopup();
60
+        });
61
+	function mf(formato) {
62
+    var hor, hor2, x;
63
+    var fechai = new Date();
64
+    var ano = fechai.getYear() + 1900;
65
+    var ano2 = ("" + (fechai.getYear() + 1900)).slice(-2);
66
+    var mes = ("0" + (fechai.getMonth() + 1)).slice(-2);
67
+    var dia = ("0" + fechai.getDate()).slice(-2);
68
+    hor = hor2 = ("0" + fechai.getHours()).slice(-2);
69
+    hor2 = fechai.getHours();
70
+	if (fechai.getHours() > 12) {
71
+        hor2 = fechai.getHours() - 12;
72
+	}
73
+	
74
+	
75
+    if (fechai.getHours() > 11) {
76
+             x = "PM";
77
+    } else {
78
+        x = "AM";
79
+    }
80
+    var min = ("0" + fechai.getMinutes()).slice(-2);
81
+    var seg = ("0" + fechai.getSeconds()).slice(-2);
82
+    var fecha = formato.reemplaza("YyMDHhISx".split(''), [ano, ano2, mes, dia, hor, hor2, min, seg, x]);
83
+    return fecha;
84
+}
85
+String.prototype.reemplaza = function(busca, reemplaza) {
86
+    var b = Array.isArray(busca);
87
+    var r = Array.isArray(reemplaza);
88
+    var ret = this;
89
+    if (b) {
90
+        for (var i in busca) {
91
+            var c = r ? (reemplaza[i] ? reemplaza[i] : "") : reemplaza;
92
+            ret = ret.replace(new RegExp(busca[i], 'g'), c);
93
+        }
94
+    } else {
95
+        var c = r ? reemplaza[0] : reemplaza;
96
+        ret = ret.replace(new RegExp(busca, 'g'), c);
97
+    }
98
+    return ret;
99
+}
100
+
101
+function memorySizeOf(obj) {
102
+    var bytes = 0;
103
+
104
+    function sizeOf(obj) {
105
+        if(obj !== null && obj !== undefined) {
106
+            switch(typeof obj) {
107
+            case 'number':
108
+                bytes += 8;
109
+                break;
110
+            case 'string':
111
+                bytes += obj.length * 2;
112
+                break;
113
+            case 'boolean':
114
+                bytes += 4;
115
+                break;
116
+            case 'object':
117
+                var objClass = Object.prototype.toString.call(obj).slice(8, -1);
118
+                if(objClass === 'Object' || objClass === 'Array') {
119
+                    for(var key in obj) {
120
+                        if(!obj.hasOwnProperty(key)) continue;
121
+                        sizeOf(obj[key]);
122
+                    }
123
+                } else bytes += obj.toString().length * 2;
124
+                break;
125
+            }
126
+        }
127
+        return bytes;
128
+    };
129
+
130
+    function formatByteSize(bytes) {
131
+        if(bytes < 1024) return bytes + " bytes";
132
+        else if(bytes < 1048576) return(bytes / 1024).toFixed(3) + " KiB";
133
+        else if(bytes < 1073741824) return(bytes / 1048576).toFixed(3) + " MiB";
134
+        else return(bytes / 1073741824).toFixed(3) + " GiB";
135
+    };
136
+
137
+    return formatByteSize(sizeOf(obj));
138
+};
139
+
140
+var updateXml, lastMd,callSign;
141
+
142
+function updateCall() {
143
+	callSign = document.getElementById("callsign").value.toUpperCase();
144
+	document.getElementById("callsign").value = document.getElementById("callsign").value.toUpperCase();
145
+	console.error(document.getElementById("callsign").value.toUpperCase());
146
+  if (fs.existsSync('/RMS Express/'+callSign+'/FormData/SPOTREP-2.txt')) {
147
+    		document.getElementById("file-status").innerHTML = ' <img src="./images/good.png" width="24px" />';
148
+		  		clearInterval(updateXml);
149
+		runapipull();
150
+		lastMd = 0;
151
+
152
+}
153
+if (!fs.existsSync('/RMS Express/'+callSign+'/FormData/SPOTREP-2.txt')) {
154
+document.getElementById("file-status").innerHTML = ' <img src="./images/bad.png" width="24px" />';
155
+document.getElementById("status-bar").innerHTML = 'Error: File does not exist: C:\\RMS Express\\'+callSign+'\\FormData\\SPOTREP-2.txt';
156
+		document.getElementById("status").innerHTML = '<h2>Upload Status</h2><img src="./images/bad.png" width="64px" />';
157
+  console.error(err);
158
+  		clearInterval(updateXml);
159
+}
160
+	
161
+	
162
+}
163
+const fs = require('fs')
164
+
165
+function runapipull() {
166
+
167
+try {
168
+  if (fs.existsSync('/RMS Express/'+callSign+'/FormData/SPOTREP-2.txt')) {
169
+    //file exists
170
+  }
171
+} catch(err) {
172
+  console.error(err)
173
+}
174
+
175
+fs.readFile('/RMS Express/'+callSign+'/FormData/SPOTREP-2.txt', 'utf8' , (err, data) => {
176
+  if (err) {
177
+    console.error(err)
178
+    return
179
+  }
180
+if (md5(data) != lastMd) {
181
+  var formData = new FormData();
182
+  formData.append('spotrep', data);
183
+  formData.append('callsign', callSign);
184
+
185
+
186
+    // Set up the request
187
+    var xhr = new XMLHttpRequest();
188
+
189
+    // Open the connection
190
+    xhr.open('POST', 'http://k9rcp-server.local.mesh/spotrep/upload.php', true);
191
+
192
+    // Set up a handler for when the task for the request is complete
193
+    xhr.onload = function () {
194
+	document.getElementById("status").innerHTML = '<h2>Upload Status</h2><img src="./images/upload.png" width="64px" />';
195
+      if (xhr.status == 200) {
196
+        console.log('Upload copmlete!');
197
+		document.getElementById("status-bar").innerHTML = 'Status: Last upload - '+mf("h:Ix")+' - '+memorySizeOf(data)+' - '+md5(data);
198
+		document.getElementById("status").innerHTML = '<h2>Upload Status</h2><img src="./images/good.png" width="64px" />';
199
+		lastMd = md5(data);
200
+		clearInterval(updateXml);
201
+		updateXml = setInterval( function(){ runapipull(); }, 5000 );
202
+      } else {
203
+        document.getElementById("status-bar").innerHTML = 'Status: Error with Status code: '+xhr.status;
204
+		document.getElementById("status").innerHTML = '<h2>Upload Status</h2><img src="./images/bad.png" width="64px" />';
205
+		clearInterval(updateXml);
206
+		updateXml = setInterval( function(){ runapipull(); }, 5000 );
207
+      }
208
+    };
209
+    // Send the data.
210
+		document.getElementById("status").innerHTML = '<h2>Upload Status</h2><img src="./images/bad.png" width="64px" />';
211
+        document.getElementById("status-bar").innerHTML = 'Status: Error Can\'t Access k9rcp-server.local.mesh';
212
+        xhr.send(formData);
213
+
214
+}})
215
+}
216
+        document.getElementById("status-bar").innerHTML = 'Please type your RMS Callsign in to Start';
217
+</script>
218
+  </body>
219
+
220
+</html>
0 221
\ No newline at end of file