Example #1
0
def append_serial_slave(proto, unit, listen):
    try:
        try:
            port, baudrate, bytesize, parity, stopbits = listen.split(':')
        except:
            port = listen
            baudrate = 9600
            bytesize = 8
            parity = 'N'
            stopbits = 1
        a = safe_int(unit)
        baudrate = int(baudrate)
        bytesize = int(bytesize)
        stopbits = int(stopbits)
        try:
            port = int(port)
        except:
            pass
        if proto not in ['rtu', 'ascii', 'binary']:
            raise Exception(f'Invalid Modbus slave framer: {proto}')
        config.slave['serial'].append({
            'a': a,
            'p': port,
            'f': proto,
            'b': baudrate,
            'bs': bytesize,
            'pt': parity,
            's': stopbits
        })
        logging.debug('modbus.slave.serial = {}.{}:{}:{}:{}:{}:{}'.format(
            hex(a), proto, port, baudrate, bytesize, parity, stopbits))
    except:
        eva.core.log_traceback()
Example #2
0
def append_serial_slave(c):
    try:
        a, port = c.split(',')
        framer, port, baudrate, bytesize, parity, stopbits = port.split(':')
        a = safe_int(a)
        baudrate = int(baudrate)
        bytesize = int(bytesize)
        stopbits = int(stopbits)
        try:
            port = int(port)
        except:
            pass
        if framer not in ['rtu', 'ascii', 'binary']:
            raise Exception('Invalid Modbus slave framer: {}'.format(framer))
        config.slave['serial'].append({
            'a': a,
            'p': port,
            'f': framer,
            'b': baudrate,
            'bs': bytesize,
            'pt': parity,
            's': stopbits
        })
        logging.debug('modbus.slave.serial = {}.{}:{}:{}:{}:{}:{}'.format(
            hex(a), framer, port, baudrate, bytesize, parity, stopbits))
    except:
        eva.core.log_traceback()
Example #3
0
def append_ip_slave(c, proto):
    try:
        a, h = c.split(',')
        host, port = parse_host_port(h, 502)
        a = safe_int(a)
        if not host: raise Exception
        config.slave[proto].append({'a': a, 'h': host, 'p': port})
        logging.debug('modbus.slave.{} = {}.{}:{}'.format(
            proto, hex(a), host, port))
    except:
        eva.core.log_traceback()
Example #4
0
def append_ip_slave(proto, unit, listen):
    try:
        host, port = parse_host_port(listen, 502)
        a = safe_int(unit)
        if not host:
            raise Exception
        config.slave[proto].append({'a': a, 'h': host, 'p': port})
        logging.debug('modbus.slave.{} = {}.{}:{}'.format(
            proto, hex(a), host, port))
    except:
        eva.core.log_traceback()
Example #5
0
 def __init__(self, **kwargs):
     self.modbus_port = self.phi_cfg.get('port')
     if not modbus.is_port(self.modbus_port):
         self.log_error('modbus port ID not specified or invalid')
         self.ready = False
         return
     try:
         self.addr = safe_int(self.phi_cfg.get('addr'))
     except:
         self.log_error('modbus addr not specified or invalid')
         self.ready = False
         return
Example #6
0
 def registerEventHandler(self, addr, f):
     if not self.event_handlers_lock.acquire(
             timeout=eva.core.config.timeout):
         logging.critical(
             'WatchBlock::registerEventHandler locking broken')
         eva.core.critical()
         return False
     try:
         self.event_handlers.setdefault(safe_int(addr), set()).add(f)
     except:
         logging.error(
             'Unable to register modbus handler for address {}'.format(
                 addr))
         eva.core.log_traceback()
     finally:
         self.event_handlers_lock.release()
Example #7
0
 def unregisterEventHandler(self, addr, f):
     if not self.event_handlers_lock.acquire(
             timeout=eva.core.config.timeout):
         logging.critical(
             'WatchBlock::unregisterEventHandler locking broken')
         eva.core.critical()
         return False
     try:
         a = safe_int(addr)
         if a in self.event_handlers:
             try:
                 self.event_handlers[a].remove(f)
             except KeyError:
                 pass
             except:
                 logging.error(
                     'Unable to register modbus handler for address {}'.
                     format(addr))
                 eva.core.log_traceback()
     finally:
         self.event_handlers_lock.release()
Example #8
0
File: unit.py Project: alttch/eva3
 def set_prop(self, prop, val=None, save=False):
     if prop == 'action_exec':
         if self.action_exec != val:
             if val and val[0] == '|':
                 d = eva.uc.driverapi.get_driver(val[1:])
                 if not d:
                     logging.error(
                         'Can not set ' + \
                             '%s.action_exec = %s, no such driver'
                             % (self.oid, val))
                     return False
             self.action_exec = val
             self.log_set(prop, val)
             self.set_modified(save)
         return True
     elif prop == 'action_driver_config':
         if val is None:
             self.action_driver_config = None
             self.log_set(prop, None)
             self.set_modified(save)
             return True
         else:
             try:
                 v = dict_from_str(val)
             except:
                 eva.core.log_traceback()
                 return False
             self.action_driver_config = v
             self.log_set(prop, 'dict')
             self.set_modified(save)
             return True
     elif prop == 'modbus_status':
         if self.modbus_status == val:
             return True
         if val is None:
             self.unregister_modbus_status_updates()
             self.modbus_status = None
         else:
             if val[0] not in ['h', 'c']:
                 return False
             try:
                 addr = safe_int(val[1:])
                 if addr > eva.uc.modbus.slave_reg_max or addr < 0:
                     return False
             except:
                 return False
             self.unregister_modbus_status_updates()
             self.modbus_status = val
             self.modbus_update_status(addr,
                                       eva.uc.modbus.get_data(addr, val[0]))
             self.register_modbus_status_updates()
         self.log_set('modbus_status', val)
         self.set_modified(save)
         return True
     elif prop == 'action_always_exec':
         v = val_to_boolean(val)
         if v is not None:
             if self.action_always_exec != v:
                 self.action_always_exec = v
                 self.log_set(prop, v)
                 self.set_modified(save)
             return True
         else:
             return False
     elif prop == 'update_exec_after_action':
         v = val_to_boolean(val)
         if v is not None:
             if self.update_exec_after_action != v:
                 self.update_exec_after_action = v
                 self.log_set(prop, v)
                 self.set_modified(save)
             return True
         else:
             return False
     elif prop == 'update_state_after_action':
         v = val_to_boolean(val)
         if v is not None:
             if self.update_state_after_action != v:
                 self.update_state_after_action = v
                 self.log_set(prop, v)
                 self.set_modified(save)
             return True
         else:
             return False
     elif prop == 'update_if_action':
         v = val_to_boolean(val)
         if v is not None:
             if self.update_if_action != v:
                 self.update_if_action = v
                 self.log_set(prop, v)
                 self.set_modified(save)
             return True
         else:
             return False
     elif prop == 'auto_off':
         if val is None:
             auto_off = 0
         else:
             try:
                 auto_off = float(val)
             except:
                 return False
         if auto_off < 0:
             return False
         if self.auto_off != auto_off:
             self.auto_off = auto_off
             self.log_set(prop, auto_off)
             self.set_modified(save)
             if self.auto_off == 0:
                 self.stop_auto_processor()
             else:
                 self.start_auto_processor()
         return True
     elif prop == 'status_labels' and isinstance(val, dict):
         self.status_labels = val
         self.log_set(prop, 'dict')
         self.set_modified(save)
         self.ieid = eva.core.generate_ieid()
         return True
     elif prop[:7] == 'status:':
         try:
             s = int(prop.split(':')[1])
         except:
             return False
         s = str(s)
         if s == '0' and (val is None or val == ''):
             if self.status_labels['0'] != status_label_off:
                 self.status_labels['0'] = status_label_off
                 self.log_set('status_labels[0]', status_label_off)
                 self.set_modified(save)
                 self.ieid = eva.core.generate_ieid()
         elif s == '1' and (val is None or val == ''):
             if self.status_labels['1'] != status_label_on:
                 self.status_labels['1'] = status_label_on
                 self.log_set('status_labels[1]', status_label_on)
                 self.set_modified(save)
                 self.ieid = eva.core.generate_ieid()
         elif val is not None and val != '':
             if not s in self.status_labels or self.status_labels[s] != val:
                 self.status_labels[s] = val
                 self.log_set('status_labels[' + s + ']', val)
                 self.set_modified(save)
                 self.ieid = eva.core.generate_ieid()
         else:
             if not s in self.status_labels:
                 return False
             del self.status_labels[s]
             self.set_modified(save)
             self.ieid = eva.core.generate_ieid()
         return True
     else:
         return super().set_prop(prop, val, save)
Example #9
0
def _parse_reg(reg):
    reg_type = reg[0]
    addr = safe_int(reg[1:])
    if reg_type not in ['c', 'd', 'i', 'h']:
        raise ValueError(f'Invalid register type: {reg_type}')
    return reg_type, addr