Creating an indexed array

Hello all,

I’m making a flowrate totalization estimation program, but I’m having trouble figuring out how arrays work in octave. I know there is not an array data type for JavaScript, but most of my programming experience comes from C and C++. My goal is to make an array of twelve numbers where every minute a new variable gets stored replacing the oldest value. This way I can take an average of the last twelve minutes of data at any time.

This is how I was trying to implement my program.

I also tried to create an array to store hour, minute, and second data here:


image

I think this is possible after looking at the way time is stored under util on the FX30

If someone could help me out on how to program this properly then that would be much appreciated!

Thanks,
Wilson Teachey

Hi Wilson,

All of this is possible

Example of one way to read a resource array. and manipulate values

var device_datetime_obj = JSON.parse(JSON.stringify(Resource.readValue(“/util/time/value”)));
var device_datetime_wday = device_datetime_obj.wday;
//console.log("device_datetime_wday: " + device_datetime_wday);
var device_time_offset = Math.abs(device_datetime_obj.offset);
var device_datetime_local_hr = device_datetime_obj.hr + device_time_offset;
var device_datetime = “” + device_datetime_local_hr + device_datetime_obj.min;

Hey David,

Thank you for the response. I have been able to read array values, but I’m also having trouble storing/creating them. I have been able to create an array using your example and send it in the stream, but have been unable to store it in memory. I try returning it in a return statement, but the value under Resources does not change. Could you give an example of returning and and reading an array from the Resources tab?

// Test Code for Array
var buffer = new ArrayBuffer(4);
buffer[1] = 5;
var index = 2;
buffer[index] = 5;

var payload = ;
var obj = {};
obj[“test”] = buffer;
payload.push(obj);

return{
“cl://”:[payload],
“/virtual/Test_Time/value”:[buffer]
}

Stream event:
image

Variable Test_Time: (Remains unchanged after edge action is run)

I also tried changing the variable Test_Time to a string. I think I lack the syntax knoledge to return the array so that it can be read in and used again later by another edge action. Any more help you can provide will be much appreciated!

Thanks,
Wilson Teachey

Hello Wilson,

Virtual Resources are what you need here to store strings, arrays (objects), numbers, boolean, etc data types

Here is an example:
function(event) {
var temperature = Datahub.read(“/redSensor/imu/temp/value”,0).value;
var pressure = Datahub.read(“/redSensor/pressure/value”,0).value;
var lat = 0;
var lon = 0;
var pos = Datahub.read(“/location/coordinates/value”,0);
if (pos) {
lat = pos.lat;
lon = pos.lon
}
var light = Datahub.read(“/redSensor/light/value”,0).value;
var bars = Datahub.read(“/util/cellular/signal/value”,0).value.bars;
var technology = Datahub.read(“/util/cellular/signal/value”,0).value.rat;
// Create the contents of an array
var report = {
“temperature”: temperature,
“pressure”: pressure,
“location”: { “lat”: lat, “lon”: lon },
“light”: light,
“signal_bars”: bars,
“cell_technology”: technology
}

return {

// store the contents of the array in a virtual resource If the virtual resource does not already exist in the /
// resource tree, the virtual resource will be created.
“vr://report”: [report]
}
}

You can learn more about virtual resources here:

Here is a bit more complex definition of a lighting schedule to turn off and on lights from a schedule. It is stored in a virtual resource.

“vr_lighting_schedule”: {
“dt”: 4,
“v”: {
“Lighting_Schedule_Table”: [
{
“day”: “Sunday”,
“end_time”: “20:00”,
“id”: “0”,
“start_time”: “16:00”
},
{
“day”: “Monday”,
“end_time”: “19:00”,
“id”: 1,
“start_time”: “16:00”
},
{
“day”: “Tuesday”,
“end_time”: “19:00”,
“id”: 2,
“start_time”: “16:00”
},
{
“day”: “Wednesday”,
“end_time”: “19:00”,
“id”: “3”,
“start_time”: “16:00”
},
{
“day”: “Thursday”,
“end_time”: “20:00”,
“id”: “4”,
“start_time”: “16:00”
},
{
“day”: “Friday”,
“end_time”: “19:00”,
“id”: “5”,
“start_time”: “16:00”
},
{
“day”: “Saturday”,
“end_time”: “19:00”,
“id”: “6”,
“start_time”: “16:00”
}
]
}
},

If you were to create this via the Octave UI rather than in an edge action as in the previous example, it would look like this:

I hope this helps.

Hey David,

Thanks for all the sample code, I think I’m very close to understanding how this works. My only issue now is that I don’t think that I’m reading the values in the array correctly. I can get the array inside a variable but I am not able to get one component such as “hr” into its own variable. Below I will attach pictures of the two arrays I made, the edge action I made to use it, and the stream event shown in the edge action.
Arrays:

Edge Actions:

Stream events:



I need to be able to access the array elements themselves but I am not able to get them into their own variable and put them in the stream.

Thanks again,
Wilson Teachey

Update: I figured out what was wrong with my arrays. Now I am trying to overwrite the array that I made. Below is what my Resources screen looks like currently:


Here is the Edge Action that I wrote that is supposed to change the FR_Time value and overwrite it:

I thought that by returning array here that it would overwrite the value in memory but that is not the case. I published array in the Octave stream and the new time value is being saved in array. Picture of that shown below:

If anyone knows the proper syntax, I would greatly appreciate it! I’ve read the Virtual Resources documentation that David sent, and I’m not sure where I’m going wrong here.

Hello Wilson,

I think your syntax is a bit off, I will set this up on my device and respond.

Hey David,

I think that we figured it out this morning. I changed the return statement and the new array values are now being saved over the old ones. Here are the changes I made:

Resources:


I’m not sure why changing the code to “vr://resource”: [array] from “/virtual/resource/value”:[array] made a difference, but it worked. I thought the two expressions were interchangeable after reading the Virtual Resources guide. Are there cases where you should use one rather than the other?

Thanks,
Will Teachey

Hello everyone,

I think I see the problem now. In my return statement I left off the “dh:/” before the /virtual/resource/value. I haven’t tested it, but I believe this is why it wasn’t working. The other statement works fine.

Thanks everyone,
Wilson Teachey