Skip to content

Commit

Permalink
Merge pull request #6 from jalibu/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jalibu authored Oct 3, 2021
2 parents dfd6ed8 + da45bf1 commit 02a2f2b
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 137 deletions.
17 changes: 7 additions & 10 deletions MMM-BoschSmartHome.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
margin-bottom: 4px;
align-items: center;
}
.bsh-error {
font-size: 0.5em;
color: red;
}
.bsh-title-left {
flex-grow: 4;
}
Expand Down Expand Up @@ -126,7 +130,7 @@
}

.gauge-semi-circle::before {
content: "";
content: '';
position: absolute;
bottom: 0;
left: 13%;
Expand Down Expand Up @@ -181,7 +185,7 @@
}

.gauge-semi-circle--mask::before {
content: "";
content: '';
position: absolute;
top: 0;
left: 0;
Expand Down Expand Up @@ -215,14 +219,7 @@
}

.gauge-semi-circle.humidity {
background: linear-gradient(
to right,
#c0392b 0%,
#f1c40f 15%,
#1abc9c 50%,
#f1c40f 85%,
#c0392b 100%
);
background: linear-gradient(to right, #c0392b 0%, #f1c40f 15%, #1abc9c 50%, #f1c40f 85%, #c0392b 100%);
}

.gauge-semi-circle.temperature {
Expand Down
4 changes: 2 additions & 2 deletions MMM-BoschSmartHome.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions node_helper.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mmm-bosch-smart-home",
"version": "1.1.1",
"version": "1.2.0",
"description": "A client interface for the Bosch Smart Home System on the MagicMirror² platform.",
"main": "MMM-BoschSmartHome.js",
"repository": {
Expand Down
10 changes: 7 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ const bannerText = `/*! ********************************************************
export default [
{
input: './src/frontend/Frontend.ts',
external: ['logger'],
plugins: [typescript({ module: 'ESNext' }), nodeResolve(), commonjs(), terser(), banner2(() => bannerText)],
output: {
file: './' + pkg.main,
format: 'iife'
format: 'iife',
globals: {
logger: 'Log'
}
}
},
{
input: './src/backend/Backend.ts',
external: ['node_helper', 'bosch-smart-home-bridge', 'fs'],
plugins: [typescript({ module: 'ESNext' }), terser(), banner2(() => bannerText)],
external: ['node_helper', 'logger', 'bosch-smart-home-bridge', 'fs'],
plugins: [typescript({ module: 'ESNext' }), nodeResolve(), terser(), banner2(() => bannerText)],
output: {
file: './node_helper.js',
format: 'cjs'
Expand Down
99 changes: 18 additions & 81 deletions src/backend/Backend.ts
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
})
}
}
})
Loading

0 comments on commit 02a2f2b

Please sign in to comment.