Community
OpenEnergyMonitor

Community

Emonhub save to usb csv

emonhub
Tags: #<Tag:0x00007f13e6dcb788>

(Dafydd Hirst) #1

On the old version of emonhub I was able log data to a usb stick thanks to
https://openenergymonitor.org/forum-archive/node/11087.html

ive sofar manages to hack away and create a file called EmonHubUSBInterfacer on the new emon hub and am trying to hack it to save to a usb disk

I have managed to get the interfacer to start now with the code below but am not getting any data.

any help appreciated

import time, json, Cargo, csv
from emonhub_interfacer import EmonHubInterfacer

“”"class EmonHubUSBInterfacer

Template interfacer for use in development

“”"

class EmonHubUSBInterfacer(EmonHubInterfacer):

def __init__(self, name, port_nb=50011):
    """Initialize Interfacer
    
    """

    # Initialization
    super(EmonHubUSBInterfacer, self).__init__(name)

    # add or alter any default settings for this interfacer
    # defaults previously defined in inherited emonhub_interfacer
    # here we are just changing the batchsize from 1 to 100
    # and the interval from 0 to 30
    # self._defaults.update({'batchsize': 100,'interval': 30})
    
    # This line will stop the default values printing to logfile at start-up
    self._settings.update(self._defaults)

    # Interfacer specific settings
    # (settings not included in the inherited EmonHubInterfacer)
    # The set method below is called from emonhub.py on
    # initialisation and settings change and copies the 
    # interfacer specific settings over to _settings
    
    # read_interval is just an example setting here
    # and can be removed and replaced with applicable settings 
    self._template_settings = {'read_interval':10.0}
    
    # set an absolute upper limit for number of items to process per post
    self._item_limit = 250
self._settings.update({'file': ''})
self._item_limit = 250



def _process_post(self, databuffer):
   """Save data in csv file."""
    
   try:
       # f = open('/mnt/usbstorage/data.csv', 'a+')  #open file for appending

   sysname = socket.gethostname()
   writepath = '/home/pi/'
   datepath = datetime.date.today().strftime("%d-%m-%y")
   filedev = '_'
   fileextention = '.csv'
   mode = 'a' if os.path.exists(writepath + sysname + filedev + datepath + fileextention) else 'w'
   f =  open(writepath + sysname + filedev + datepath + fileextention, mode)	


       writer = csv.writer(f)
       writer.writerows( databuffer )
       return True
   except IOError,v:
       self._log.warning("IO error upon writing to USB drive: "+self._settings['file']+'error code'+str(v[0])+str(v[1]))
   finally:
       try:
           f.close()
       except:
           pass

        








def set(self, **kwargs):
    """

    """

    for key, setting in self._template_settings.iteritems():
        # Decide which setting value to use
        if key in kwargs.keys():
            setting = kwargs[key]
        else:
            setting = self._template_settings[key]
        if key in self._settings and self._settings[key] == setting:
            continue
        elif key == 'read_interval':
            self._log.info("Setting " + self.name + " read_interval: " + str(setting))
            self._settings[key] = float(setting)
            continue
        else:
            self._log.warning("'%s' is not valid for %s: %s" % (str(setting), self.name, key))

    # include kwargs from parent
    super(EmonHubUSBInterfacer, self).set(**kwargs)