Jump to content

Change variable name with a modifier (python 2.7) due to limitations

Dujith

So for this KNX聽server (Gira Homeserver) its possible to write your own code, now there are limitations (1 being its Python 2.7 with a fixed library)

And i want to simplify an action, but i'm not sure how. My python knowledge is low and most stuff i just google 馃槃

Now for the problem:

Whenever i make a logic module i first have to specify the inputs and outputs. After that a program generates a py file in which i can enter my own code.

The module i'm making will talk via TCP to a security system and tell me which zones are activated for all kinds of stuff i want todo with KNX (lights, visualization of doors on the touchscreen ect.)

I get that data by a binaire number that tells me the zones activated:聽https://linustechtips.com/topic/1330742-decoding-a-tcp-response/?tab=comments#comment-14678574聽again thanks to @Eigenvektor

So i made a module that has 10 inputs and 10 ouputs (3 more inputs for the IP, port and trigger) and it generated this:

class Integra10834(hsl20_3.BaseModule):

    def __init__(self, homeserver_context):
        hsl20_3.BaseModule.__init__(self, homeserver_context, "Integra")
        self.FRAMEWORK = self._get_framework()
        self.LOGGER = self._get_logger(hsl20_3.LOGGING_NONE,())
        self.PIN_I_IPADRES=1
        self.PIN_I_POORT=2
        self.PIN_I_ZONE_STATUS=3
        self.PIN_I_ZONE1=4
        self.PIN_I_ZONE2=5
        self.PIN_I_ZONE3=6
        self.PIN_I_ZONE4=7
        self.PIN_I_ZONE5=8
        self.PIN_I_ZONE6=9
        self.PIN_I_ZONE7=10
        self.PIN_I_ZONE8=11
        self.PIN_I_ZONE9=12
        self.PIN_I_ZONE10=13
        self.PIN_O_ZONESTATUS1=1
        self.PIN_O_ZONESTATUS2=2
        self.PIN_O_ZONESTATUS3=3
        self.PIN_O_ZONESTATUS4=4
        self.PIN_O_ZONESTATUS5=5
        self.PIN_O_ZONESTATUS6=6
        self.PIN_O_ZONESTATUS7=7
        self.PIN_O_ZONESTATUS8=8
        self.PIN_O_ZONESTATUS9=9
        self.PIN_O_ZONESTATUS10=10
        self.FRAMEWORK._run_in_context_thread(self.on_init)

At some point i check the binary number to get the zones with this:

                    for x in databin:
                        if x == "1":
                            zones.append(bit)
                        else:
                            if bit in zones:
                                zones.remove(bit)
                        bit += 1

databin being the stored binary number in a string. This way i get the zone number in zones

Now i just have to compare the zone numbers i put into 1 of the 10 pins and trigger the output pin.

But how can i do this in a better way then make 10 if statements?

Link to comment
Share on other sites

Link to post
Share on other sites

After doing some more searching it seems i can do something with exec, gonna聽be a bit hacky but it works聽馃槃

for y in range(1,11):
	exec "yy = self._get_input_value(self.PIN_I_ZONE" + str(y) + ")"
 

This is then followed by a check in the zones list i made for that value (numbers are unique so can do a simple in statement.)

and with the y i can set the output number to on or off.

There prob is a better way todo this, but then i would need to spend alot more time in python聽馃槆

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Sakuriru said:

I don't really quite understand why you're trying to do here, but why not use enums. Then you can just do:

If i could i would have, but i'm limited on how the KNX server presents its inputs and outputs in python.

When i specify how much inputs and outputs a module will have it generates that code i posted in the first post.

<?xml version="1.0" encoding="utf-8"?>
<config>
    <modules>
        <module category="Logic2_0" context="Integra" id="10834" name="Integra" version="1.0">
            <inputs>
                <input type="string" const_name="ipadres" init_value="192.168.1.50">IP adres</input>
                <input type="number" const_name="poort" init_value="7094">Poort</input>
                <input type="number" const_name="zone_status" init_value="0">Zone Status</input>
                <input type="number" const_name="zone1" init_value="1">Zone Nummer (1)</input>
                <input type="number" const_name="zone2" init_value="2">Zone Nummer (2)</input>
                <input type="number" const_name="zone3" init_value="3">Zone Nummer (3)</input>
                <input type="number" const_name="zone4" init_value="4">Zone Nummer (4)</input>
                <input type="number" const_name="zone5" init_value="5">Zone Nummer (5)</input>
                <input type="number" const_name="zone6" init_value="6">Zone Nummer (6)</input>
                <input type="number" const_name="zone7" init_value="7">Zone Nummer (7)</input>
                <input type="number" const_name="zone8" init_value="8">Zone Nummer (8)</input>
                <input type="number" const_name="zone9" init_value="9">Zone Nummer (9)</input>
                <input type="number" const_name="zone10" init_value="10">Zone Nummer (10)</input>
            </inputs>
            <outputs>
                <output type="number" const_name="ZoneStatus1" init_value="0">Zone Status (1)</output>
                <output type="number" const_name="ZoneStatus2" init_value="0">Zone Status (2)</output>
                <output type="number" const_name="ZoneStatus3" init_value="0">Zone Status (3)</output>
                <output type="number" const_name="ZoneStatus4" init_value="0">Zone Status (4)</output>
                <output type="number" const_name="ZoneStatus5" init_value="0">Zone Status (5)</output>
                <output type="number" const_name="ZoneStatus6" init_value="0">Zone Status (6)</output>
                <output type="number" const_name="ZoneStatus7" init_value="0">Zone Status (7)</output>
                <output type="number" const_name="ZoneStatus8" init_value="0">Zone Status (8)</output>
                <output type="number" const_name="ZoneStatus9" init_value="0">Zone Status (9)</output>
                <output type="number" const_name="ZoneStatus10" init_value="0">Zone Status (10)</output>
                
            </outputs>
        </module>
    </modules>
 </config>

self.PIN_I_ZONE1 being the first one and so on.

Then i need to use self._get_input_value(self.PIN_I_ZONE1) to get the value in that input, and i set an output with self._set_output_value(self.PIN_O_ZONESTATUS1, 1)

Its just how the module works and i cannot change that part sadly.

image.png.231fc8fab7103395cac8ce0b90d0de05.png

In the KNX server it look like this. So for a system with 100 zones i can pick out a few i need to switch on lights (Door opens at night so the hallway will come on for example)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now