Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
General CML/Programming Question
#1
I have an array of fields, like Field1_bananas, Field2_bananas, etc...

I find that I am often doing something like:

Code:
If (Number = 1)
    Define this, this and this1;
    So that we're r/w to the correct field1;
ElseIf (Number = 2)
    Define this, this and this2;
    So that we're r/w to the correct field2;
......

Is there a faster way to do this? Like a way to insert the "Number" on the fly? Like "m_ID + Number" or something that I don't know?
Reply
#2
Not sure I completely understand what you are up to, but CML has support for collections (arrays) and CASE statements.

Also if you create your fields sequentially, you can access them mathematically (i.e. keep field1's id in a member, and then access field 5 by using field1_id + 4).

--Bob
Reply
#3
The ids are assigned in the order that they are put into the list when they are registered. So you can do as Bob indicates above, and for a specific chunk of fields just store the base field id and offset it. Or just store the field ids for chunks of contiguous related fields in an array (if known number) or vector (if runtime variable number) and use the number to index that. The lattter is probably safer since it makes fewer assumptions.

If you need to keep up info about the things that the field represents, then create a small class that holds both the info and the field. Then you can search the list for the one that as this name or that type or whatever, and then pull the field id out.
Dean Roddey
Explorans limites defectum
Reply
#4
Thinking it through, I didn't ask the question very well. The problem is specific data for each thermostat that needs to be sent with each command. It's stored in something like "m_IdentifierX". If I could just quickly grab it then it would save a lot of space and time. For instance:

Code:
If (number = 1)
    Identifier := m_Identifier1;
    humidifierBool := m_humidifier1;
....
EndIf;

If (humidifierBool = False)
    ThrowError();
Else
    JSONPost(url, body, Identifier, setPoint)
EndIf;

where it would be easier to do something like:
Code:
If (m_humidifier [number] = True)
    JSONPost(url, body, m_Identifier [number], setPoint)
Else
    ThrowError();
EndIf;
Reply
#5
For that, I would create a helper class. Store the info about each thermo in an instance of that class (along with any field ids related to that thermo.) When you need to to something with a particular thermo, just provide a few helpers like this:

Code:
Method FindByFldId([In] Card4 ToFind, [Out] Card4 Index) Returns Boolean;
Method FindByName([In] String ToFind, [Out] Card4 Index) Returns Boolean;

And so forth. If you find the one you are looking for, you have the index of it, so you can get any info out you need. You could just return the actual object, but that requires making a copy of it. If it's small, that's probably most efficient. Otherwise, just return the index. When you use an index to reference a collection you are directly accessing the object, so you don't end up copying it.

In the example above, if you don't find it in the list, then you throw, i.e. if the find method returns false, throw.

Note that, if you know you can only have a certain number of them, and it's not a large number, just create an array of them. Have the default constructor (which is what is used to initialize each member of the array) set some flag indicating it's not used. Go back and set the ones that are used. Then you can use the number directly as an index into the list with no lookup. If the one you find is marked as active, you are ok, else throw.

If you could have a lot of them, but in reality will only have a smaller number, then the vector with a lookup is probably better, since you don't create a large number of unused objects.
Dean Roddey
Explorans limites defectum
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  SSL Certificate Question gReatAutomation 1 950 10-26-2020, 07:48 AM
Last Post: gReatAutomation
  V2 Question - OpenWeatherMap Using their New JSON Data Feed kblagron 7 2,134 05-07-2020, 01:40 PM
Last Post: Dean Roddey
  Dynamic Overlay Question bryanb 4 1,369 04-17-2020, 03:48 AM
Last Post: bryanb
  Triggered Event on Timer Question znelbok 7 3,413 12-09-2019, 02:10 PM
Last Post: znelbok
  Question on CQC Driver Pack Install kblagron 1 1,305 11-26-2019, 06:38 PM
Last Post: Dean Roddey
  SONOS Driver Question gReatAutomation 13 5,678 08-23-2019, 12:37 PM
Last Post: Dean Roddey
  Insteon V2 Driver Question kblagron 1 1,276 08-01-2019, 10:51 AM
Last Post: kblagron
  Question on Parsing a String in a CML Macro kblagron 2 1,637 06-18-2019, 06:21 PM
Last Post: Dean Roddey
  ELK M1 Client Interface question lleo 1 1,334 06-12-2019, 08:15 AM
Last Post: Dean Roddey
  Question on Socket Error kblagron 4 2,262 04-07-2019, 09:19 PM
Last Post: kblagron

Forum Jump:


Users browsing this thread: 1 Guest(s)