当前位置: 首页>>代码示例>>Python>>正文


Python Config.tryGetWithDefault方法代码示例

本文整理汇总了Python中config.config.Config.tryGetWithDefault方法的典型用法代码示例。如果您正苦于以下问题:Python Config.tryGetWithDefault方法的具体用法?Python Config.tryGetWithDefault怎么用?Python Config.tryGetWithDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在config.config.Config的用法示例。


在下文中一共展示了Config.tryGetWithDefault方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from config.config import Config [as 别名]
# 或者: from config.config.Config import tryGetWithDefault [as 别名]
class Core:
    def __init__(self, configFile=None, debug=False):
        self.configFile = "core.config" if configFile is None else configFile
        self.debug = debug
        self.tripId = None
        self.start_time = None
        self.config = Config(self.configFile)
        self.error = []
        self.errorPollingInterval = self.config.tryGetWithDefault("errorPollingInterval", 15)
        self.pollingInterval = self.config.tryGetWithDefault("pollingInterval", 2)

    def __enter__(self):
        self.gpsd = gpspoller.GpsPoller()
        self.obd = car.Car(self.config.tryGetWithDefault("obdConfigFile", "obd.config"), self.debug)
        self.obdFetchEnabled = False
        if not self.obd.connected:
            self.__DBG("OBD not connected on startup.")

        if self.tripId is None:
            self.__startTrip()

    def __exit__(self, exc_type, exc_value, traceback):
        self.close()

    def __startTrip(self):
        self.start_time = str(datetime.datetime.now())
        self.tripId = "{0}:{1}".format(self.config.reg, self.start_time)
        self.dbWrapper = dbwrapper.DbWrapper(self.config.api, self.config.user, self.config.password, self.tripId)
        self.dbWrapper.debug = self.debug
        self.identity = state.Identity()

        startTripMessage = {"type": "trip", "id": self.tripId, "car_id": self.config.reg, "start_time": self.start_time}
        self.__DBG(json.dumps(startTripMessage, sort_keys=True))
        self.dbWrapper.sendPriorityMessage(startTripMessage)

    def run(self):
        start = time.time()
        while True:
            pollingStart = time.time()
            if not self.obdFetchEnabled and self.obd.connected:
                dataTypes = self.obd.getSupportedDataTypes()
                if len(dataTypes) == 0:
                    self.obd.connected = False
                else:
                    self.obd.enableFetch(dataTypes)
                    self.obdFetchEnabled = True

            remain = start + self.errorPollingInterval - time.time()
            if remain <= 0.0:
                if self.obd.connected:
                    self.error = self.obd.checkForCarErrors()
                start = time.time()

            carDataList = {}
            if self.obd.connected:
                carDataList = self.obd.fetchData()

            position = self.gpsd.getPosition()
            dataPoint = {
                "type": "data",
                "trip_id": self.tripId,
                "registration_time": str(datetime.datetime.now()),
                "latitude": position[0],
                "longitude": position[1],
                "errors": self.error,
            }

            for dataType, value in carDataList.items():
                dataPoint[self.obd.pidToVariableName[str(dataType)]] = value[0]

            driver_id = self.identity.get_id()
            if driver_id:
                credentials = {"type": "user", "user_id": driver_id, "trip_id": self.tripId}
                self.dbWrapper.send(credentials)
                self.__DBG("RFID Card: " + driver_id)

            self.__DBG(json.dumps(dataPoint, sort_keys=True))
            self.dbWrapper.send(dataPoint)
            self.__DBG("Data sent!")

            if not self.obd.connected:
                self.obd.Reconnect()
            timeToSleep = pollingStart + self.pollingInterval - time.time()
            self.__DBG("Time to sleep", timeToSleep)
            if timeToSleep > 0.0:
                time.sleep(timeToSleep)

    def close(self):
        self.obd.close()
        self.gpsd.disconnect()

    def __DBG(self, *args):
        if self.debug:
            msg = " ".join([str(a) for a in args])
            print(msg)
开发者ID:msholsve,项目名称:triptrackr,代码行数:97,代码来源:Core.py


注:本文中的config.config.Config.tryGetWithDefault方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。