Skip to content

Commit

Permalink
write commands back to serial
Browse files Browse the repository at this point in the history
  • Loading branch information
scottrfrancis committed Aug 18, 2020
1 parent d5bc534 commit 4b90dac
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
10 changes: 7 additions & 3 deletions Connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 1 addition & 4 deletions PentairProtocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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'),[
Expand Down
10 changes: 5 additions & 5 deletions SerialReader.py → SerialConnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = []
Expand Down
21 changes: 18 additions & 3 deletions pentair-control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -210,14 +223,16 @@ def update(self, commands):
'''
deltas = ObservableDeepArray()
commands = ObservableDeepArray()
commandStreams = ObservableArray()
commandStreams = ObservableString()

deltaCommandProcessor = DeltaCommandProcessor(commands, protocol)
deltas.addObserver(deltaCommandProcessor)

commandFramer = CommandFramer(commandStreams, protocol)
commands.addObserver(commandFramer)

outputWriter = OutputWriter(outputConnection)
commandStreams.addObserver(outputWriter)


try:
Expand Down

0 comments on commit 4b90dac

Please sign in to comment.