-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/turbovac #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
renereimann
wants to merge
20
commits into
develop
Choose a base branch
from
feature/turbovac
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/turbovac #231
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
21d04f4
Initial commit for a turbovac implementation. We resue the telegram m…
renereimann 69da191
renamed file to reflect class name
renereimann cf37bee
switch to most recent dripline version
renereimann 83225d4
since the filename changed we have to take it here into account as well
renereimann 932e250
use relative imports so that the modules are found correctly
renereimann a5c7b29
include *.txt files in python site-packages, they are needed in turbo…
renereimann 8fc5a4e
implementation of the USS protocol. The protocol uses the \x02 byte a…
renereimann c4e5c6a
Implementation of a TurboVACTelegramEntity. We use the telegram class…
renereimann 670858a
adding a config file for the TurboVAC class
renereimann ec45f8d
adding a pure get and a pure set entity for TurboVACTelegram
renereimann d560b73
fixing class name from get to set
renereimann b62e6fe
fixing initialization of parent class
renereimann 506b49e
I changed the behaviour to also control the pump. THe pump is control…
renereimann b17660b
gerge branch 'develop' into feature/turbovac
renereimann 14bcba7
Merge <develop> to <feature/turbovac>
renereimann 858120f
changing prints to logging, changing raise Exception to raise ThrowRe…
renereimann 02ba021
Merge branch 'develop' into feature/turbovac
renereimann 1ec9611
Merge branch 'develop' into feature/turbovac
renereimann 5437b89
Updating class documentation, updating variable name so _log_action_i…
renereimann 3da0ca1
removing example configurations in .yaml file and docker-compose.yaml
renereimann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| recursive-include dragonfly *.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| # Subdirectories | ||
| from . import jitter | ||
| from . import turbovac | ||
|
|
||
| # Modules in this directory | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| The files contained in telegram are a copy from the telegram folder of | ||
| https://github.com/fkivela/TurboCtl/tree/work | ||
| I modified the files to be compatible with python versions < 3.10, by changing some of the newer syntax features to clasical implementations. | ||
| We use the TurboCtl.telegram to generate the content of the telegram but use the dripline service to send it. Thus we can not make use of any of the other implementations. | ||
|
|
||
| The TurboVac Ethernet Service implements the USS protocol which consists of a STX-byte (\x02), LENGTH-byte, ADDRESS-byte, payload, XOR-checksum-byte. | ||
| The Payload is configured within the Endpoint class and makes use of the TurboCtl.telegram module. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| __all__ = [] | ||
|
|
||
| from .ethernet_uss_service import * | ||
| from .turbovac_endpoint import * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import re | ||
| import socket | ||
| import threading | ||
|
|
||
| from dripline.core import Service, ThrowReply | ||
|
|
||
renereimann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import logging | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
| __all__ = [] | ||
|
|
||
|
|
||
| __all__.append('EthernetUSSService') | ||
| class EthernetUSSService(Service): | ||
| ''' | ||
| ''' | ||
| def __init__(self, | ||
| socket_timeout=1.0, | ||
| socket_info=('localhost', 1234), | ||
| STX = b'\x02', | ||
| **kwargs | ||
| ): | ||
| ''' | ||
| Args: | ||
| socket_timeout (int): number of seconds to wait for a reply from the device before timeout. | ||
| socket_info (tuple or string): either socket.socket.connect argument tuple, or string that | ||
| parses into one. | ||
| STX (binary): the STX byte of the USS Protocol. Defaults to b'\x02' | ||
| ''' | ||
| Service.__init__(self, **kwargs) | ||
|
|
||
| if isinstance(socket_info, str): | ||
| logger.debug(f"Formatting socket_info: {socket_info}") | ||
| re_str = "\([\"'](\S+)[\"'], ?(\d+)\)" | ||
| (ip,port) = re.findall(re_str,socket_info)[0] | ||
| socket_info = (ip,int(port)) | ||
|
|
||
| self.alock = threading.Lock() | ||
| self.socket = socket.socket() | ||
| self.socket_timeout = float(socket_timeout) | ||
| self.socket_info = socket_info | ||
| self.STX = STX | ||
| self.control_bits = [] | ||
| self._reconnect() | ||
|
|
||
| def _reconnect(self): | ||
| ''' | ||
| Method establishing socket to ethernet instrument. | ||
| Called by __init__ or send (on failed communication). | ||
| ''' | ||
| # close previous connection | ||
| # open new connection | ||
| # check if connection was successful | ||
|
|
||
| self.socket.close() | ||
| self.socket = socket.socket() | ||
| try: | ||
| self.socket = socket.create_connection(self.socket_info, self.socket_timeout) | ||
| except (socket.error, socket.timeout) as err: | ||
| logger.warning(f"connection {self.socket_info} refused: {err}") | ||
| raise ThrowReply('resource_error_connection', f"Unable to establish ethernet socket {self.socket_info}") | ||
| logger.info(f"Ethernet socket {self.socket_info} established") | ||
|
|
||
| def send_to_device(self, commands, **kwargs): | ||
| ''' | ||
| Standard device access method to communicate with instrument. | ||
| NEVER RENAME THIS METHOD! | ||
|
|
||
| commands (list||None): list of command(s) to send to the instrument following (re)connection to the instrument, still must return a reply! | ||
| : if impossible, set as None to skip | ||
| ''' | ||
| if not isinstance(commands, list): | ||
| commands = [commands] | ||
|
|
||
| self.alock.acquire() | ||
|
|
||
| try: | ||
| data = self._send_commands(commands) | ||
| except socket.error as err: | ||
| logger.warning(f"socket.error <{err}> received, attempting reconnect") | ||
| self._reconnect() | ||
| data = self._send_commands(commands) | ||
| logger.warning("Ethernet connection reestablished") | ||
| # exceptions.DriplineHardwareResponselessError | ||
| except Exception as err: | ||
| logger.warning(str(err)) | ||
| try: | ||
| self._reconnect() | ||
| data = self._send_commands(commands) | ||
| logger.info("Query successful after ethernet connection recovered") | ||
| except socket.error: # simply trying to make it possible to catch the error below | ||
| logger.critical("Ethernet reconnect failed, dead socket") | ||
| raise ThrowReply('resource_error_connection', "Broken ethernet socket") | ||
| except Exception as err: ##TODO handle all exceptions, that seems questionable | ||
| logger.critical("Query failed after successful ethernet socket reconnect") | ||
| raise ThrowReply('resource_error_no_response', str(err)) | ||
| finally: | ||
| self.alock.release() | ||
| to_return = b''.join(data) | ||
| logger.info(f"should return:\n{to_return}") | ||
| return to_return | ||
|
|
||
| def _send_commands(self, commands): | ||
| ''' | ||
| Take a list of telegrams, send to instrument and receive responses, do any necessary formatting. | ||
|
|
||
| commands (list||None): list of command(s) to send to the instrument following (re)connection to the instrument, still must return a reply! | ||
| : if impossible, set as None to skip | ||
| ''' | ||
| all_data=[] | ||
|
|
||
| for command in commands: | ||
| if not isinstance(command, bytes): | ||
| raise ThrowReply('invalid_argument_type', "Command is not of type bytes: {command}, {type(command)}") | ||
| logger.debug(f"sending: {command}") | ||
| self.socket.send(command) | ||
| data = self._listen() | ||
| logger.debug(f"sync: {repr(command)} -> {repr(data)}") | ||
| all_data.append(data) | ||
| return all_data | ||
|
|
||
| def _listen(self): | ||
| ''' | ||
| Query socket for response. | ||
| ''' | ||
| data = b'' | ||
| length = None | ||
| try: | ||
| while True: | ||
| tmp = self.socket.recv(1024) | ||
| data += tmp | ||
| if self.STX in data: | ||
| start_idx = data.find(self.STX) | ||
| data = data[start_idx:] # get rid of everything before the start | ||
| if len(data)>1: # if >1 data we have a length info | ||
| length = int(data[1])+2 | ||
| if len(data) >= length: # if we are >= LENGTH we have all we need | ||
| break | ||
| if tmp == '': | ||
| raise ThrowReply('resource_error_no_response', "Empty socket.recv packet") | ||
| except socket.timeout: | ||
| logger.warning(f"socket.timeout condition met; received:\n{repr(data)}") | ||
| raise ThrowReply('resource_error_no_response', "Timeout while waiting for a response from the instrument") | ||
| logger.info(repr(data)) | ||
| data = data[0:length] | ||
| return data | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| """This package is used to form telegrams that send commands to the | ||
| pump. | ||
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| """This module defines an API of functions which can be used to send commands | ||
| to the pump. | ||
|
|
||
| All functions in this module share the following common arguments: | ||
|
|
||
| *connection*: | ||
| This is a :class:`serial.Serial` instance, which is used to send the | ||
| command. | ||
|
|
||
| *pump_on*: | ||
| If this evaluates to ``True``, control bits telling the pump to turn or | ||
| stay on are added to the telegram. Otherwise receiving the telegram | ||
| will cause the pump to turn off if it is on. | ||
|
|
||
| If a command cannot be sent due to an error in the connection, a | ||
| :class:`serial.SerialException` will be raised. | ||
|
|
||
| The functions return both the query sent to the pump and the reply received | ||
| back as :class:`~turboctl.telegram.telegram.TelegramReader` instances. | ||
| """ | ||
|
|
||
| from .codes import ControlBits | ||
| from .telegram import (Telegram, TelegramBuilder, | ||
| TelegramReader) | ||
|
|
||
|
|
||
| _PUMP_ON_BITS = [ControlBits.COMMAND, ControlBits.ON] | ||
| _PUMP_OFF_BITS = [ControlBits.COMMAND] | ||
|
|
||
|
|
||
| def send(connection, telegram): | ||
| """Send *telegram* to the pump. | ||
|
|
||
| Args: | ||
| telegram: A :class:`~turboctl.telegram.telegram.Telegram` instance. | ||
| """ | ||
| connection.write(bytes(telegram)) | ||
| reply_bytes = connection.read(Telegram.LENGTH) | ||
| reply = TelegramBuilder().from_bytes(reply_bytes).build() | ||
| return TelegramReader(telegram, 'query'), TelegramReader(reply, 'reply') | ||
|
|
||
|
|
||
| def status(connection, pump_on=None): | ||
| """Request pump status. | ||
|
|
||
| This function sends an empty telegram to the pump, which causes it to send | ||
| back a reply containing some data about the status of the pump. | ||
|
|
||
| This can also be used for turning the pump on or off by setting *pump_on* | ||
| to ``True`` or ``False``. | ||
| """ | ||
| builder = TelegramBuilder() | ||
| if pump_on: | ||
| builder.set_flag_bits(_PUMP_ON_BITS) | ||
| else: | ||
| builder.set_flag_bits(_PUMP_OFF_BITS) | ||
|
|
||
| query = builder.build() | ||
| return send(connection, query) | ||
|
|
||
|
|
||
| def reset_error(connection): | ||
| """Reset the error status of the pump. | ||
|
|
||
| This function sends a "reset error" command to the pump. | ||
| """ | ||
| builder = TelegramBuilder() | ||
| clear_error = [ControlBits.COMMAND, ControlBits.RESET_ERROR] | ||
| builder.set_flag_bits(clear_error) | ||
| query = builder.build() | ||
| return send(connection, query) | ||
|
|
||
|
|
||
| def _access_parameter(connection, mode, number, value, index, pump_on): | ||
| """This auxiliary function provides functionality for both reading and | ||
| writing parameter values, since the processes are very similar. | ||
| """ | ||
| builder = (TelegramBuilder() | ||
| .set_parameter_mode(mode) | ||
| .set_parameter_number(number) | ||
| .set_parameter_index(index) | ||
| .set_parameter_value(value) | ||
| ) | ||
|
|
||
| if pump_on: | ||
| builder.set_flag_bits(_PUMP_ON_BITS) | ||
|
|
||
| query = builder.build() | ||
| return send(connection, query) | ||
|
|
||
|
|
||
| def read_parameter(connection, number, index=0, pump_on=True): | ||
| """Read the value of an index of a parameter. | ||
|
|
||
| Args: | ||
| number | ||
| The number of the parameter. | ||
|
|
||
| index | ||
| The index of the parameter (0 for unindexed parameters). | ||
|
|
||
| Raises: | ||
| ValueError: | ||
| If *number* or *index* have invalid values. | ||
| """ | ||
| return _access_parameter(connection, 'read', number, 0, index, pump_on) | ||
|
|
||
|
|
||
| def write_parameter(connection, number, value, index=0, pump_on=True): | ||
| """Write a value to an index of a parameter. | ||
|
|
||
| Args: | ||
| number: | ||
| The number of the parameter. | ||
|
|
||
| value: | ||
| The value to be written. | ||
|
|
||
| index: | ||
| The index of the parameter (0 for unindexed parameters). | ||
|
|
||
| Raises: | ||
| ValueError: | ||
| If *number* or *index* have invalid values. | ||
| """ | ||
| return _access_parameter( | ||
| connection, 'write', number, value, index, pump_on) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.