register with mqtt host

This commit is contained in:
Eric House 2021-02-21 21:59:03 -08:00
parent 033290bd69
commit c8f22f5fb1
3 changed files with 50 additions and 5 deletions

View file

@ -46,6 +46,7 @@ DEFINES += -DXWFEATURE_STREAMREF
DEFINES += -DNATIVE_NLI DEFINES += -DNATIVE_NLI
DEFINES += -DDEBUG_REF DEFINES += -DDEBUG_REF
DEFINES += -Wno-switch DEFINES += -Wno-switch
DEFINES += -DGITREV=\"$(shell git describe --tags --dirty)\"
ifeq ($(MEMDEBUG),TRUE) ifeq ($(MEMDEBUG),TRUE)
DEFINES += -DMEM_DEBUG -DDEBUG -O0 DEFINES += -DMEM_DEBUG -DDEBUG -O0

View file

@ -156,9 +156,11 @@ EM_JS(void, call_get_string, (const char* msg, const char* dflt,
nbGetString( jsMgs, jsDflt, proc, closure ); nbGetString( jsMgs, jsDflt, proc, closure );
} ); } );
EM_JS(void, call_haveDevID, (void* closure, const char* devid, StringProc proc), { EM_JS(void, call_haveDevID, (void* closure, const char* devid,
onHaveDevID(closure, UTF8ToString(devid), proc); const char* gitrev, int now, StringProc proc), {
}); let jsgr = UTF8ToString(gitrev);
onHaveDevID(closure, UTF8ToString(devid), jsgr, now, proc);
});
EM_JS(bool, call_mqttSend, (const char* topic, const uint8_t* ptr, int len), { EM_JS(bool, call_mqttSend, (const char* topic, const uint8_t* ptr, int len), {
let topStr = UTF8ToString(topic); let topStr = UTF8ToString(topic);
@ -550,7 +552,8 @@ initDeviceGlobals( Globals* globals )
XP_UCHAR buf[32]; XP_UCHAR buf[32];
XP_SNPRINTF( buf, VSIZE(buf), MQTTDevID_FMT, devID ); XP_SNPRINTF( buf, VSIZE(buf), MQTTDevID_FMT, devID );
XP_LOGFF( "got mqtt devID: %s", buf ); XP_LOGFF( "got mqtt devID: %s", buf );
call_haveDevID( globals, buf, onConflict ); int now = dutil_getCurSeconds( globals->dutil, NULL );
call_haveDevID( globals, buf, GITREV, now, onConflict );
} }
static void static void

View file

@ -8,7 +8,46 @@ function ccallString(proc, closure, str) {
[proc, closure, str]); [proc, closure, str]);
} }
function onHaveDevID(closure, devid, proc) { function registerOnce(devid, gitrev, now) {
let nextTimeKey = 'next_reg';
let gitKey = 'last_write';
let nextTime = localStorage.getItem(nextTimeKey);
let prevGit = localStorage.getItem(gitKey);
// false: for now, always register. PENDING
if ( false && prevGit == gitrev && now < nextTime ) {
// console.log("registerOnce: skipping");
} else {
let args = { devid: devid,
gitrev: gitrev,
loc: navigator.language,
os: navigator.appName,
vers: '0.0',
dbg: true,
myNow: now,
vrntName: 'wasm',
};
let body = JSON.stringify(args);
// console.log('registerOnce(): args: ' + body);
fetch('/xw4/api/v1/register', {
method: 'post',
body: body,
headers: {
'Content-Type': 'application/json'
},
}).then(res => {
return res.json();
}).then(data => {
console.log('data: ' + JSON.stringify(data));
if ( data.success ) {
localStorage.setItem(nextTimeKey, data.atNext);
localStorage.setItem(gitKey, gitrev);
}
});
}
}
function onHaveDevID(closure, devid, gitrev, now, proc) {
// Set a unique tag so we know if somebody comes along later // Set a unique tag so we know if somebody comes along later
let tabID = Math.random(); let tabID = Math.random();
localStorage.setItem('tabID', tabID); localStorage.setItem('tabID', tabID);
@ -22,6 +61,8 @@ function onHaveDevID(closure, devid, proc) {
}; };
window.addEventListener('storage', listener); window.addEventListener('storage', listener);
registerOnce(devid, gitrev, now);
state.closure = closure; state.closure = closure;
document.getElementById("mqtt_span").textContent=devid; document.getElementById("mqtt_span").textContent=devid;