Avvistamenti
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
// A field log on a device: Sightings, a nature journal whose own screen (ui/field-log.html) uses everything a
// device page may ask the phone for: the camera with a live preview, a photo from the phone's own
// camera or gallery, the microphone for a voice note, the location, and links that open outside
// the app. The web server is server.mjs; this file makes it reachable through Connectivitizer.
//
// CZ_DEVICE_KEY a device key of your workspace (czd_...)
// CZ_SHARED_KEY the key your app has too (czs_...); `npx connectivitizer keygen` makes one
// CZ_DEVICE_NAME how apps address this device (default: field-log)
// CZ_API_URL the platform, when it is not the default one
// CZ_RELAY_ONLY=1 never connect peer to peer
import { createAgent } from '@huelake/connectivitizer';
import { createFieldLogServer } from './server.mjs';
const need = (name) => {
const value = process.env[name];
if (!value) {
console.error(`${name} is not set. See the comment at the top of index.mjs.`);
process.exit(64);
}
return value;
};
const apiUrl = process.env.CZ_API_URL || undefined;
const deviceKey = need('CZ_DEVICE_KEY');
const sharedKey = need('CZ_SHARED_KEY');
const name = process.env.CZ_DEVICE_NAME || 'field-log';
// An ordinary web server on a local port. It knows nothing about Connectivitizer.
let agent;
const server = createFieldLogServer({ appsConnected: () => agent?.openSessions.length ?? 0 });
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
const port = server.address().port;
agent = createAgent({
apiUrl,
deviceKey,
name,
sharedKey,
// The web server above, under the name "api". Nothing else on this machine is reachable.
services: { api: `127.0.0.1:${port}` },
disableDirect: process.env.CZ_RELAY_ONLY === '1',
log: (message) => console.log(message),
});
// Ctrl+C, and `docker stop`.
process.on('SIGINT', () => void agent.stop());
process.on('SIGTERM', () => void agent.stop());
console.log(`Serving on http://127.0.0.1:${port}/.cz/ui.html; connecting as "${name}" ...`);
agent.waitUntilConnected().then(
() => console.log(`Reachable through Connectivitizer as "${name}". Ctrl+C stops.`),
() => undefined,
);
try {
await agent.run();
} catch (error) {
console.error(`Stopped: ${error?.code ?? ''} ${error?.message ?? error}`);
process.exitCode = 2;
}
server.close();
server.closeAllConnections?.();