This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mockdata.py
91 lines (78 loc) · 4.31 KB
/
mockdata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Created on Aug 17 13:36 2018
@author: nishit
"""
import configparser
import json
import os
from config.configUpdater import ConfigUpdater
from mock_data.mockGenericDataPublisher import MockGenericDataPublisher
from utils_intern.messageLogger import MessageLogger
class MockData:
def __init__(self, config):
self.config = config
self.publishers = {}
def startMockDataPublisherThreads(self, logger):
for section in config.sections():
try:
if not (section.startswith('IO')):
logger.info("topic: " + section)
mock_params = {"section" : section}
horizon_steps = int(config.get(section, "horizon.steps"))
pub_frequency_sec = int(config.get(section, "pub.frequency.sec"))
delta_time_sec = int(config.get(section, "delta.time.sec"))
mqtt_topic = config.get(section, "mqtt.topic")
if horizon_steps is None or pub_frequency_sec is None or \
mqtt_topic is None or delta_time_sec is None:
logger.info("Topic "+str(section)+" does not have required information.")
continue
else:
mock_params["horizon_steps"] = horizon_steps
mock_params["pub_frequency_sec"] = pub_frequency_sec
mock_params["delta_time_sec"] = delta_time_sec
mock_params["mqtt_topic"] = json.loads(mqtt_topic)
mock_source = config.get(section, "mock.source", fallback="random")
if mock_source == "file":
mock_file_path = config.get(section, "mock.file.path")
if mock_file_path is None:
logger.error("No mock.file.path specified for topic : "+str(section))
continue
mock_file_path = os.path.join(os.getcwd(), mock_file_path)
logger.info("mock file path = "+str(mock_file_path))
mock_params["mock_file_path"] = mock_file_path
mock_params["mock_source"] = mock_source
elif mock_source == "random":
mock_params["mock_source"] = mock_source
mock_random_min = float(config.get(section, "mock.random.min", fallback="0"))
mock_random_max = float(config.get(section, "mock.random.max", fallback="1"))
mock_data_type = config.get(section, "mock.data.type", fallback="float")
if mock_random_min > mock_random_max:
mock_random_min, mock_random_max = mock_random_max, mock_random_min
mock_params["mock_random_min"] = mock_random_min
mock_params["mock_random_max"] = mock_random_max
mock_params["mock_data_type"] = mock_data_type
elif mock_source == "constant":
mock_params["mock_source"] = mock_source
contant_value = config.get(section, "mock.constant.val", fallback=None)
if contant_value is None:
logger.error("no constant value specified for "+str(section))
continue
mock_params["mock_constant"] = contant_value
mock_params["horizon_steps"] = 1
mgdp = MockGenericDataPublisher(config, mock_params)
mgdp.start()
self.publishers[section] = mgdp
except Exception as e:
logger.error("mockdata start error "+str(e))
def stopMockDataPublisherThreads(self):
if self.publishers is not None:
for pub in self.publishers.values():
pub.Stop()
if __name__ == '__main__':
config = None
config_path = "/usr/src/app/mock_data/resources/mockConfig.properties"
config_path_default = "/usr/src/app/config/mockConfig.properties"
config, logger = ConfigUpdater.get_config_and_logger("mockdata", config_path_default, config_path)
logger.info("Starting mock data generation")
mockData = MockData(config)
mockData.startMockDataPublisherThreads(logger)