Exemplo n.º 1
0
 def test_save_data(self, TinyDBMock):
     persistence = Persistence('path/to/db')
     persistence.save(self.measurement)
     assert_that(
         persistence.table.insert.call_args[0][0],
         has_entries(self.json_object_matcher)
     )
Exemplo n.º 2
0
class CollectData:
    """
    Primary class to collect samples from a specific solar source at specified intervals.
    Collects voltage and current information at each point in time and persists the data
    to using the persistence library.
    """
    def __init__(self, data_name, sample_interval, source_type, db_filename):
        self.data_name = data_name
        self.sample_interval = sample_interval
        self.source_type = SourceType[source_type]
        self.persistence = Persistence(db_filename)
        self.i2c = I2C(Ina260Measurement.DEVICE_ADDRESS)

    def _collect_sample(self):
        sample = Ina260Measurement(
            self.data_name, self.source_type,
            self.i2c.read_word(Ina260Measurement.VOLTAGE_REGISTER),
            self.i2c.read_word(Ina260Measurement.CURRENT_REGISTER))
        self.persistence.save(sample)

    def _sample_loop(self):
        while True:
            self._collect_sample()
            sleep(self.sample_interval)

    def start(self):
        print("Starting collection:", self.data_name)
        self.thread = Thread(target=self._sample_loop)
        self.thread.start()
Exemplo n.º 3
0
 def test_upsert(self, TinyDBMock):
     persistence = Persistence('path/to/db')
     persistence.upsert(self.measurement)
     self.assertEqual(len(persistence.table.upsert.call_args[0]), 2)
     assert_that(
         persistence.table.upsert.call_args[0][0],
         has_entries(self.json_object_matcher)
     )
Exemplo n.º 4
0
 def test_get_date_range_bad_inputs(self, TinyDBMock):
     persistence = Persistence('path/to/db')
     persistence.table.search.return_value = [self.json_response]
     today = datetime.today()
     yesterday = today - timedelta(1)
     with self.assertRaises(Exception) as cm:
         persistence.get_date_range(today, yesterday)
     self.assertIn('end date is before', cm.exception.args[0])
     persistence.table.search.assert_not_called()
Exemplo n.º 5
0
 def on_post(self, req, res):
     persistence = Persistence(self.filename)
     body = json.loads(
         req.stream.read(req.content_length or 0).decode('utf-8'))
     start = parser.parse(body['startDate'])
     end = parser.parse(body['endDate'])
     results = persistence.get_date_range(start, end)
     res.media = [data.get_converted_json() for data in results]
     res.status = falcon.HTTP_200
Exemplo n.º 6
0
 def test_get_date(self, TinyDBMock):
     persistence = Persistence('path/to/db')
     persistence.table.search.return_value = [self.json_response]
     today = datetime.today()
     result = persistence.get_date(today)
     assert_that(
         result[0],
         instance_of(Ina260Measurement)
     )
     persistence.table.search.assert_called()
Exemplo n.º 7
0
 def test_get_latest(self, TinyDBMock):
     persistence = Persistence('path/to/db')
     persistence.table = MagicMock()
     persistence.table.__len__.return_value = 3
     persistence.table.get.return_value = self.json_response
     result = persistence.get_latest()
     assert_that(
         result,
         instance_of(Ina260Measurement)
     )
     persistence.table.get.assert_called_with(doc_id=3)
Exemplo n.º 8
0
 def test_get_all(self, TinyDBMock):
     persistence = Persistence('path/to/db')
     persistence.table = MagicMock()
     persistence.table.search.return_value = [self.json_response]
     result = persistence.get_all()
     self.assertEqual(len(result), 1)
     assert_that(
         result[0],
         instance_of(Ina260Measurement)
     )
     persistence.table.search.assert_called_with(where('timestamp').exists())
Exemplo n.º 9
0
 def __init__(self, data_name, sample_interval, source_type, db_filename):
     self.data_name = data_name
     self.sample_interval = sample_interval
     self.source_type = SourceType[source_type]
     self.persistence = Persistence(db_filename)
     self.i2c = I2C(Ina260Measurement.DEVICE_ADDRESS)
Exemplo n.º 10
0
 def on_get(self, req, res):
     persistence = Persistence(self.filename)
     results = persistence.get_all()
     res.media = [data.get_converted_json() for data in results]
     res.status = falcon.HTTP_200