def main(): payload = pydantic.parse_file_as( request_data_generator_call.RequestDataGenRawCall, os.path.join('..', '..', 'reference', 'sample_request_data_gen_initial.json')) result_raw = request_data_generation.generate_raw_data( payload.descriptions) print(json.dumps({'result': result_raw}, indent=2, default=utils.serialize)) payload = pydantic.parse_file_as( request_data_generator_call.RequestDataGenQueuedCall, os.path.join('..', '..', 'reference', 'sample_request_data_gen_queued.json')) result_queued = request_data_generation.simulate_queued_data( payload.eventTimes, payload.processingTimes, payload.numQueues) print( json.dumps({'result': result_queued}, indent=2, default=utils.serialize)) payload = pydantic.parse_file_as( request_data_generator_call.RequestDataGenAggregateCall, os.path.join('..', '..', 'reference', 'sample_request_data_gen_aggregate.json')) result_aggregate = request_data_generation.aggregate_data( payload.timeVals, payload.dataVals, payload.aggregationLevel, payload.aggregationMethod) print( json.dumps({'result': result_aggregate}, indent=2, default=utils.serialize))
def __init__( self, trader_type: TraderType, templates_repository: ItemTemplatesRepository, trader_view_factory: Callable[..., BaseTraderView], config: TradersConfig, ): self.__templates_repository = templates_repository self.__view_factory = trader_view_factory self.__config = config self.type: Final[TraderType] = trader_type self.path = db_dir.joinpath("traders", self.type.value) self.__barter_scheme_generator: TraderAssortGenerator if trader_type == TraderType.Fence: self.__barter_scheme_generator = FenceAssortGenerator(self) else: self.__barter_scheme_generator = TraderAssortGenerator(self) self._base: Final[TraderBase] = TraderBase.parse_file( self.path.joinpath("base.json")) self.loyal_level_items: Final[Dict[str, int]] = pydantic.parse_file_as( Dict[str, int], self.path.joinpath("loyal_level_items.json")) self.quest_assort: Final[QuestAssort] = QuestAssort.parse_file( self.path.joinpath("questassort.json")) self.__update()
def __init__( self, config: FleaMarketConfig, templates_repository: ItemTemplatesRepository, globals_repository: GlobalsRepository, item_factory: ItemFactory, ) -> None: self.__config = config self.__templates_repository = templates_repository self.__globals_repository = globals_repository self.__item_factory = item_factory # Creates dictionary with item prices from templates and updates it with prices from flea_prices.json self.item_prices = { tpl.id: tpl.props.CreditsPrice for tpl in self.__templates_repository.templates.values() if tpl.id in category_repository.item_categories and tpl.props.CreditsPrice } item_prices: Dict[TemplateId, int] = pydantic.parse_file_as( Dict[TemplateId, int], db_dir.joinpath("flea_prices.json")) self.item_prices.update( {tpl: price for tpl, price in item_prices.items() if price > 0}) # Load seller usernames. self.seller_names = pydantic.parse_file_as( List[str], db_dir.joinpath("traders", "ragfair", "sellers.json")) # All the item templates that we have prices for self.item_templates = [ tpl for tpl in self.__templates_repository.templates.values() if tpl.id in self.item_prices ] prices = list(self.item_prices.values()) median_price = statistics.median(prices) prices_sorted = sorted(prices) # Calculates low/high percentile, they're used to weight too cheap/expensive items self.percentile_high: int = prices_sorted[int( len(prices) * self.__config.percentile_high)] self.percentile_low: int = prices_sorted[int( len(prices) * self.__config.percentile_low)] self.item_templates_weights = [ self._get_item_template_weight(tpl, median_price) for tpl in self.item_templates ]
def _read_item_template_categories( ) -> Dict[TemplateId, ItemTemplateCategoryModel]: template_categories: List[ ItemTemplateCategoryModel] = pydantic.parse_file_as( List[ItemTemplateCategoryModel], db_dir.joinpath("templates", "items.json"), ) return {tpl.Id: tpl for tpl in template_categories}
def main( # pylint: disable=too-many-arguments path_to_data: Path = typer.Argument( ..., exists=True, file_okay=True, dir_okay=False, readable=True, help= ("Path to a json blob consisting of a list wrapping around the data items," " which should be objects."), ), path_to_dataprops: Path = typer.Argument( ..., exists=True, file_okay=True, dir_okay=False, readable=True, help= ("Path to the json blob consisting of the object that describes the " "keys/properties that will be extracted from the data items and used to " "determine the heterogeneous groups."), ), algorithm: AlgorithmUserInput = typer.Option( AlgorithmUserInput.SAME_SIZE, "-a", "--algo", "--algorithm", case_sensitive=False, help= ("The heterogeneous grouping algorithm to be used on the dataset to form " "the groups."), ), num_groups: int = typer.Option( 1, "-n", "--num-groups", help="Number of groups to divide the items in the dataset into.", ), verbose: Optional[bool] = typer.Option(None, "-v", "--verbose"), version: Optional[bool] = typer.Option( # pylint: disable=unused-argument None, "-V", "--version", callback=version_callback, is_eager=True), ) -> None: """ Separate a given dataset into heterogeneous groups. """ with open(path_to_data, "r") as datafile: grouper = Grouper(load(datafile), parse_file_as(DataProperties, path_to_dataprops)) if verbose: typer.echo(grouper) typer.echo(grouper.data) typer.echo(grouper.scaled_grid()) typer.echo(grouper.difference_matrix()) typer.echo(Algorithm[algorithm.name].value(grouper, num_groups))
def validate_model_data(cls, v: Union[Path, "ModelData"], values: Dict[str, Any]) -> ModelData: if isinstance(v, Path): v = parse_file_as(ModelData, v) verts = values["vertices"] if verts is not None: # scoped vertices down to requested by params. _new_verts = {k: v for k, v in v.vertices.items() if k in verts} v.vertices = _new_verts return v
def __init__(self, trader: Trader): super().__init__() self.trader = trader self.__items = { item.id: item for item in pydantic.parse_file_as( List[Item], self.trader.path.joinpath("items.json"), ) }
def extract_namespace_mapping( filename: Path, ) -> Dict[str, IdentifiersOrgNamespaceModel]: """ Extract a namespace mapping from a JSON file. Parameters ---------- filename : pathlib.Path The path to the JSON file. Returns ------- dict A map from namespace prefixes to Identifiers.org namespace data models. """ old_value = IdentifiersOrgNamespaceModel.Config.allow_population_by_field_name IdentifiersOrgNamespaceModel.Config.allow_population_by_field_name = True mapping = parse_file_as(Dict[str, IdentifiersOrgNamespaceModel], filename) IdentifiersOrgNamespaceModel.Config.allow_population_by_field_name = old_value return mapping
def read_config(): print(env.config_directory) settings = parse_file_as(ChatSettings, config_file_path) return settings
# create a fiware header object fiware_header = FiwareHeader(service=SERVICE, service_path=SERVICE_PATH) # clear the state of your service and scope clear_iot_agent(url=IOTA_URL, fiware_header=fiware_header) clear_context_broker(url=CB_URL, fiware_header=fiware_header) clear_quantumleap(url=QL_URL, fiware_header=fiware_header) # instantiate simulation model sim_model = SimulationModel(t_start=T_SIM_START, t_end=T_SIM_END, temp_max=TEMPERATURE_MAX, temp_min=TEMPERATURE_MIN, temp_start=TEMPERATURE_ZONE_START) # Create clients and restore devices and groups from file groups = parse_file_as(List[ServiceGroup], READ_GROUPS_FILEPATH) devices = parse_file_as(List[Device], READ_DEVICES_FILEPATH) sub = parse_file_as(List[Subscription], READ_SUBSCRIPTIONS_FILEPATH)[0] sub.notification.mqtt.topic = TOPIC_CONTROLLER sub.notification.mqtt.user = MQTT_USER sub.notification.mqtt.passwd = MQTT_PW cbc = ContextBrokerClient(url=CB_URL, fiware_header=fiware_header) cbc.post_subscription(subscription=sub) iotac = IoTAClient(url=IOTA_URL, fiware_header=fiware_header) iotac.post_groups(service_groups=groups) iotac.post_devices(devices=devices) # Get the group and device configurations from the server group = iotac.get_group(resource="/iot/json", apikey=APIKEY) weather_station = iotac.get_device(device_id="device:001") zone_temperature_sensor = iotac.get_device(device_id="device:002")
def _read_categories() -> Dict[CategoryId, CategoryModel]: categories: List[CategoryModel] = pydantic.parse_file_as( List[CategoryModel], db_dir.joinpath("templates", "categories.json"), ) return {category.Id: category for category in categories}
Path("./e5_iot_thermal_zone_control_groups.json") READ_DEVICES_FILEPATH = \ Path("./e5_iot_thermal_zone_control_devices.json") READ_ENTITIES_FILEPATH = \ Path("./e3_context_entities_entities.json") # ## Main script if __name__ == '__main__': # create a fiware header object fiware_header = FiwareHeader(service=SERVICE, service_path=SERVICE_PATH) # clear the state of your service and scope clear_iot_agent(url=IOTA_URL, fiware_header=fiware_header) clear_context_broker(url=CB_URL, fiware_header=fiware_header) # Create clients and restore devices and groups from file groups = parse_file_as(List[ServiceGroup], READ_GROUPS_FILEPATH) devices = parse_file_as(List[Device], READ_DEVICES_FILEPATH) entities = parse_file_as(List[ContextEntity], READ_ENTITIES_FILEPATH) cbc = ContextBrokerClient(url=CB_URL, fiware_header=fiware_header) for entity in entities: cbc.post_entity(entity=entity) iotac = IoTAClient(url=IOTA_URL, fiware_header=fiware_header) iotac.post_groups(service_groups=groups) iotac.post_devices(devices=devices) # ToDo: Retrieve all iot resources from the IoT-Agent # Get the group and device configurations from the server group = iotac.get_group(resource="/iot/json", apikey=APIKEY) weather_station = iotac.get_device(device_id="device:001") zone_temperature_sensor = iotac.get_device(device_id="device:002")
def get_people(filepath: Path) -> Mapping[PersonId, Person]: people = parse_file_as(List[Person], filepath) return {person.id: person for person in people}
clear_context_broker(url=CB_URL, fiware_header=fiware_header) # instantiate simulation model sim_model = SimulationModel(t_start=T_SIM_START, t_end=T_SIM_END, temp_max=TEMPERATURE_MAX, temp_min=TEMPERATURE_MIN, temp_start=TEMPERATURE_ZONE_START) # define lists to store historical data history_weather_station = [] history_zone_temperature_sensor = [] history_heater = [] # Create clients and restore devices and groups from file groups = parse_file_as(List[ServiceGroup], READ_GROUPS_FILEPATH) devices = parse_file_as(List[Device], READ_DEVICES_FILEPATH) cbc = ContextBrokerClient(url=CB_URL, fiware_header=fiware_header) iotac = IoTAClient(url=IOTA_URL, fiware_header=fiware_header) iotac.post_groups(service_groups=groups) iotac.post_devices(devices=devices) # ToDo: Get the device configurations from the server weather_station = iotac.get_device(device_id="device:001") zone_temperature_sensor = iotac.get_device(device_id="device:002") # ToDo: Get the service group configurations from the server group = iotac.get_group(resource="/iot/json", apikey=APIKEY) # ToDo: Create and additional device holding a command attribute and # post it to the IoT-Agent. It should be mapped to the `type` heater
def read_config(): settings = parse_file_as(OpenDirectorySettings, config_file_path) return settings
name: str chat_id: int app = typer.Typer(add_completion=False) helps = { "to": "broadcast receiver", "message": "broadcast message", "clipboard": "take the message from the clipboard", "yes": "skip message confirmation", "enable_wp": "enable web page preview", "production": "use the production bot", } mapper_path = (Path(__file__).absolute().parent.with_name("app") / "db/files/broadcast_channels.json") mapper = parse_file_as(List[BroadcastChannel], mapper_path) valid_names = [x.name for x in mapper] BroadCastReceiver = Enum("BroadCastReceiver", {x: x for x in valid_names}) def get_chat_id_from_receiver(name: BroadCastReceiver): for channel in mapper: if channel.name == name.name: return channel.chat_id raise ClickException(f"Invalid name: {name!r}") @app.command() def send_message( to: BroadCastReceiver = typer.Argument(..., case_sensitive=False,
def read_config(): print(env.config_directory) settings = parse_file_as(ApplicationServerSettings, config_file_path) return settings
def save_config(config: OpenDirectorySettings): with open(config_file_path, "w") as f: f.write(config.json()) settings = parse_file_as(OpenDirectorySettings, config_file_path) return settings
def get_aemo_tableset() -> AEMOTableSet: """Reads the stored tableset fixture path and returns an AEMOTableSet""" ts = pydantic.parse_file_as(path=str(TABLESET_PATH), type_=AEMOTableSet) return ts
def _read_items(self) -> List[Item]: return pydantic.parse_file_as( List[Item], self.trader.path.joinpath("items.json"), )
def read_config(): settings = parse_file_as(NFSSettings, config_file_path) return settings
def read_config(): settings = parse_file_as(WebObjectsSettings, config_file_path) return settings
def save_config(config: WebObjectsSettings): with open(config_file_path, "w") as f: f.write(config.json()) settings = parse_file_as(WebObjectsSettings, config_file_path) return settings
def simulation( TEMPERATURE_MAX=10, # maximal ambient temperature TEMPERATURE_MIN=-5, # minimal ambient temperature TEMPERATURE_ZONE_START=10, # start value of the zone temperature T_SIM_START=0, # simulation start time in seconds T_SIM_END=24 * 60 * 60, # simulation end time in seconds COM_STEP=60 * 15, # 1 min communication step in seconds SLEEP_TIME=0.2 # sleep time between every simulation step ): # create a fiware header object fiware_header = FiwareHeader(service=SERVICE, service_path=SERVICE_PATH) # instantiate simulation model sim_model = SimulationModel(t_start=T_SIM_START, t_end=T_SIM_END, temp_max=TEMPERATURE_MAX, temp_min=TEMPERATURE_MIN, temp_start=TEMPERATURE_ZONE_START) # Create clients and restore devices and groups from files groups = parse_file_as(List[ServiceGroup], READ_GROUPS_FILEPATH) devices = parse_file_as(List[Device], READ_DEVICES_FILEPATH) cbc = ContextBrokerClient(url=CB_URL, fiware_header=fiware_header) iotac = IoTAClient(url=IOTA_URL, fiware_header=fiware_header) iotac.post_groups(service_groups=groups, update=True) iotac.post_devices(devices=devices, update=True) # Get the device configurations from the server weather_station = iotac.get_device(device_id="device:001") zone_temperature_sensor = iotac.get_device(device_id="device:002") heater = iotac.get_device(device_id="device:003") # Get the service group configurations from the server group = iotac.get_group(resource="/iot/json", apikey=APIKEY) # Create a http subscriptions that get triggered by updates of your # device attributes and send data to Quantum Leap. qlc = QuantumLeapClient(url=QL_URL, fiware_header=fiware_header) qlc.post_subscription(entity_id=weather_station.entity_name, entity_type=weather_station.entity_type, cb_url="http://orion:1026", ql_url="http://quantumleap:8668", throttling=0) qlc.post_subscription(entity_id=zone_temperature_sensor.entity_name, entity_type=zone_temperature_sensor.entity_type, cb_url="http://orion:1026", ql_url="http://quantumleap:8668", throttling=0) qlc.post_subscription(entity_id=heater.entity_name, entity_type=heater.entity_type, cb_url="http://orion:1026", ql_url="http://quantumleap:8668", throttling=0) # create a MQTTv5 client with paho-mqtt and the known groups and devices. mqttc = IoTAMQTTClient( protocol=mqtt.MQTTv5, devices=[weather_station, zone_temperature_sensor, heater], service_groups=[group]) # set user data if required mqttc.username_pw_set(username=MQTT_USER, password=MQTT_PW) # Implement a callback function that gets triggered when the # command is sent to the device. The incoming command should update the # heater power of the simulation model def on_command(client, obj, msg): """ Callback for incoming commands """ # Decode the message payload using the libraries builtin encoders apikey, device_id, payload = \ client.get_encoder(PayloadProtocol.IOTA_JSON).decode_message( msg=msg) # Update the heating power of the simulation model sim_model.heater_power = payload["heater_power"] # Acknowledge the command. client.publish(device_id=device_id, command_name=next(iter(payload)), payload=payload) # Add the command callback to your MQTTClient. This will get # triggered for the specified device_id mqttc.add_command_callback(device_id=heater.device_id, callback=on_command) # connect to the mqtt broker and subscribe to your topic mqtt_url = urlparse(MQTT_BROKER_URL_EXPOSED) mqttc.connect(host=mqtt_url.hostname, port=mqtt_url.port, keepalive=60, bind_address="", bind_port=0, clean_start=mqtt.MQTT_CLEAN_START_FIRST_ONLY, properties=None) # subscribe to all incoming command topics for the registered devices mqttc.subscribe() # create a non-blocking thread for mqtt communication mqttc.loop_start() # define lists to store historical data history_weather_station = [] history_zone_temperature_sensor = [] history_heater_power = [] # simulation without heater # Create a loop that publishes regularly a message to the broker # that holds the simulation time "simtime" and the corresponding # temperature "temperature" the loop should. You may use the `object_id` # or the attribute name as key in your payload. print("Simulation starts") for t_sim in range(sim_model.t_start, sim_model.t_end + int(COM_STEP), int(COM_STEP)): # publish the simulated ambient temperature mqttc.publish(device_id=weather_station.device_id, payload={ "temperature": sim_model.t_amb, "simtime": sim_model.t_sim }) # publish the simulated zone temperature mqttc.publish(device_id=zone_temperature_sensor.device_id, payload={ "temperature": sim_model.t_zone, "simtime": sim_model.t_sim }) # publish the 'simtime' for the heater device mqttc.publish(device_id=heater.device_id, payload={"simtime": sim_model.t_sim}) # simulation step for next loop sim_model.do_step(int(t_sim + COM_STEP)) # wait for one second before publishing the next values time.sleep(SLEEP_TIME) # Get corresponding entities and write values to history weather_station_entity = cbc.get_entity( entity_id=weather_station.entity_name, entity_type=weather_station.entity_type) # append the data to the local history history_weather_station.append({ "simtime": weather_station_entity.simtime.value, "temperature": weather_station_entity.temperature.value }) # Get ZoneTemperatureSensor and write values to history zone_temperature_sensor_entity = cbc.get_entity( entity_id=zone_temperature_sensor.entity_name, entity_type=zone_temperature_sensor.entity_type) history_zone_temperature_sensor.append({ "simtime": zone_temperature_sensor_entity.simtime.value, "temperature": zone_temperature_sensor_entity.temperature.value }) # Get ZoneTemperatureSensor and write values to history heater_entity = cbc.get_entity(entity_id=heater.entity_name, entity_type=heater.entity_type) history_heater_power.append({ "simtime": heater_entity.simtime.value, "heater_power": sim_model.heater_power }) # close the mqtt listening thread mqttc.loop_stop() # disconnect the mqtt device mqttc.disconnect() clear_iot_agent(url=IOTA_URL, fiware_header=fiware_header) clear_context_broker(url=CB_URL, fiware_header=fiware_header) return history_weather_station, history_zone_temperature_sensor, history_heater_power
def save_config(config: ApplicationServerSettings): with open(config_file_path, "w") as f: f.write(config.json()) settings = parse_file_as(ApplicationServerSettings, config_file_path) return settings