vapor_logging.setup_logging(default_path='logging_emulator.json') logger.info("========================================") logger.info("Starting DeviceBus Emulator.") if hardware == devicebus.DEVICEBUS_RPI_HAT_V1: import devicebus_interfaces.plc_rpi_v1 as plc_rpi_v1 for x in range(2): config = plc_rpi_v1.OPENDCRE_DEFAULT_CONFIGURATION # FIXME: the emulator is a subordinate device on the bus, and needs to handle # FIXME: auto_sleep differently than the master, where we instead receive a WUM and need to wake # FIXME: ourselves up based on the incoming WUM. For most testing purposes, this is not terrifically # FIXME: useful or necessary, but is something that should be added here when possible. config['auto_sleep'] = False config['auto_wum'] = False plc_rpi_v1.wake() # just to be on the safe side plc_rpi_v1.configure(serial_device=emulatorDevice, configuration=config) # the JSON configuration file name is # passed in as the second parameter to this program configFile = sys.argv[2] emulator_data = open(configFile, "r") data = emulator_data.read() configuration = json.loads(data) while True: try: main(emulatorDevice) except Exception as e: logger.error("Exception encountered processing packet. (%s)", e)
def __init__(self, hardware_type=DEVICEBUS_UNKNOWN_HARDWARE, device_name=None, timeout=0.25): """ Create a new instance of the DeviceBus used to communicate with devices on the device bus. Depending on the hardware_type, different configuration actions may be taken. This also sets up the serial_device based on device_name, which is used for reads/writes/flushes. hardware_type is the type of hardware to initialize the DeviceBus with: DEVICEBUS_RPI_HAT_V1 : Configure the RPI V1 HAT (OpenDCRE) for PLC. DEVICEBUS_VEC_V1 : Configure the V1 VEC (Vapor CORE) for PLC. (Not implemented) DEVICEBUS_EMULATOR_V1 : Configure the serial emulator. device_name is the name of the serial device on the local system that is used for serial communications (e.g. /dev/ttyAMA0 on RPI). timeout is the time, in seconds, before a read or write operation times out on the serial device. """ if device_name is None: logger.error( "Attempt to initialize DeviceBus with no device_name.") raise ValueError( "Must specify a valid device_name for serial device.") self.hardware_type = hardware_type self.serial_device_name = device_name self.timeout = timeout self.speed_bps = DEVICEBUS_DEFAULT_BPS # first, configure the hardware itself, based on hardware type if self.hardware_type == DEVICEBUS_RPI_HAT_V1: import devicebus_interfaces.plc_rpi_v1 as plc_rpi_v1 try: # First, wake up the SIG60 plc_rpi_v1.wake() logger.debug("Wake succeeded for PLC modem.") except plc_rpi_v1.WakeException: # if wake fails, log and move on - TODO: retry logger.error( "Unable to wake PLC modem - modem may not be awake, and communication errors may result." ) plc_rpi_v1.configure( serial_device=self.serial_device_name, configuration=plc_rpi_v1.OPENDCRE_DEFAULT_CONFIGURATION) self.speed_bps = plc_rpi_v1.OPENDCRE_DEFAULT_CONFIGURATION[ 'bit_rate'] elif self.hardware_type == DEVICEBUS_VEC_V1: raise NotImplementedError("VEC_V1 is not yet supported.") elif self.hardware_type == DEVICEBUS_EMULATOR_V1: # no special hardware config needed pass else: logger.error("Invalid hardware_type for reading device bus. (%d)", self.hardware_type) raise ValueError( "Must specify a valid hardware_type for device bus.") # at this point, the serial connection is ready to go, # so set up the serial_device based on device_name self.serial_device = serial.Serial(self.serial_device_name, self.speed_bps, timeout=self.timeout) self.flush_all() logger.debug( "Initialized DeviceBus with hardware: %d device_name: %s speed: %d timeout: %d", self.hardware_type, self.serial_device_name, self.speed_bps, self.timeout or 0)
def __init__(self, hardware_type=DEVICEBUS_UNKNOWN_HARDWARE, device_name=None, timeout=0.25): """ Create a new instance of the DeviceBus used to communicate with devices on the device bus. Depending on the hardware_type, different configuration actions may be taken. This also sets up the serial_device based on device_name, which is used for reads/writes/flushes. hardware_type is the type of hardware to initialize the DeviceBus with: DEVICEBUS_RPI_HAT_V1 : Configure the RPI V1 HAT (OpenDCRE) for PLC. DEVICEBUS_VEC_V1 : Configure the V1 VEC (Vapor CORE) for PLC. (Not implemented) DEVICEBUS_EMULATOR_V1 : Configure the serial emulator. device_name is the name of the serial device on the local system that is used for serial communications (e.g. /dev/ttyAMA0 on RPI). timeout is the time, in seconds, before a read or write operation times out on the serial device. """ if device_name is None: logger.error("Attempt to initialize DeviceBus with no device_name.") raise ValueError("Must specify a valid device_name for serial device.") self.hardware_type = hardware_type self.serial_device_name = device_name self.timeout = timeout self.speed_bps = DEVICEBUS_DEFAULT_BPS # first, configure the hardware itself, based on hardware type if self.hardware_type == DEVICEBUS_RPI_HAT_V1: import devicebus_interfaces.plc_rpi_v1 as plc_rpi_v1 try: # First, wake up the SIG60 plc_rpi_v1.wake() logger.debug("Wake succeeded for PLC modem.") except plc_rpi_v1.WakeException: # if wake fails, log and move on - TODO: retry logger.error("Unable to wake PLC modem - modem may not be awake, and communication errors may result.") plc_rpi_v1.configure( serial_device=self.serial_device_name, configuration=plc_rpi_v1.OPENDCRE_DEFAULT_CONFIGURATION ) self.speed_bps = plc_rpi_v1.OPENDCRE_DEFAULT_CONFIGURATION["bit_rate"] elif self.hardware_type == DEVICEBUS_VEC_V1: raise NotImplementedError("VEC_V1 is not yet supported.") elif self.hardware_type == DEVICEBUS_EMULATOR_V1: # no special hardware config needed pass else: logger.error("Invalid hardware_type for reading device bus. (%d)", self.hardware_type) raise ValueError("Must specify a valid hardware_type for device bus.") # at this point, the serial connection is ready to go, # so set up the serial_device based on device_name self.serial_device = serial.Serial(self.serial_device_name, self.speed_bps, timeout=self.timeout) self.flush_all() logger.debug( "Initialized DeviceBus with hardware: %d device_name: %s speed: %d timeout: %d", self.hardware_type, self.serial_device_name, self.speed_bps, self.timeout or 0, )