diff --git a/libs/microbit/control.cpp b/libs/microbit/control.cpp index 2d8cc1f5..051c512a 100644 --- a/libs/microbit/control.cpp +++ b/libs/microbit/control.cpp @@ -159,7 +159,7 @@ namespace control { /** * Gets a friendly name for the device derived from the its serial number */ - //% blockId="control_device_name" block="device name" weight=10 + //% blockId="control_device_name" block="device name" weight=10 blockGap=8 StringData* deviceName() { return ManagedString(microbit_friendly_name()).leakData(); } @@ -167,7 +167,7 @@ namespace control { /** * Derive a unique, consistent serial number of this device from internal data. */ - //% + //% blockId="control_device_serial_number" block="device serial number" weight=9 int deviceSerialNumber() { return microbit_serial_number(); } diff --git a/libs/microbit/messages.ts b/libs/microbit/messages.ts new file mode 100644 index 00000000..6d674823 --- /dev/null +++ b/libs/microbit/messages.ts @@ -0,0 +1,51 @@ +namespace messages { + var streamid: string; + + export function setStreamId(id: string) { + streamid = id; + } + + /** + * Creates a new message that includes the board serial number and the stream id if any + */ + export function createMessage() : Message { + let m = new Message(); + m.addNumber('board', control.deviceSerialNumber()); + if (streamid != null && streamid.length > 0) + m.addString('stream', streamid); + return m; + } + + /** + * A message containig custom data + */ + export class Message { + private buffer:string = ''; + + /** + * Adds a string field to the message + */ + //% + public addString(name:string, value:string) { + if (this.buffer.length > 0) this.buffer += ','; + this.buffer += name + ':"' + value + '"'; + } + + /** + * Adds a number field to the message + */ + //% + public addNumber(name:string, value: number) { + if (this.buffer.length > 0) this.buffer += ','; + this.buffer += name + ':' + value; + } + + /** + * Converts the message to a JSON payload + */ + //% + public toJSON() : string { + return '{' + this.buffer + '}'; + } + } +} \ No newline at end of file diff --git a/libs/microbit/shims.d.ts b/libs/microbit/shims.d.ts index 84c4ff0d..c5fb8383 100644 --- a/libs/microbit/shims.d.ts +++ b/libs/microbit/shims.d.ts @@ -346,13 +346,13 @@ declare namespace control { /** * Gets a friendly name for the device derived from the its serial number */ - //% blockId="control_device_name" block="device name" weight=10 shim=control::deviceName + //% blockId="control_device_name" block="device name" weight=10 blockGap=8 shim=control::deviceName function deviceName(): string; /** * Derive a unique, consistent serial number of this device from internal data. */ - //% shim=control::deviceSerialNumber + //% blockId="control_device_serial_number" block="device serial number" weight=9 shim=control::deviceSerialNumber function deviceSerialNumber(): number; } diff --git a/sim/libmbit.ts b/sim/libmbit.ts index 6c65f5b5..2d3979be 100644 --- a/sim/libmbit.ts +++ b/sim/libmbit.ts @@ -233,6 +233,13 @@ namespace pxsim.control { ? b.id.slice(0, 4) : 'abcd'; } + + export function deviceSerialNumber(): number { + let b = board(); + return parseInt(b && b.id + ? b.id.slice(1) + : '42'); + } export function onEvent(id: number, evid: number, handler: RefAction) { pxt.registerWithDal(id, evid, handler) diff --git a/sim/state.ts b/sim/state.ts index 18832f26..e639534e 100644 --- a/sim/state.ts +++ b/sim/state.ts @@ -511,7 +511,7 @@ namespace pxsim { constructor() { super() - this.id = "b" + Math.random().toString().slice(1); + this.id = "b" +Math_.random(2147483647); this.animationQ = new AnimationQueue(runtime); this.bus = new EventBus(runtime); this.radio = new RadioBus(runtime);