Skip to content
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

Feature/messagequeue #72

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
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
14 changes: 10 additions & 4 deletions BrewPiUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
import os
import serial
import autoSerial
import Queue

try:
import configobj
except ImportError:
print("BrewPi requires ConfigObj to run, please install it with 'sudo apt-get install python-configobj")
sys.exit(1)

# Message queue
messageQueue = Queue.Queue()

def addSlash(path):
"""
Expand Down Expand Up @@ -83,12 +86,15 @@ def configSet(configFile, settingName, value):
def printStdErr(*objs):
print("", *objs, file=sys.stderr)

def logMessage(message):
"""
Prints a timestamped message to stderr
"""
def logMessage(message, messageType="error"):
printStdErr(time.strftime("%b %d %Y %H:%M:%S ") + message)
messageQueue.put({'messageType': messageType, 'message': message})

def getLogMessages():
result_list = []
while not messageQueue.empty():
result_list.append(messageQueue.get())
return result_list

def scriptPath():
"""
Expand Down
26 changes: 16 additions & 10 deletions brewpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from BrewPiUtil import printStdErr
from BrewPiUtil import logMessage
from BrewPiUtil import getLogMessages

# Check needed software dependencies to nudge users to fix their setup
if sys.version_info < (2, 7):
Expand Down Expand Up @@ -440,6 +441,9 @@ def renameTempKey(key):
"t": "Time"}
return rename.get(key, key)

def sendData(data):
conn.send(json.dumps({'response': data, 'messages': getLogMessages()}))

while run:
if config['dataLogging'] == 'active':
# Check whether it is a new day
Expand All @@ -466,24 +470,26 @@ def renameTempKey(key):
if messageType == "ack": # acknowledge request
conn.send('ack')
elif messageType == "lcd": # lcd contents requested
conn.send(json.dumps(lcdText))
sendData((lcdText))
elif messageType == "getMode": # echo cs['mode'] setting
conn.send(cs['mode'])
elif messageType == "getFridge": # echo fridge temperature setting
conn.send(json.dumps(cs['fridgeSet']))
sendData(cs['fridgeSet'])
elif messageType == "getBeer": # echo fridge temperature setting
conn.send(json.dumps(cs['beerSet']))
sendData(cs['beerSet'])
elif messageType == "getControlConstants":
conn.send(json.dumps(cc))
sendData(cc)
elif messageType == "getControlSettings":
if cs['mode'] == "p":
profileFile = util.addSlash(util.scriptPath()) + 'settings/tempProfile.csv'
with file(profileFile, 'r') as prof:
cs['profile'] = prof.readline().split(",")[-1].rstrip("\n")
cs['dataLogging'] = config['dataLogging']
conn.send(json.dumps(cs))
sendData(cs)
elif messageType == "getControlVariables":
conn.send(cv)
elif messageType == "getMessages":
sendData({'messages': getLogMessages()})
elif messageType == "refreshControlConstants":
bg_ser.write("c")
raise socket.timeout
Expand Down Expand Up @@ -580,16 +586,16 @@ def renameTempKey(key):
elif messageType == "startNewBrew": # new beer name
newName = value
result = startNewBrew(newName)
conn.send(json.dumps(result))
sendData(result)
elif messageType == "pauseLogging":
result = pauseLogging()
conn.send(json.dumps(result))
sendData(result)
elif messageType == "stopLogging":
result = stopLogging()
conn.send(json.dumps(result))
sendData(result)
elif messageType == "resumeLogging":
result = resumeLogging()
conn.send(json.dumps(result))
sendData(result)
elif messageType == "dateTimeFormatDisplay":
config = util.configSet(configFile, 'dateTimeFormatDisplay', value)
changeWwwSetting('dateTimeFormatDisplay', value)
Expand Down Expand Up @@ -660,7 +666,7 @@ def renameTempKey(key):
shield=hwVersion.shield,
deviceList=deviceList,
pinList=pinList.getPinList(hwVersion.board, hwVersion.shield))
conn.send(json.dumps(response))
sendData(response)
else:
conn.send("device-list-not-up-to-date")
elif messageType == "applyDevice":
Expand Down