更新 main.js

This commit is contained in:
钟进 2019-01-16 05:08:53 +00:00
parent 795e0bb309
commit 796d71524e

172
main.js
View File

@ -89,51 +89,6 @@ adapter.on('stateChange', (id, state) => {
}
});
function filterSerialPorts(path) {
fs = fs || require('fs');
// get only serial port names
if (!(/(tty(S|ACM|USB|AMA|MFD)|rfcomm)/).test(path)) return false;
return fs
.statSync(path)
.isCharacterDevice();
}
function listSerial(ports) {
ports = ports || [];
const path = require('path');
fs = fs || require('fs');
// Filter out the devices that aren't serial ports
let devDirName = '/dev';
let result;
try {
result = fs
.readdirSync(devDirName)
.map(function (file) {
return path.join(devDirName, file);
})
.filter(filterSerialPorts)
.map(function (port) {
let found = false;
for (let v = 0; v < ports.length; v++) {
if (ports[v].comName === port) {
found = true;
break;
}
}
if (!found) ports.push({comName: port});
return {comName: port};
});
} catch (e) {
if (require('os').platform() !== 'win32') {
adapter.log.error('Cannot read "' + devDirName + '": ' + e);
}
result = [];
}
return result;
}
function addToEnum(enumName, id, callback) {
adapter.getForeignObject(enumName, function (err, obj) {
@ -209,35 +164,7 @@ function syncEnums(enumGroup, id, newEnumName, callback) {
}
const type_items_len = {
'uint8be': 1,
'uint8le': 1,
'int8be': 1,
'int8le': 1,
'uint16be': 1,
'uint16le': 1,
'int16be': 1,
'int16le': 1,
'int16be1': 1,
'int16le1': 1,
'uint32be': 2,
'uint32le': 2,
'uint32sw': 2,
'uint32sb': 2,
'int32be': 2,
'int32le': 2,
'int32sw': 2,
'int32sb': 2,
'uint64be': 4,
'uint64le': 4,
'int64be': 4,
'int64le': 4,
'floatbe': 2,
'floatle': 2,
'floatsw': 2,
'floatsb': 2,
'doublebe': 4,
'doublele': 4,
'string': 0
'uint8be': 1
};
const _rmap = {
@ -343,9 +270,6 @@ function prepareConfig(config) {
}
let deviceIds = [];
checkDeviceIds(options, config.disInputs, deviceIds);
checkDeviceIds(options, config.coils, deviceIds);
checkDeviceIds(options, config.inputRegs, deviceIds);
checkDeviceIds(options, config.holdingRegs, deviceIds);
deviceIds.sort();
@ -379,36 +303,6 @@ function prepareConfig(config) {
options.devices[deviceId] = {};
let device = options.devices[deviceId];
if (options.config.slave) {
device.disInputs = {
fullIds: [],
changed: true,
addressHigh: 0,
addressLow: 0,
values: [],
mapping: {},
offset: parseInt(params.disInputsOffset, 10)
};
device.coils = {
fullIds: [],
changed: true,
addressHigh: 0,
addressLow: 0,
values: [],
mapping: {},
offset: parseInt(params.coilsOffset, 10)
};
device.inputRegs = {
fullIds: [],
changed: true,
addressHigh: 0,
addressLow: 0,
values: [],
mapping: {},
offset: parseInt(params.inputRegsOffset, 10)
};
device.holdingRegs = {
fullIds: [],
changed: true,
@ -419,34 +313,6 @@ function prepareConfig(config) {
offset: parseInt(params.holdingRegsOffset, 10)
};
} else {
device.disInputs = {
deviceId: deviceId,
addressLow: 0,
length: 0,
config: [],
blocks: [],
offset: parseInt(params.disInputsOffset, 10)
};
device.coils = {
deviceId: deviceId,
addressLow: 0,
length: 0,
config: [],
blocks: [],
cyclicWrite: [], // only holdingRegs and coils
offset: parseInt(params.coilsOffset, 10)
};
device.inputRegs = {
deviceId: deviceId,
addressLow: 0,
length: 0,
config: [],
blocks: [],
offset: parseInt(params.inputRegsOffset, 10)
};
device.holdingRegs = {
deviceId: deviceId,
addressLow: 0,
@ -498,10 +364,6 @@ function checkObjects(options, regType, regName, regFullName, tasks, newObjects)
deviceId: regs[i].deviceId
}
};
if (regType === 'coils') {
objects[id].native.poll = regs[i].poll;
objects[id].native.wp = regs[i].wp;
} else
if (regType === 'inputRegs' || regType === 'holdingRegs') {
objects[id].common.unit = regs[i].unit || '';
@ -542,15 +404,6 @@ function checkObjects(options, regType, regName, regFullName, tasks, newObjects)
}
}
// localOptions = {
// multiDeviceId
// showAliases
// doNotRoundAddressToWord
// directAddresses
// isSlave
// maxBlock
// maxBoolBlock
// };
function iterateAddresses(isBools, deviceId, result, regName, regType, localOptions) {
const config = result.config;
if (config && config.length) {
@ -682,11 +535,6 @@ function parseConfig(callback) {
let oldObjects = list;
let newObjects = [];
adapter.config.disInputs.sort(sortByAddress);
adapter.config.coils.sort(sortByAddress);
adapter.config.inputRegs.sort(sortByAddress);
adapter.config.holdingRegs.sort(sortByAddress);
let tasks = [];
for (let _deviceId in options.devices) {
@ -694,9 +542,6 @@ function parseConfig(callback) {
let device = options.devices[_deviceId];
let deviceId = parseInt(_deviceId, 10);
device.disInputs.config = adapter.config.disInputs. filter(e => e.deviceId === deviceId);
device.coils.config = adapter.config.coils. filter(e => e.poll && e.deviceId === deviceId);
device.inputRegs.config = adapter.config.inputRegs. filter(e => e.deviceId === deviceId);
device.holdingRegs.config = adapter.config.holdingRegs.filter(e => e.poll && e.deviceId === deviceId);
// ----------- remember poll values --------------------------
@ -722,21 +567,12 @@ function parseConfig(callback) {
}
// Discrete inputs
iterateAddresses(true, deviceId, device.disInputs, 'discreteInputs', 'disInputs', localOptions);
iterateAddresses(true, deviceId, device.coils, 'coils', 'coils', localOptions);
iterateAddresses(false, deviceId, device.inputRegs, 'inputRegisters', 'inputRegs', localOptions);
iterateAddresses(false, deviceId, device.holdingRegs, 'holdingRegisters', 'holdingRegs', localOptions);
// ------------- create states and objects ----------------------------
checkObjects(adapter.config, 'disInputs', 'discreteInputs', 'Discrete inputs', tasks, newObjects);
checkObjects(adapter.config, 'coils', 'coils', 'Coils', tasks, newObjects);
checkObjects(adapter.config, 'inputRegs', 'inputRegisters', 'Input registers', tasks, newObjects);
checkObjects(adapter.config, 'holdingRegs', 'holdingRegisters', 'Holding registers', tasks, newObjects);
if (options.config.slave) {
device.disInputs.fullIds = adapter.config.disInputs .filter(e => e.deviceId === deviceId).map(e => e.fullId);
device.coils.fullIds = adapter.config.coils .filter(e => e.deviceId === deviceId).map(e => e.fullId);
device.inputRegs.fullIds = adapter.config.inputRegs .filter(e => e.deviceId === deviceId).map(e => e.fullId);
device.holdingRegs.fullIds = adapter.config.holdingRegs.filter(e => e.deviceId === deviceId).map(e => e.fullId);
}
@ -814,8 +650,4 @@ function main() {
});
}
function sortByAddress(a, b) {
let ad = parseFloat(a.address);
let bd = parseFloat(b.address);
return ((ad < bd) ? -1 : ((ad > bd) ? 1 : 0));
}