<commit_before>from django.core.management.base import BaseCommand

from dragonfly import models

import requests
import time

import serial
from os import walk
import json

from multiprocessing import Process


def getInfo(device):
    time.sleep(1)
    print "starting device"
    ser = serial.Serial('/dev/{}'.format(device), 9600)
    Alive = True
    pollRate = 10
    start = time.time() - pollRate
    while(Alive):
        if time.time() > start + pollRate:
            data = ser.readline()
            data = data.replace("'", '"')
            if data.startswith('["data'):
                start = time.time()
                serData = json.loads(data)
                for i in serData:
                    if "station" not in i:
                        continue
                    for j in i['sensors']:
                        try:
                            sensor = models.Sensor.objects.get(name=j['sensor'])
                        except Exception, e:
                            print "Creating new sensor"
                            print e
                            sensor = models.Sensor(name=j['sensor'], description='mydesc', coefficients="(1,0)")
                            sensor.save()

                        newReading = models.Reading(sensor=sensor, value=j['value'])
                        newReading.save()

                        print i

                # print json.loads(data).json()


class Command(BaseCommand):
    help = 'Load a days worth of data.'

    def handle(self, *args, **options):
        pollTimer = time.time() - 10
        currentDevices = {}
        while(True):
            if time.time() > pollTimer + 10:
                pollTimer = time.time()

                f = []
                for (dirpath, dirnames, filenames) in walk("/dev/"):
                    f.extend(filenames)
                devices = []
                for i in f:
                    if i.startswith('ttyUSB'):
                        devices.append(i)
                print"Devices found:"
                print devices
                for j in devices:
                    if j not in currentDevices or currentDevices[j].is_alive() is False:
                        print "New device found, starting serial: {}".format(j)
                        p = Process(target=getInfo, args=(j, ))<commit_msg>added type to sensor polling<commit_after>from django.core.management.base import BaseCommand

from dragonfly import models

import requests
import time

import serial
from os import walk
import json

from multiprocessing import Process


CommandsQueue = []


def getInfo(device):
    time.sleep(1)
    print "starting device"
    ser = serial.Serial('/dev/{}'.format(device), 9600)
    Alive = True
    pollRate = 10
    start = time.time() - pollRate
    while(Alive):
        if time.time() > start + pollRate:
            data = ser.readline()
            data = data.replace("'", '"')
            if data.startswith('["data'):
                start = time.time()
                serData = json.loads(data)
                for i in serData:
                    if "station" not in i:
                        continue
                    for j in i['sensors']:
                        try:
                            sensor = models.Sensor.objects.get(name=j['sensor'])
                        except Exception, e:
                            print "Creating new sensor"
                            print e
                            sensor = models.Sensor(name=j['sensor'], description='mydesc', coefficients="(1,0)", type=j['type'])
                            sensor.save()

                        newReading = models.Reading(sensor=sensor, value=j['value'])
                        newReading.save()
        if len(CommandsQueue) > 0:
            ser.write(CommandsQueue[0])
            CommandsQueue.remove(0)


class Command(BaseCommand):
    help = 'Load a days worth of data.'

    def handle(self, *args, **options):
        pollTimer = time.time() - 10
        currentDevices = {}
        while(True):
            if time.time() > pollTimer + 10:
                pollTimer = time.time()

                f = []
                for (dirpath, dirnames, filenames) in walk("/dev/"):
                    f.extend(filenames)
                devices = []
                for i in f:
                    if i.startswith('ttyUSB'):
                        devices.append(i)
                print"Devices found:"
                print devices
                for j in devices:
                    if j not in currentDevices or currentDevices[j].is_alive() is False:
                        print "New device found, starting serial: {}".format(j)
                        p = Process(target=getInfo, args=(j, ))
{'fraction_non_alphanumeric': 0.10313618185645128, 'fraction_numerical': 0.006103978109871606, 'mean_word_length': 1.364179104477612, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '10526', 'n_tokens_mistral': 1255, 'n_tokens_neox': 1171, 'n_words': 329}