Hi,
Windows 10 - NR 0.20.7
I have been trying to use persistent storage to allow continuation over a restart. I have set the contextStorage to 'localfilesystem'. I can see the files as described in the documentation so I assumed all was well until I lost some global context items as follows:
I defined an object to be stored in global context (global.set("xxxx")) which consists of a set of methods and a data array associated storing the status of various devices. The methods allow access to the array, greatly simplifying and standardising updates etc.
stats1 = {
getname : function() {
var addr = arguments[0];
var name = "Not Known";
for (var devnum in this.devices) {
if (addr == this.devices[devnum].address) {
name = this.devices[devnum].name;
}
}
return name;
},
setname : function() {
var addr = arguments[0];
var name = arguments[1];
for (var devnum in this.devices) {
if (addr == this.devices[devnum].address) {
this.devices[devnum].name = name;
return devnum;
}
}
return -1;
},
getaddr : function() {
var name = arguments[0];
var addr = "Not Known";
for (var devnum in this.devices) {
if (name == this.devices[devnum].name) {
addr = this.devices[devnum].address;
}
}
return addr;
},
setaddr : function() {
var name = arguments[0];
var addr = "Not Known";
for (var devnum in this.devices) {
if (name == this.devices[devnum].name) {
this.devices[devnum].address = addr;
}
}
return addr;
},
setvisible : function() {
var addr = arguments[0];
var what = arguments[1];
for (var devnum in this.devices) {
if (addr == this.devices[devnum].address) {
this.devices[devnum].visible = what;
}
}
return addr;
},
add : function() {
var addr = arguments[0];
var dev = {};
var date = new Date();
dev.address = addr;
dev.name = "Device" + (this.devices.length);
dev.value = "Not Set";
dev.date = this.formatDate(date);
dev.time = this.formatTime(date);
dev.visible = false;
this.devices.push(dev); // add to array
return this.devices.length;
},
exists : function() {
var addr = arguments[0];
for (var devnum in this.devices) {
if (addr.toUpperCase() === this.devices[devnum].address.toUpperCase()) {
return devnum;
}
}
return -1;
},
updateValue : function() {
var date = new Date();
var addr = arguments[0];
var what = arguments[1];
var exists = this.exists(addr);
if (exists !== -1) {
if (what.toLowerCase() !== "silent") this.devices[exists].value = what;
this.devices[exists].date = this.formatDate(date);
this.devices[exists].time = this.formatTime(date);
}
return exists;
},
formatDate : function(date) {
var d = date,
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [day, month, year].join('/');
},
formatTime : function (date) {
hours = '' + date.getHours();
if (hours.length < 2) hours = "0" + hours;
minutes = '' + date.getMinutes();
if (minutes.length < 2) minutes = "0" + minutes;
seconds = '' + date.getSeconds();
if (seconds.length < 2) seconds = "0" + seconds;
return [hours, minutes, seconds].join(':');
},
devices : []
};
Anything stored within the 'devices' array is persisted, the methods are not!
Am I breaking some rules I don't know about or should this work?
Regards