Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SEBv15 committed Jul 31, 2020
1 parent 8b17e85 commit f05b90a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 10 additions & 4 deletions pycertifspec/Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .SpecSocket import SpecSocket, SpecMessage
from .SpecError import SpecError
from typing import Callable, List
import traceback

class Client:
def __init__(self, host="localhost", port=None, port_range=(6510, 6530), ports=[], timeout=0.1, log_messages=False):
Expand Down Expand Up @@ -50,6 +51,9 @@ def __init__(self, host="localhost", port=None, port_range=(6510, 6530), ports=[
self._last_console_print = ""
self._console_print_lines = []

self.counter_names = collections.OrderedDict()
self._get_counter_names()

self.subscribe("error", None, nowait=True)
self.subscribe("output/tty", self._console_listener)

Expand Down Expand Up @@ -88,6 +92,7 @@ def _send(self, command:str, data_type:int=0, property_name:str="", body:bytes=b
Returns:
(SpecMessage): Reply from SPEC if it occurred within wait_for_response seconds
"""
#print(traceback.print_stack())
with self._send_lock:
self._sn_counter = self._sn_counter + 1
if callback is not None:
Expand All @@ -105,7 +110,7 @@ def _send(self, command:str, data_type:int=0, property_name:str="", body:bytes=b
del self._reply_events[self._sn_counter]


def subscribe(self, prop:str, callback:Callable[[SpecMessage], None], nowait:bool=False, timeout:float=0.1) -> bool:
def subscribe(self, prop:str, callback:Callable[[SpecMessage], None], nowait:bool=False, timeout:float=0.2) -> bool:
"""
Subscribe to changes in a property.
Expand Down Expand Up @@ -200,7 +205,7 @@ def res_cb(msg):
res["val"] = msg
res["event"].set()

self._send(event, property_name=console_command, callback=res_cb)
self._send(event, body=console_command.encode("ascii"), callback=res_cb)

if blocking:
res["event"].wait()
Expand All @@ -223,17 +228,18 @@ def set(self, prop, value, wait_for_error=0.1):
if prop in self._watch_values.keys():
self._watch_values[prop]["body"] = value.encode("ascii")

def get(self, prop):
def get(self, prop, force_fetch=False):
"""
Get a property.
Attributes:
prop_name (string): The name of the property
force_fetch (boolean): If the property is watched, force fetch the value from SPEC
Returns:
None if property doesn't exist
"""
if prop in self._watch_values.keys():
if (not force_fetch) and prop in self._watch_values.keys():
return self._watch_values[prop]
return self._send(EventTypes.SV_CHAN_READ, DataTypes.SV_STRING, property_name=prop, wait_for_response=0.5)

Expand Down
4 changes: 2 additions & 2 deletions pycertifspec/SpecSocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SpecSocket(socket.socket):
def __init__(self, *args, **kwargs):
super(SpecSocket, self).__init__(*args, **kwargs)

def connect_spec(self, host, port=None, port_range=(6510, 6530), ports=[], timeout=0.1):
def connect_spec(self, host, port=None, port_range=(6510, 6530), ports=[], timeout=0.5):
"""
Scan ports for a SPEC server and connect when found. If port is already known .connect() can be used instead
Expand Down Expand Up @@ -101,7 +101,7 @@ def recv_spec(self):
magic, vers, size = struct.unpack("IiI", head1)

if magic != self.SV_SPEC_MAGIC:
raise ValueError("Response didn't contain the correct SPEC magic number")
raise ValueError("Response didn't contain the correct SPEC magic number. Was {}, expected {}".format(magic, self.SV_SPEC_MAGIC))

if vers < 4:
raise Exception("Server respondend with protocol version {}. Need at least 4".format(vers))
Expand Down

0 comments on commit f05b90a

Please sign in to comment.