Source code for emod_controller_python_binding.emeter3_module

# Example of Python binding to a C library
#
# ctypes module is part of the standard library.
# With ctypes, you need to satisfy any compile time dependency on python, and your binding will work on any python that has ctypes, not just the one it was compiled against.
# It should be noted that ctypes only works with C functions.

# Binding
from ctypes import *
import ctypes
from enum import Enum

try:
    c_lib = ctypes.CDLL("/usr/lib/libemeter3_module.so")
except OSError:
    c_lib = None
    print("C Library not found.")

[docs]class PhaseEnergyParameters(ctypes.Structure): _fields_ = [ ("active_energy", c_float), ("aparent_energy", c_float), ("inductive_energy", c_float), ("capacitive_energy", c_float), ("exported_active_energy", c_float), ("exported_aparent_energy", c_float), ("exported_inductive_energy", c_float), ("exported_capacitive_energy", c_float), ("imported_active_energy", c_float), ("imported_aparent_energy", c_float), ("imported_inductive_energy", c_float), ("imported_capacitive_energy", c_float), ]
[docs]class PhasePowerParameters(ctypes.Structure): _fields_ = [ ("voltage", c_float), ("current", c_float), ("frequency", c_float), ("cosine", c_float), ("angle", c_float), ("active_power", c_float), ("aparent_power", c_float), ("reactive_power", c_float), ("exported_active_power", c_float), ("exported_aparent_power", c_float), ("exported_inductive_power", c_float), ("exported_capacitive_power", c_float), ("imported_active_power", c_float), ("imported_aparent_power", c_float), ("imported_inductive_power", c_float), ("imported_capacitive_power", c_float), ("maximeter", c_float), ]
EM3 = 10 EM3_250 = 14 EnergyMeter3ModuleCallback_Type = ctypes.CFUNCTYPE( None, ctypes.POINTER(ctypes.c_uint8), ctypes.c_uint16, ctypes.c_uint8, ctypes.POINTER(ctypes.c_void_p), )
[docs]class EnergyMeter3Module: def __init__(self): c_lib.EnergyMeter3Module_create.argtypes = None c_lib.EnergyMeter3Module_create.restype = ctypes.POINTER(ctypes.c_void_p) self.obj = c_lib.EnergyMeter3Module_create()
[docs] def init(self, em3_type): """ Initialize module. It is important to emphasize that init() method is mandatory to call it and must be called always after instantiating the module. It can be called several times, each of which the module is initialized again. Args: em3_type: it indicates if the 1A module or the 250mA module is used. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.EnergyMeter3Module_init.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_int, ] c_lib.EnergyMeter3Module_init.restype = ctypes.c_int32 return c_lib.EnergyMeter3Module_init(self.obj, ctypes.c_int(em3_type))
[docs] def init_with_callback(self, em3_type, callback): """ Initialize module. It is important to emphasize that this method is mandatory to call it. It can be called several times, each of which the module is initialized again. Args: em3_type: it indicates if the 1A module or the 250mA module is used. callback: function that will be called when a configured event occurs. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.EnergyMeter3Module_init.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_int, EnergyMeter3ModuleCallback_Type, ctypes.POINTER(ctypes.c_void_p), ] c_lib.EnergyMeter3Module_init.restype = ctypes.c_int ret = c_lib.EnergyMeter3Module_init( self.obj, ctypes.c_int(em3_type), callback, self.obj ) return ret
[docs] def init_v(self, callback, variant): """ Initialize module. It is important to emphasize that this method is mandatory to call it. It can be called several times, each of which the module is initialized again. Args: em3_type: it indicates if the 1A module or the 250mA module is used. callback: function that will be called when a configured event occurs. variant: identifies the module in case more than one module of the same type are present. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.EnergyMeter3Module_init_v.argtypes = [ ctypes.POINTER(ctypes.c_void_p), EnergyMeter3ModuleCallback_Type, ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint8, ] c_lib.EnergyMeter3Module_init_v.restype = ctypes.c_int32 return c_lib.EnergyMeter3Module_init_v( self.obj, callback, self.obj, ctypes.c_uint8(variant) )
[docs] def config_current_full_scale( self, full_current_L1, full_current_L2, full_current_L3 ): """ Configs the full scale amperage, 1:N ratio of the current transformer. Secondary full scale is 1 Amper. Args: full_current_L1: full scale or ratio of conversion of the transformer (clamp, tiroidal, ...) at L1. full_current_L2: full scale or ratio of conversion of the transformer (clamp, tiroidal, ...) at L2. full_current_L3: full scale or ratio of conversion of the transformer (clamp, tiroidal, ...) at L3. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.EnergyMeter3Module_configCurrentFullScale.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_float, ctypes.c_float, ctypes.c_float, ] c_lib.EnergyMeter3Module_configCurrentFullScale.restype = ctypes.c_int32 return c_lib.EnergyMeter3Module_configCurrentFullScale( self.obj, ctypes.c_float(full_current_L1), ctypes.c_float(full_current_L2), ctypes.c_float(full_current_L3), )
[docs] def config_voltage_full_scale( self, full_voltage_L1, full_voltage_L2, full_voltage_L3 ): """ Configs the full scale of the primary voltage of the transformer. If no transformer is present, this value should be the means voltage, ex. 230V. Args: full_voltage_L1: full scale of the transformer's primary at L1. full_voltage_L2: full scale of the transformer's primary at L2. full_voltage_L3: full scale of the transformer's primary at L3. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.EnergyMeter3Module_configVoltageFullScale.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_float, ctypes.c_float, ctypes.c_float, ] c_lib.EnergyMeter3Module_configVoltageFullScale.restype = ctypes.c_int32 return c_lib.EnergyMeter3Module_configVoltageFullScale( self.obj, ctypes.c_float(full_voltage_L1), ctypes.c_float(full_voltage_L2), ctypes.c_float(full_voltage_L3), )
[docs] def config_secondary_voltage_full_scale( self, full_voltage_L1, full_voltage_L2, full_voltage_L3 ): """ Configs the full scale of the secondary voltage of the transformer. In most scenarios this value is 230V. Args: full_voltage_L1: full scale of the transformer's secondary at L1. full_voltage_L2: full scale of the transformer's secondary at L2. full_voltage_L3: full scale of the transformer's secondary at L3. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.EnergyMeter3Module_configSecondaryVoltageFullScale.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_float, ctypes.c_float, ctypes.c_float, ] c_lib.EnergyMeter3Module_configSecondaryVoltageFullScale.restype = ( ctypes.c_int32 ) return c_lib.EnergyMeter3Module_configSecondaryVoltageFullScale( self.obj, ctypes.c_float(full_voltage_L1), ctypes.c_float(full_voltage_L2), ctypes.c_float(full_voltage_L3), )
[docs] def config_current_direction( self, current_direction_L1, current_direction_L2, current_direction_L3 ): """ Configs the current direction of each phase. The current direction will be reversed if it is set to true. Args: current_direction_L1: current direction at L1. current_direction_L2: current direction at L2. current_direction_L3: current direction at L3. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.EnergyMeter3Module_configCurrentDirection.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint8, ctypes.c_uint8, ctypes.c_uint8, ] c_lib.EnergyMeter3Module_configCurrentDirection.restype = ctypes.c_int32 return c_lib.EnergyMeter3Module_configCurrentDirection( self.obj, ctypes.c_uint8(current_direction_L1), ctypes.c_uint8(current_direction_L2), ctypes.c_uint8(current_direction_L3), )
[docs] def config_work_mode(self, mode): """ Configs the work mode as explained in SEM Three manual. Args: mode: Mode 0 (All L1, L2 & L3 are single phased), Mode 1 (L2 & L3 are single phased, L1 isequilibrated three phased), Mode 2 (L3 is single phased, L1 & L2 are equilibrated three phased), Mode 3 (All L1, L2 & L3 are equilibrated three phased). Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.EnergyMeter3Module_configWorkMode.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_int16, ] c_lib.EnergyMeter3Module_configWorkMode.restype = ctypes.c_int32 return c_lib.EnergyMeter3Module_configWorkMode(self.obj, ctypes.c_int16(mode))
[docs] def config_metering_standard(self, standard): """ Configs the metering standard. Args: standard: 0 (Circutor), 1 (IEC62053-23), 2 (IEEE 1459-2000). Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.EnergyMeter3Module_configMeteringStandard.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_int16, ] c_lib.EnergyMeter3Module_configMeteringStandard.restype = ctypes.c_int32 return c_lib.EnergyMeter3Module_configMeteringStandard( self.obj, ctypes.c_int16(standard) )
[docs] def reset_all_energy_meter(self): """ Resets to 0 all energy meters.. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.EnergyMeter3Module_resetAllEnergyMeter.argtypes = [ ctypes.POINTER(ctypes.c_void_p) ] c_lib.EnergyMeter3Module_resetAllEnergyMeter.restype = ctypes.c_int32 return c_lib.EnergyMeter3Module_resetAllEnergyMeter(self.obj)
[docs] def get_current_full_scale(self): """ Gets the full scale amperage, 1:N ratio of the current transformer.. Returns: It returns the full scale amperage for each phase [L1, L2, L3]. """ c_lib.EnergyMeter3Module_getCurrentFullScale.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ] c_lib.EnergyMeter3Module_getCurrentFullScale.restype = ctypes.c_int32 full_current_L1 = ctypes.c_float(0) full_current_L2 = ctypes.c_float(0) full_current_L3 = ctypes.c_float(0) ret = c_lib.EnergyMeter3Module_getCurrentFullScale( self.obj, ctypes.byref(full_current_L1), ctypes.byref(full_current_L2), ctypes.byref(full_current_L3), ) return full_current_L1.value, full_current_L2.value, full_current_L3.value
[docs] def get_voltage_full_scale(self): """ Gets the full scale of the primary voltage of the transformer. Returns: It returns the full scale of the primary voltage [L1, L2, L3]. """ c_lib.EnergyMeter3Module_getVoltageFullScale.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ] c_lib.EnergyMeter3Module_getVoltageFullScale.restype = ctypes.c_int32 full_voltage_L1 = ctypes.c_float(0) full_voltage_L2 = ctypes.c_float(0) full_voltage_L3 = ctypes.c_float(0) ret = c_lib.EnergyMeter3Module_getVoltageFullScale( self.obj, ctypes.byref(full_voltage_L1), ctypes.byref(full_voltage_L2), ctypes.byref(full_voltage_L3), ) return full_voltage_L1.value, full_voltage_L2.value, full_voltage_L3.value
[docs] def get_secondary_voltage_full_scale(self): """ Gets the full scale of the secondary voltage of the transformer. Returns: It returns the full scale of the secondary voltage [L1, L2, L3]. """ c_lib.EnergyMeter3Module_getSecondaryVoltageFullScale.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ] c_lib.EnergyMeter3Module_getSecondaryVoltageFullScale.restype = ctypes.c_int32 full_voltage_L1 = ctypes.c_float(0) full_voltage_L2 = ctypes.c_float(0) full_voltage_L3 = ctypes.c_float(0) ret = c_lib.EnergyMeter3Module_getSecondaryVoltageFullScale( self.obj, ctypes.byref(full_voltage_L1), ctypes.byref(full_voltage_L2), ctypes.byref(full_voltage_L3), ) return full_voltage_L1.value, full_voltage_L2.value, full_voltage_L3.value
[docs] def get_current_direction(self): """ Gets the current direction of each phase. The current direction is reversed if set to true.. Returns: It returns the current direction of each phase [L1,L2,L3]. """ c_lib.EnergyMeter3Module_getCurrentDirection.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_uint8), ctypes.POINTER(ctypes.c_uint8), ctypes.POINTER(ctypes.c_uint8), ] c_lib.EnergyMeter3Module_getCurrentDirection.restype = ctypes.c_int32 current_direction_L1 = ctypes.c_uint8(0) current_direction_L2 = ctypes.c_uint8(0) current_direction_L3 = ctypes.c_uint8(0) ret = c_lib.EnergyMeter3Module_getCurrentDirection( self.obj, ctypes.byref(current_direction_L1), ctypes.byref(current_direction_L2), ctypes.byref(current_direction_L3), ) return ( current_direction_L1.value, current_direction_L2.value, current_direction_L3.value, )
[docs] def get_work_mode(self): """ Gets the work mode as explained in SEM Three manual. Returns: It returns the work mode configured. """ c_lib.EnergyMeter3Module_getWorkMode.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_uint16), ] c_lib.EnergyMeter3Module_getWorkMode.restype = ctypes.c_int32 mode = ctypes.c_uint16(0) ret = c_lib.EnergyMeter3Module_getWorkMode(self.obj, ctypes.byref(mode)) return mode.value
[docs] def get_metering_standard(self): """ Gets the metering standard. Returns: It returns the metering standard configured. """ c_lib.EnergyMeter3Module_getMeteringStandard.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_uint16), ] c_lib.EnergyMeter3Module_getMeteringStandard.restype = ctypes.c_int32 standard = ctypes.c_uint16(0) ret = c_lib.EnergyMeter3Module_getMeteringStandard( self.obj, ctypes.byref(standard) ) return standard.value
[docs] def get_power_parameters(self, phase): """ Gets all the power parameters for a given phase. Args: phase: integer corresponding to phases L1 L2 L3. Returns: It returns a structured list of parameters. """ c_lib.EnergyMeter3Module_getPowerParameters.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_int32, ctypes.POINTER(PhasePowerParameters), ] c_lib.EnergyMeter3Module_getPowerParameters.restype = ctypes.c_int32 power_params = PhasePowerParameters() ret = c_lib.EnergyMeter3Module_getPowerParameters( self.obj, ctypes.c_int32(phase), ctypes.byref(power_params) ) return power_params
[docs] def get_energy_parameters(self, phase): """ Gets all the energy parameters for a given phase. Args: phase: integer corresponding to phases L1 L2 L3. Returns: It returns a structured list of parameters. """ c_lib.EnergyMeter3Module_getEnergyParameters.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_int32, ctypes.POINTER(PhaseEnergyParameters), ] c_lib.EnergyMeter3Module_getEnergyParameters.restype = ctypes.c_int32 energy_params = PhaseEnergyParameters() ret = c_lib.EnergyMeter3Module_getEnergyParameters( self.obj, ctypes.c_int32(phase), ctypes.byref(energy_params) ) return energy_params
[docs] def get_energy_parameters_combined(self): """ Gets all the power parameters for the combination of the three phases. Returns: It returns a structured list of parameters. """ c_lib.EnergyMeter3Module_getEnergyParameters_L123Combined.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(PhaseEnergyParameters), ] c_lib.EnergyMeter3Module_getEnergyParameters_L123Combined.restype = ( ctypes.c_int32 ) energy_params = PhaseEnergyParameters() ret = c_lib.EnergyMeter3Module_getEnergyParameters_L123Combined( self.obj, ctypes.byref(energy_params) ) return energy_params
[docs] def config_event_at_time_interval(self, time_interval): """ Configures module for calling init callback_func at specified period. Args: time_interval: period in milliseconds. Minimum value is 500ms. event_mask: bit mask representing inputs that must trigger the event. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.EnergyMeter3Module_configEventAtTimeInterval.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint32, ] c_lib.EnergyMeter3Module_configEventAtTimeInterval.restype = ctypes.c_int32 return c_lib.EnergyMeter3Module_configEventAtTimeInterval( self.obj, ctypes.c_uint32(time_interval) )
[docs] def reset_event_config(self): """ Resets all previously configured events. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.EnergyMeter3Module_resetEventConfig.argtypes = [ ctypes.POINTER(ctypes.c_void_p) ] c_lib.EnergyMeter3Module_resetEventConfig.restype = ( ctypes.c_int32 ) return c_lib.EnergyMeter3Module_resetEventConfig(self.obj)