Skip to content

Commit

Permalink
### 2.1.1 (2016-05-20)
Browse files Browse the repository at this point in the history
* (bluefox) try to fix "Duplicate name" error
* (bluefox) modify readFile/wrieFile commands
* (gh-god) fix stop of script and unsubscribe
  • Loading branch information
GermanBluefox committed May 21, 2016
1 parent cf3ae59 commit ab02d77
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 51 deletions.
31 changes: 20 additions & 11 deletions README.md
Expand Up @@ -661,24 +661,33 @@ $('channel[role=switch][state.id=*.STATE](rooms=Wohnzimmer)').each(function (id,
readFile (fileName, function (error, bytes) {})

The result will be given in callback.
File will be stored on the disk
Read file from DB from folder "javascript".

### writeFile
writeFile (fileName, bytes, function (error) {})

The optional error code will be given in callback.
fileName is the name of file on local disk.
fileName is the name of file in DB. All files are stored in folder "javascript". if you want to write to other folders, e.g. to "/vis.0/" use setFile for that.

### getFile
getFile (fileName, function (error, bytes) {})
The file that looks like '/subfolder/file.txt' will be stored under "/javascript/subfolder/file.txt" and can be accessed over web server with ```http://ip:8082/javascript/subfolder/file.txt```

The result will be given in callback.
File will be stored in DB and can be accessed from any host under name javascript.X.fileName

### setFile
setFile (fileName, bytes, function (error) {})
```
// store screenshot in DB
var fs = require('fs');
var data = fs.readFileSync('/tmp/screenshot.png');
writeFile('/screenshots/1.png', data, function (error) {
console.log('file written');
});
```

The optional error code will be given in callback. Read file from DB. Works on any host.
```
// store file in '/vis.0' in DB
var fs = require('fs');
var data = fs.readFileSync('/tmp/screenshot.png');
writeFile('vis.0', '/screenshots/1.png', data, function (error) {
console.log('file written');
});
```

### getHistory
getHistory (instance, options, function (error, result, options, instance) {});
Expand Down Expand Up @@ -744,7 +753,7 @@ Scripts can be activated and deactivated by controlling of this state with ack=f
## Changelog
### 2.1.1 (2016-05-20)
* (bluefox) try to fix "Duplicate name" error
* (bluefox) add getFile/setFile commands
* (bluefox) modify readFile/wrieFile commands
* (gh-god) fix stop of script and unsubscribe

### 2.1.0 (2016-05-13)
Expand Down
44 changes: 16 additions & 28 deletions javascript.js
Expand Up @@ -1762,11 +1762,23 @@
return adapter.formatDate(date, format);
},

writeFile: function (fileName, data, callback) {
adapter.writeFile(null, fileName, data, callback);
writeFile: function (_adapter, fileName, data, callback) {
if (typeof data === 'function' || !data) {
callback = data;
data = fileName;
fileName = _adapter;
_adapter = null;
}

adapter.writeFile(_adapter, fileName, data, callback);
},
readFile: function (fileName, callback) {
adapter.readFile(null, fileName, callback);
readFile: function (_adapter, fileName, callback) {
if (typeof fileName === 'function') {
callback = fileName;
fileName = _adapter;
_adapter = null;
}
adapter.readFile(_adapter, fileName, callback);
},
getHistory: function (instance, options, callback) {
if (typeof instance === 'object') {
Expand Down Expand Up @@ -1816,30 +1828,6 @@
callback = null;
});
},
getFile: function (fileName, callback) {
var parts = fileName.replace(/\\/g, '/').split('/');
if (!parts[0]) parts.splice(0, 1);
if (!parts[0] || !parts[1]) {
var err = 'Invalid file name "' + fileName + '". It must be like: /vis.0/main/image.png';
adapter.log.error(err);
if (callback) callback(err);
return;
}
var a = parts.shift();
adapter.readFile(a, parts.join('/'), callback);
},
setFile: function (fileName, data, callback) {
var parts = fileName.replace(/\\/g, '/').split('/');
if (!parts[0]) parts.splice(0, 1);
if (!parts[0] || !parts[1]) {
var err = 'Invalid file name "' + fileName + '". It must be like: /vis.0/main/image.png';
adapter.log.error(err);
if (callback) callback(err);
return;
}
var a = parts.shift();
adapter.writeFile(a, parts.join('/'), callback);
},
toInt: function (val) {
if (val === true || val === 'true') val = 1;
if (val === false || val === 'false') val = 0;
Expand Down
35 changes: 29 additions & 6 deletions test/lib/setup.js
Expand Up @@ -356,17 +356,18 @@ function copyAdapterToController() {
console.log('Adapter copied.');
}

function clearControllerLog() {
var dirPath = rootDir + 'tmp/log';
function deleteAllFilesInDir(dirPath, isCreate) {
var files;
try {
if (fs.existsSync(dirPath)) {
console.log('Clear controller log...');
console.log('Clear folder "' + dirPath + '"...');
files = fs.readdirSync(dirPath);
} else {
} else if (isCreate) {
console.log('Create controller log directory...');
files = [];
fs.mkdirSync(dirPath);
} else {
files = [];
}
} catch(e) {
console.error('Cannot read "' + dirPath + '"');
Expand All @@ -378,9 +379,31 @@ function clearControllerLog() {
var filePath = dirPath + '/' + files[i];
fs.unlinkSync(filePath);
}
console.log('Controller log cleared');
console.log('Controller folder "' + dirPath + '" cleared');
} catch (err) {
console.error('cannot clear log: ' + err);
console.error('Cannot clear folder "' + dirPath + '": ' + err);
}
}
}

function clearControllerLog() {
deleteAllFilesInDir(rootDir + 'tmp/log');

// clear store files
var dirPath = rootDir + 'tmp/' + appName + '-data/files';
var files;
try {
if (fs.existsSync(dirPath)) {
files = fs.readdirSync(dirPath);
}
} catch(e) {
console.error('Cannot read "' + dirPath + '"');
return;
}
if (files.length > 0) {
for (var i = 0; i < files.length; i++) {
if (files[i] === 'admin.admin' || files[i] === 'javascript.admin') continue;
deleteAllFilesInDir(dirPath + '/' + files[i]);
}
}
}
Expand Down
71 changes: 65 additions & 6 deletions test/testFunctions.js
Expand Up @@ -112,7 +112,7 @@ describe('Test JS', function() {
this.timeout(5000);
checkConnectionOfAdapter(done);
});

/*
it('Test JS: check creation of state', function (done) {
this.timeout(2000);
// add script
Expand Down Expand Up @@ -673,15 +673,16 @@ describe('Test JS', function() {
objects.setObject(script._id, script, function (err) {
expect(err).to.be.not.ok;
});
});
});*/

it('Test JS: test write file', function (done) {
it('Test JS: test write file to "javascript"', function (done) {
this.timeout(5000);
// add script
var script = {
"common": {
"name": "test ON any",
"engineType": "Javascript/js",
"source": "createState('testScheduleResponse2', false, function () {writeFile('/javascript.admin/test.txt', 'test', function () {setState('testScheduleResponse2', true, true);});});",
"source": "createState('testScheduleResponse2', false, function () {writeFile('/test.txt', 'test', function () {setState('testScheduleResponse2', true, true);});});",
"enabled": true,
"engine": "system.adapter.javascript.0"
},
Expand All @@ -701,13 +702,15 @@ describe('Test JS', function() {
expect(err).to.be.not.ok;
});
});
it('Test JS: test read file', function (done) {

it('Test JS: test read file from "javascript"', function (done) {
this.timeout(5000);
// add script
var script = {
"common": {
"name": "test ON any",
"engineType": "Javascript/js",
"source": "readFile('/javascript.admin/test.txt', function (err, data) {setState('testScheduleResponse2', data, true);});",
"source": "readFile('/test.txt', function (err, data) {setState('testScheduleResponse2', data, true);});",
"enabled": true,
"engine": "system.adapter.javascript.0"
},
Expand All @@ -728,6 +731,62 @@ describe('Test JS', function() {
});
});

it('Test JS: test write file to "vis.0"', function (done) {
this.timeout(5000);
// add script
var script = {
"common": {
"name": "test ON any",
"engineType": "Javascript/js",
"source": "createState('testScheduleResponse2', false, function () {writeFile('vis.0', '/test1.txt', 'test', function () {setState('testScheduleResponse2', true, true);});});",
"enabled": true,
"engine": "system.adapter.javascript.0"
},
"type": "script",
"_id": "script.js.test_write1",
"native": {}
};

onStateChanged = function (id, state) {
if (id === 'javascript.0.testScheduleResponse2' && state.val === true) {
onStateChanged = null;
done();
}
};

objects.setObject(script._id, script, function (err) {
expect(err).to.be.not.ok;
});
});

it('Test JS: test read file from "vis.0"', function (done) {
this.timeout(5000);
// add script
var script = {
"common": {
"name": "test ON any",
"engineType": "Javascript/js",
"source": "readFile('vis.0', '/test1.txt', function (err, data) {setState('testScheduleResponse2', data, true);});",
"enabled": true,
"engine": "system.adapter.javascript.0"
},
"type": "script",
"_id": "script.js.test_read1",
"native": {}
};

onStateChanged = function (id, state) {
if (id === 'javascript.0.testScheduleResponse2' && state.val === 'test') {
onStateChanged = null;
done();
}
};

objects.setObject(script._id, script, function (err) {
expect(err).to.be.not.ok;
});
});

after('Test JS: Stop js-controller', function (done) {
this.timeout(6000);

Expand Down

0 comments on commit ab02d77

Please sign in to comment.