Example #1
0
class Node(rom.Model):
    name = rom.String(required=True, index=True, keygen=rom.FULL_TEXT)
    uid = rom.String(required=True, index=True, unique=True, keygen=rom.FULL_TEXT)
    presence = rom.Boolean(default=False)  # Is it working, or has LastWill fired
    location = rom.ManyToOne('Location', required=True, on_delete='no action')  # human readable location
    # location_url = rom.String(required=True)  # TODO tree-based nested url eg farm/field1/greenhouse3/bed2
    tags = rom.String(index=True, keygen=rom.FULL_TEXT)
    # jobs = rom.Json()
    sensors = rom.OneToMany('Sensor')
    created_at = rom.DateTime()  # When was this node first activated
    woken_at = rom.DateTime()  # When did this node last wake up
    power = rom.Float()  # Battery power remaining
    lon = rom.Float()
    lat = rom.Float()
    def geo_cb(data):
        return data
    geo_index = [
        # callback function passed to GeoIndex as the 2nd argument *must*
        # return a dictionary containing 'lon' and 'lat' values, as degrees
        rom.GeoIndex('geo_index', geo_cb),
        ]

    def init(self):
        # self.jobs = {}
        # self.sensors = {}
        self.created_at = datetime.now()
        self.woken_at = datetime.now()
        self.presence = False
        self.power = 0.0
        return self

    def update_from_report(self, report):
        return

    def update_presence(self, presence):
        if presence.lower() == "connected":
            self.presence = True
        elif presence.lower() == "disconnected":
            self.presence = False
        self.save()

    def to_json(self):
        d = {}
        d['name'] = self.name
        d['uid'] = self.uid
        d['presence'] = self.presence
        d['tags'] = self.tags
        d['created_at'] = self.created_at
        d['woken_at'] = self.woken_at
        d['power'] = self.power
        d['lat'] = self.lat
        d['lon'] = self.lon
        d['location'] = self.location.url
        d['sensors'] = [s.to_json() for s in self.sensors]
        return d
Example #2
0
class Source(rom.Model):
    '''A service from which to receive requests'''
    created = rom.DateTime(required=True, default=datetime.now)
    name = rom.String(required=True)
    slug = rom.String(required=True, unique=True)
    destinations = rom.OneToMany('Destination')
    requests = rom.OneToMany('Request')

    def __init__(self, *args, **kwargs):
        # HACK: ideally, this happens on save.
        # rom does validation on init, so we need to do it here
        kwargs.setdefault('slug', slugify(kwargs.get('name', '')))
        super(Source, self).__init__(*args, **kwargs)
Example #3
0
class Location(rom.Model):
    name = rom.String(required=True, index=True, keygen=rom.FULL_TEXT)
    url = rom.String(required=True, index=True, unique=True, keygen=rom.FULL_TEXT)
    description = rom.String(required=True)
    parent = rom.ManyToOne('Location', on_delete='cascade')
    children = rom.OneToMany('Location', column='parent')
    nodes = rom.OneToMany('Node', column='location')
    # outline of grid coordinates for map
    def to_json(self):
        d = {}
        d['name'] = str(self.name.decode('utf-8'))
        d['description'] = str(self.description.decode('utf-8'))
        d['url'] = str(self.url.decode('utf-8'))
        d['parent'] = {'name': self.parent.name.decode('utf-8'), 'url': self.parent.url.decode('utf-8')}
        d['children'] = [{'name':c.name.decode('utf-8'), 'url':c.url.decode('utf-8')} for c in self.children]
        d['nodes'] = [str(n.uid.decode('utf-8')) for n in self.nodes]
        return dumps(d)
Example #4
0
class Sensor(rom.Model):
    name = rom.String(required=True, index=True, keygen=rom.FULL_TEXT)
    uid = rom.String(required=True, index=True, unique=True, keygen=rom.FULL_TEXT)
    # uid = "~".join([_self.name, sensor_type, job_name, pin])  # Tag so we can identify this function later.
    sensor_type = rom.String(required=True, index=True, keygen=rom.FULL_TEXT)  # What kind of sensor is this? eg DHT_11
    pin = rom.String()  # Pin identifier on chip, adc, i2c
    period = rom.Integer()  # In seconds
    units = rom.String()  # Human readable units
    node = rom.ManyToOne('Node', on_delete='cascade', required=True)
    def to_json(self):
        s = {}
        s['name'] = str(self.name.decode('utf-8'))
        s['uid'] = str(self.uid.decode('utf-8'))
        s['sensor_type'] = str(self.sensor_type.decode('utf-8'))
        s['pin'] = str(self.pin.decode('utf-8'))
        s['period'] = str(self.period)
        s['units'] = str(self.units.decode('utf-8'))
        s['node'] = str(self.node)
        return s
Example #5
0
class Users(rom.Model):
    email = rom.String(required=True, unique=True)
    salt = rom.String()
    hash = rom.String()
    phone_number = rom.String(default="")
    balance = rom.Float()
Example #6
0
class User(rom.Model):
    email = rom.String(required=True, unique=True, suffix=True)
    salt = rom.String()
    password = rom.String()
    created_at = rom.Float(default=time.time)
Example #7
0
class Post(rom.Model):
    user = rom.String(required=True)
    title = rom.String(required=True)
    text = rom.String(required=True)
    created_at = rom.Float(default=time.time)
Example #8
0
class Destination(rom.Model):
    '''A place to forward requests that come from a source'''
    created = rom.DateTime(required=True, default=datetime.now)
    name = rom.String(required=True)
    url = rom.String(required=True)
    source = rom.ManyToOne('Source', required=True)