-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for message requesting (#47)
Fixes #46
- Loading branch information
1 parent
fc9c520
commit 523ef3b
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters