Page 1 of 3

Can somebody help me with a function, please?

Posted: Thu Jan 15, 2015 8:52 am
by Mastiff
What I want to do, is this:

Code: Select all

eg.globals.Ovnmodus_WC = int(eg.plugins.System.RegistryQuery(HKEY_CURRENT_USER, u'Software\\Automatisering\\Styring av ovner\\WC', u'Modus', 1, u''))
And this:

Code: Select all

eg.plugins.System.RegistryChange(2147483649L, u'Software\\Automatisering\\Styring av ovner\\WC', u'Modus', 0, None, u'{eg.globals.Ovnmodus_WC}')
(I first had to change the registry plugin file as in this thread to get it to accept variables: http://www.eventghost.org/forum/viewtop ... f=5&t=5329)

But I would very much like to have it in a function. Which I am very far from managing... I'm guessing that it may be easy for somebody who actually know how to code, which I don't. The ideal function for getting stuff from the registry would be something like this:

Code: Select all

GetModus WC
And one for setting the registry:

Code: Select all

SetModus WC
Then I could use that with an event where the payload is the room to control. So the event fired externally to EG would be:

Code: Select all

C:\Program Files (x86)\EventGhost\EventGhost.exe -event Modus WC 3
And then "Modus" would trigger the script, and "WC" would be the payload for the room to control and 3 the mode to set (in my house I have 1 for day temp, 2 for night temp and 3 for making sure nothing freezes in the room, so around 5 degrees).

Have I explained this even remotely understandable? :mrgreen:

Edit: I can do the splitting of payloads and other stuff myself no problem, it's just the registry setting and getting that I would like to automate.

Re: Can somebody help me with a function, please?

Posted: Thu Jan 15, 2015 6:33 pm
by krambriw
Have I explained this even remotely understandable?
Unfortunately not really,,,,

I understand that you want to read and write (variable values) to registry

1) What would be the benefit to have that in function calls? You would still have to provide all parameters to such a call. Typically I could imagine it would look something like this:

Code: Select all

def SetModus(key, path, modus, flag, var):
    eg.plugins.System.RegistryChange(
        key,
        path,
        modus,
        flag, 
        None, 
        var
    )

def GetModus(key, path, modus, flag, var):
    res = eg.plugins.System.RegistryQuery(
        key,
        path,
        modus,
        flag, 
        var
    )
    return int(res)

SetModus(
    2147483649L,
    u'Software\\Automatisering\\Styring av ovner\\WC', 
    u'Modus', 
    0, 
    None, 
    u'{eg.globals.Ovnmodus_WC}'
)

myModus = GetModus(
        HKEY_CURRENT_USER, 
        u'Software\\Automatisering\\Styring av ovner\\WC', 
        u'Modus', 
        1, 
        u''
    )



Re: Can somebody help me with a function, please?

Posted: Thu Jan 15, 2015 6:41 pm
by Mastiff
Thank you very much! :mrgreen: I will play around with this and see what the result is! At the moment I'm rebuilding another part of the temperature control system. The reason for the function is the same as the other one, I'm trying to simplify the setup so I can make do with as few single macros as possible. Functions in Python are like LUA, that they can be called on EG startup and will stay persistant, right?

Re: Can somebody help me with a function, please?

Posted: Thu Jan 15, 2015 7:47 pm
by Mastiff
I think I see how this works now and why you don't think it's much easier. It may be my "background" in LUA, or my bad explanation. What I'm looking for is a function that lets me only use two parameters in the command, so SetModus to call the function and WC and 1 as parameters. Something like this (and this is the command using the function, not the function itself):

Code: Select all

SetModus WC 1
or if necessary:

Code: Select all

SetModus, 'WC', '1'
Does that make it any clearer? Here's an example of a small function in Girder that does about the same:

Code: Select all

function Kom.C15(msg,pld1,pld2,pld3,pld4) 
    g2g.ClientSendEvent("192.168.0.15",msg,200,1,pld1,pld2,pld3,pld4) 
end 
That helps me make each command a bit easier:

Code: Select all

Kom.C15("JRMC","Play","3")
This tells the receiving end to start playback in zone 3 of J.River MediaCenter. I do have much more complex functions that I've been helped with, this is just to explain what I'm trying to do. So the command becomes shorter than using the full synthax:

Code: Select all

g2g.ClientSendEvent("192.168.0.15","JRMC",200,1,"Play","3",nil,nil)

Re: Can somebody help me with a function, please?

Posted: Thu Jan 15, 2015 8:23 pm
by krambriw
I see what you mean. Personally I have never called a function without ALL required params so I do not know if there is some trick you can use in python (most likely there is but I do not know about it)

BestR

Re: Can somebody help me with a function, please?

Posted: Thu Jan 15, 2015 8:34 pm
by Mastiff
OK, thanks. I'll try to dig a bit more, but maybe I will have to use one for each room instead.

Re: Can somebody help me with a function, please?

Posted: Fri Jan 16, 2015 5:32 am
by krambriw
Well, in some way you have to tell the function what to do. You do it either by providing parameters or have them locally inside the function. The more generic you would like the function to be, more parameters are normally required.

In your particular example, you could easily reduce the number of parameters but then you would have to create more functions tailored to your various rooms

So what is the goal? I personally would prefer to have less number of functions and more parameters to simplify maintenance and reduce possible bugs.

Re: Can somebody help me with a function, please?

Posted: Fri Jan 16, 2015 7:29 am
by Mastiff
Yeah, you're probably right, since you know what you're doing and I don't! It's just that I only really need to change two paramters, room and mode. And that accounts for all the functions I was planning to build on the basis of this one. As long as I can change the second to last part, which is the registry key, and the last part, which is the room (so I can choose Office, Master bedroom, WC, Bathroom and so on as the room) and then the value (heater mode 1, 2 or 3), that would be enough. Instead of a function I could probably manage with my first attempt, which was something like this. I had the original:

Code: Select all

eg.globals.Ovnmodus_WC = eg.plugins.System.RegistryQuery(HKEY_CURRENT_USER, u'Software\\Automatisering\\Styring av ovner\\WC', u'Modus', 1, u'')
And then tried to replace it by using a couple of variables:

Code: Select all

ModusGet1 = 'eg.globals.Ovnmodus_'
ModusGet2 =  '= eg.plugins.System.RegistryQuery(HKEY_CURRENT_USER, u'Software\\Automatisering\\Styring av ovner\\'
ModusGet3 = "', u'"
ModusGet4 =  ... 
And so on. Then having it sortet and concatenated with the two variables put in at three places (WC is used twice) . You see why that didn't work very well and why I gave up! ;) So it just struck me that in LUA I would be told: "You need to get that into a function" Which I've heard a few times... ;) I like having complicated stuff hidden in functions and then only use the necessary parameters in the actual command. But I may have to bite the bullet on this one...

Re: Can somebody help me with a function, please?

Posted: Fri Jan 16, 2015 8:18 am
by krambriw
When you write a function you have to think about what will be variables and what can be constants. Constants can reside inside the function and variables provided as parameters.

If you have a string like u'Software\\Automatisering\\Styring av ovner\\WC' and you know that the first part will always be the same then you can of course divide it into one constant and one variable

constant: u'Software\\Automatisering\\Styring av ovner\\'
variable: u'WC'

So in a function and a call it could be treated like this:

Code: Select all

def sample(var):
    const = u'Software\\Automatisering\\Styring av ovner\\'
    res = const + var
    return res

myRes = sample(u'WC')
print myRes

Re: Can somebody help me with a function, please?

Posted: Fri Jan 16, 2015 8:41 am
by Mastiff
Scary... I think I'm understanding this! :shock: Which just goes to show that the one about old dogs and new tricks don't apply to Mastiffs! ;) Thank you, master! So here's what I managed to do in ten minutes (changed the registry key variable to what the plugin sees, no point in having the HKEY and so on in the function):

Code: Select all

def GetReg(var1, var2):
    const1 = "eg.plugins.System.RegistryQuery(2147483649L, u'Software\\Automatisering\\Styring av ovner\\'"
    const2 = ", u'Modus', "
    const3 = ", u'')"
    res = const1 + var1 + const2 + var2 + const3
    return res

myRes = GetReg(u'WC',u'1')
print myRes
Now the scary part is that I think I get what I should get!

Code: Select all

   eg.plugins.System.RegistryQuery(2147483649L, u'Software\Automatisering\Styring av ovner\'WC, u'Modus', 1, u'')
What looks like a regular quote at the end is actually two single ' quotes. They are very close together on the board, but look right in the result in EG. So next step is to run the code and see if it destroys my registry!

Re: Can somebody help me with a function, please?

Posted: Fri Jan 16, 2015 8:55 am
by Mastiff
OK, of course not that easy. I can get it this far, but what do I use to make it actually run the query when the function is called, not just print it? Is there an execute statement or something that should be used for that?

Re: Can somebody help me with a function, please?

Posted: Fri Jan 16, 2015 9:12 am
by krambriw
Maybe this works better ???

Code: Select all

def GetReg(var1, var2):
    const1 = 2147483649L
    const2 = u'Software\\Automatisering\\Styring av ovner\\'
    const3 = u'Modus'
    const4 = u''
    res = eg.plugins.System.RegistryQuery(const1, const2+var1, const3, var2, const4)
    return res

myRes = GetReg(u'WC',1)
print myRes


Re: Can somebody help me with a function, please?

Posted: Fri Jan 16, 2015 9:56 am
by Mastiff
He shoots, he scores! Sweden wins the game in triple overtime! :mrgreen: That's it! :D :D :D Now the getting function works. The setting function is a bit more wonky, I'm sure I'm doing something wrong with all the ) ' and " because I'm trying to achieve this string:

(2147483649L, u'Software\\Automatisering\\Styring av ovner\\WC', u'Modus', 0, None, u'1')
The bold is the two variables.

I've spent the last half hour or so of my work day (which shows how eager I am to get this working - I work for myself and won't make any money if I don't do my job) experimenting with different syntaxes, but I just can't get it! This is the closest I have gotten so far (just trying to print for now, the setting will work with the code from the GetReg function):

Code: Select all

def SetReg(var1, var2):
    const1 = 2147483649L
    const2 = u'Software\\Automatisering\\Styring av ovner\\'
    const3 = "u'Modus', 0, None, u'"
    const4 = ''#"')"
    res = (const1, const2+var1, const3, var2, const4)
    return res


myRes = SetReg(u'WC',1)
print myRes
When that is run, I get this:
(2147483649L, u'Software\\Automatisering\\Styring av ovner\\WC', "u'Modus', 0, None, u'", 1, '')

Note the extra symbols which are bold and underscored here. I tried several other versions, but they all gave me either too many " or extra () which I had no idea how came in there. Are you able to put me out of my misery on this one too?

Re: Can somebody help me with a function, please?

Posted: Fri Jan 16, 2015 10:40 am
by krambriw
Here they are, both put together, seems to work when I try. I now have one of your rooms (the guest toilet) in my registry... :D

Code: Select all

def SetReg(var1, var2):
    const1 = 2147483649L
    const2 = u'Software\\Automatisering\\Styring av ovner\\'
    const3 = u'Modus'
    res = eg.plugins.System.RegistryChange(const1, const2+var1, const3, 0, None, var2)
    return res


def GetReg(var1, var2):
    const1 = 2147483649L
    const2 = u'Software\\Automatisering\\Styring av ovner\\'
    const3 = u'Modus'
    const4 = u''
    res = eg.plugins.System.RegistryQuery(const1, const2+var1, const3, var2, const4)
    return res

myRes = SetReg(u'WC',3)
print myRes

myRes = GetReg(u'WC',1)
print myRes

Re: Can somebody help me with a function, please?

Posted: Fri Jan 16, 2015 10:50 am
by Mastiff
Actually it's more of a men's room... ;) Me and my son being the smelly ones in the family! :lol: And that was probably too much information... But I think there's something missing on my system. Maybe a variable you have floating around that isn't here? Because I get this when I run it:

Code: Select all

   Traceback (most recent call last):
     Python script "94", line 17, in <module>
       myRes = SetReg(u'WC',3)
     Python script "94", line 5, in SetReg
       res = eg.plugins.System.RegistryChange(const1, const2+var1, const3, 0, None, var2)
     File "C:\Program Files (x86)\EventGhost\plugins\System\Registry.py", line 738, in __call__
       newValue = eg.ParseString(newValue)
     File "C:\Program Files (x86)\EventGhost\eg\Utils.py", line 185, in ParseString
       last = len(text) - 1
   TypeError: object of type 'int' has no len()