We have moved at community.getvera.com

Author Topic: Why Create Variables? Example posted  (Read 10547 times)

Offline guessed

  • Community Beta
  • Master Member
  • ******
  • Posts: 5301
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: Virtual Switches or Create Variables?
« Reply #15 on: May 24, 2011, 06:31:13 pm »
Quote
@Guessed indicated to use a unique service ID so that it can't be changed by anything but user code...
Explain if you agree or disagree.....or did I misunderstand him?

IMHO, adding user-defined services/variables to preexisting Luup devices is a hack that might work - but are we really sure that adding user-defined services/variables to standard (upnp, micasaverde namespace) Luup devices doesn't break Vera clients (SQ Remote, iVera, ...)?

Do you want Vera clients to display your user-defined services/variables? If your answer is 'no', user-defined services/variables might be the way to go. If your answer is 'yes', you should use virtual Luup devices.
To be clear, my original note said specifically NOT to add them to the existing Namespace, and to create a new namespace for these to go into.  UPnP "things" are permitted to support and expose multiple namespaces, not all of which are standard.  They have specific naming conventions, outlined in the spec, for the non-standard namespaces which should be conformed to but there's nothing stopping people from using these.  I also recommended that Tim avoid using duplicate Names, within these newly defined Namespaces, in order to avoid poorly coded CP's.

The downside, as you point out, is there's no way to "remove" these Variables once they've been created.  I believe this has already been bugged, but it a small price to pay compared to an explosion of Dashboard Devices, and resulting scale problems for Vera, but both options are valid techniques.

The other risk run with a Custom namespace, is that the Vera Advanced UI's may not now and/or always handle escaping/encoding correctly.  If this happens, and you put "interesting" data into it, then you run the risk of not being able to navigate to the Advanced tab.  Of course, this would be a poorly coded UI, and a bug in MiOS, as we already know 8)

Tim, you'll want to read that part of the spec as "Tim" isn't a valid/conforming namespace, even for a custom one.

Offline TimAlls

  • Hero Member
  • *****
  • Posts: 722
  • Karma: +0/-0
Re: Why Create Variables? Example posted
« Reply #16 on: May 24, 2011, 06:56:58 pm »
@Guessed,
Thanks for the clarification....can you give me a link that will educate me as to "Namespace"  and the proper use. As I mentioned, I am way out of my expertise on this one.
I thought is was a creative way to solve some problems though!
If the downsides to this are too great one could simply use this to create working code and then switch to general variables.
The pro's are enormous....not just debugging but also letting users change variables without rewriting their code.....that is a big plus IMHO!
Regards
Tim

@Guessed, should I remove this post until it is an approved method by our more advanced users? I don't want to Brick anyones machine!
Also, your reply make me think you favor this method over Virtual Devices....Am I understanding you correctly?
Quote
Tim, you'll want to read that part of the spec as "Tim" isn't a valid/conforming namespace, even for a custom one.
You all must be getting quite a laugh at my attempt to program! Ha
« Last Edit: May 24, 2011, 07:23:22 pm by TimAlls »

Offline guessed

  • Community Beta
  • Master Member
  • ******
  • Posts: 5301
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: Why Create Variables? Example posted
« Reply #17 on: May 24, 2011, 07:21:37 pm »
@Guessed,
Thanks for the clarification....can you give me a link that will educate me as to "Namespace"  and the proper use. As I mentioned, I am way out of my expertise on this one.
I thought is was a creative way to solve some problems though!
If the downsides to this are too great one could simply use this to create working code and then switch to general variables.
The pro's are enormous....not just debugging but also letting users change variables without rewriting their code.....that is a big plus IMHO!
Regards
Tim

@Guessed, should I remove this post until it is an approved method by our more advanced users? I don't want to Brick anyones machine!
Quote
Tim, you'll want to read that part of the spec as "Tim" isn't a valid/conforming namespace, even for a custom one.

Quote
You all must be getting quite a laugh at my attempt to program! Ha
Not at all, I strongly encourage it.  Most of us learn by making mistakes, and none of this stuff is going to sink the Bismarck ;)

On http://UPnP.org, there's a document ZIp download that you can get the specs from.  Specifically:

    UPnP-arch-DeviceArchitecture-v1 ... pdf

In here, it shows the definition for the <serviceId> block, and the naming conventions that must be used, quoted here for reference:
Quote
For non-standard services specified by UPnP vendors, MUST begin with “urn:”,
followed by a Vendor Domain Name, followed by “:serviceId:”, followed by a
service ID suffix, i.e., “urn:domain-name:serviceId:serviceID”. If this instance
of the specified service type (i.e. the <serviceType> element above)
corresponds to one of the services defined by the specified device type (i.e.
the <deviceType> element above), then the value of the service ID suffix
MUST be the service ID defined by the device type for this instance of the
service. Period characters in the Vendor Domain Name MUST be replaced with
hyphens in accordance with RFC 2141.

In yourcase, you end up with something like urn:allseas-com:serviceId:<blah>

Where <blah> is of your choice, largely, but the string overall must be <= 64 bytes, and the Service names are expected to be initial-capital for each word used in them

« Last Edit: May 24, 2011, 08:39:37 pm by guessed »

Offline TimAlls

  • Hero Member
  • *****
  • Posts: 722
  • Karma: +0/-0
Re: Why Create Variables? Example posted
« Reply #18 on: May 24, 2011, 07:26:30 pm »
Thanks Guessed,
So much to learn and so little time!
I will update this thread as quickly as I can.
Regards
Tim

Offline oTi@

  • Community Beta
  • Master Member
  • ******
  • Posts: 4041
  • Karma: +32/-6
  • UI what ?!
Re: Virtual Switches or Create Variables?
« Reply #19 on: May 24, 2011, 11:36:15 pm »
Hours*100 + minutes = total minutes? The math is faulty and so the results are unpredictable.
Nowhere does the conversion imply, or is used as 'total minutes' though. It's a conversion to a single number for easy comparison. The left-shift of the hours by 2 positions preserves an easy readable format of the variables in the log file, for debugging (e.g.: 22:30 -> 2230, 05:30 -> 530)?
Dezwaved at the moment...

Offline TimAlls

  • Hero Member
  • *****
  • Posts: 722
  • Karma: +0/-0
Re: Why Create Variables? Example posted
« Reply #20 on: May 25, 2011, 01:11:49 am »
Hours*100 + minutes = total minutes? The math is faulty and so the results are unpredictable.
Nowhere does the conversion imply, or is used as 'total minutes' though. It's a conversion to a single number for easy comparison. The left-shift of the hours by 2 positions preserves an easy readable format of the variables in the log file, for debugging (e.g.: 22:30 -> 2230, 05:30 -> 530)?

Makes sense now that you explain.....when it counted to 60 and jumped to 100 it didn't make sense to me.
Anyhow....
For visual learners like me the method I posted works well....maybe some of you can try it out and see if you think it is a step in the right direction.
Tim

Offline Henk

  • Hero Member
  • *****
  • Posts: 820
  • Karma: +3/-0
Re: Why Create Variables? Example posted
« Reply #21 on: May 25, 2011, 04:38:42 pm »
@Tim

Maybe its a suggestion to simply add screenshot and/or code snippets and examples in this thread?
So we can all learn, give feedback and suggestions and contribute woth our own examples eventually?
« Last Edit: May 25, 2011, 05:06:23 pm by Henk »
| Vera2 @ UI4 1.1.1350 / 3.20 | Vera Lite @ UI5 | Vera 3 @ UI5 | 2x Merten  504519 | 1x Duewi  064374 | 1x Everspring SM103 doorbell mod |1 Y-cam IP cam | various LUUP plugins |

Offline TimAlls

  • Hero Member
  • *****
  • Posts: 722
  • Karma: +0/-0
Re: Why Create Variables? Example posted
« Reply #22 on: May 25, 2011, 05:58:44 pm »
Good Idea!

I hope I have not stepped on any toes here.....Everyday I learn a little more. I am hoping that my new program can be an educational tool for Newbies who are afraid to write code. I have my first proposed code installed on my Report Generator and I need feedback as to the technique and quality of the "lesson".
If the method is accepted then I would ask authors of tested code to send me their examples and I will format them, put them in the program, and acknowledge them as the author of the code so that users can ask them question directly.
If everyone pitches in we can capture a huge amount of code. This is an attempt to capture basic and intermediate code, long advanced ones are beyond the scope of the program.

Here is the code from the first example...this was adapted from the posted Wiki....I am unaware of the author and would ask the author to help me and NOT take offense to my obvious lack of experience!

Code:

local startTime = luup.variable_get("urn:user-com:serviceId:myvar","Ex1_TimeA", ##)
local endTime  = luup.variable_get("urn:user-com:serviceId:myvar","Ex1_TimeB", ##)
 
local hour = tonumber(startTime:sub(startTime:find("%d+") ) )
local minute = tonumber(startTime:sub(-2))
 
startTime = hour * 60 + minute
luup.variable_set("urn:user-com:serviceId:myvar","Ex1_STime",startTime, ##)
 
hour = tonumber( endTime:sub( endTime:find("%d+") ) )
minute = tonumber(endTime:sub(-2))
 
endTime = hour * 60 + minute
luup.variable_set("urn:user-com:serviceId:myvar","Ex1_ETime",endTime , ##)
 
local currentTime = os.date("*t")
currentTime = currentTime.hour * 60 + currentTime.min
luup.variable_set("urn:user-com:serviceId:myvar","Ex1_CTime",currentTime, ##)
 
luup.log("startTime = " .. startTime .. "; currentTime = " .. currentTime .. "; endTime = " ..

endTime)
 
if startTime <= endTime then
    -- Both the start time and the end time are in the same day:
    -- if the current time is in the given interval, run the scene.
    if startTime <= currentTime and currentTime <= endTime then
        luup.variable_set("urn:user-com:serviceId:myvar","Ex1_Between?","Yes", ##)
        return true
    end
else
    -- The start time is before midnight, and the end time is after midnight:
    -- if the current time is not outside the given interval, run the scene.
    if not (endTime < currentTime and currentTime < startTime) then
        luup.variable_set("urn:user-com:serviceId:myvar","Ex1_Between?","Yes", ##)
        return true
    end
end
luup.variable_set("urn:user-com:serviceId:myvar","Ex1_Between?","No",##)
return false
End Code.......

@guessed,
Please look at my formating and let me know if I have conformed to the standard.
The use of "Ex1_XXXXXXXX" as my variable format is so that new variables can be used in every Example, and the variables can be identified as to where they came from.
When I created the new Service ID and reused the same variables.....both popped up as new variables even though the older version was not in the Code! There need to be strict policies on how to implement these New Variables....
So far I haven't seen any ill effects.

In the program you select the Device from a drop down box that parses the system file, gives the name of every device, and then replaces the ## in the code with the device number....the user does nothing but copy and paste......There are step by step instructions for the creation of the scene, timers, luup code, and variables.
Feedback please....

Regards
Tim Alls
« Last Edit: May 25, 2011, 08:20:41 pm by TimAlls »

Offline oTi@

  • Community Beta
  • Master Member
  • ******
  • Posts: 4041
  • Karma: +32/-6
  • UI what ?!
Re: Why Create Variables? Example posted
« Reply #23 on: May 25, 2011, 08:40:29 pm »
Here is the code from the first example...this was adapted from the posted Wiki....I am unaware of the author..
I had assumed @mcvflorin.
Dezwaved at the moment...

Offline TimAlls

  • Hero Member
  • *****
  • Posts: 722
  • Karma: +0/-0
Re: Why Create Variables? Example posted
« Reply #24 on: May 25, 2011, 10:25:34 pm »
Here is the code from the first example...this was adapted from the posted Wiki....I am unaware of the author..
I had assumed @mcvflorin.
I was unable to get the original codes error check to work....take a look and tell me if you think that the "invalid time" function is correct, that is why I deleted it. I would like to have an error check for the code that works.
Regards
Tim