Skip to content

Commit addb1b1

Browse files
Minor spelling mistakes and promises now return booleans instead of strings (#28)
1 parent fb39b43 commit addb1b1

File tree

5 files changed

+33
-27
lines changed

5 files changed

+33
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ coverage
66
samsung-tv-control-*.tgz
77
log*.txt
88
yarn-error*
9+
.idea

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const control = new Samsung(config)
4545

4646
control.turnOn()
4747
control
48-
.isAvaliable()
48+
.isAvailable()
4949
.then(() => {
5050
// Get token for API
5151
control.getToken(token => {

example/index-dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const control = new Samsung(config)
1717

1818
async function main() {
1919
await control.turnOn()
20-
await control.isAvaliable()
20+
await control.isAvailable()
2121

2222
let token = await control.getTokenPromise()
2323
console.log('$$ token:', token)

example/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const control = new Samsung(config)
1818

1919
control.turnOn()
2020
control
21-
.isAvaliable()
21+
.isAvailable()
2222
.then(() => {
2323
// Get token for API
2424
control.getToken(token => {

src/samsung.ts

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class Samsung {
121121
if (token) {
122122
resolve(token)
123123
} else {
124-
reject()
124+
reject(new Error('Did not receive token from Samsung TV'))
125125
}
126126
})
127127
})
@@ -253,50 +253,55 @@ class Samsung {
253253
})
254254
}
255255

256-
public isAvaliable(): Promise<string> {
256+
public isAvailable(): Promise<boolean> {
257257
return new Promise((resolve, reject) => {
258258
request.get(
259259
{ url: `http://${this.IP}:8001${this.PORT === 55000 ? '/ms/1.0/' : '/api/v2/'}`, timeout: 3000 },
260260
(err: Error, res: request.RequestResponse) => {
261+
if (err) {
262+
return reject(err)
263+
}
264+
261265
if (!err && res.statusCode === 200) {
262266
this.LOGGER.log(
263-
'TV is avaliable',
267+
'TV is available',
264268
{ request: res.request, body: res.body as string, code: res.statusCode },
265-
'isAvaliable'
269+
'isAvailable'
266270
)
267-
resolve('TV is avaliable')
268-
} else {
269-
this.LOGGER.error('TV is avaliable', { err }, 'isAvaliable')
270-
reject('No response from TV')
271+
resolve(true)
271272
}
273+
274+
this.LOGGER.error('TV is not available', { err }, 'isAvailable')
275+
resolve(false)
272276
}
273277
)
274278
})
275279
}
276280

277-
public isAvaliablePing(): Promise<string> {
278-
return new Promise((resolve, reject) => {
281+
public isAvailablePing(): Promise<boolean> {
282+
return new Promise((resolve) => {
279283
exec('ping -c 1 -W 1 ' + this.IP, (error, stdout, _) => {
280284
if (error) {
281-
this.LOGGER.error('TV is avaliable', { error }, 'isAvaliable')
282-
reject('No response from TV')
285+
this.LOGGER.error('TV is not available', { error }, 'isAvailable')
286+
// Do not reject since we're testing for the TV to be available
287+
resolve(false)
283288
} else {
284-
this.LOGGER.log('TV is avaliable', { stdout }, 'isAvaliable')
285-
resolve('TV is avaliable')
289+
this.LOGGER.log('TV is available', { stdout }, 'isAvailable')
290+
resolve(true)
286291
}
287292
})
288293
})
289294
}
290295

291-
public turnOn(): Promise<string> {
296+
public turnOn(): Promise<boolean> {
292297
return new Promise((resolve, reject) => {
293298
wol.wake(this.MAC, { num_packets: 30 }, (err: Error) => {
294299
if (err) {
295300
this.LOGGER.error('Fail turn on', err, 'turnOn')
296-
reject('Fail turn on')
301+
reject(err)
297302
} else {
298303
this.LOGGER.log('WOL sent command to TV', '', 'turnOn')
299-
resolve('TV is avaliable')
304+
resolve(true)
300305
}
301306
})
302307
})
@@ -356,7 +361,7 @@ class Samsung {
356361
// this.ws.close()
357362
}
358363

359-
// TODO, additional check on avaliable instead of ws.open
364+
// TODO, additional check on available instead of ws.open
360365
// if(data.event == "ms.channel.connect") { _sendCMD() }
361366
})
362367

@@ -408,10 +413,10 @@ class Samsung {
408413
this._send(
409414
command,
410415
(err, res) => {
411-
if (!err) {
412-
resolve(res)
413-
} else {
416+
if (err) {
414417
reject(err)
418+
} else {
419+
resolve(res)
415420
}
416421
},
417422
eventHandle
@@ -473,10 +478,10 @@ class Samsung {
473478
private _sendLegacyPromise(key: KEYS) {
474479
return new Promise((resolve, reject) => {
475480
this._sendLegacy(key, (err, res) => {
476-
if (!err) {
477-
resolve(res)
478-
} else {
481+
if (err) {
479482
reject(err)
483+
} else {
484+
resolve(res)
480485
}
481486
})
482487
})

0 commit comments

Comments
 (0)