Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Sat Jan 30 11:14:05 CET 2016 Flavio Castelli <flavio@castelli.name>

* Initial release 0.1.0
Sun Feb 05 04:55:23 CET 2017 Emanuele Leuzzi <emanuele@leuzzi.me>
* Release 0.1.1
* Release 0.1.0 (Inizial by Flavio Castelli <flavio@castelli.name>)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please drop this change, I'll take care of updating the changelog once this is over. The release version is probably going to be different (I think 0.2.0), plus the format is wrong (you should not change existing entries of the changelog, just keep adding the new stuff on the top of it).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please feel free to make whatever changes you want. I'm not a coder. I committed this 'cause many people asked me this by email. Sorry if I made mistakes.

75 changes: 52 additions & 23 deletions scsgate/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,81 @@
with the SCSGate device """

import serial as pyserial


import socket
class Connection:

""" Connection to SCSGate device """

def __init__(self, device, logger):
def __init__(self, device, logger, port):
""" Initialize the class

Arguments:
device: string containing the serial device allocated to SCSGate
logger: instance of logging
"""
self._serial = pyserial.Serial(device, 115200)
self.device = device
self.logger = logger
self.port = port
if port != 0:
try:
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._socket.settimeout(1)
except socket.error:
raise RuntimeError("Failed to create sockt")
else:
self._socket = pyserial.Serial(device, 115200)

logger.info("Clearing buffers")
self._serial.write(b"@b")
ret = self._serial.read(1)
if ret != b"k":
self.send(b"@b")
ret = self.receive()
if (ret != b"k") or (ret is None):
raise RuntimeError("Error while clearing buffers")

# ensure pending operations are terminated (eg: @r, @l)
self._serial.write(b"@c")
ret = self._serial.read()
if ret != b"k":
self.send(b"@c")
ret = self.receive()
if (ret != b"k") or (ret is None):
raise RuntimeError("Error while cancelling pending operations")

logger.info("Enabling ASCII mode")
self._serial.write(b"@MA")
ret = self._serial.read(1)
if ret != b"k":
self.send(b"@MA")
ret = self.receive()
if (ret != b"k") or (ret is None):
raise RuntimeError("Error while enabling ASCII mode")

logger.info("Filter Ack messages")
self._serial.write(b"@F2")
ret = self._serial.read(1)
if ret != b"k":
self.send(b"@F2")
ret = self.receive()
if (ret != b"k") or (ret is None):
raise RuntimeError("Error while setting filter")

@property
def serial(self):
""" Returns the pyserial.Serial instance """
return self._serial

def close(self):
""" Closes the connection to the serial port and ensure no pending
operatoin are left """
self._serial.write(b"@c")
self._serial.read()
self._serial.close()
self.send(b"@c")
self.receive()
self._socket.close()

def send(self, message):
if self.port != 0:
self._socket.sendto(message, (self.device, self.port))
else:
self._socket.write(message)

def receive(self):
ret = None
if self.port != 0:
try:
ret = self._socket.recvfrom(1024);
except socket.timeout:
self.logger.info("Socket Timeout")
return
else:
lenght = int(self._socket.read(), 16)
if lenght == b'k':
return lenght
else:
data = self._socket.read(lenght * 2)
return lenght + data
return ret[0]
1 change: 1 addition & 0 deletions scsgate/reactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ def stop(self):

def append_task(self, task):
""" Adds a tasks to the list of the jobs to execute """
self._logger.debug("scsgate.Reactor: add to queue")
self._request_queue.put(task)
23 changes: 13 additions & 10 deletions scsgate/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
scsgate.Reactor """

from scsgate.messages import compose_telegram, parse, StateMessage

import socket

class ExecutionError(BaseException):
""" Error raised when something goes wrong while executing a task """
Expand All @@ -26,11 +26,14 @@ def __init__(self, notification_endpoint):
self._last_raw_state_message = None

def execute(self, connection):
connection.serial.write(b"@r")
length = int(connection.serial.read(), 16)
connection.send(b"@r")
raw = connection.receive()
if (raw == b'k') or (raw is None):
return
length = int(raw[:1], 16)
if length == 0:
return
data = connection.serial.read(length * 2)
data = raw[1:]
message = parse(data)
# Filter duplicated state messages. The filtering feature
# of SCSGate is buggy and causes @r to always return 0 available
Expand Down Expand Up @@ -58,9 +61,9 @@ def execute(self, connection):
action=self._action,
target=self._target)

connection.serial.write(str.encode(command))
ret = connection.serial.read()
if ret != b'k':
connection.send(str.encode(command))
ret = connection.receive()
if (ret != b'k') or (ret is None):
raise ExecutionError(
"Error while setting status. Command {}, got {}".format(
command, ret))
Expand Down Expand Up @@ -143,9 +146,9 @@ def execute(self, connection):
b"00",
b"15",
b"00"])
connection.serial.write(command)
ret = connection.serial.read()
if ret != b'k':
connection.send(command)
ret = connection.receive()
if (ret != b'k') or (ret is None):
raise ExecutionError(
"Error while requesting status. Command {}, got {}".format(
command, ret))
Expand Down