Skip to content

Commit

Permalink
Add support for message requesting (#47)
Browse files Browse the repository at this point in the history
Fixes #46
  • Loading branch information
evertonstz authored Apr 29, 2024
1 parent fc9c520 commit 523ef3b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/components/MainContent/SelectedDevice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
BatteryPercentageMessage,
BrightnessMessage,
ColorThemeMessage,
RequestStatusMessage,
} from '@/pax/core/messages';
import { Messages } from '@/pax/shared/enums';
import { ColorTheme } from '@/pax/shared/types';
import { usePaxContext } from '@/state/hooks';
import { isEqual } from 'lodash';
Expand Down Expand Up @@ -129,6 +131,21 @@ export const SelectedDevice = ({ currentDevice }: SelectedDeviceProps) => {
>
{bluetoothState.connected ? 'Disconnect' : 'Connect'}
</Button>
<Button
onClick={() => {
const toPost = post(
RequestStatusMessage.createWithMessage([
Messages.ATTRIBUTE_COLOR_THEME,
Messages.ATTRIBUTE_BRIGHTNESS,
]),
currentDevice,
);
void bluetoothState.writeToMainService(toPost.packet);
}}
variant="secondary"
>
Update stats
</Button>
<ThemePicker
loading={!bluetoothState.connected}
colorThemes={hardcodedThemes}
Expand Down
69 changes: 69 additions & 0 deletions src/pax/core/messages/RequestStatusMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { PaxDecryptedPacket } from '@/pax/containers/lib';
import { Messages } from '@/pax/shared/enums';

import { MessageAbs } from './MessageAbs';
import { ReadAndWriteMessageAbs } from './ReadAndWriteMessageAbs';

export class RequestStatusMessage
extends ReadAndWriteMessageAbs
implements MessageAbs
{
readonly messages: Messages[];
readonly messageType: Messages;
readonly packet: PaxDecryptedPacket;

readonly PaxBrightnessMin = 0;
readonly PaxBrightnessMax = 128;

constructor(
builder: RequestStatusMessageBuilderFromValue<RequestStatusMessage>,
) {
super();
this.messageType = Messages.ATTRIBUTE_STATUS_UPDATE;
if (builder instanceof RequestStatusMessageBuilderFromValue) {
this.messages = builder.getMessages();
const value = this.messages.reduce((acc, attributeId) => {
return acc + Math.pow(2, attributeId);
}, 0);
const buffer = new ArrayBuffer(16);
const view = new PaxDecryptedPacket(buffer);
view.setUint8(0, this.messageType);
view.setBigUint64(1, BigInt(value), true);
this.packet = view;
} else {
throw new Error('Invalid builder');
}
}

static createWithMessage(message: Messages[]): RequestStatusMessage {
const builder =
new RequestStatusMessageBuilderFromValue<RequestStatusMessage>();
builder.setMessages(message);
return new RequestStatusMessage(builder);
}
}

export class RequestStatusMessageBuilderFromValue<
T extends RequestStatusMessage,
> {
private message?: Messages[];

setMessages(message: Messages[]): RequestStatusMessageBuilderFromValue<T> {
this.message = message;
return this;
}

getMessages(): Messages[] {
if (!this.message) {
throw new Error('Messages are not set');
}
return this.message;
}

build(ctor: new (builder: RequestStatusMessageBuilderFromValue<T>) => T): T {
if (!this.message) {
throw new Error('Messages are not set');
}
return new ctor(this);
}
}
1 change: 1 addition & 0 deletions src/pax/core/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './HeatingStateMessage';
export * from './ColorThemeMessage';
export * from './BatteryPercentageMessage';
export * from './BrightnessMessage';
export * from './RequestStatusMessage';

0 comments on commit 523ef3b

Please sign in to comment.