Skip to content

Commit

Permalink
ewelink: add stop service method
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato committed Dec 10, 2023
1 parent 8888188 commit 1bd12c5
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions server/services/ewelink/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = function EwelinkService(gladys, serviceId) {
*/
async function stop() {
logger.info('Stopping eWeLink service');
await eWeLinkHandler.stop();
}

return Object.freeze({
Expand Down
13 changes: 13 additions & 0 deletions server/services/ewelink/lib/ewelink.stop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @description Stop eWeLink service.
* @example
* await this.stop();
*/
async function stop() {
this.ewelinkWebAPIClient = null;
this.updateStatus({ connected: false });
}

module.exports = {
stop,
};
2 changes: 2 additions & 0 deletions server/services/ewelink/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const { handleRequest } = require('./handlers/ewelink.handleRequest');
const { handleResponse } = require('./handlers/ewelink.handleResponse');

const { init } = require('./ewelink.init');
const { stop } = require('./ewelink.stop');
const { upgrade } = require('./versions/ewelink.upgrade');

/**
Expand Down Expand Up @@ -62,6 +63,7 @@ EweLinkHandler.prototype.setValue = setValue;
EweLinkHandler.prototype.getStatus = getStatus;

EweLinkHandler.prototype.init = init;
EweLinkHandler.prototype.stop = stop;
EweLinkHandler.prototype.upgrade = upgrade;

module.exports = EweLinkHandler;
7 changes: 6 additions & 1 deletion server/test/services/ewelink/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const { SERVICE_ID } = require('./lib/constants');
const { fake, assert } = sinon;

const EweLinkHandlerMock = sinon.stub();
EweLinkHandlerMock.prototype.init = fake.returns(null);
EweLinkHandlerMock.prototype.init = fake.resolves(null);
EweLinkHandlerMock.prototype.stop = fake.resolves(null);

const EweLinkService = proxyquire('../../../services/ewelink/index', {
'./lib': EweLinkHandlerMock,
Expand All @@ -28,12 +29,16 @@ describe('EweLinkService', () => {
.to.have.property('controllers')
.and.be.instanceOf(Object);
});

it('should start service', async () => {
await eweLinkService.start();
assert.calledOnceWithExactly(eweLinkService.device.init);
assert.notCalled(eweLinkService.device.stop);
});

it('should stop service', async () => {
await eweLinkService.stop();
assert.notCalled(eweLinkService.device.init);
assert.calledOnceWithExactly(eweLinkService.device.stop);
});
});
38 changes: 38 additions & 0 deletions server/test/services/ewelink/lib/ewelink.stop.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const sinon = require('sinon');

const { fake, assert } = sinon;

const { expect } = require('chai');
const EwelinkHandler = require('../../../../services/ewelink/lib');
const { SERVICE_ID } = require('./constants');
const { EVENTS, WEBSOCKET_MESSAGE_TYPES } = require('../../../../utils/constants');

describe('eWeLinkHandler stop', () => {
let eWeLinkHandler;
let gladys;

beforeEach(() => {
gladys = {
event: {
emit: fake.returns(null),
},
};
eWeLinkHandler = new EwelinkHandler(gladys, null, SERVICE_ID);
eWeLinkHandler.ewelinkWebAPIClient = {};
eWeLinkHandler.status = { configured: true, connected: true };
});

afterEach(() => {
sinon.reset();
});

it('should emit event', async () => {
await eWeLinkHandler.stop();
expect(eWeLinkHandler.ewelinkWebAPIClient).to.eq(null);

assert.calledOnceWithExactly(gladys.event.emit, EVENTS.WEBSOCKET.SEND_ALL, {
type: WEBSOCKET_MESSAGE_TYPES.EWELINK.STATUS,
payload: { configured: true, connected: false },
});
});
});
2 changes: 0 additions & 2 deletions server/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,8 +930,6 @@ const WEBSOCKET_MESSAGE_TYPES = {
},
EWELINK: {
STATUS: 'ewelink.status',
NEW_DEVICE: 'ewelink.new-device',
ERROR: 'ewelink.error',
},
BROADLINK: {
LEARN_MODE: 'broadlink.learn',
Expand Down

0 comments on commit 1bd12c5

Please sign in to comment.