Skip to content

Commit

Permalink
Merge pull request #49 from openenergymonitor/emonhub_buffer
Browse files Browse the repository at this point in the history
Emonhub buffer interim fix
  • Loading branch information
TrystanLea authored Feb 17, 2018
2 parents 56a65ee + 32d4385 commit 195cc2f
Show file tree
Hide file tree
Showing 21 changed files with 845 additions and 471 deletions.
File renamed without changes.
68 changes: 14 additions & 54 deletions src/emonhub.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,63 +16,23 @@
import signal
import argparse
import pprint

try:
import pymodbus
pymodbus_found = True
except ImportError:
pymodbus_found = False

try:
import bluetooth
bluetooth_found = True
except ImportError:
bluetooth_found = False

import glob, os

import emonhub_setup as ehs
import interfacers.emonhub_interfacer as ehi
import emonhub_coder as ehc
import emonhub_interfacer as ehi
from interfacers import *

# this namespace and path
namespace = sys.modules[__name__]
path = os.path.dirname(__file__)

import interfacers.EmonHubSerialInterfacer
import interfacers.EmonHubJeeInterfacer
import interfacers.EmonHubSocketInterfacer
import interfacers.EmonHubPacketGenInterfacer
import interfacers.EmonHubMqttInterfacer
import interfacers.EmonHubTesterInterfacer
import interfacers.EmonHubEmoncmsHTTPInterfacer
import interfacers.EmonHubSmilicsInterfacer
import interfacers.EmonHubVEDirectInterfacer
import interfacers.EmonHubGraphiteInterfacer
import interfacers.EmonHubBMWInterfacer
import interfacers.EmonHubTx3eInterfacer

if bluetooth_found:
import interfacers.EmonHubSMASolarInterfacer

if pymodbus_found:
import interfacers.EmonModbusTcpInterfacer
import interfacers.EmonFroniusModbusTcpInterfacer

ehi.EmonHubSerialInterfacer = interfacers.EmonHubSerialInterfacer.EmonHubSerialInterfacer
ehi.EmonHubJeeInterfacer = interfacers.EmonHubJeeInterfacer.EmonHubJeeInterfacer
ehi.EmonHubSocketInterfacer = interfacers.EmonHubSocketInterfacer.EmonHubSocketInterfacer
ehi.EmonHubPacketGenInterfacer = interfacers.EmonHubPacketGenInterfacer.EmonHubPacketGenInterfacer
ehi.EmonHubMqttInterfacer = interfacers.EmonHubMqttInterfacer.EmonHubMqttInterfacer
ehi.EmonHubTesterInterfacer = interfacers.EmonHubTesterInterfacer.EmonHubTesterInterfacer
ehi.EmonHubEmoncmsHTTPInterfacer = interfacers.EmonHubEmoncmsHTTPInterfacer.EmonHubEmoncmsHTTPInterfacer
ehi.EmonHubSmilicsInterfacer = interfacers.EmonHubSmilicsInterfacer.EmonHubSmilicsInterfacer
ehi.EmonHubVEDirectInterfacer = interfacers.EmonHubVEDirectInterfacer.EmonHubVEDirectInterfacer
ehi.EmonHubGraphiteInterfacer = interfacers.EmonHubGraphiteInterfacer.EmonHubGraphiteInterfacer
ehi.EmonHubBMWInterfacer = interfacers.EmonHubBMWInterfacer.EmonHubBMWInterfacer
ehi.EmonHubTx3eInterfacer = interfacers.EmonHubTx3eInterfacer.EmonHubTx3eInterfacer

if bluetooth_found:
ehi.EmonHubSMASolarInterfacer = interfacers.EmonHubSMASolarInterfacer.EmonHubSMASolarInterfacer

if pymodbus_found:
ehi.EmonModbusTcpInterfacer = interfacers.EmonModbusTcpInterfacer.EmonModbusTcpInterfacer
ehi.EmonFroniusModbusTcpInterfacer = interfacers.EmonFroniusModbusTcpInterfacer.EmonFroniusModbusTcpInterfacer
# scan interfacers directory and import all interfacers
for f in glob.glob(path+"/interfacers/*.py"):
name = f.replace(".py","").replace(path+"/interfacers/","")
if name!="__init__":
# print "Loading: "+name
setattr(ehi,name,getattr(getattr(namespace,name),name))

"""class EmonHub
Expand All @@ -86,7 +46,7 @@

class EmonHub(object):

__version__ = "emonHub emon-pi variant v2.0.0"
__version__ = "emonHub emon-pi variant v2.1.0"

def __init__(self, setup):
"""Setup an OpenEnergyMonitor emonHub.
Expand Down
115 changes: 115 additions & 0 deletions src/emonhub_buffer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
"""
This code is released under the GNU Affero General Public License.
OpenEnergyMonitor project:
http://openenergymonitor.org
"""

import logging

"""class AbstractBuffer
Represents the actual buffer being used.
"""


class AbstractBuffer():

def storeItem(self, data):
raise NotImplementedError

def retrieveItems(self, number):
raise NotImplementedError

def retrieveItem(self):
raise NotImplementedError

def discardLastRetrievedItem(self):
raise NotImplementedError

def discardLastRetrievedItems(self, number):
raise NotImplementedError

def hasItems(self):
raise NotImplementedError

"""
This implementation of the AbstractBuffer just uses an in-memory data structure.
It's basically identical to the previous (inline) buffer.
"""


class InMemoryBuffer(AbstractBuffer):

def __init__(self, bufferName, buffer_size):
self._bufferName = str(bufferName)
self._buffer_type = "memory"
self._maximumEntriesInBuffer = int(buffer_size)
self._data_buffer = []
self._log = logging.getLogger("EmonHub")

def hasItems(self):
return self.size() > 0

def isFull(self):
return self.size() >= self._maximumEntriesInBuffer

def getMaxEntrySliceIndex(self):
return max(0,
self.size() - self._maximumEntriesInBuffer - 1)

def discardOldestItems(self):
self._data_buffer = self._data_buffer[self.getMaxEntrySliceIndex():]

def discardOldestItemsIfFull(self):
if self.isFull():
self._log.warning(
"In-memory buffer (%s) reached limit of %d items, deleting oldest"
% (self._bufferName, self._maximumEntriesInBuffer))
self.discardOldestItems()

def storeItem(self, data):
self.discardOldestItemsIfFull()
self._data_buffer.append(data)

def retrieveItem(self):
return self._data_buffer[0]

def retrieveItems(self, number):
blen = len(self._data_buffer)
if number > blen:
number = blen
return self._data_buffer[:number]

def discardLastRetrievedItem(self):
del self._data_buffer[0]

def discardLastRetrievedItems(self, number):
blen = len(self._data_buffer)
if number > blen:
number = blen
self._data_buffer = self._data_buffer[number:]

def size(self):
return len(self._data_buffer)


"""
The getBuffer function returns the buffer class corresponding to a
buffering method passed as argument.
"""
bufferMethodMap = {
'memory': InMemoryBuffer
}


def getBuffer(method):
"""Returns the buffer class corresponding to the method
method (string): buffering method
"""
return bufferMethodMap[method]

Loading

0 comments on commit 195cc2f

Please sign in to comment.