def main(): temper = Temper() temper_data = temper.read() # for device in temper_data: device = temper_data[DEVICE_ID] temp = device.get('internal temperature') temp_ext = device.get('external temperature') hum = device.get('internal humidity') temp_str = None temp_ext_str = None if temp and hum: temp_str = str(temp+TEMP_CORRECTION) + ';' + str(hum+HUM_CORRECTION) + ';0' elif temp: temp_str = str(temp+TEMP_CORRECTION) if temp_ext: temp_ext_str = str(temp_ext+TEMP_EXT_CORRECTION) # Update device in Domoticz if temp_str: url = "%s/json.htm?type=command¶m=udevice&idx=%d&nvalue=0&svalue=%s" % \ (DOMOTICZ_URL, DOMOTICZ_DEVICE_IDX, temp_str) print(url) resp = requests.get(url) print(resp.text) else: print("Unknown or no response from device %s" % device) # Update external temperature device in Domoticz (if available) if DOMOTICZ_DEVICE_IDX_EXT and temp_ext_str: url = "%s/json.htm?type=command¶m=udevice&idx=%d&nvalue=0&svalue=%s" % \ (DOMOTICZ_URL, DOMOTICZ_DEVICE_IDX_EXT, temp_ext_str) print(url) resp = requests.get(url) print(resp.text)
def render_to_string(self, context=None): t = Temper({ 'indent': None, 'end': '', 'strip': True, }) d = {} for c in RequestContext(self.request): d.update(c) if context: d.update(context) return t.render(self.render, d)
def get_current_temperature(): """ Get the current temperature from the TEMPer USB device. """ temper = Temper() devices: list[dict] = temper.read() keys = ["celsius", "internal temperature"] for device in devices: for key in keys: try: return device[key] except KeyError: continue raise Exception( f"None of the data dicts from the {len(devices)} devices read by Temper contained any of " f"these keys: {''.join(keys)}. Data: {devices!r}")
def collect_from_data_source(self) -> Dict[str, str]: """ Build the data payload. :return: the data """ temper = Temper() retries = 0 while retries < MAX_RETRIES: results = temper.read() if "internal temperature" in results[0].keys(): degrees = {"thermometer_data": str(results)} break self.context.logger.debug( "Couldn't read the sensor I am re-trying.") time.sleep(0.5) retries += 1 return degrees
def _build_data_payload(self) -> Dict[str, Any]: """ Build the data payload. :return: a tuple of the data and the rows """ if self._has_sensor: temper = Temper() while True: results = temper.read() if "internal temperature" in results[0].keys(): degrees = {"thermometer_data": str(results)} else: self.context.logger.debug( "Couldn't read the sensor I am re-trying.") else: degrees = {"thermometer_data": str(randrange(10, 25))} # nosec self.context.logger.info(degrees) return degrees
def __init__(self, tempermgr): self.tempermgr = tempermgr if tempermgr is None: raise RuntimeError("The TemperDeviceMgr class requires a" " TemperMgr but the argument was None.") if _enable_temperusb: self.th = None try: self.th = TemperHandler() except: print("A TEMPerV1 compatible device is not plugged in," " or you have no permission to the usb device." " Try running this script with sudo (sudo python" " TemperatureSanitizer.py) or search online for" " linux usb permissions.") exit(2) self.tds = self.th.get_devices() if len(self.tds) < 1: raise RuntimeError(no_dev_msg) self.tmpr = Temper()
class TemperDeviceMgr: def __init__(self, tempermgr): self.tempermgr = tempermgr if tempermgr is None: raise RuntimeError("The TemperDeviceMgr class requires a" " TemperMgr but the argument was None.") if _enable_temperusb: self.th = None try: self.th = TemperHandler() except: print("A TEMPerV1 compatible device is not plugged in," " or you have no permission to the usb device." " Try running this script with sudo (sudo python" " TemperatureSanitizer.py) or search online for" " linux usb permissions.") exit(2) self.tds = self.th.get_devices() if len(self.tds) < 1: raise RuntimeError(no_dev_msg) self.tmpr = Temper() def getTemp(self, deviceIndex=0): ''' Get the current temperature of the given device. raises: PermissionError (see the permission_help function) ValueError (bad settings['scale']) ''' if _enable_temperusb: return self.tds[deviceIndex].get_temperature( format=self.tempermgr.get('scale') ) devices = self.tmpr.read() c = devices[0]['internal temperature'] if self.tempermgr.isF(): return c_to_f(c) elif self.tempermgr.isC(): return c else: scale = self.tempermgr.get('scale') raise ValueError("The scale {} is unknown.".format(scale))
def main(): # init and call temper to render temper = Temper() print(temper.render(Index().render)) print(temper.render(Page().render)) print(temper.render(SeriousPage().render))
#!/usr/bin/env python from temper import Temper silent_if_ok = False import sys for i in range(1, len(sys.argv)): arg = sys.argv[i] if arg == "--silent-if-ok": silent_if_ok = True else: print("Unknown argument: {}".format(arg)) exit(6) t = Temper() if hasattr(t, 'doctype'): # THIS IS BAD. print("You have the HTML templating engine, which has nothing to") print(" do with github.com/ccwienk/temper but has the same name.") exit(2) elif hasattr(t, 'usb_devices'): if not silent_if_ok: print("You have the correct temper module.") exit(0) else: print("Your temper package is ambiguous. It may either be the HTML" " template engine or the temperature sensor driver.") exit(5)
such as ["Temper High Accurate USB Thermometer Temperature Sensor Data Logger Record for PC Laptop"](https://www.amazon.com/gp/product/B009YRP906/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1) This program uses ccwienk/temper https://github.com/ccwienk/temper as a library (or any fork of the unmaintained urwen/temper), so instead of calling temper.py which would run main, it does something similar to what the main function of that file does. ''' import sys from temper import Temper tmpr = Temper() try: result = tmpr.read() scale = "C" for i in range(1, len(sys.argv)): arg = sys.argv[i] if arg == "-f": scale = "F" if len(result) < 1: print("temper didn't find any devices.") exit(1) # print("result[0]['internal temperature']: {}" # "".format(result[0]['internal temperature'])) from tempermgr import c_to_f c = result[0]['internal temperature']