コード例 #1
0
ファイル: httpserver.py プロジェクト: daviessm/temper-python
def main():
    args = parse_args()
    quiet = args.celsius or args.fahrenheit or args.humidity

    logging.basicConfig(level = logging.ERROR if quiet else logging.WARNING)

    th = TemperHandler()
    devs = th.get_devices()
    if not quiet:
        print("Found %i devices" % len(devs))

    if(args.http_port):
        for dev in devs:
            if args.sensor_count is not None:
                # Override auto-detection from args
                dev.set_sensor_count(int(args.sensor_count))

            if args.sensor_ids == 'all':
                sensors = range(dev.get_sensor_count())
            else:
                sensors = [int(args.sensor_ids)]

            dev.attach()

        HTTPRequestHandler.devs = devs
        HTTPRequestHandler.sensors = sensors
        HTTPRequestHandler.quiet = quiet
        HTTPRequestHandler.args = args
        httpd = HTTPServer(('', args.http_port), HTTPRequestHandler)
        httpd.serve_forever()
コード例 #2
0
 def _initialize(self):
     with self.usb_lock:
         try:
             self.th = TemperHandler()
             self.devs = self.th.get_devices()
             self.logger.write_log('Found %i thermometer devices.' % len(self.devs))
             for i, d in enumerate(self.devs):
                 self.logger.write_log('Initial temperature of device #%i: %0.1f degree celsius' % (i, d.get_temperature()))
         except Exception, e:
             self.logger.write_log('Exception while initializing: %s' % str(e))
コード例 #3
0
    def temp_data():
        args = parse_args()
        quiet = args.celsius or args.fahrenheit
        logging.basicConfig(level = logging.ERROR if quiet else logging.WARNING)

        th = TemperHandler()
        devs = th.get_devices()
        if not quiet:
            print("Found %i devices" % len(devs))
        
        readings = []

        for dev in devs:
            if args.sensor_count is not None:
                # Override auto-detection from args
                dev.set_sensor_count(int(args.sensor_count))

            if args.sensor_ids == 'all':
                sensors = range(dev.get_sensor_count())
            else:
                sensors = [int(args.sensor_ids)]

            temperatures = dev.get_temperatures(sensors=sensors)
            humidities = dev.get_humidity(sensors=sensors)
            combinations = {}
            for k, v in temperatures.items():
                c = v.copy()
                try:
                    c.update(humidities[k])
                except:
                    pass
                combinations[k] = c
            readings.append(combinations)

        for i, reading in enumerate(readings):
            output = ''
            if quiet:
                if args.celsius:
                    dict_key = 'temperature_c'
                elif args.fahrenheit:
                    dict_key = 'temperature_f'
                elif args.humidity:
                    dict_key = 'humidity_pc'

                for sensor in sorted(reading):
                    output += '%0.1f; ' % reading[sensor][dict_key]
                output = output[0:len(output) - 2]
            else:
                portinfo = []
                tempinfo = [-1000]
                huminfo = [-1000]
                for sensor in sorted(reading):
                    if args.disp_ports and portinfo == '':
                        portinfo = " (bus %(bus)s - port %(ports)s)" % reading[sensor]
                    try:
                        tempinfo = reading[sensor]['temperature_c']
                    except:
                        pass
                    try:
                        huminfo = reading[sensor]['humidity_pc']
                    except:
                        pass
                # tempinfo = tempinfo[0:len(output) - 2]
                # huminfo = huminfo[0:len(output) - 2]
    
        return tempinfo,huminfo
コード例 #4
0
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], ":hpcfq", ["help"])
    except getopt.GetoptError as err:
        print(str(err))
        usage()
        sys.exit(2)
    degree_unit = False
    disp_ports = False
    quiet_output = False
    for o, a in opts:
        if o == "-p":
            disp_ports = True
        elif o == "-c":
            degree_unit = 'c'
        elif o == "-f":
            degree_unit = 'f'
        elif o == "-q":
            quiet_output = True
        elif o in ("-h", "--help"):
            usage()
            sys.exit()
        else:
            assert False, "unhandled option"

    if quiet_output and not degree_unit:
        print('You need to specify unit (-c of -f) when using -q option')
        sys.exit(1)

    th = TemperHandler()
    devs = th.get_devices()
    readings = []
    if not quiet_output:
        print("Found %i devices" % len(devs))

    for i, dev in enumerate(devs):
        readings.append({
            'device':
            i,
            'temperature_c':
            dev.get_temperature(),
            'temperature_f':
            dev.get_temperature(format="fahrenheit"),
            'ports':
            dev.get_ports(),
            'bus':
            dev.get_bus()
        })

    for reading in readings:
        if quiet_output:
            if degree_unit == 'c':
                print('%0.1f' % reading['temperature_c'])
            elif degree_unit == 'f':
                print('%0.1f' % reading['temperature_f'])
            else:
                print('how did I end up here?')
                sys.exit(1)
        else:
            if disp_ports:
                portinfo = " (bus %s - port %s)" % (reading['bus'],
                                                    reading['ports'])
            else:
                portinfo = ""
            print('Device #%i%s: %0.1f°C %0.1f°F' %
                  (reading['device'], portinfo, reading['temperature_c'],
                   reading['temperature_f']))
コード例 #5
0
def getTemp():
    th = TemperHandler()
    devs = th.get_devices()
    t = devs[0].get_temperatures()
    return  t[0]['temperature_c']