Skip to content

Commit

Permalink
Added more debugging logic to sensor. Set default logging level to
Browse files Browse the repository at this point in the history
INFO
  • Loading branch information
craig8 committed Oct 2, 2019
1 parent a9375b1 commit ff1b77f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
8 changes: 4 additions & 4 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ Service Configuration
---------------------

The sensor-config in the above image shows an example of how to configure a portion of the system to have sensor output.
Each mrid (`_001cc221-d6e6-485d-bdcc-b84cb643d1ec`) will be monitored by this service and either use the default values
Each mrid (`_99db0dc7-ccda-4ed5-a772-a7db362e9818`) will be monitored by this service and either use the default values
or use the specified values during the service runtime.

.. code-block:: json
{
"_001cc221-d6e6-485d-bdcc-b84cb643d1ec": {
"_99db0dc7-ccda-4ed5-a772-a7db362e9818": {
"nominal-voltage": 100,
"perunit-confidence-rate": 0.95,
"aggregation-interval": 30,
"perunit-drop-rate": 0.01
},
"_00313f7c-5140-47cf-b750-0146bb3d9024":{},
"_cbc569c1-846c-415f-8c27-b2745b52b20a": {}
"_ee65ee31-a900-4f98-bf57-e752be924c4d":{},
"_f2673c22-654b-452a-8297-45dae11b1e14": {}
}
The other options for the service are:
Expand Down
3 changes: 2 additions & 1 deletion sensor_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ def get_opts():
os.makedirs(os.path.dirname(log_file))

with open(log_file, 'w') as fp:
logging.basicConfig(stream=fp, level=logging.DEBUG)
logging.basicConfig(stream=fp, level=logging.INFO)
logging.getLogger().info(f"user options: {user_options}")
run_sensors = Sensors(gapp, read_topic=read_topic, write_topic=write_topic,
user_options=user_options)
run_sensors.main_loop()
27 changes: 20 additions & 7 deletions sensors/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
_log = logging.getLogger(__file__)

DEFAULT_SENSOR_CONFIG = {
"default-perunit-confidence-rate": 0.01,
"default-perunit-confidence-rate": 0.95,
"default-aggregation-interval": 30,
"default-perunit-drop-rate": 0.01,
'default-nominal-voltage': 100
Expand Down Expand Up @@ -101,6 +101,8 @@ def __init__(self, gridappsd, read_topic, write_topic, user_options: dict = None
DEFAULT_SENSOR_CONFIG['default-aggregation-interval'])
self.default_nominal_voltage = user_options.get('default-nominal-voltage',
DEFAULT_SENSOR_CONFIG['default-nominal-voltage'])

_log.debug(f"sensors_config is: {sensors_config}")
for k, v in sensors_config.items():
agg_interval = v.get("aggregation-interval", self.default_aggregation_interval)
perunit_drop_rate = v.get("perunit-drop-rate", self.default_drop_rate)
Expand All @@ -111,7 +113,7 @@ def __init__(self, gridappsd, read_topic, write_topic, user_options: dict = None
perunit_drop_rate=perunit_drop_rate,
perunit_confidence_rate=perunit_confidence_rate)

_log.debug("Created {} sensors".format(len(self._sensors)))
_log.info("Created {} sensors".format(len(self._sensors)))

def on_simulation_message(self, headers, message):
"""
Expand All @@ -124,7 +126,7 @@ def on_simulation_message(self, headers, message):
:param message:
Simulation measurement message.
"""

_log.debug("Measurement Detected")
configured_sensors = set(self._sensors.keys())

measurement_out = {}
Expand All @@ -142,7 +144,12 @@ def on_simulation_message(self, headers, message):
measurement_mrid=mrid
)

item = message['message']['measurements'][mrid]
_log.debug(f"Getting message from sensor: {mrid}")
item = message['message']['measurements'].get(mrid)

if not item:
_log.error(f"Invalid sensor mrid configured {mrid}")
continue

# Create new values for data from the sensor.
for prop, value in item.items():
Expand All @@ -155,19 +162,24 @@ def on_simulation_message(self, headers, message):
new_value = self._sensors[mrid].get_new_value(timestamp, value)
if new_value is None:
new_measurement = None
_log.debug(f"Not reporting measurement for {mrid}")
break

_log.debug(f"mrid: {mrid} prop: {prop} new_value: {new_value}")
new_measurement[prop] = new_value

if new_measurement is not None:
_log.debug(f"Adding new measurement: {new_measurement}")
measurement_out[mrid] = new_measurement

if len(measurement_out) > 0:
message['message']['measurements'] = measurement_out
if self._log_statistics:
self._log_sensors()

_log.info(f"Sensor Measurements:\n{measurement_out}")
self._gappsd.send(self._write_topic, json.dumps(message))
else:
_log.info("No sensor output.")

def _log_sensors(self):
for s in self._sensors:
Expand All @@ -194,6 +206,7 @@ def __init__(self, random_seed, nominal_voltage, aggregation_interval, perunit_d
"""
self._nominal = nominal_voltage
self._perunit_dropping = perunit_drop_rate
self._perunit_confidence_rate = perunit_confidence_rate
self._stddev = nominal_voltage * perunit_confidence_rate / 1.96 # for normal distribution
self._seed = random_seed
self._interval = aggregation_interval
Expand All @@ -210,8 +223,8 @@ def __init__(self, random_seed, nominal_voltage, aggregation_interval, perunit_d

def __repr__(self):
return f"""
<Sensor(seed={self.seed}, nominal={self.nominal}, interval={self.interval}, output_topic={self.output_topic},
perunit_dropping={self.perunit_dropping})>"""
<Sensor(seed={self.seed}, nominal={self.nominal}, interval={self.interval}, perunit_drop_rate={self.perunit_dropping},
perunit_confidence_rate={self._perunit_confidence_rate})>"""

@property
def seed(self):
Expand Down

0 comments on commit ff1b77f

Please sign in to comment.