Value and time of one reading
64-bitA single reading of a feed with its time.
t = tt × 1000 if tt < 1e11, else tt
Values are not rounded or scaled. A numeric time below 1e11 is multiplied by 1000, a larger one is kept as milliseconds; a non-finite time becomes null.
// ---------------------------------------------------------------- payloads
export function readPayload(p, { valueKey = 'value', timeKey = 'time' } = {}) {
const s = (typeof p === 'string' ? p : dec.decode(p)).trim();
if (!s || s.length > 64 * 1024) return null;
const n = numOf(s);
if (Number.isFinite(n)) return { v: n, t: null };
if (s[0] !== '{') return null;
let o;
try { o = JSON.parse(s); } catch { return null; }
const v = typeof o?.[valueKey] === 'number' ? o[valueKey] : numOf(o?.[valueKey]);
if (!Number.isFinite(v)) return null;
const tt = o[timeKey] ?? o.ts ?? o.timestamp;
const t = typeof tt === 'number' ? (tt < 1e11 ? tt * 1000 : tt) : timeOf(tt);
return { v, t: Number.isFinite(t) ? t : null };
}