EspecNorthAmerica / ChamberConnectLibrary

Python library for ESPEC North America chambers with P-300, SCP-220, Watlow F4, F4T controllers.
http://www.espec.com/na/support/software/
MIT License
29 stars 24 forks source link

Only one connection #9

Closed dimamotsik closed 6 years ago

dimamotsik commented 6 years ago

Hello!

Can I check that only one user connected to the chamber ? For example: I connected to chamber and the user from his desctop try to connect to the chamber and set temp. How can i forbid him to working with chamber, when I'm connected and working now.

Thanks!

MylesMetzler commented 6 years ago

Assuming you are working over TCP with a Watlow F4T or a P300/SCP-220 w/ web controller then they only allow a single active connection at a time (either rejecting extra connections or accepting then closing them).

You can do something like this:

controller = WatlowF4T() # or P300 or SCP220 etc.
controller.connect()
# the rest of your program goes here
controller.close()

The library will automatically call open and close on each command if a connection has not already been established, or if one has it will just use that connection.

I hope this helps?

dimamotsik commented 6 years ago

Yes, its help me.

And I have another question, can chamber send me some info about current conection ?

For example: User "A" connect to chamber. User "B" wants to connect to chamber too, but he does not know anything about the user "A" and his connection. Can user "B" using you library get some info about active connection with chamber (maybe count of active connection, IP or something else) ?

MylesMetzler commented 6 years ago

No, I can't think of a way to do that with only the library and chamber controller. You could look into using something like a redis for managing that, it is what I use on ENA's web controller for ensuring exclusive access to the physical serial ports across multiple threads/processes.

If you want to allow "multiple connections" the library is designed for that by using a custom lock (do not use the connect/close pattern from my previous answer). Here is an example lock that will allow multiple instances of a software package to share one controller using a share redis server:

import time
import redis
from chamberconnectlibrary.watlowf4t import WatlowF4T

class LockTimeout(Exception):
    '''Exception for redis lock timeout'''
    pass

class RedisLock(object):
    '''lock the chamber interface with a redis server'''
    def __init__(self, key, expires=300, timeout=60):
        self.key = key
        self.timeout = timeout
        self.expires = expires
        self.redis = redis.Redis(host='localhost', port=6379)
    def __enter__(self):
        timeout = self.timeout
        while timeout >= 0:
            expires = time.time() + self.expires + 0.1
            if self.redis.setnx(self.key, expires):
                return
            cval = self.redis.get(self.key)
            if cval and float(cval) < time.time() and self.redis.getset(self.key, expires) == cval:
                return
            timeout -= 0.1
            time.sleep(0.1)
        raise LockTimeout("Timeout waiting for lock")
    def __exit__(self, *args, **kwargs):
        self.redis.delete(self.key)

controller = WatlowF4T(lock=RedisLock("a key specific to each chamber goes here"), **more_kwargs)
dimamotsik commented 6 years ago

Thanks for answer!