Coder Social home page Coder Social logo

Confusion about 'sensor' about mycodo HOT 24 CLOSED

kizniche avatar kizniche commented on August 24, 2024
Confusion about 'sensor'

from mycodo.

Comments (24)

kizniche avatar kizniche commented on August 24, 2024

This is the number of sensors for each class of sensors (temperature, temperature/humidity, co2, pressure). It's used mainly to identify settings by being used as an array index, such as sensor_t_activated[sensor]. Yes, the 1 is added for display purposes to the user (logs, web interface).

from mycodo.

kizniche avatar kizniche commented on August 24, 2024

You'll probably notice the daemon runs with many global variables. I've been trying to be better at moving away from globals. I've found ways to make many of them local, but there's probably a lot more that can be done. I started creating the daemon with all globals, so it's a slow process to fix some of my initial mistakes.

from mycodo.

Cabalist avatar Cabalist commented on August 24, 2024

Would renaming sensor to sensor_id make sense to you? That way it isn't confused with a sensor object or anything besides an index.

Yeah there are a LOT of globals. I've been trying to suss out whether they were there for GPIO pins or if it was a coding style. Sounds like it is a coding style that you'd like to move away from. Your code works and people seem to enjoy it. I hope you aren't worried about the style mistakes. 😄

from mycodo.

kizniche avatar kizniche commented on August 24, 2024

I agree, sensor_id makes more sense. 788 matches for the word 'sensor' 😦

I have a lot of bad coding style habits. I don't mind criticism, as long as I can learn something. I'm sure there are a lot of armature mistakes throughout all my code. I used to care only about functionality, but I also wanted to share my code. So I realized there needs to be a balance between functionality and readability.

from mycodo.

Cabalist avatar Cabalist commented on August 24, 2024

I'll make a pull request for this. One moment.

Just 709 that are the full word and not in comments or literals.

from mycodo.

Cabalist avatar Cabalist commented on August 24, 2024

Hmm... be careful not to do a blind find/replace.

This looks like a bug:

   def exposed_ReadCO2Sensor(self, pin, sensor):
        logging.info("[Client command] Read CO2 Sensor %s from GPIO pin %s", sensor, pin)
        if sensor == 'K30':
            read_co2_sensor(sensor - 1)
            return sensor_co2_read_co2
        else:
            return 'Invalid Sensor Name'

It checks wether sensor is a string and then tries to do math on it.

from mycodo.

kizniche avatar kizniche commented on August 24, 2024

Oh, I see it now. That is rather odd.

from mycodo.

kizniche avatar kizniche commented on August 24, 2024

Should be:

    def exposed_ReadCO2Sensor(self, pin, sensor):
        logging.info("[Client command] Read CO2 Sensor %s from GPIO pin %s", sensor, pin)
        read_co2_sensor(sensor - 1)
        return sensor_co2_read_co2

from mycodo.

kizniche avatar kizniche commented on August 24, 2024

...Another bad use of a global.

from mycodo.

kizniche avatar kizniche commented on August 24, 2024

Actually, this is what it should be:

    def exposed_ReadCO2Sensor(self, device, sensor):
        logging.info("[Client command] Read %s CO2 Sensor %s", device, sensor)
        if device == 'K30':
            return read_co2_sensor(sensor - 1)
        else:
            return 'Invalid Sensor Name'

from mycodo.

Cabalist avatar Cabalist commented on August 24, 2024

Don't you mean.... sensor_id? haha

from mycodo.

kizniche avatar kizniche commented on August 24, 2024

Haha. Yeah. I actually did use sensor_id in the commit. There was a lot to fix in that small function.

from mycodo.

Cabalist avatar Cabalist commented on August 24, 2024

So looking at the fixed exposed_ReadCO2Sensor I notice that instead of doing +1 in the log string you are -1 when passing it to the read_co2_sensor function. This is worth raising because some of your code does this the other way around.

Is there a reason to treat the sensor_ids differently here?

from mycodo.

kizniche avatar kizniche commented on August 24, 2024

When going from the stored value, the index starts at 0, so 1 must be added when presented to the user, but when receiving as a user input, 1 must be subtracted to reach the appropriate value that is stored in the database.

from mycodo.

kizniche avatar kizniche commented on August 24, 2024

The exposed functions are used to execute code from the mycodo-client.py script. I suppose we could have the client script subtract 1 before calling exposed_ReadCO2Sensor

from mycodo.

Cabalist avatar Cabalist commented on August 24, 2024

I think that would be right. We want to make sure that abstraction layers don't mix. The 'core' should act consistently and if you make a user layer you talk to the core using that language. The DISPLAY may show other stuff. If the relays were distinguished by color (red relay, blue relay, etc.) we would still have use the numeric address internally but it would be up to the UI to convert that into red/blue/etc so that the human would know which one we were referring to.

It isn't wasteful to have something like this:

def exposed_ReadCO2Sensor(self, device, sensor):
    """
    User facing function takes sensor_id as numbered on webpage.
    """
    return readco2sensor(self, device, sensor - 1)

def readco2sensor(self,device, raw_sensor_id):
    """
    Internal implementation of readco2sensor.  May change over time or as sensors are added.  Consistent internal notation 
    """

    logging.info("[Client command] Read %s CO2 Sensor %s", device, raw_sensor_id + 1)
    if device == 'K30':
        return read_co2_sensor(raw_sensor_id)
    else:
        return 'Invalid Sensor Name'

from mycodo.

kizniche avatar kizniche commented on August 24, 2024

Got all sensor functions created as per that template, but getting this error when I test one:

File "/var/www/mycodo/cgi-bin/mycodo.py", line 364, in exposed_ReadHTSensor
    return readhtsensor(self, device, sensor_id - 1)
NameError: global name 'readhtsensor' is not defined

from mycodo.

kizniche avatar kizniche commented on August 24, 2024

Ah, think I need to call self.readhtsensor()

from mycodo.

Cabalist avatar Cabalist commented on August 24, 2024

yep that would do it. :)

from mycodo.

kizniche avatar kizniche commented on August 24, 2024

Success!

kiz@tango:www/mycodo$ ./cgi-bin/mycodo-client.py --sensorht DHT22 1                          11/25/15 12:54 AM
2015 11 25 00 55 00 [Remote command] Read DHT22 HT sensor 1
2015 11 25 00 55 02 [Remote Command] Daemon Returned: Temperature: 25.9°C Humidity: 36.6%

from mycodo.

kizniche avatar kizniche commented on August 24, 2024

All finished. T, HT, CO2, and Press sensor querying from mycodo-client now working with the new functions.

from mycodo.

kizniche avatar kizniche commented on August 24, 2024

I'm going to have to start getting to bed earlier. I live on the east coast, so I'm a few hours ahead of you. Luckily it's a slow week and I can sleep in until noon, and I've thoroughly enjoyed the late-night programming sessions, but our schedules don't quite align. You may start to see me drop out around midnight EST, instead of 4am / 5am.

from mycodo.

Cabalist avatar Cabalist commented on August 24, 2024

Good. Sleep is good. haha :)

On Wed, Nov 25, 2015 at 2:57 PM, Kyle Gabriel [email protected]
wrote:

I'm going to have to start getting to bed earlier. I live on the east
coast, so I'm a few hours ahead of you. Luckily it's a slow week and I can
sleep in until noon, and I've thoroughly enjoyed the late-night programming
sessions, but our schedules don't quite align. You may start to see me
drop out around midnight EST, instead of 4am / 5am.


Reply to this email directly or view it on GitHub
#57 (comment).

from mycodo.

kizniche avatar kizniche commented on August 24, 2024

Seems we've fully explored this topic. closing...

from mycodo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.