pxt-calliope/libs/core/messages.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

2016-04-28 19:46:03 +02:00
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 + '}';
}
}
}