Skip to content

Commit

Permalink
added closeConnextion
Browse files Browse the repository at this point in the history
  • Loading branch information
Toxblh committed Feb 27, 2020
1 parent 174c1a9 commit d4b79ee
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Also you can use the lib in your Node-RED https://github.com/Toxblh/node-red-con
You can try [example code](example/index.js)

```js
// import Samsung, { APPS, KEYS } from 'samsung-tv-control'
const { Samsung, KEYS, APPS } = require('samsung-tv-control')

const config = {
Expand Down Expand Up @@ -73,6 +74,10 @@ control
console.log('# Response openApp', res)
}
})

// Control will keep connection for next messages in 1 minute
// If you would like to close it immediately, you can use `closeConnection()`
control.closeConnection()
})
.catch(e => console.error(e))
```
Expand Down
1 change: 1 addition & 0 deletions example/index-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async function main() {
// await control.openApp(APPS.YouTube)
await control.sendKeyPromise(KEYS.KEY_POWER)
await control.getLogs()
control.closeConnection()
return
}

Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Samsung from './samsung'

export { default as Samsung } from './samsung'
export { default as KEYS } from './keys';
export { default as APPS } from './apps';
export { default as KEYS } from './keys'
export { default as APPS } from './apps'
export default Samsung
25 changes: 22 additions & 3 deletions src/samsung.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ import * as WebSocket from 'ws'
import { KEYS } from './keys'
import Logger from './logger'
import { Configuration, WSData, App, Command } from './types'
import { base64, chr, getVideoId, getMsgInstalledApp, getMsgLaunchApp, getCommandByKey, getSendTextCommand } from './helpers'
import {
base64,
chr,
getVideoId,
getMsgInstalledApp,
getMsgLaunchApp,
getCommandByKey,
getSendTextCommand
} from './helpers'

class Samsung {
private IP: string
Expand Down Expand Up @@ -230,7 +238,11 @@ class Samsung {
},
(err, response) => {
if (!err) {
this.LOGGER.log('Link sended', { status: response.statusCode, body: response.body, headers: response.headers }, 'openYouTubeLink')
this.LOGGER.log(
'Link sended',
{ status: response.statusCode, body: response.body, headers: response.headers },
'openYouTubeLink'
)
resolve('Link sended')
} else {
this.LOGGER.error('While send a link, somthing went wrong', { err }, 'openYouTubeLink')
Expand Down Expand Up @@ -294,6 +306,13 @@ class Samsung {
this.LOGGER.saveLogToFile()
}

/**
* If you don't need to keep connection, you can to close immediately
*/
public closeConnection() {
this.wsClose()
}

private _send(
command: Command,
done?: (err: null | (Error & { code: string }), res: WSData | null) => void,
Expand Down Expand Up @@ -514,4 +533,4 @@ class Samsung {
}
}

export default Samsung
export default Samsung
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"skipLibCheck": true
},
"include": ["src/**/*"],
"exclude": ["src/**/*.spec.*"]
"exclude": ["src/**/*.spec.*", "**/*.spec.ts"]
}
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
},
"rulesDirectory": [],
"linterOptions": {
"exclude": ["node_modules/**/*.{ts,tsx}", "**/*.{js,jsx,mjs,json}"]
"exclude": ["node_modules/**/*.{ts,tsx}", "**/*.{js,jsx,mjs,json}", "src/**/*.spec.*"]
}
}

0 comments on commit d4b79ee

Please sign in to comment.