From 4b90dac6b9264f950f9b624f353f6c2605c665d6 Mon Sep 17 00:00:00 2001 From: scott francis Date: Tue, 18 Aug 2020 13:19:05 -0700 Subject: [PATCH] write commands back to serial --- Connection.py | 10 +++++++--- PentairProtocol.py | 5 +---- SerialReader.py => SerialConnection.py | 10 +++++----- pentair-control.py | 21 ++++++++++++++++++--- 4 files changed, 31 insertions(+), 15 deletions(-) rename SerialReader.py => SerialConnection.py (81%) diff --git a/Connection.py b/Connection.py index 9e24fdd..5a69884 100644 --- a/Connection.py +++ b/Connection.py @@ -10,10 +10,14 @@ def __init__(self): # self.open() def open(self): - print('YOU MUST OVERRIDE THIS METHOD') + pass def isOpen(self): - print('YOU MUST OVERRIDE THIS METHOD') + pass def listen(self): - print('YOU MUST OVERRIDE THIS METHOD') + pass + +class IOConnection(Connection): + def send(self, message): + pass diff --git a/PentairProtocol.py b/PentairProtocol.py index cf18108..568af81 100644 --- a/PentairProtocol.py +++ b/PentairProtocol.py @@ -445,7 +445,6 @@ def parseFrame(self, f): # commands are dicts with fields separated out -- just as if parsed def createCommand(self, desiredState): cmd = {} - print("creating Command for " + json.dumps(desiredState)) try: k = list(desiredState.keys())[0] @@ -456,12 +455,10 @@ def createCommand(self, desiredState): except Exception as e: pass - return cmd def createFrame(self, command): - frame = self.START_BYTE - print("creating frame from " ) #+ json.dumps(command)) + frame = self.RECORD_SEPARATOR + self.START_BYTE + self.RECORD_SEPARATOR try: frame = b''.join(list(map( lambda x: x.to_bytes(1, 'big'),[ diff --git a/SerialReader.py b/SerialConnection.py similarity index 81% rename from SerialReader.py rename to SerialConnection.py index a0339cf..ce0ca96 100644 --- a/SerialReader.py +++ b/SerialConnection.py @@ -2,10 +2,10 @@ # For details on pentair protocol # import serial -from Connection import Connection +from Connection import Connection, IOConnection -class SerialReader(Connection): +class SerialConnection(IOConnection): def __init__(self, device): super().__init__() self.device = device @@ -23,9 +23,9 @@ def open(self): def isOpen(self): return self.ser.isOpen() - def send(self, commands): - self.ser.write((self.RECORD_SEPARATOR.join( - commands) + self.RECORD_SEPARATOR).encode()) + def send(self, message): + count = self.ser.write(message) + return count def listen(self): # events = [] diff --git a/pentair-control.py b/pentair-control.py index 24c9eb6..c6acf21 100755 --- a/pentair-control.py +++ b/pentair-control.py @@ -5,7 +5,7 @@ from GreengrassAwareConnection import * from Observer import * from PentairProtocol import PentairProtocol -from SerialReader import SerialReader +from SerialConnection import SerialConnection import argparse from datetime import datetime @@ -105,6 +105,17 @@ def update(self, commands): if len(c) > 0: self.frames.append(self.protocol.createFrame(c)) +class OutputWriter(Observer): + def __init__(self, connection): + super().__init__() + self.connection = connection + + def update(self, messages): + if len(messages) > 0: + self.connection.send(messages) + # force a state update -- needs a refactor + streamData.append(connection.listen()) + # Configure logging logger = logging.getLogger("Pentair-Thing.core") @@ -165,13 +176,15 @@ def update(self, commands): +outputConnection = None if len(inFile) > 0: print(f'using {inFile} as source') connection = FileReader(inFile) else: port = args.port print(f'using {port} as source') - connection = SerialReader(port) + connection = SerialConnection(port) + outputConnection = connection timeout = float(args.timeout) # connection will read from either sourcse @@ -210,7 +223,7 @@ def update(self, commands): ''' deltas = ObservableDeepArray() commands = ObservableDeepArray() -commandStreams = ObservableArray() +commandStreams = ObservableString() deltaCommandProcessor = DeltaCommandProcessor(commands, protocol) deltas.addObserver(deltaCommandProcessor) @@ -218,6 +231,8 @@ def update(self, commands): commandFramer = CommandFramer(commandStreams, protocol) commands.addObserver(commandFramer) +outputWriter = OutputWriter(outputConnection) +commandStreams.addObserver(outputWriter) try: