Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var EventEmitter = require('events').EventEmitter;
- var net = require('net');
- var util = require('util');
- var topic, message;
- var previous = {
- 0: "",
- 1: "",
- 2: "",
- 3: "",
- 4: "",
- 5: "",
- 6: "",
- 7: "",
- 8: "",
- 9: "",
- 10: "",
- 11: "",
- 12: "",
- 13: "",
- 14: "",
- 15: "",
- 16: "",
- 17: "",
- 18: "",
- 19: "",
- 20: "",
- 21: "",
- 22: "",
- 23: "",
- 24: "",
- 25: "",
- 26: "",
- 27: "",
- 28: "",
- 29: "",
- 30: "",
- 31: "",
- 32: "",
- 33: "",
- 48: "",
- 49: "",
- 50: "",
- 56: "",
- 57: "",
- 58: "",
- 100: "",
- 115: "",
- 116: "",
- 117: "",
- 118: "",
- 119: "",
- 120: "",
- 121: "",
- 122: "",
- 123: "",
- 124: "",
- 125: "",
- 126: "",
- 127: ""
- };
- var opentherm_ids = {
- 0: "flame_status",
- 1: "control_setpoint",
- 2: "master_config_flags/master_member_id",
- 3: "slave_config_flags/slave_member_id",
- 4: "remote_command",
- 5: "application-specific_fault_flag/OEM_fault_code",
- 6: "remote_boiler_parameter_transfer-enable/read_write_flags",
- 7: "cooling_control_signal",
- 8: "control_setpoint_2nd_circuit",
- 9: "remote_override_setpoint",
- 10: "number_transparent_slave_parameters_supported_slave",
- 11: "index_number/value_referred_to_transparent_slave_parameter",
- 12: "size_of_fault_history_buffer_slave",
- 13: "index_number/value_referred_to_fault_history_buffer",
- 14: "max_relative_modulation",
- 15: "max_boiler_capacity/minimum_boiler_modulation",
- 16: "room_setpoint",
- 17: "relative_modulation",
- 18: "ch_water_pressure",
- 19: "flow_dhw",
- 20: "day_of_week/time_of_day",
- 21: "calender_date",
- 22: "calender_year",
- 23: "room_setpoint_2nd_circuit",
- 24: "room_temperature",
- 25: "boiler_water_temperature",
- 26: "dhw_temperature",
- 27: "outside_temperature",
- 28: "return_water_temperature",
- 29: "solar_storage_temperature",
- 30: "solar_collector_temperature",
- 31: "flow_water_temperature_2nd_circuit",
- 32: "dhw_temperature_2",
- 33: "boiler_exhaust_temperature",
- 48: "dhw_upper_bound/dhw_lower_bound",
- 49: "max_ch_setpoint_upper_bound/lower_bound",
- 50: "otc_curve_upper_bound/lower_bound",
- 56: "dhw_setpoint",
- 57: "max_ch_setpoint",
- 58: "otc_curve_ratio",
- 100: "remote_override_function",
- 115: "OEM_diagnostic_code",
- 116: "burner_starts",
- 117: "ch_pump_starts",
- 118: "dhw_pump_starts",
- 119: "dhw_burner_starts",
- 120: "burner_operation_hours",
- 121: "ch_pump_operation_hours",
- 122: "dhw_pump_operation_hours",
- 123: "dhw_burner_operation_hours",
- 124: "opentherm_version_master",
- 125: "opentherm_version_slave",
- 126: "master_version",
- 127: "slave_version"
- };
- var opentherm_ids_types = {
- 0: "flag8",
- 1: "f8.8",
- 2: "flag8",
- 3: "flag8",
- 4: "u8",
- 5: "flag8",
- 6: "flag8",
- 7: "f8.8",
- 8: "f8.8",
- 9: "f8.8",
- 10: "u8",
- 11: "u8",
- 12: "u8",
- 13: "u8",
- 14: "f8.8",
- 15: "u8",
- 16: "f8.8",
- 17: "f8.8",
- 18: "f8.8",
- 19: "f8.8",
- 20: "u8",
- 21: "u8",
- 22: "u16",
- 23: "f8.8",
- 24: "f8.8",
- 25: "f8.8",
- 26: "f8.8",
- 27: "f8.8",
- 28: "f8.8",
- 29: "f8.8",
- 30: "f8.8",
- 31: "f8.8",
- 32: "f8.8",
- 33: "s16",
- 48: "s8",
- 49: "s8",
- 50: "s8",
- 56: "f8.8",
- 57: "f8.8",
- 58: "f8.8",
- 100: "flag8",
- 115: "u16",
- 116: "u16",
- 117: "u16",
- 118: "u16",
- 119: "u16",
- 120: "u16",
- 121: "u16",
- 122: "u16",
- 123: "u16",
- 124: "f8.8",
- 125: "f8.8",
- 126: "u8",
- 127: "u8"
- };
- util.inherits(OTGateway, EventEmitter);
- function padLeft(nr, n, str){
- return Array(n-String(nr).length+1).join(str||'0')+nr;
- }
- function OTGateway(ip, port) {
- this.ip = ip;
- this.port = port;
- this.interval = 60000;
- this.isConnected = false;
- this.busy = false;
- this.callback = null;
- this.dutyCycle = 0;
- this.memorySlots = 0;
- this.rooms = [];
- this.devices = {};
- this.deviceCount = 0;
- this.client = new net.Socket();
- var self = this;
- this.client.on('error', function(err){
- self.emit('error', err);
- });
- this.client.on('data', this.onData.bind(this));
- this.connect();
- }
- OTGateway.prototype.connect = function () {
- if (!this.isConnected) {
- this.client.connect(this.port, this.ip, function() {
- //console.log('Connected');
- this.isConnected = true;
- this.emit('connected');
- }.bind(this));
- } else {
- // this.send('l:\r\n');
- }
- setTimeout(this.connect.bind(this), this.interval);
- };
- OTGateway.prototype.send = function (message, callback) {
- if (!this.busy) {
- //console.log('Sending command: ' + message.substr(0,1));
- this.busy = true;
- this.client.write(message + '\r\n', 'utf-8', callback);
- }
- };
- OTGateway.prototype.onData = function (data) {
- data = data.toString('utf-8');
- data = data.split('\r\n');
- data.forEach(function (line) {
- if (line.length > 0) {
- // check for OT packets
- // console.log(line);
- opentherm_target = line.slice(0, 1); // B, T, A, R, E
- // console.log(opentherm_target);
- opentherm_type = line.slice(1, 2); //
- opentherm_id = parseInt(line.slice(3, 5), 16); //
- opentherm_payload = line.slice(-4); // last 4 chars
- if (opentherm_target == "B" || opentherm_target == "T" || opentherm_target == "A" || opentherm_target == "R" || opentherm_target == "E") {
- //if (opentherm_target == "B" || opentherm_target == "T" || opentherm_target == "A") {
- if (opentherm_type == "1" || opentherm_type == "4" || opentherm_type == "C" || opentherm_type == "9") {
- //if (opentherm_type == "1" || opentherm_type == "4") {
- if (opentherm_id in opentherm_ids) {
- // topic = opentherm_ids[opentherm_id];
- topic = opentherm_target + "_" + opentherm_ids[opentherm_id];
- // console.log(topic);
- switch (opentherm_ids_types[opentherm_id]) {
- case 'flag8':
- message = parseInt(opentherm_payload, 16).toString(2);
- for (var i = 15 - message.length; i >= 0; i--) {
- message = "0" + message;
- };
- break;
- case 'f8.8':
- message = (parseInt(opentherm_payload, 16) / 256).toFixed(2);
- break;
- case 'u16':
- message = parseInt(opentherm_payload, 16);
- break;
- case 'u8':
- message = parseInt(opentherm_payload, 8);
- break;
- case 's8':
- message = 0;
- break;
- }
- //console.log(String(previous[topic] + previous[message]));
- //console.log(String(topic+message));
- //console.log(String((topic + message) (previous[topic] + previous[message])));
- if ((message) != (previous[opentherm_id])) {
- this.emit(topic, String(message));
- // previous[topic] = topic;
- previous[opentherm_id] = message;
- }
- }
- }
- } //15:12:42.394368 AC0090000 Read-Data Remote override room setpoint: 0.00
- if ((opentherm_target == "T" && (opentherm_type == "T" || opentherm_type == "C")) || line == "NG" || line == "SE" || line == "BV" || line == "OR" || line == "NS" || line == "NF" || line == "OE") {
- this.emit('response',line);
- }
- }
- }.bind(this));
- this.busy = false;
- };
- //NOT functional atm
- OTGateway.prototype.setTemperature = function (mode, temperature, callback) {
- if (!this.isConnected) {
- callback(new Error("Not connected"));
- return;
- }
- var data;
- switch (mode) {
- case 'auto':
- data = 'TT=00';
- break;
- case 'manu':
- data = 'TT=' + temperature;
- break;
- case 'boost':
- data = 'TC=' + temperature;
- break;
- default:
- callback(new Error('Unknown mode: ' + mode));
- return false;
- }
- this.send(data, function(err) {
- if(err && callback) {
- callback(err);
- callback = null;
- }
- });
- this.once('response', function(res) {
- if(!callback) {
- return;
- }
- if(res == "NG" || res == "SE" || res == "BV" || res == "OR" || res == "NS" || res == "NF" || res == "OE") {
- var reason = "";
- switch(res) {
- case "NG":
- reason = "No Good: The command code is unknown.";
- break;
- case "SE":
- reason = "Syntax Error: The command contained an unexpected character or was incomplete.";
- break;
- case "BV":
- reason = "Bad Value: The command contained a data value that is not allowed.";
- break;
- case "OR":
- reason = "Out of Range: A number was specified outside of the allowed range.";
- break;
- case "NS":
- reason = "No Space: The alternative Data-ID could not be added because the table is full.";
- break;
- case "NF":
- reason = "Not Found: The specified alternative Data-ID could not be removed because it does not exist in the table.";
- break;
- case "OE":
- reason = "Overrun Error: The processor was busy and failed to process all received characters.";
- break;
- }
- callback(new Error('Command was rejected, Reason: ' + reason));
- } else {
- callback(null);
- }
- callback = null;
- });
- };
- module.exports = OTGateway;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement