def init_db(umod): tb_name= "cfg_wifi_sta" tb_schema = OrderedDict([ ("name", ""), ("ssid", ""), ("passwd", ""), ("active", 0), ]) umod.mod_add(tb_name, tb_schema) tb_name= "cfg_wifi_ap" tb_schema = OrderedDict([ ("name", ""), ("essid", ""), ("channel", ""), ("password", ""), ("authmode", ""), ("active", 1), ("delay", 360), ]) umod.mod_add(tb_name, tb_schema)
def init_db(umod): tb_name = "board_cfg" tb_schema = OrderedDict([("name", ""), ("board", ""), ("uid", ""), ("client", ""), ("init", 0)]) umod.mod_add(tb_name, tb_schema) tb_name = "board_mod" tb_schema = OrderedDict([ ("name", ""), ("active", ""), ("status", ""), ]) umod.mod_add(tb_name, tb_schema)
class networkTable(uorm.Model): # Create network table __db__ = _dbc __table__ = "network" __schema__ = OrderedDict([ ("timestamp", uorm.now), ("spi", 0), ("cs", ""), ("rst", ""), ("ssid", ""), ("key", ""), ("fbssid", ""), ("fbkey", ""), ("ip", ""), ("gateway", ""), ("subnet", ""), ("dns", ""), ("mode", "STA"), ]) @classmethod def getrow(cls): try: res = next(cls.get()) except StopIteration: return None return res
class pluginstoreTable(uorm.Model): # Create plugin table __db__ = _dbc __table__ = "pluginstore" __schema__ = OrderedDict([ ("timestamp", uorm.now), ("name", ""), ("data", ""), ]) @classmethod def mapkeys(cls, obj): return [obj.get(k) for k in cls.__schema__.keys()] @classmethod def public(cls): res = [x for x in cls.get()] return res @classmethod def getrow(cls): res = next(cls.get()) return res @classmethod def list(cls): res = [x for x in cls.scan()] return res.values
class LoginData(uorm.Model): __db__ = db __table__ = "login" __schema__ = OrderedDict([ ("timestamp", ("TIMESTAMP", uorm.now)), ("archived", ("INT", 0)), ("username", ("TEXT", "")), ("password", ("TEXT", "")), ("email", ("TEXT", "")), ("street", ("TEXT", "")), ("city", ("TEXT", "")), ("postcode", ("TEXT", "")), ("country", ("TEXT", "")), ("mobile", ("TEXT", "")), ("content", ("TEXT", "")), ]) @classmethod def public(cls): print("public") for v in cls.__db__.db.values(None, None, btree.DESC): res = ujson.loads(v) row = cls.Row(*res) if row.archived: continue yield row
class configTable(uorm.Model): # Create config table __db__ = _dbc __table__ = "config" __schema__ = OrderedDict([ ("timestamp", uorm.now), ("name", "uPyEasy"), ("unit", 0), ("port", 80), ("ssl", "off"), ("password", ""), ("sleepenable", ""), ("sleeptime", 60), ("sleepfailure", ""), ("version", core.__build__), ]) @classmethod def getrow(cls): # if cached: return cached record if hasattr(cls, '_config'): return cls._config # no cache: fetch it! try: cls._config = next(cls.get()) except StopIteration: return None return cls._config
class LoginData(uorm.Model): __db__ = db __table__ = "login" __schema__ = OrderedDict([ ("timestamp", ("TIMESTAMP", uorm.now)), ("archived", ("INT", 0)), ("username", ("TEXT", "")), ("password", ("TEXT", "")), ("email", ("TEXT", "")), ("street", ("TEXT", "")), ("city", ("TEXT", "")), ("postcode", ("TEXT", "")), ("country", ("TEXT", "")), ("mobile", ("TEXT", "")), ("content", ("TEXT", "")), ]) @classmethod def mapkeys(cls, obj): return [obj.get(k) for k in cls.__schema__.keys()] @classmethod def public(cls): res = [x for x in cls.scan() if x.archived == 0] res.sort(key=lambda x: x.timestamp, reverse=True) return res
class ruleTable(uorm.Model): # Create script table __db__ = _dbc __table__ = "rule" __schema__ = OrderedDict([ ("timestamp", uorm.now), ("id", 1), ("enable", "off"), ("name", ""), ("filename", ""), ("delay", 60), ]) @classmethod def mapkeys(cls, obj): return [obj.get(k) for k in cls.__schema__.keys()] @classmethod def public(cls): res = [x for x in cls.get()] return res @classmethod def getrow(cls): res = next(cls.get()) return res @classmethod def list(cls): res = [x for x in cls.scan()] return res.values
class serviceTable(uorm.Model): # Create service table __db__ = _dbc __table__ = "service" __schema__ = OrderedDict([ ("timestamp", uorm.now), ("id", 1), ("name", ""), ("server", ""), ("port", ""), ("template", ""), ]) @classmethod def mapkeys(cls, obj): return [obj.get(k) for k in cls.__schema__.keys()] @classmethod def public(cls): res = [x for x in cls.get()] return res @classmethod def getrow(cls): res = next(cls.get()) return res @classmethod def list(cls): res = [x for x in cls.scan()] return res
class Config(uorm.Model): __db__ = db __table__ = "config" __schema__ = OrderedDict([ ("key", ("TEXT", "")), ("value", ("TEXT", "")), ])
class notificationTable(uorm.Model): # Create notification table __db__ = _dbc __table__ = "notification" __schema__ = OrderedDict([ ("timestamp", uorm.now), ("id", 1), ("serviceid", 1), ("enable", ""), ]) @classmethod def mapkeys(cls, obj): return [obj.get(k) for k in cls.__schema__.keys()] @classmethod def public(cls): res = [x for x in cls.get()] return res @classmethod def getrow(cls): res = next(cls.get()) return res @classmethod def list(cls): res = [x for x in cls.scan()] return res
def __init__(self, *args, **kwargs): if len(args) == 1 and type(args[0]) in (bytes, bytearray, memoryview): b = memoryview(args[0]) elif 'type' in kwargs: if kwargs['type'] == SNMP_TRAP: b = memoryview(_SNMP_TRAP_TEMPL) else: b = memoryview(_SNMP_GETSET_TEMPL) else: raise ValueError('buffer or type=x required') ptr = 1 + frombytes_lenat(b, 0)[1] ptr = self._frombytes_props(b, ptr, _SNMP_PROPS) self.type = b[ptr] ptr += 1 + frombytes_lenat(b, ptr)[1] #step into seq if self.type == SNMP_TRAP: ptr = self._frombytes_props(b, ptr, _SNMP_TRAP_PROPS) else: ptr = self._frombytes_props(b, ptr, _SNMP_GETSET_PROPS) ptr += 1 + frombytes_lenat(b, ptr)[1] try: self.varbinds = OrderedDict() except: self.varbinds = {} while ptr < len(b): ptr += 1 + frombytes_lenat(b, ptr)[1] #step into seq oid = frombytes_tvat(b, ptr)[1] ptr += 1 + sum(frombytes_lenat(b, ptr)) tv = frombytes_tvat(b, ptr) ptr += 1 + sum(frombytes_lenat(b, ptr)) self.varbinds[oid] = tv for arg in kwargs: if hasattr(self, arg): setattr(self, arg, kwargs[arg])
class Note(uorm.Model): __db__ = db __table__ = "note" __schema__ = OrderedDict([ ("id", ("INT", uorm.id)), ("timestamp", ("TIMESTAMP", uorm.now)), ("archived", ("INT", 0)), ("content", ("TEXT", "")), ]) @classmethod def public(cls): print("public") for v in cls.__db__.db.values(None, None, btree.DESC): res = ujson.loads(v) row = cls.Row(*res) if row.archived: continue yield row @classmethod def get_keys(cls): keys = [] for key in cls.__db__.db: keys.append(key) return keys @classmethod def del_key(cls, key): del cls.__db__.db[key] cls.__db__.db.flush() return 0
class pluginstoreTable(uorm.Model): # Create plugin table __db__ = _dbc __table__ = "pluginstore" __schema__ = OrderedDict([ ("timestamp", uorm.now), ("name", ""), ("data", ""), ]) @classmethod def public(cls): try: res = [x for x in cls.get()] except StopIteration: return None return res @classmethod def getrow(cls): try: res = next(cls.get()) except StopIteration: return None return res @classmethod def delete(cls, timestamp): # delete the table record try: os.remove(cls.fname(timestamp)) except KeyError: return False return True
def save(self, ordered=False): # dump updated setting into json print("Writing new config item to file %s" % self.file) with open(self.file, 'w') as f: if ordered: ujson.dump(self.config, f) else: ujson.dump(OrderedDict(self.config), f)
class dxmapTable(uorm.Model): # Create dxpin table __db__ = _dbc __table__ = "dxmap" __schema__ = OrderedDict([ ("timestamp", uorm.now), ("count", 0), ("d0", ""), ("d1", ""), ("d2", ""), ("d3", ""), ("d4", ""), ("d5", ""), ("d6", ""), ("d7", ""), ("d8", ""), ("d9", ""), ("d10", ""), ("d11", ""), ("d12", ""), ("d13", ""), ("d14", ""), ("d15", ""), ("d16", ""), ("d17", ""), ("d18", ""), ("d19", ""), ("d20", ""), ("d21", ""), ("d22", ""), ("d23", ""), ("d24", ""), ("d25", ""), ("d26", ""), ("d27", ""), ("d28", ""), ("d29", ""), ("d30", ""), ("d31", ""), ("d32", ""), ("d33", ""), ("d34", ""), ("d35", ""), ("d36", ""), ("d37", ""), ("d38", ""), ("d39", ""), ("d40", ""), ]) @classmethod def getrow(cls): try: res = next(cls.get()) except StopIteration: return None return res
def create_payload(dht_sensor): dht_sensor.measure() measures = OrderedDict([('temperature', dht_sensor.temperature()), ('humidity', dht_sensor.humidity())]) values = [v for _, v in measures.items()] packed = pack('>HH', *values) base64 = b2a_base64(packed) return base64.strip() # dump trailing \n
def init_db(umod): tb_name = "board_pin" tb_schema = OrderedDict([ ("name", ""), ("npin", ""), ("bpin", "") ]) umod.mod_add(tb_name, tb_schema, active="1", up="main")
def mod_get(self, m_name): self.tbl_add() if self._tbl == m_name: return True s_mod = self.mod_sel(m_name) if len(s_mod): self.tbl_add(s_mod[0]["name"], OrderedDict(s_mod[0]["sch"])) return True return False
def schema_init(self): self.config = None if self.schema == Store.__schema__: self.config = Store.__config__ else: sch_obj = self.select_one(self.schema, schema=True) if sch_obj: self.config = OrderedDict(sch_obj["sch"]) self.schema_location()
def __init__(self, store_path="u_config"): _sch_name = "_schema" _sch_conf = OrderedDict([ ("name", ("str", "")), ("sch", ("dict", ())), ]) self.store = uorm.Store(store_schema=_sch_name, store_path=store_path, store_config=_sch_conf) self.lock = asyncio.Lock()
def init_db(umod): tb_name = "cfg_telnet" tb_schema = OrderedDict([ ("name", ""), ("port", ""), ("pswd", ""), ]) umod.mod_add(tb_name, tb_schema, active="1", up="ap")
def __init__(self, scr, num, pin, rings=None): self.screen = scr self.screen.print("{:d} leds, pin {:d}".format(num, pin)) self.leds = neopixel.NeoPixel(machine.Pin(pin), num, timing=1, use_dma=True) self.leds.fill((0, 0, 0)) self.leds.write() self._patterns = OrderedDict([("rainbow", self.pat_rainbow), ("cylon", self.pat_bounce), ("rainbow cylon", self.pat_rainbowcyl), ("marquee", self.pat_marquee), ("solid", self.pat_solid), ("pulse", self.pat_pulse), ("rgb party", self.pat_rgb_party), ("flame", self.pat_flame), ("flame_g", self.pat_flame_g), ("flame_b", self.pat_flame_b)]) self._oneshots = OrderedDict([ ("bump", self.one_bump), ("whoosh", self.one_whoosh), ("rgb", self.one_rgb), ]) if (rings): ring_pats = OrderedDict([("r_radar", self.pat_radar), ("r_tunnel", self.pat_tunnel), ("r_bubbles", self.pat_bubbles)]) self.rings = rings self._patterns.update(ring_pats) else: self.rings = None self.hue = 0 self._colors = OrderedDict([("red", 0), ("orange", 30), ("yellow", 50), ("green", 120), ("blue", 240), ("indigo", 260), ("violet", 280)]) self.intens = 0.2 # 0-1, float self._active = self.pat_rainbow self.period = 0.2 # seconds self.stop_thread = False self.pat_chg = False _thread.start_new_thread(self.led_timer_thread, (None, ))
def mod_get(self, m_name): self.tbl_add() if self._tbl == m_name: return self._sch s_mod = self.mod_sel(m_name) if len(s_mod): sch = OrderedDict(s_mod[0]["sch"]) self.tbl_add(s_mod[0]["name"], sch) return sch return False
def UNION(fields): res = OrderedDict() off = 0 for k, t in fields.items(): if isinstance(t, tuple): assert t[0] == 0 res[k] = (off, t[1]) else: res[k] = off | t off = uctypes.PREV_OFFSET return res
def init_db(umod): tb_name = "cfg_mqtt" tb_schema = OrderedDict([ ("name", ""), ("type", ""), ("ip", ""), ("port", ""), ]) umod.mod_add(tb_name, tb_schema, active="1", up="sta")
def __init__(self, db="u_db"): self.db = uorm.DB(db) self.model = uorm.Model self.model.__db__ = self.db self._tbl = "modules" self._sch = OrderedDict([ ("name", ""), ("sch", ""), ("active", ""), ("up", ""), ])
class advancedTable(uorm.Model): # Create advanced table __db__ = _dbc __table__ = "advanced" __schema__ = OrderedDict([ ("timestamp", uorm.now), ("scripts", "off"), ("rules", "off"), ("notifications", "off"), ("mqttretain", ""), ("messagedelay", 0), ("ntphostname", "pool.ntp.org"), ("ntptimezone", 60), ("ntpdst", ""), ("sysloghostname", ""), ("sysloglevel", 0), ("serialloglevel", 1), ("webloglevel", 0), ("webloglines", 10), ("enablesdlog", ""), ("sdloglevel", 0), ("enableserial", ""), ("serialbaudrate", 115200), ("enablesync", ""), ("syncport", 8888), ("fixedipoctet", 0), ("wdi2caddress", 0), ("usessdp", ""), ("connectfailure", 0), ("i2cstretchlimit", 0), ]) @classmethod def mapkeys(cls, obj): return [obj.get(k) for k in cls.__schema__.keys()] @classmethod def public(cls): res = [x for x in cls.get()] return res @classmethod def getrow(cls): res = next(cls.get()) return res @classmethod def list(cls): res = [x for x in cls.scan()] return res
def adc_gen(pin, delay=None): sample = 0 adc = pyb.ADC(pin) ms_time = utime.ticks_ms() while True: if (delay is None) or (utime.ticks_diff(utime.ticks_ms(), ms_time) > delay): ms_time = utime.ticks_ms() value = adc.read() hist = OrderedDict() hist['#'] = sample hist['adc'] = value yield (sample, value, hist) sample += 1
def init_db(umod): tb_name = "cfg_push" tb_schema = OrderedDict([ ("name", ""), ("b_pin", ""), ("p_on", ""), ("p_mode", ""), ("p_pull", ""), ("ps_mode", ""), ("ps_pin", ""), ("value", ""), ]) umod.mod_add(tb_name, tb_schema, active="1", up="main")