Beispiel #1
0
 def aggregate_values(self, shop_id, values):
     result_dup = []
     for value in values.values():
         result_dup = result_dup + value
     result = list(set(result_dup))
     count_topic = get_sensor_topic(shop_id, self.sensor_type, None,
                                    'count')
     count_result = MqttHandler.MqttMessage(
         count_topic, self.generate_payload(shop_id,
                                            {'count': len(result)}))
     list_topic = get_sensor_topic(shop_id, self.sensor_type, None, 'list')
     list_result = MqttHandler.MqttMessage(
         list_topic, self.generate_payload(shop_id, {'clients': result}))
     return [count_result, list_result]
Beispiel #2
0
 def aggregate_values(self, shop_id, values):
     result_transposed = {}
     for value in values.values():
         value_transposed = {value: key for key, value in value.items()}
         result_transposed.update(value_transposed)
     result = {value: key for key, value in result_transposed.items()}
     count_topic = get_sensor_topic(shop_id, self.sensor_type, None,
                                    'count')
     count_result = MqttHandler.MqttMessage(
         count_topic, self.generate_payload(shop_id,
                                            {'count': len(result)}))
     list_topic = get_sensor_topic(shop_id, self.sensor_type, None, 'list')
     list_result = MqttHandler.MqttMessage(
         list_topic, self.generate_payload(shop_id, {'clients': result}))
     return [count_result, list_result]
Beispiel #3
0
 def aggregate_values(self, shop_id, values):
     result = 0
     for value in values.values():
         result += value
     count_topic = get_sensor_topic(shop_id, self.sensor_type, None,
                                    'count')
     count_result = MqttHandler.MqttMessage(
         count_topic, self.generate_payload(shop_id, {'count': result}))
     return count_result
Beispiel #4
0
def main():
    global running
    parser = argparse.ArgumentParser(description='RoboRinth Robot Simulator')
    parser.add_argument('--host',
                        type=str,
                        help='host of the mqtt broker to connect',
                        default="192.168.0.200")
    parser.add_argument('--port',
                        type=int,
                        default=1883,
                        help='port of the mqtt broker to connect')
    parser.add_argument('--mapFile',
                        type=str,
                        default="default.map",
                        help='port of the mqtt broker to connect')
    parser.add_argument('--clientId',
                        type=str,
                        default="",
                        help='MQTT client connection id')
    parser.add_argument('--topicPrefix',
                        type=str,
                        default="roboraptorz",
                        help="topic prefix for mqtt topics")
    parser.add_argument(
        '--log',
        type=str,
        choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
        default="DEBUG",
        help='Loglevel ')

    args = parser.parse_args()

    _initializeLogging(args.log)
    logger.debug("Main started")

    # Setup mqtt client
    handler = MqttHandler(args.host, args.port, args.mapFile, args.clientId,
                          args.topicPrefix)

    handler._client.startAsync()
    time.sleep(2)
    printerThread = threading.Thread(target=mapPrinter, args=[handler.map])
    printerThread.start()

    # block until we want to shut down
    input("\n\nPress Enter to abort...\n\n")
    running = False

    # Terminate
    printerThread.join()
    handler._client.stop()

    logger.debug("Terminate")
Beispiel #5
0
 def _map_message(self, suffix, message):
     if 'sensor_id' not in message:
         logging.warning("Can not map %s sensor message to shop: sensor_id attribute missing", self.sensor_type)
         return
     sensor_id = message['sensor_id']
     if sensor_id not in self.sensor_to_shop_dict:
         logging.warning("No shop found for sensor from type %s with id: %s", self.sensor_type, sensor_id)
         return
     shop = self.sensor_to_shop_dict[sensor_id]
     logging.debug("Mapped sensor %s %s to shop %s", self.sensor_type, sensor_id, shop)
     message.update({'shop_id': shop, 'sensor_type': self.sensor_type})
     return MqttHandler.MqttMessage(get_sensor_topic(shop, self.sensor_type, sensor_id, suffix, is_raw=True),
                                    message)
Beispiel #6
0
    def __init__(self, ip):
        self.ip = ip
        # TODO not logging .error type for now, add in config
        logging.basicConfig(level=logging.INFO)
        logging.info(self.ip)
        logging.info(socket.gethostbyname(socket.gethostname()))

        # Node Handler
        self.nodeHandler = NodeHandler()

        # MqttCommuncation
        self.mqttHandler = MqttHandler(self.ip, self)

        #InfluxDB
        self.influxClient = InfluxDB(self.ip)
Beispiel #7
0
 def _process(self, topic, message):
     assert isinstance(message, dict)
     if 'shop_id' not in message or 'count' not in message:
         logging.warning("Can not process people count message: shop_id or count attribute missing, topic: %s", topic)
         return
     shop_id = message['shop_id']
     people_count = message['count']
     if shop_id not in self.shops_max_people_dict:
         logging.warning("Shop not found in max_people dict: %s", shop_id)
         return
     max_peoples = self.shops_max_people_dict[shop_id]
     utilization = translate(people_count,0, max_peoples, 0,100)
     action = self._utilization_action(utilization)
     message = {"shop_id": shop_id, "actor_type":"display", "action": action}
     logging.debug("Action message for shop %s with utilization %s: %s", shop_id, utilization, action['message'])
     return MqttHandler.MqttMessage(get_sensor_topic(shop_id, 'display', None, 'action', use_type_subtopic='actuators'),
                                    message)
 def aggregate_values(self, shop_id, values):
     import statistics
     counts = [value['count'] for value in values.values()]
     aggregated_sensors = [
         value['aggregated_sensors'] for value in values.values()
     ]
     result = ceil(statistics.median(counts))
     total_aggregated_sensors = sum(aggregated_sensors)
     count_topic = get_sensor_topic(shop_id,
                                    'people',
                                    None,
                                    'count',
                                    use_type_subtopic=False)
     count_result = MqttHandler.MqttMessage(
         count_topic,
         self.generate_payload(
             shop_id, {
                 'count': result,
                 'aggregated_sensors': total_aggregated_sensors,
                 'aggregated_sensor_types': list(values.keys())
             }))
     return count_result
Beispiel #9
0
from MqttHandler import MqttHandler
from ShopMapper import main as shop_mapper
from DisplayController import main as display_controller
from WifiAggregator import main as wifi_aggregator
from BleAggregator import main as ble_aggregator
from CamAggregator import main as cam_aggregator
from PeopleAggregator import main as people_aggregator

if __name__ == '__main__':
    MqttHandler.retain_messages = True
    # Set to true to ignore all messages with the retain flag. Only useful for debugging
    MqttHandler.ignore_retained_messages = False
    # Set to true to delete all received messages with the retain flag on the broker. Only useful for debugging
    MqttHandler.delete_retained_messages = False


def main(mqtt_handler):
    shop_mapper(mqtt_handler)
    wifi_aggregator(mqtt_handler)
    ble_aggregator(mqtt_handler)
    cam_aggregator(mqtt_handler)
    people_aggregator(mqtt_handler)
    display_controller(mqtt_handler)

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)

    mqtt_handler = MqttHandler()
    main(mqtt_handler)
    mqtt_handler.loop_forever()