-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from jalibu/dev
Dev
- Loading branch information
Showing
14 changed files
with
178 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,35 @@ | ||
import * as NodeHelper from 'node_helper' | ||
import * as fs from 'fs' | ||
import * as BSMB from 'bosch-smart-home-bridge' | ||
import * as Log from 'logger' | ||
import BshbClient from './BshClient' | ||
import { Config } from '../types/Config' | ||
import { Device } from '../types/Device' | ||
import { Service } from '../types/Service' | ||
|
||
module.exports = NodeHelper.create({ | ||
cert: null, | ||
key: null, | ||
logger: null, | ||
client: null, | ||
rooms: null, | ||
start() { | ||
this.cert = fs.readFileSync(`${__dirname}/client-cert.pem`).toString() | ||
this.key = fs.readFileSync(`${__dirname}/client-key.pem`).toString() | ||
|
||
// Override Logger to avoid some annoying logs | ||
this.logger = new BSMB.DefaultLogger() | ||
this.logger.fine = () => {} | ||
this.logger.info = (msg: string) => { | ||
if (msg.indexOf('Using existing certificate') >= 0 || msg.indexOf('Check if client with identifier') >= 0) { | ||
return | ||
} | ||
console.info(msg) | ||
} | ||
|
||
console.log(`${this.name} helper method started...`) | ||
this.client = new BshbClient() | ||
Log.log(`${this.name} helper method started...`) | ||
}, | ||
|
||
async establishConnection(config: Config) { | ||
if (!this.client) { | ||
try { | ||
const bshb = BSMB.BoschSmartHomeBridgeBuilder.builder() | ||
.withHost(config.host) | ||
.withClientCert(this.cert) | ||
.withClientPrivateKey(this.key) | ||
.withLogger(this.logger) | ||
.build() | ||
async socketNotificationReceived(notification, payload) { | ||
if (notification === 'BSH_CONFIG_REQUEST') { | ||
const config = payload as Config | ||
this.client.setConfig(config) | ||
|
||
await bshb.pairIfNeeded(config.name, config.identifier, config.password).toPromise() | ||
this.client = bshb.getBshcClient() | ||
} catch (err) { | ||
console.log(err) | ||
this.getClientData() | ||
if (!this.schedule) { | ||
this.schedule = setInterval(this.getClientData.bind(this), config.refreshIntervalInSeconds * 1000) | ||
} | ||
} | ||
}, | ||
|
||
async loadData() { | ||
async getClientData() { | ||
try { | ||
if (!this.rooms) { | ||
const { parsedResponse: rooms } = await this.client.getRooms().toPromise() | ||
this.rooms = rooms | ||
} | ||
|
||
const { | ||
parsedResponse: devices | ||
}: { | ||
parsedResponse: Device[] | ||
} = await this.client.getDevices().toPromise() | ||
|
||
const { | ||
parsedResponse: services | ||
}: { | ||
parsedResponse: Service[] | ||
} = await this.client.getDevicesServices().toPromise() | ||
|
||
for (const device of devices) { | ||
device.services = services.filter((service) => service.deviceId === device.id) | ||
} | ||
|
||
for (const room of this.rooms) { | ||
room.devices = devices.filter((device) => device.roomId === room.id) | ||
} | ||
const rooms = await this.client.getRooms() | ||
this.sendSocketNotification('BSH_ROOMS_RESPONSE', rooms) | ||
} catch (err) { | ||
console.error(err.message) | ||
} | ||
}, | ||
|
||
async socketNotificationReceived(notification, config) { | ||
if (notification === 'GET_STATUS') { | ||
if (config.mocked) { | ||
const data = fs.readFileSync(`${__dirname}/debugResponse.json`).toString() | ||
this.rooms = JSON.parse(data) | ||
} else { | ||
await this.establishConnection(config) | ||
await this.loadData() | ||
|
||
if (config.debug) { | ||
fs.writeFileSync(`${__dirname}/debugResponse.json`, JSON.stringify(this.rooms)) | ||
} | ||
} | ||
this.sendSocketNotification('STATUS_RESULT', this.rooms) | ||
} else { | ||
console.warn(`${notification} is invalid notification`) | ||
this.sendSocketNotification('BSH_ERROR_RESPONSE', { | ||
type: 'WARNING', | ||
message: err.message | ||
}) | ||
} | ||
} | ||
}) |
Oops, something went wrong.