Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/logic/mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export async function handleMessage(blob: Buffer) {
console.log("[info] Verified signature");

if (payloadHadPublicKey) {
console.log("[info] Payload contained public key:", publicKey);
// save public key with device EUI mapping if not already saved
const existingMeter = getMeterByPublicKey(`0x${publicKey}`);

Expand Down Expand Up @@ -127,6 +128,8 @@ export async function handleMessage(blob: Buffer) {
throw new Error("Meter not found for public key: " + publicKey);
}

console.log("[info] Found meter:", m3ter);

if (m3ter.latestNonce % SYNC_EPOCH === 0) {
// sync with blockchain every SYNC_EPOCH transactions
await pruneAndSyncOnchain(m3ter.tokenId);
Expand Down Expand Up @@ -185,6 +188,8 @@ export async function handleMessage(blob: Buffer) {

updateMeterNonce(`0x${publicKey}`, expectedNonce);

console.log("[debug] Current all meters:", getAllMeterRecords());

console.log("[info] Updated meter nonce to:", expectedNonce);

try {
Expand All @@ -206,7 +211,7 @@ export async function handleMessage(blob: Buffer) {
}

const state =
decoded.nonce === expectedNonce ? { is_on: true } : { nonce: m3ter.latestNonce - 1, is_on: true };
decoded.nonce === expectedNonce ? { is_on: true } : { nonce: m3ter.latestNonce, is_on: true };

// TODO: remove the following block after testing
// if transaction nonce is 0 and the latest nonce is 0
Expand Down