Beispiel #1
0
def path_print(points, index):
    t = str(timeutil.timestamp(ms=True)).replace(".", "-")
    log.info("Drawing path...")
    ctx = drawing.Context(3000,
                          int(3000 / RATIO),
                          relative=True,
                          flip=True,
                          hsv=True)
    ctx.image("basemap/basemap.png")
    midline = sum([point.x for point in points]) / len(points)
    poss = []
    for p in range(len(points)):
        x1, y1 = points[p].x, points[p].y
        if p < len(points) - 1:
            x2, y2 = points[p + 1].x, points[p + 1].y
            ctx.line(x1, y1, x2, y2, stroke=(0., 0., .5, 1.), thickness=5.0)
        ctx.arc(x1,
                y1,
                15 / ctx.width,
                15 / ctx.height,
                fill=(0., 0., 0., 1.),
                thickness=0.0)
        flip = False
        if x1 < midline:
            flip = True
        for pos in poss:
            dist_x = abs(x1 - pos[0]) * ctx.height
            dist_y = abs(y1 - pos[1]) * ctx.height
            if dist_y <= 100 and dist_x <= 400:
                flip = not flip
        if not flip:
            x = x1 + (30 / ctx.width)
        else:
            x = x1 - (50 / ctx.width)
        y = y1 - (12 / ctx.height)
        poss.append((x, y))
        ctx.label(x,
                  y,
                  str(p + 1),
                  stroke=(0., 0., 0., 1.),
                  font="Monaco",
                  size=36)
    for p, point in enumerate(points):
        label = "%d) %s %s%s" % (p + 1, "Wake up at" if p == 0 else "%s," %
                                 point.display_time, point.address, "" if p !=
                                 (len(points) - 1) else " ... sleep")
        ctx.label((200 / ctx.width),
                  1.0 - ((200 + (40 * p)) / ctx.height),
                  label,
                  stroke=(0., 0., 0., 1.),
                  font="Monaco",
                  size=36)
    ctx.output("images/%s_path.png" % (index, ))
    log.info("--> done")
Beispiel #2
0
def get_user(user_ids):
    location = {
        '$geoWithin': {
            '$geometry': {
                'type':
                "Polygon",
                'coordinates': [[[LON_1, LAT_1], [LON_2,
                                                  LAT_1], [LON_2, LAT_2],
                                 [LON_1, LAT_2], [LON_1, LAT_1]]]
            }
        }
    }
    for u, user_id in enumerate(user_ids):
        log.info("USER %s..." % user_id)
        cursor = db.entries.find({
            'user_id': user_id,
            'location': location,
            't': {
                '$gt':
                timeutil.timestamp(
                    timeutil.string_to_dt(config['start_date'],
                                          tz="America/New_York")),
                '$lt':
                timeutil.timestamp(
                    timeutil.string_to_dt(config['stop_date'],
                                          tz="America/New_York"))
            }
        }).sort('t')
        points = [
            Point(point['location']['coordinates'][0],
                  point['location']['coordinates'][1], point['t'])
            for point in cursor
        ]
        log.info("--> %d points" % len(points))
        yield user_id, points
    yield (None, None)
Beispiel #3
0
 def run(self):
     t_start = time.time()
     while True:
         try:
             message, address = self.socket.recvfrom(
                 1024)  # shit, this is where the limit comes from
             ip, port = address
             data = message.decode('utf-8')
             if not len(data):
                 continue
             try:
                 data = data.split(',')
                 esp_id = int(data[0])
                 if esp_id not in self.events:
                     self.events[esp_id] = []
                 if esp_id not in self.rates:
                     self.rates[esp_id] = 0
                 self.events[esp_id].append(1)
                 data = {
                     'id': esp_id,
                     'rssi': int(data[1]),
                     'bat': int(float(data[2])),
                     'rate': self.rates[esp_id],
                     'ip': ip,
                     't_utc': timeutil.timestamp(ms=True),
                     't': float(data[3]) / 1000.0,
                     'mag': float(data[4])
                 }
                 self.messages.put(data)
             except Exception as e:
                 log.error(log.exc(e))
                 continue
             elapsed_t = time.time() - t_start
             if elapsed_t >= 1:
                 for esp_id in self.events:
                     events = len(self.events[esp_id])
                     rate = math.floor(events / elapsed_t)
                     self.rates[esp_id] = rate
                     self.events[esp_id] = []
                 t_start = time.time()
         except Exception as e:
             log.error(log.exc(e))
Beispiel #4
0
def draw():
    t_now = timeutil.timestamp(ms=True)

    # do labels
    for s, (esp_id, (t, rssi)) in enumerate(sensor_rssi.copy().items()):
        if t_now - t > 3:
            bar = 0.01
        else:
            bar = 1.0 - (max(abs(rssi) - 25, 0) / 100)
        x = (20 + (s * 20)) / ctx.width
        ctx.line(x, .1, x, (bar * 0.9) + .1, color=COLORS[esp_id % len(COLORS)], thickness=10)
        if esp_id not in labels:
            print("Adding label for esp_id %s" % esp_id)
            labels.append(esp_id)
            ctx.label(x, .05, str(esp_id), font="Monaco", size=10, width=10, center=True)

    # data
    for s, esp_id in enumerate(list(sensor_data)):
        samples = sensor_data[esp_id]    
        if len(samples):
            ctx.lines([ ((t_now - sample[0]) / 10.0, (sample[1] - RANGE[0] - 9.8) / (RANGE[1] - RANGE[0]) ) for sample in list(samples)], color=COLORS[esp_id % len(COLORS)])    # subtract 9.8 to center it
Beispiel #5
0
def path(points):
    t = str(timeutil.timestamp(ms=True)).replace(".", "-")
    log.info("Drawing path...")
    ctx = drawing.Context(3000,
                          int(3000 / RATIO),
                          relative=True,
                          flip=True,
                          hsv=True)
    ctx.image("basemap/basemap.png")
    for p in range(len(points)):
        x1, y1 = points[p].x, points[p].y
        color = points[p].period / PERIODS, 1., 1., 1.
        ctx.arc(x1,
                y1,
                5 / ctx.width,
                5 / ctx.height,
                fill=color,
                thickness=0.0)
        if p < len(points) - 1:
            x2, y2 = points[p + 1].x, points[p + 1].y
            ctx.line(x1, y1, x2, y2, stroke=color, thickness=1.0)
    ctx.output("images/%s_path.png" % t)
    log.info("--> done")
Beispiel #6
0
            'ph': ph,
            #'oxygen_mgl': oxygen,
            }

log.info("Processing...")

tses = {}
for label, results in streams.items():
    ts = [v['t_utc'] for v in results]
    duration = ts[-1] - ts[0]
    log.info("--> duration (%s)\t%s %s %s" % (label, timeutil.seconds_to_string(duration), timeutil.t_to_string(ts[0], tz='America/Denver'), timeutil.t_to_string(ts[-1], tz='America/Denver')))
    tses[label] = ts

t_min = min([ts[0] for ts in tses.values()])
t_max = max([ts[-1] for ts in tses.values()])
t_min = timeutil.timestamp(timeutil.string_to_dt("2015-01-01", tz='America/Denver'))
t_max = timeutil.timestamp(timeutil.string_to_dt("2016-01-01", tz='America/Denver'))

log.info("T_MIN %s" % t_min)
log.info("T_MAX %s" % t_max)

signals = []
labels = list(streams.keys())
log.info("LABELS %s" % labels)
for label in labels:
    log.info(label)
    ts = tses[label]
    ts = [t_min] + ts + [t_max]
    values = [d[label] if label in d else None for d in streams[label]]
    values = [values[0]] + values + [values[-1]]
    values = sp.remove_shots(values, nones=True)  # repair missing values
Beispiel #7
0
print("Parsing...")
with open(PATH) as csvfile:
    reader = csv.reader(csvfile)
    for r, row in enumerate(reader):
        if r < 1:
            continue
        if len(''.join(row).strip()) == 0:
            continue

        row = [strings.as_numeric(item) for item in row]
        row = [(None if type(item) == str and not len(item) else item) for item in row]
        
        try:
            if row[0] is not None:
                dt_1 = timeutil.string_to_dt(row[0], tz="America/Denver")
                t_utc_1 = timeutil.timestamp(dt_1)
                datestring_1 = timeutil.t_to_string(t_utc_1, tz="America/Denver") 
                oxygen_mgl.append(dict(zip(['t_utc', 'oxygen_mgl'], [t_utc_1, row[1]])))
        except Exception as e:
            pass

        try:
            if row[2] is not None:
                dt_2 = timeutil.string_to_dt(row[2], tz="America/Denver")
                t_utc_2 = timeutil.timestamp(dt_2)
                datestring_2 = timeutil.t_to_string(t_utc_2, tz="America/Denver") 
                discharge_cfs.append(dict(zip(['t_utc', 'discharge_cfs'], [t_utc_2, row[3]])))
        except Exception as e:
            continue

print("Sorting...")
Beispiel #8
0
# start = "2016-04-06"
start = "2016-04-15"
stop = "2016-05-30"

# start = "2016-07-07"
# start = "2016-08-01"
# stop = "2016-08-07"

start = "2017-02-27"
stop = "2017-03-01"

print(config['sites'][site])
query = {
    'site': site,
    't_utc': {
        '$gt': timeutil.timestamp(timeutil.string_to_dt(start,
                                                        tz=config['tz'])),
        '$lt': timeutil.timestamp(timeutil.string_to_dt(stop, tz=config['tz']))
    }
}
log.info(query)
results = db.entries.find(query)
results = list(results)
print(json.dumps(results[0], indent=4, default=lambda d: str(d)))

ts = [d['t_utc'] for d in results]
duration = ts[-1] - ts[0]
print("DURATION %s %s" % (duration, strings.format_time(duration)))
signals = []
rates = []
labels = list(config['labels'].values())
labels.sort()
Beispiel #9
0
# stop = "2016-04-16"
# start = "2016-05-01"

# start = "2016-04-06"
start = "2016-04-15"
stop = "2016-05-30"

# start = "2016-07-07"
# start = "2016-08-01"
# stop = "2016-08-07"

start = "2017-02-27"
stop = "2017-03-01"

print(config['sites'][site])
query = {'site': site, 't_utc': {'$gt': timeutil.timestamp(timeutil.string_to_dt(start, tz=config['tz'])), '$lt': timeutil.timestamp(timeutil.string_to_dt(stop, tz=config['tz']))}}
log.info(query)
results = db.entries.find(query)
results = list(results)
print(json.dumps(results[0], indent=4, default=lambda d: str(d)))

ts = [d['t_utc'] for d in results]
duration = ts[-1] - ts[0]
print("DURATION %s %s" % (duration, strings.format_time(duration)))
signals = []
rates = []
labels = list(config['labels'].values())
labels.sort()
for i, label in enumerate(labels):
    log.info(label)
    try:
Beispiel #10
0
data = []
with open(FILENAME) as f:
    for l, line in enumerate(f):
        try:
            line = line.strip()
            if not len(line):
                continue
            line = line.split(',')
            if l == 0:
                fields = line
                continue
            line = [strings.as_numeric(field.strip('"')) for field in line]
            entry = dict(zip(fields, line))
            del entry['uid']
            entry['user_id'] = entry['i']
            del entry['i']
            point = Point((entry['lon'], entry['lat'], entry['alt']))
            entry['location'] = point
            del entry['lon']
            del entry['lat']
            del entry['alt']
            dt = timeutil.string_to_dt(entry['date'])
            entry['t'] = timeutil.timestamp(dt)
            print(json.dumps(entry, indent=4))
            db.entries.insert(entry)
        except Exception as e:
            log.exc(log.warning(e))


## NOTE: this ends up labeling all local times as UTC without conversion
## that's intended, because then we can just work naively
Beispiel #11
0
 def run(self):
     while True:
         try:
             message, address = self.socket.recvfrom(1024)   # shit, this is where the limit comes from
             ip, port = address
             batch = message.decode('utf-8')
             batch = batch.split(';')
             for d, data in enumerate(batch):
                 if not len(data):
                     continue
                 try:
                     data = data.split(',')
                     esp_id = int(data[0])
                     data = {'id': esp_id, 'rssi': int(data[1]), 'bat': int(float(data[2])), 'ip': ip, 't_utc': timeutil.timestamp(ms=True), 't': float(data[3]) / 1000.0, 'mag': float(data[4])}
                     self.messages.put(data)
                 except Exception as e:
                     log.error(log.exc(e))
         except Exception as e:
             log.error(log.exc(e))