def addFlowmeter(self, number): outputNumber = 1 if (number % 2) else 0 fm = flowmeter.Flowmeter("flow" + str(outputNumber), "keg" + str(number)) self.flowmeters.append(fm) config.printMessage("> Flowmeter '" + fm.name + "' (on device '" + self.device + "') added for '" + fm.kegId + "'")
def process(self): config.printMessage("> Processing Message ID of " + str(self.messageId.encode('hex'))) if (self.messageId.encode('hex') == "0010"): self.processMeterReading() elif (self.messageId.encode('hex') == "0001"): self.processHelloMessage() else: config.printMessage( "Error: Not able to process unrecognized message_id") self.processFooter()
def __init__(self, device, baudrate): config.printMessage("############################") config.printMessage("# INITIALIZING SERIAL PORT #") config.printMessage("############################") self.device = device self.baudrate = baudrate self.initialize() config.printMessage("> Finished Initializing Serial Port\n")
def __init__(self, serial, messageId, payloadLength): config.printMessage("############################################") config.printMessage("# BEGIN PROCESSING KEGOMATE SERIAL MESSAGE #") config.printMessage("############################################") self.serial = serial self.messageId = messageId self.payloadLength = payloadLength self.process() config.printMessage("> Finished Processing Message\n")
def initializeSerialPorts(): global mainLoopFlag global serialPorts global flowmeterCounter # Logic to initialize the Serial Ports if (hasattr(config, 'seriaLBaudRate') == False or hasattr(config, 'serialDevices') == False or len(config.serialDevices) == 0): config.printMessage("ERROR: Couldn't Initialize Serial Ports") mainLoopFlag = False else: # Only initialize if the Congifurations appear in order for device in config.serialDevices: sp = serialPort.SerialPort(device, config.seriaLBaudRate) sp.addFlowmeter(flowmeterCounter) # Adding flow0 flowmeterCounter += 1 sp.addFlowmeter(flowmeterCounter) # Adding flow1 flowmeterCounter += 1 serialPorts.append(sp)
def processMeterReading(self): config.printMessage("> Identified as a Meter Reading Message") self.values.append(self.processTypeLengthValue()) self.values.append(self.processTypeLengthValue()) for i in self.values: if (i['tag'].encode('hex') == "01"): config.printMessage("> Flow Meter Name: " + i['value']) elif (i['tag'].encode('hex') == "02"): rawValue = i['value'].encode('hex') meterReading = rawValue[2] + rawValue[3] + rawValue[ 0] + rawValue[1] config.printMessage("> Flow Meter Reading: " + str(int(meterReading, 16))) else: config.printMessage("Error: Unknown tag value")
import config import struct import ksm import serialPort import serial import requests import sys mainLoopFlag = True serialPorts = [] flowmeterCounter = 0 # The OPTIONAL second command line attribute would be the serial port if (len(sys.argv) > 1): config.printMessage("> Serial port '" + sys.argv[1] + "' passed from command line") config.serialDevices[0] = sys.argv[1] else: config.printMessage( "> No Serial port passed from command line (Defaulting to '" + config.serialDevices[0] + "')") # The OPTIONAL third command line attribute would be the starting keg number (requires serial port passed in as well) if (len(sys.argv) > 2): config.printMessage("> Starting Keg Number of '" + sys.argv[2] + "' passed from command line") flowmeterCounter = int(sys.argv[2]) else: config.printMessage( "> No Starting Keg Number passed from command line (Defaulting to '" + str(flowmeterCounter) + "')")
def processFooter(self): self.crc = self.serial.read(2) self.trailer = self.serial.read(2) config.printMessage("> CRC is " + self.crc.encode('hex'))
def processHelloMessage(self): config.printMessage("> Identified as a Hello Reading Message") self.values.append(self.processTypeLengthValue()) self.values.append(self.processTypeLengthValue()) self.values.append(self.processTypeLengthValue()) for i in self.values: if (i['tag'].encode('hex') == "01"): rawValue = i['value'].encode('hex') firmwareVersion = rawValue[0] + rawValue[1] config.printMessage("> Firmware Version: " + str(int(firmwareVersion, 16))) elif (i['tag'].encode('hex') == "02"): rawValue = i['value'].encode('hex') protocolVersion = rawValue[2] + rawValue[3] + rawValue[ 0] + rawValue[1] config.printMessage("> Protocal Version: " + str(int(protocolVersion, 16))) elif (i['tag'].encode('hex') == "03"): config.printMessage("> Board Serial Number: " + i['value']) elif (i['tag'].encode('hex') == "04"): rawValue = i['value'].encode('hex') millis = rawValue[2] + rawValue[3] + rawValue[0] + rawValue[1] config.printMessage("> Uptime in Milliseconds: " + str(int(millis, 16))) elif (i['tag'].encode('hex') == "05"): rawValue = i['value'].encode('hex') days = rawValue[2] + rawValue[3] + rawValue[0] + rawValue[1] config.printMessage("> Uptime in Milliseconds: " + str(int(days, 16))) else: config.printMessage("Error: Unknown tag value")
def initialize(self): config.printMessage("> Initializing Serial Port at '" + self.device + "' and at " + str(self.baudrate) + " baudrate") self.ser = serial.Serial(self.device, self.baudrate, serial.EIGHTBITS, serial.PARITY_NONE) self.ser.flushInput()