Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jasl committed Nov 4, 2023
0 parents commit 95e026e
Show file tree
Hide file tree
Showing 33 changed files with 2,412 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.DS_Store
*.swp
.vscode
.idea

**/target
**/*.rs.bk
.wasm-binaries
rls*.log
**/Cargo.lock
!/Cargo.lock

*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

**/node_modules
**/logs
**/tmp
**/.env
**/.vitepress/cache
**/.vitepress/dist
**/.svelte-kit
**/vite.config.js.timestamp-*
**/vite.config.ts.timestamp-*
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 DePHY

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
DePHY NoStr sample
====

## Requirement

- [Deno](https://docs.deno.com/runtime/manual/getting_started/installation)

## Run

`deno run --allow-all index.ts`

## License

MIT
3 changes: 3 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"lock": false
}
155 changes: 155 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// deno-lint-ignore-file no-explicit-any

import { decodeBase58 } from "https://deno.land/std/encoding/base58.ts";
import { encodeHex } from "https://deno.land/std/encoding/hex.ts";
import * as ethUtil from "npm:@ethereumjs/util";

import { sleep } from "https://deno.land/x/sleep/mod.ts";
import { Nostr } from "https://deno.land/x/nostr_deno_client/mod.ts";

import * as RawMessage from "./proto/messages/dephy/message/RawMessage.ts"
import * as SignedMessage from "./proto/messages/dephy/message/SignedMessage.ts"
import * as PbPayload from "./proto/messages/dephy/stpw/PbPayload.ts"

const DePhyMessageKind = 1111;

function getNostr() {
const nostr = new Nostr();
// nostr.debugMode = true;
nostr.setMaxListeners(5);

nostr.relayList.push({
name: "Dephy",
url: "wss://relay-poc.dephy.io",
});

// nostr.on(
// "relayConnected",
// (relay: Relay) => console.log("Relay connected.", relay.name),
// );
nostr.on("relayError", (err: Error) => {
console.log("Relay error;", err);
Deno.exit(1);
});
nostr.on("relayNotice", (notice: string[]) => {
console.log("Notice", notice); // Probably not good news
Deno.exit(1);
});

return nostr;
}

// const nostr = getNostr();
// await nostr.connect();

const savedSinceFilePath = "./tmp/oldest_message_timestamp";
let CreatedAtSince = (() => {
const savedSince = (() => {
try {
const fromFile = parseInt(
Deno.readTextFileSync(savedSinceFilePath).trim(),
);
return fromFile > 0 ? fromFile : 0;
} catch (_e) {
return 0;
}
})();
if (savedSince > 0) {
return savedSince;
}

const defaultSince = parseInt(Deno.env.get("CREATED_AT_SINCE") ?? "0");
if (defaultSince > 0) {
return defaultSince;
}

return undefined;
})();
let CreatedAtUntil;

do {
// The NoStr seems has bug that leak subscriptions, we will disconnect after we get the result
// await nostr.connect();

const messages: any[] = [];
do {
console.log(
`Fetching messages from ${CreatedAtSince ?? "oldest"} to ${CreatedAtUntil ?? "now"}`,
);

// The NoStr seems has bug that leak subscriptions, simply disconnect after we get the result
const nostr = getNostr();
await nostr.connect();

// Fetch a batch, NoStr always DESC by `created_at`, so it will get messages from latest to oldest
const messagesInBatch: any[] = [];
await nostr.filter({
kinds: [DePhyMessageKind],
since: CreatedAtSince,
until: CreatedAtUntil,
limit: 100,
}).each((message: any) => {
// console.debug(message);
// console.log(message.created_at);

messages.push(message);
messagesInBatch.push(message);
});
console.log(
`Received ${messagesInBatch.length} messages in a batch, ${messages.length} messages in total`,
);

// The NoStr seems has bug that leak subscriptions, simply disconnect after we get the result
nostr.disconnect();

if (messagesInBatch.length === 0) {
break;
}

CreatedAtUntil = messagesInBatch[messagesInBatch.length - 1].created_at - 1;
} while (true);
if (messages.length === 0) {
console.log("No new message, sleep 60s");
await sleep(60);
continue;
}

console.log(`Received ${messages.length} messages in total`);
// Make the order ASC by `created_at`
messages.reverse();

// TODO: Process messages here
// This is a code sample, to show how to extract info from a NoStr message
for (const message of messages) {
// console.log(message);

const decodedContent = SignedMessage.decodeBinary(decodeBase58(message.content));
const rawMessage = RawMessage.decodeBinary(decodedContent.raw);

const deviceEthAddress = `0x${encodeHex(rawMessage.fromAddress)}`;
const payload = PbPayload.decodeBinary(rawMessage.payload);

console.log(deviceEthAddress);
console.log(payload);
// Data example
// 0x52fffd4ec1767c67fa60a29b7f151c6ace26aaf2
// {
// currAvg: 22.04503631591797,
// voltAvg: 22.04503631591797,
// powerAvg: 22.04503631591797,
// pfAvg: 22.04503631591797,
// deltaEnerge: 22.04503631591797,
// highTemp: 22.04503631591797,
// bleMacList: [],
// wifiMacList: [],
// timestamp: "1699084895"
// }
}

// Next iteration will end to newest in the batch
CreatedAtSince = messages[messages.length - 1].created_at + 1;
CreatedAtUntil = undefined;

// Save cursor to disk
// Deno.writeTextFileSync(savedSinceFilePath, (CreatedAtSince + 1).toString());
} while (true);
6 changes: 6 additions & 0 deletions proto/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @ts-nocheck
import * as messages from "./messages/index.ts";

export type {
messages,
};
8 changes: 8 additions & 0 deletions proto/messages/dephy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-nocheck
import * as message from "./message/index.ts";
import * as stpw from "./stpw/index.ts";

export type {
message,
stpw,
};
Loading

0 comments on commit 95e026e

Please sign in to comment.