show contacts list on connect

This commit is contained in:
liamcottle 2025-02-11 23:30:54 +13:00
parent 865ca127ac
commit 995e740347
5 changed files with 227 additions and 72 deletions

View file

@ -66,6 +66,9 @@ class BleConnection extends Connection {
this.onFrameReceived(frame);
});
// fire connected event
this.onConnected();
}
async close() {

View file

@ -5,6 +5,10 @@ import EventEmitter from "../events.js";
class Connection extends EventEmitter {
onConnected() {
this.emit("connected");
}
async close() {
throw new Error("This method must be implemented by the subclass.");
}
@ -166,32 +170,32 @@ class Connection extends EventEmitter {
}
onAdvertPush(bufferReader) {
this.emit("AdvertPush", {
this.emit(Constants.PushCodes.Advert, {
publicKey: bufferReader.readBytes(32),
});
}
onSendConfirmedPush(bufferReader) {
this.emit("SendConfirmedPush", {
this.emit(Constants.PushCodes.SendConfirmed, {
ackCode: bufferReader.readBytes(4),
roundTrip: bufferReader.readUInt32LE(),
});
}
onMsgWaitingPush(bufferReader) {
this.emit("MsgWaitingPush", {
this.emit(Constants.PushCodes.MsgWaiting, {
});
}
onContactsStartResponse(bufferReader) {
this.emit("ContactsStartResponse", {
this.emit(Constants.ResponseCodes.ContactsStart, {
count: bufferReader.readUInt32LE(),
});
}
onContactResponse(bufferReader) {
this.emit("ContactResponse", {
this.emit(Constants.ResponseCodes.Contact, {
publicKey: bufferReader.readBytes(32),
type: bufferReader.readByte(),
flags: bufferReader.readByte(),
@ -206,19 +210,19 @@ class Connection extends EventEmitter {
}
onEndOfContactsResponse(bufferReader) {
this.emit("EndOfContactsResponse", {
this.emit(Constants.ResponseCodes.EndOfContacts, {
mostRecentLastmod: bufferReader.readUInt32LE(),
});
}
onSentResponse(bufferReader) {
this.emit("SentResponse", {
this.emit(Constants.ResponseCodes.Sent, {
});
}
onSelfInfoResponse(bufferReader) {
this.emit("SelfInfoResponse", {
this.emit(Constants.ResponseCodes.SelfInfo, {
type: bufferReader.readByte(),
txPower: bufferReader.readByte(),
maxTxPower: bufferReader.readByte(),
@ -235,19 +239,19 @@ class Connection extends EventEmitter {
}
onCurrTimeResponse(bufferReader) {
this.emit("CurrTimeResponse", {
this.emit(Constants.ResponseCodes.CurrTime, {
epochSecs: bufferReader.readUInt32LE(),
});
}
onNoMoreMessagesResponse(bufferReader) {
this.emit("NoMoreMessagesResponse", {
this.emit(Constants.ResponseCodes.NoMoreMessages, {
});
}
onContactMsgRecvResponse(bufferReader) {
this.emit("ContactMsgRecvResponse", {
this.emit(Constants.ResponseCodes.ContactMsgRecv, {
pubKeyPrefix: bufferReader.readBytes(6),
pathLen: bufferReader.readByte(),
txtType: bufferReader.readByte(),
@ -256,6 +260,44 @@ class Connection extends EventEmitter {
});
}
getSelfInfo() {
return new Promise(async (resolve, reject) => {
// listen for response
this.once(Constants.ResponseCodes.SelfInfo, (selfInfo) => {
resolve(selfInfo);
});
// request self info
await this.sendCommandAppStart();
});
}
getContacts() {
return new Promise(async (resolve, reject) => {
// add contacts we receive to a list
const contacts = [];
const onContactReceived = (contact) => {
contacts.push(contact);
}
// listen for contacts
this.on(Constants.ResponseCodes.Contact, onContactReceived);
// there's no more contacts to receive, stop listening and resolve the promise
this.once(Constants.ResponseCodes.EndOfContacts, () => {
this.off(Constants.ResponseCodes.Contact, onContactReceived);
resolve(contacts);
});
// request contacts from device
await this.sendCommandGetContacts();
});
}
}
export default Connection;

View file

@ -6,12 +6,20 @@ import Connection from "./connection.js";
class SerialConnection extends Connection {
constructor(serialPort) {
super();
this.serialPort = serialPort;
this.reader = serialPort.readable.getReader();
this.writable = serialPort.writable;
this.readBuffer = [];
this.readLoop();
// fire connected callback after constructor has returned
setTimeout(() => {
this.onConnected();
}, 0);
}
static async open() {

View file

@ -26,6 +26,24 @@ class EventEmitter {
}
once(event, callback) {
// internal callback to handle the event
const internalCallback = (...data) => {
// we received an event, so lets remove the event listener
this.off(event, internalCallback);
// fire the original callback provided by the user
setTimeout(() => callback(...data), 0);
};
// listen to this event
this.on(event, internalCallback);
}
emit(event, ...data) {
// invoke each listener for this event