Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM node:alpine
FROM node:10
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NodeJS v10 is pretty old and not supported anymore. Are there any reasons we can't use the latest LTS version as it was before?

Copy link
Author

@Xenomes Xenomes Oct 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats my bad, mcrypt needs the full version of node and not specially v10. Used V10 because that was the default on my test system Debian Buster the OS for Domoticz stable.


WORKDIR /opt/ewpe-smart-mqtt
COPY package*.json ./
RUN npm ci
COPY . .
CMD ["npm", "start"]
CMD ["npm", "start"]
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,38 @@ docker run -it \
demydiuk/ewpe-smart-mqtt:latest
```

## Installation (service)
Move directory and prepare
```
sudo mv ewpe-smart-mqtt /opt/.
```

Create file
`sudo nano /etc/systemd/system/ewpe-smart-mqtt.service`
```
[Unit]
Description=ewpe-smart-mqtt
After=mosquitto.target

[Service]
ExecStart=/usr/bin/node /opt/ewpe-smart-mqtt/index.js --NETWORK="192.168.1.255" --MQTT_SERVER="mqtt://127.0.0.1" --MQTT_PORT="1883" --MQTT_USERNAME="" --MQTT_PASSWORD="" --MQTT_BASE_TOPIC="ewpe-smart" --DEVICE_POLL_INTERVAL="5000"
# Required on some systems
WorkingDirectory=/opt/ewpe-smart-mqtt
StandardOutput=inherit
StandardError=inherit
Restart=always
RestartSec=10
User=pi
Group=pi

[Install]
WantedBy=multi-user.target
```

Enable service with `sudo systemctl enable ewpe-smart-mqtt.service`

Start/stop service with `sudo systemctl start|stop ewpe-smart-mqtt.service`

## Communicating with the bridge

- Publish to `ewpe-smart/devices/list` to receive list of registered devices
Expand Down
10 changes: 5 additions & 5 deletions app/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Connection extends EventEmitter {
const messageHandler = (msg, rinfo) => {
const message = JSON.parse(msg.toString());
let response;

// Check device address data
if (rinfo.address !== address || rinfo.port !== port) {
return;
Expand Down Expand Up @@ -94,11 +94,11 @@ class Connection extends EventEmitter {

resolve(response);
}

logger.debug(`Sending request to ${address}:${port}: ${JSON.stringify(payload)}`);

this.socket.on('message', messageHandler);

const toSend = Buffer.from(JSON.stringify(request));
this.socket.send(toSend, 0, toSend.length, port, address);
});
Expand All @@ -113,4 +113,4 @@ class Connection extends EventEmitter {
}
}

module.exports = Connection;
module.exports = Connection;
10 changes: 5 additions & 5 deletions app/device_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DeviceManager extends EventEmitter {
}

async _registerDevice(message, rinfo) {
const deviceId = message.cid || message.mac;
const deviceId = message.cid;
logger.info(`New device found: ${message.name} (mac: ${deviceId}), binding...`)
const { address, port } = rinfo;

Expand Down Expand Up @@ -71,11 +71,11 @@ class DeviceManager extends EventEmitter {
...acc,
[key]: response.dat[index]
}), {});

if('TemSen' in deviceStatus){
deviceStatus['TemSen'] +=TEMPERATURE_SENSOR_OFFSET;
}

this.emit('device_status', deviceId, deviceStatus);
return deviceStatus;
}
Expand All @@ -97,12 +97,12 @@ class DeviceManager extends EventEmitter {
const response = await this.connection.sendRequest(device.address, device.port, device.key, payload);
const deviceStatus = response.opt.reduce((acc, key, index) => ({
...acc,
[key]: response.val[index]
[key]: response.key
}), {});

this.emit('device_status', deviceId, deviceStatus);
return deviceStatus;
}
}

module.exports = DeviceManager
module.exports = DeviceManager
30 changes: 19 additions & 11 deletions app/encryptor.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
const crypto = require('crypto');
const MCrypt = require('mcrypt').MCrypt;

const defaultKey = 'a3K8Bx%2r8Y7#xDh';

function encrypt(data, key = defaultKey) {
const cipher = crypto.createCipheriv('aes-128-ecb', key, '');
const str = cipher.update(JSON.stringify(data), 'utf8', 'base64');
const request = str + cipher.final('base64');
return request;
const desEcb = new MCrypt('rijndael-128', 'ecb');
desEcb.open(key);
console.log(JSON.stringify(data));
const ciphertext = desEcb.encrypt(JSON.stringify(data));
const request = ciphertext.toString('base64');
return request;
}

function decrypt(data, key = defaultKey) {
const decipher = crypto.createDecipheriv('aes-128-ecb', key, '');
const str = decipher.update(data, 'base64', 'utf8');
const response = JSON.parse(str + decipher.final('utf8'));

return response;
const aesEcb = MCrypt('rijndael-128', 'ecb')
aesEcb.open(key);
const ciphertext = new Buffer(data, 'base64');
const plaintext = aesEcb.decrypt(ciphertext).toString();
if (plaintext.includes("�")) {null} else {
const response = plaintext.slice(0,plaintext.lastIndexOf('}'))+'}';
result = JSON.parse(response.toString());
result.cid = result.cid || result.mac;
result.name = result.name || result.mac.substring(result.mac.length - 8);
}
return result
}

module.exports = {
defaultKey,
encrypt,
decrypt
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ewpe-smart-mqtt",
"version": "1.0.5",
"version": "1.0.6",
"description": "MQTT Bridge for EWPE Smart powered air conditioners",
"license": "MIT",
"author": "Stanislav Demydiuk",
Expand All @@ -17,6 +17,7 @@
},
"dependencies": {
"dotenv": "^8.1.0",
"mcrypt": "^0.1.17",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I see mcrypt package has been deprecated and not maintained anymore. The author suggests using cryptian instead

Copy link
Author

@Xenomes Xenomes Oct 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mcrypt was the only decoder showing the strange characters after } in the json returning from the ac device and cryptian requires V19 and the use base is complete different.

"mqtt": "^4.2.8",
"winston": "^3.3.3"
}
Expand Down