<commit_before>'''
Created on Nov 7, 2015
Don't blink...
@author: Juan_Insuasti
'''

import sys
import datetime

class Sensor:
    def __init__(self, device, sensorId, type, version, enabled):
        self.db = device.db
        self.device = device
        self.id = sensorId
        self.type = type
        self.version = version
        self.enabled = enabled
        self.path = device.path + "/sensors/" + str(self.id)
        self.timestamp = ""
        self.avgData = ""
        self.filter = False
        self.filterSamples = ""
        self.storageRoute = "/" + str(self.device.id) + "/" + self.id + "/"
        self.dataset = []
        self.datasetLabel = []
        self.datasetMax = 1500;

    #Running AVG filter implementation
    def filterEnable(self, samples):
        self.filter = True
        self.filterSamples = samples
        self.filterData = [0] * self.filterSamples
        self.avgData = 0

    def filterRun(self, data):
        self.filterData.pop()
        self.filterData.insert(0,int(data))
        self.avgData = sum(self.filterData)/self.filterSamples
        print(self.filterData)
        print(self.avgData)

    def filterDisable(self):
        self.filter = False
        self.avgData = ""
        self.filterSamples = ""


    #Update sensor data from device readings
    def updateData(self, data):
        self.data = data
        self.timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        #Saving sensor dataset
        self.datasetDataEntry(data)

        #Running AVG filter implementation
        if self.filter:
            self.filterRun(data)

    #Sets the string that is going to be sent to the database
    def getSensorData(self):
        data = {
            "id": self.id,
            "type": self.type,
            "version": self.version,
            "enabled": self.enabled,
            "data":  self.data,
            "timestamp": self.timestamp,
            "filter": self.filter,
            "avgData": self.avgData,
            "filterSamples": self.filterSamples
            "dataset": self.dataset
            "datasetLabel": self.datasetLabel
            }
        return data

    #Updates data into the Database
    def saveSensorToDB(self):
        data = self.getSensorData()
        self.db.updateData(self.path,data)
        print("Sensor Saved")

    #Saves data into a file
    def saveHistoricRecord(self):
        self.historicFilename = "device_" + self.device.id + "_sensor_" + self.id + "_" + datetime.datetime.now().strftime("%Y-%m-%d") + ".txt"
        fileData = str(self.timestamp) + "," + str(self.data)
        if self.filter:
            fileData += "," + str(self.avgData)
        fileData +=  "\n"

        file = open(self.historicFilename,"a")
        file.write(fileData)
        file.close()
        return self.historicFilename

    #Saves historic data into cloud storage<commit_msg>syntax error<commit_after>'''
Created on Nov 7, 2015
Don't blink...
@author: Juan_Insuasti
'''

import sys
import datetime

class Sensor:
    def __init__(self, device, sensorId, type, version, enabled):
        self.db = device.db
        self.device = device
        self.id = sensorId
        self.type = type
        self.version = version
        self.enabled = enabled
        self.path = device.path + "/sensors/" + str(self.id)
        self.timestamp = ""
        self.avgData = ""
        self.filter = False
        self.filterSamples = ""
        self.storageRoute = "/" + str(self.device.id) + "/" + self.id + "/"
        self.dataset = []
        self.datasetLabel = []
        self.datasetMax = 1500;

    #Running AVG filter implementation
    def filterEnable(self, samples):
        self.filter = True
        self.filterSamples = samples
        self.filterData = [0] * self.filterSamples
        self.avgData = 0

    def filterRun(self, data):
        self.filterData.pop()
        self.filterData.insert(0,int(data))
        self.avgData = sum(self.filterData)/self.filterSamples
        print(self.filterData)
        print(self.avgData)

    def filterDisable(self):
        self.filter = False
        self.avgData = ""
        self.filterSamples = ""


    #Update sensor data from device readings
    def updateData(self, data):
        self.data = data
        self.timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        #Saving sensor dataset
        self.datasetDataEntry(data)

        #Running AVG filter implementation
        if self.filter:
            self.filterRun(data)

    #Sets the string that is going to be sent to the database
    def getSensorData(self):
        data = {
            "id": self.id,
            "type": self.type,
            "version": self.version,
            "enabled": self.enabled,
            "data":  self.data,
            "timestamp": self.timestamp,
            "filter": self.filter,
            "avgData": self.avgData,
            "filterSamples": self.filterSamples,
            "dataset": self.dataset,
            "datasetLabel": self.datasetLabel
            }
        return data

    #Updates data into the Database
    def saveSensorToDB(self):
        data = self.getSensorData()
        self.db.updateData(self.path,data)
        print("Sensor Saved")

    #Saves data into a file
    def saveHistoricRecord(self):
        self.historicFilename = "device_" + self.device.id + "_sensor_" + self.id + "_" + datetime.datetime.now().strftime("%Y-%m-%d") + ".txt"
        fileData = str(self.timestamp) + "," + str(self.data)
        if self.filter:
            fileData += "," + str(self.avgData)
        fileData +=  "\n"

        file = open(self.historicFilename,"a")
        file.write(fileData)
        file.close()
        return self.historicFilename

    #Saves historic data into cloud storage
{'fraction_non_alphanumeric': 0.12062726176115803, 'fraction_numerical': 0.004135791831811132, 'mean_word_length': 2.0355648535564854, 'pattern_counts': {'":': 22, '<': 3, '<?xml version=': 0, '>': 3, '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': '1636089', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1639, 'n_words': 386}