<commit_before>import serial

import sys

import pandas as pd
import datetime

class Server():
    def __init__(self, serial_port):
        '''Constructor with default values only

        TODO have default values and user values as input
        '''
        self.serial_port     = serial_port
        self.serial_baudrate = 115200
        self.serial_parity   = serial.PARITY_ODD
        self.serial_timeout  = None
        
        self.df = pd.DataFrame(columns = ['timestamp','node_id','node_type','value'])
        self.row_iter = 0


    def readlineCR(self, port):
        '''Listen on the serial port and construct a string until \r\n is met
        '''
        print("--- Reading line")
        rline  = ""
        prevch = ''
        while True:
            ch     = str(port.read(), encoding="utf-7")
            #ch     = str(port.read())
            rline += ch
            if (prevch=='\r' or  ch=='\n') or ch=='':
                return rline
            prevch=ch

    def parse_line(self,line_str):
        coma_list = line_str.split(',')
        node_number = coma_list[0].split(':')[1]
        sensor_type, sensor_value = coma_list[1].split(':')
        return node_number, sensor_type, sensor_value
        
    def run(self):
        port = serial.Serial(self.serial_port, 
            baudrate = self.serial_baudrate, 
            timeout  = self.serial_timeout,
            parity   = self.serial_parity,
            stopbits=serial.STOPBITS_TWO,
            bytesize=serial.SEVENBITS)
        while True:
            print("--- Listening...")
            rline = self.readlineCR(port)
            try:
                print("Parsing line: "+rline)
                node_number, sensor_type, sensor_value = self.parse_line(rline)
                self.df.loc[self.row_iter] =  [datetime.datetime.utcnow(),node_number, sensor_type, float(sensor_value)]
                self.row_iter += 1
                self.df.to_csv('../../test.csv',index = False)
<commit_msg>[server-receiver] Robustified parsing<commit_after>import serial

import sys

import pandas as pd
import datetime

class Server():
    def __init__(self, serial_port):
        '''Constructor with default values only

        TODO have default values and user values as input
        '''
        self.serial_port     = serial_port
        self.serial_baudrate = 115200
        self.serial_parity   = serial.PARITY_ODD
        self.serial_timeout  = None
        
        self.df = pd.DataFrame(columns = ['timestamp','node_id','node_type','value'])
        self.row_iter = 0


    def readlineCR(self, port):
        '''Listen on the serial port and construct a string until \r\n is met
        '''
        print("--- Reading line")
        rline  = ""
        prevch = ''
        while True:
            ch     = str(port.read(), encoding="utf-7")
            #ch     = str(port.read())
            rline += ch
            if (prevch=='\r' or  ch=='\n') or ch=='':
                return rline
            prevch=ch

    def parse_line(self,line_str):
        node_number  = "-1" 
        sensor_type  = "nosensor"
        sensor_value = "-1.0"

        coma_list    = line_str.split(',')
        keys1 = []
        keys2 = []

        if (len(coma_list) < 2):
            sensor_type  = "blogmessage"
            return node_number, sensor_type, sensor_value
        else:
            keys1 = coma_list[0].split(':')
            keys2 = coma_list[1].split(':')

        if (len(keys1) > 1):
            #if keys[0] == "nodeid"
            node_number = keys1[1] 
        else:
            # No node id found
            node_number = "-2"

        if (len(keys2) > 1):
            sensor_type  = keys2[0]
            sensor_value = keys2[1]
        # else sensor_type = "nosensor"

        return node_number, sensor_type, sensor_value
        
    def run(self):
        port = serial.Serial(self.serial_port, 
            baudrate = self.serial_baudrate, 
            timeout  = self.serial_timeout,
            parity   = self.serial_parity,
            stopbits=serial.STOPBITS_TWO,
            bytesize=serial.SEVENBITS)
        while True:
            print("--- Listening...")
            rline = self.readlineCR(port)
            try:
                print("Parsing line: "+rline)
                node_number, sensor_type, sensor_value = self.parse_line(rline)
                self.df.loc[self.row_iter] =  [datetime.datetime.utcnow(), node_number, sensor_type, float(sensor_value)]
                self.row_iter += 1
                self.df.to_csv('../../test.csv',index = False)

{'fraction_non_alphanumeric': 0.11546031051825935, 'fraction_numerical': 0.009403017712661273, 'mean_word_length': 1.758745476477684, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, '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': '5607407', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1341, 'n_words': 301}