Skip to content

Commit

Permalink
Merge pull request #8 from machinefi/add-custom-event-type
Browse files Browse the repository at this point in the history
allow emit msgs with custom events
  • Loading branch information
nicky-ru authored Jul 26, 2023
2 parents fc05c72 + 00be4b0 commit c625794
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ const message = simulator.generateEvents(numberOfDataPoints);
To send a single message to the server, call the `sendSingleMessage` method:

```ts
const { res, msg } = await simulator.sendSingleMessage();
const eventType = "TEMPERATURE";

const { res, msg } = await simulator.sendSingleMessage(eventType);

console.log("response: ", res?.data);
console.log("w3bstream message: ", msg);
Expand All @@ -96,7 +98,10 @@ console.log("w3bstream message: ", msg);
To send messages at a specified interval, use the `powerOn` method:

```ts
simulator.powerOn(intervalInSec);
const intervalInSec = 1;
const eventType = "TEMPERATURE";

simulator.powerOn(intervalInSec, eventType);
```

To stop sending messages, call the `powerOff` method:
Expand Down
7 changes: 4 additions & 3 deletions src/Simulator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export class Simulator {
};
}

powerOn(intervalInSec: number): void {
powerOn(intervalInSec: number, eventType?: string): void {
const intervalInMs = intervalInSec * 1000;

this._interval = setInterval(async () => {
try {
await this.sendSingleMessage();
await this.sendSingleMessage(eventType);
} catch (e) {
console.log(e);
console.log("Stopping simulator due to error");
Expand All @@ -61,14 +61,15 @@ export class Simulator {
}
}

async sendSingleMessage(): Promise<{
async sendSingleMessage(eventType?: string): Promise<{
res: AxiosResponse | undefined;
msg: W3bStreamMessage;
}> {
const message = this.generateSingleMessage();

const header: WSHeader = {
device_id: message.deviceId,
event_type: eventType,
};
const res = await this._client?.publishDirect(header, message);

Expand Down
21 changes: 19 additions & 2 deletions src/Simulator/simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ describe("simulator", () => {
);
});
});

describe("simulation", () => {
interface TemperatureDataPoint extends DataPoint {
temperature: number;
Expand All @@ -161,6 +160,10 @@ describe("simulator", () => {
});
afterEach(() => {
mockSendMsg.mockRestore();
fs.rmSync(path.join("./", "private.key"), { force: true });
});
it("should send a message to a custom event", async () => {
await simulator1.sendSingleMessage("TEMPERATURE_EVENT");
});
it("should power on", async () => {
jest.useFakeTimers();
Expand All @@ -181,6 +184,21 @@ describe("simulator", () => {

jest.useRealTimers();
});
it("should power on with a custom event", async () => {
jest.useFakeTimers();

const eventType = "TEMPERATURE_EVENT";
simulator1.powerOn(1, eventType);
jest.spyOn(simulator1, "sendSingleMessage");

await jest.advanceTimersByTimeAsync(1_000);

expect(simulator1.sendSingleMessage).toHaveBeenCalledTimes(1);
expect(simulator1.sendSingleMessage).toHaveBeenCalledWith(eventType);

simulator1.powerOff();
jest.useRealTimers();
});
it("should power off if msg sending fails", async () => {
jest.useFakeTimers();

Expand All @@ -196,7 +214,6 @@ describe("simulator", () => {

jest.useRealTimers();
});

});
});

Expand Down

0 comments on commit c625794

Please sign in to comment.