data = ldata.split('/')

                    #change data in two arrays : nomenclature_array and value_array
                    iteration = 0
                    nomenclature_array = []
                    value_array = []
                    while iteration < len(data):
                        if (iteration == 0 or iteration % 2 == 0):
                            nomenclature_array.append(data[iteration])
                        else:
                            value_array.append(data[iteration])

                        iteration += 1

                    #check if new month
                    remove_if_new_month(now)

                    print("MongoDB: saving the document in the collection...")

                    #saving data in a JSON var
                    str_json_data = "{"
                    iteration = 0
                    while iteration < len(nomenclature_array):
                        #last iteration, do not add "," at the end
                        if iteration == len(nomenclature_array) - 1:
                            str_json_data += "\"" + nomenclature_array[
                                iteration] + "\" : " + value_array[iteration]
                        else:
                            str_json_data += "\"" + nomenclature_array[
                                iteration] + "\" : " + value_array[
                                    iteration] + ", "
Esempio n. 2
0
def main(ldata, pdata, rdata, tdata, gwid):

	now = datetime.datetime.utcnow()
	
	# this is common code to process packet information provided by the main gateway script (i.e. post_processing_gw.py)
	# these information are provided in case you need them	
	arr = map(int,pdata.split(','))
	dst=arr[0]
	ptype=arr[1]				
	src=arr[2]
	seq=arr[3]
	datalen=arr[4]
	SNR=arr[5]
	RSSI=arr[6]
	
	arr = map(int,rdata.split(','))
	bw=arr[0]
	cr=arr[1]
	sf=arr[2]

	# this part depends on the syntax used by the end-device
	# we use: thingspeak_channel#thingspeak_field#TC/22.4/HU/85... 
	#		  ##TC/22.4/HU/85... or TC/22.4/HU/85... or thingspeak_channel##TC/22.4/HU/85... or #thingspeak_field#TC/22.4/HU/85... to use some default value
				
	# get number of '#' separator
	nsharp = ldata.count('#')			
	#no separator
	if nsharp==0:
		#will use default channel and field
		data=['','']
		
		#contains ['', '', "s1", s1value, "s2", s2value, ...]
		data_array = data + re.split("/", ldata)		
	elif nsharp==1:
		#only 1 separator
		
		data_array = re.split("#|/", ldata)
		
		#if the first item has length > 1 then we assume that it is a channel write key
		if len(data_array[0])>1:
			#insert '' to indicate default field
			data_array.insert(1,'');		
		else:
			#insert '' to indicate default channel
			data_array.insert(0,'');		
	else:
		#contains [channel, field, "s1", s1value, "s2", s2value, ...]
		data_array = re.split("#|/", ldata)	
		
	#just in case we have an ending CR or 0
	data_array[len(data_array)-1] = data_array[len(data_array)-1].replace('\n', '')
	data_array[len(data_array)-1] = data_array[len(data_array)-1].replace('\0', '')	
	
	#test if there are characters at the end of each value, then delete these characters
	i = 3
	while i < len(data_array) :
		while not data_array[i][len(data_array[i])-1].isdigit() :
			data_array[i] = data_array[i][:-1]
		i += 2

	print("MongoDB: removing obsolete entries")
	
	#check if new month
	remove_if_new_month(now)

	print("MongoDB: saving the document in the collection...")
	
	#saving data in a JSON var
	str_json_data = "{"
	#start from the first nomenclature
	iteration = 2
	while iteration < len(data_array)-1 :
		#last iteration, do not add "," at the end
		if iteration == len(data_array)-2 :
			str_json_data += "\""+data_array[iteration]+"\" : "+data_array[iteration+1]
		else :
			str_json_data += "\""+data_array[iteration]+"\" : "+data_array[iteration+1]+", "
		iteration += 2
	str_json_data += "}"

	#creating document to add
	doc = {
		"type" : ptype,
		"gateway_eui" : gwid, 
		"node_eui" : src,
		"snr" : SNR, 
		"rssi" : RSSI, 
		"cr" : cr, 
		"datarate" : "SF"+str(sf)+"BW"+str(bw),
		"time" : now,
		"data" : json.dumps(json.loads(str_json_data))
	}

	#adding the document
	add_document(doc)

	print("MongoDB: saving done")	
Esempio n. 3
0
def main(ldata, pdata, rdata, tdata, gwid):

    now = datetime.datetime.utcnow()

    # this is common code to process packet information provided by the main gateway script (i.e. post_processing_gw.py)
    # these information are provided in case you need them
    arr = map(int, pdata.split(','))
    dst = arr[0]
    ptype = arr[1]
    src = arr[2]
    seq = arr[3]
    datalen = arr[4]
    SNR = arr[5]
    RSSI = arr[6]

    arr = map(int, rdata.split(','))
    bw = arr[0]
    cr = arr[1]
    sf = arr[2]

    # this part depends on the syntax used by the end-device
    # we use: TC/22.4/HU/85...
    #
    # but we accept also a_str#b_str#TC/22.4/HU/85...
    # or simply 22.4 in which case, the nomemclature will be DEF

    # get number of '#' separator
    nsharp = ldata.count('#')
    nslash = 0

    #will field delimited by '#' in the MongoDB
    data = ['', '']

    #no separator
    if nsharp == 0:
        # get number of '/' separator on ldata
        nslash = ldata.count('/')

        #contains ['', '', "s1", s1value, "s2", s2value, ...]
        data_array = data + re.split("/", ldata)
    else:
        data_array = re.split("#", ldata)

        # only 1 separator
        if nsharp == 1:
            # get number of '/' separator on data_array[1]
            nslash = data_array[1].count('/')

            # then reconstruct data_array
            data_array = data + re.split("/", data_array[1])

        # we have 2 separators
        if nsharp == 2:
            # get number of '/' separator on data_array[2]
            nslash = data_array[2].count('/')

            # then reconstruct data_array
            data_array = data + re.split("/", data_array[2])

    #just in case we have an ending CR or 0
    data_array[len(data_array) - 1] = data_array[len(data_array) - 1].replace(
        '\n', '')
    data_array[len(data_array) - 1] = data_array[len(data_array) - 1].replace(
        '\0', '')

    #test if there are characters at the end of each value, then delete these characters
    i = 3
    while i < len(data_array):
        while not data_array[i][len(data_array[i]) - 1].isdigit():
            data_array[i] = data_array[i][:-1]
        i += 2

    print("MongoDB: removing obsolete entries")

    #check if new month
    remove_if_new_month(now)

    print("MongoDB: saving the document in the collection...")

    #saving data in a JSON var
    str_json_data = "{"

    # add here the RSSI
    str_json_data += "\"RSSI\" : " + str(RSSI) + ", "

    #start from the first nomenclature
    iteration = 2

    if nslash == 0:
        # no nomenclature, use DEF
        str_json_data += "\"DEF\" : " + data_array[iteration]
    else:
        while iteration < len(data_array) - 1:
            #last iteration, do not add "," at the end
            if iteration == len(data_array) - 2:
                str_json_data += "\"" + data_array[
                    iteration] + "\" : " + data_array[iteration + 1]
            else:
                str_json_data += "\"" + data_array[
                    iteration] + "\" : " + data_array[iteration + 1] + ", "
            iteration += 2

    str_json_data += "}"

    #creating document to add
    doc = {
        "type": ptype,
        "gateway_eui": gwid,
        "node_eui": src,
        "snr": SNR,
        "rssi": RSSI,
        "cr": cr,
        "datarate": "SF" + str(sf) + "BW" + str(bw),
        "time": now,
        "data": json.dumps(json.loads(str_json_data))
    }

    #adding the document
    add_document(doc)

    print("MongoDB: saving done")
Esempio n. 4
0
def main(ldata, pdata, rdata, tdata, gwid):

	now = datetime.datetime.utcnow()
	
	# this is common code to process packet information provided by the main gateway script (i.e. post_processing_gw.py)
	# these information are provided in case you need them	
	arr = map(int,pdata.split(','))
	dst=arr[0]
	ptype=arr[1]				
	src=arr[2]
	seq=arr[3]
	datalen=arr[4]
	SNR=arr[5]
	RSSI=arr[6]
	
	arr = map(int,rdata.split(','))
	bw=arr[0]
	cr=arr[1]
	sf=arr[2]

	# this part depends on the syntax used by the end-device
	# we use: thingspeak_channel#thingspeak_field#TC/22.4/HU/85... 
	#		  ##TC/22.4/HU/85... or TC/22.4/HU/85... or thingspeak_channel##TC/22.4/HU/85... or #thingspeak_field#TC/22.4/HU/85... to use some default value
				
	# get number of '#' separator
	nsharp = ldata.count('#')			
	#no separator
	if nsharp==0:
		#will use default channel and field
		data=['','']
		
		#contains ['', '', "s1", s1value, "s2", s2value, ...]
		data_array = data + re.split("/", ldata)		
	elif nsharp==1:
		#only 1 separator
		
		data_array = re.split("#|/", ldata)
		
		#if the first item has length > 1 then we assume that it is a channel write key
		if len(data_array[0])>1:
			#insert '' to indicate default field
			data_array.insert(1,'');		
		else:
			#insert '' to indicate default channel
			data_array.insert(0,'');		
	else:
		#contains [channel, field, "s1", s1value, "s2", s2value, ...]
		data_array = re.split("#|/", ldata)	
		
	#just in case we have an ending CR or 0
	data_array[len(data_array)-1] = data_array[len(data_array)-1].replace('\n', '')
	data_array[len(data_array)-1] = data_array[len(data_array)-1].replace('\0', '')	
	
	#test if there are characters at the end of each value, then delete these characters
	i = 3
	while i < len(data_array) :
		while not data_array[i][len(data_array[i])-1].isdigit() :
			data_array[i] = data_array[i][:-1]
		i += 2

	print("MongoDB: removing obsolete entries")
	
	#check if new month
	remove_if_new_month(now)

	print("MongoDB: saving the document in the collection...")
	
	#saving data in a JSON var
	str_json_data = "{"
	
	# add here the RSSI
	str_json_data += "\"RSSI\" : "+str(RSSI)+", "
	
	#start from the first nomenclature
	iteration = 2
	while iteration < len(data_array)-1 :
		#last iteration, do not add "," at the end
		if iteration == len(data_array)-2 :
			str_json_data += "\""+data_array[iteration]+"\" : "+data_array[iteration+1]
		else :
			str_json_data += "\""+data_array[iteration]+"\" : "+data_array[iteration+1]+", "
		iteration += 2
	str_json_data += "}"
	
	#creating document to add
	doc = {
		"type" : ptype,
		"gateway_eui" : gwid, 
		"node_eui" : src,
		"snr" : SNR, 
		"rssi" : RSSI, 
		"cr" : cr, 
		"datarate" : "SF"+str(sf)+"BW"+str(bw),
		"time" : now,
		"data" : json.dumps(json.loads(str_json_data))
	}

	#adding the document
	add_document(doc)

	print("MongoDB: saving done")	
Esempio n. 5
0
					data = ldata.split('/')
				
					#change data in two arrays : nomenclature_array and value_array
					iteration = 0
					nomenclature_array = []
					value_array = []
					while iteration<len(data) :
						if (iteration == 0 or iteration%2 == 0) :
						   	nomenclature_array.append(data[iteration])
						else :
						   	value_array.append(data[iteration])
						   	
						iteration += 1
				
					#check if new month
					remove_if_new_month(now)
				
					print("MongoDB: saving the document in the collection...")
				
					#saving data in a JSON var
					str_json_data = "{"
					iteration = 0
					while iteration < len(nomenclature_array) :
						#last iteration, do not add "," at the end
						if iteration == len(nomenclature_array)-1 :
							str_json_data += "\""+nomenclature_array[iteration]+"\" : "+value_array[iteration]
						else :
							str_json_data += "\""+nomenclature_array[iteration]+"\" : "+value_array[iteration]+", "
						iteration += 1
					str_json_data += "}"
				
Esempio n. 6
0
def main(ldata, pdata, rdata, tdata, gwid):

	now = datetime.datetime.utcnow()
	
	# this is common code to process packet information provided by the main gateway script (i.e. post_processing_gw.py)
	# these information are provided in case you need them	
	arr = map(int,pdata.split(','))
	dst=arr[0]
	ptype=arr[1]				
	src=arr[2]
	seq=arr[3]
	datalen=arr[4]
	SNR=arr[5]
	RSSI=arr[6]
	
	arr = map(int,rdata.split(','))
	bw=arr[0]
	cr=arr[1]
	sf=arr[2]

	# this part depends on the syntax used by the end-device
	# we use: TC/22.4/HU/85...
	#
	# but we accept also a_str#b_str#TC/22.4/HU/85...
	# or simply 22.4 in which case, the nomemclature will be DEF
				
	# get number of '#' separator
	nsharp = ldata.count('#')
	nslash=0

	#will field delimited by '#' in the MongoDB
	data=['','']
							
	#no separator
	if nsharp==0:
		# get number of '/' separator on ldata
		nslash = ldata.count('/')
				
		#contains ['', '', "s1", s1value, "s2", s2value, ...]
		data_array = data + re.split("/", ldata)
	else:				
		data_array = re.split("#", ldata)
		
		# only 1 separator
		if nsharp==1:
			# get number of '/' separator on data_array[1]
			nslash = data_array[1].count('/')
		
			# then reconstruct data_array
			data_array=data + re.split("/", data_array[1])	

		# we have 2 separators
		if nsharp==2:
			# get number of '/' separator on data_array[2]
			nslash = data_array[2].count('/')
		
			# then reconstruct data_array
			data_array=data + re.split("/", data_array[2])
		
	#just in case we have an ending CR or 0
	data_array[len(data_array)-1] = data_array[len(data_array)-1].replace('\n', '')
	data_array[len(data_array)-1] = data_array[len(data_array)-1].replace('\0', '')	
	
	#test if there are characters at the end of each value, then delete these characters
	i = 3
	while i < len(data_array) :
		while not data_array[i][len(data_array[i])-1].isdigit() :
			data_array[i] = data_array[i][:-1]
		i += 2

	print("MongoDB: removing obsolete entries")
	
	#check if new month
	remove_if_new_month(now)

	print("MongoDB: saving the document in the collection...")
	
	#saving data in a JSON var
	str_json_data = "{"
	
	# add here the RSSI
	str_json_data += "\"RSSI\" : "+str(RSSI)+", "
	
	#start from the first nomenclature
	iteration = 2
	
	if nslash==0:
		# no nomenclature, use DEF
		str_json_data += "\"DEF\" : "+data_array[iteration]
	else:
		while iteration < len(data_array)-1 :
			#last iteration, do not add "," at the end
			if iteration == len(data_array)-2 :
				str_json_data += "\""+data_array[iteration]+"\" : "+data_array[iteration+1]
			else :
				str_json_data += "\""+data_array[iteration]+"\" : "+data_array[iteration+1]+", "
			iteration += 2
		
	str_json_data += "}"
	
	#creating document to add
	doc = {
		"type" : ptype,
		"gateway_eui" : gwid, 
		"node_eui" : src,
		"snr" : SNR, 
		"rssi" : RSSI, 
		"cr" : cr, 
		"datarate" : "SF"+str(sf)+"BW"+str(bw),
		"time" : now,
		"data" : json.dumps(json.loads(str_json_data))
	}

	#adding the document
	add_document(doc)

	print("MongoDB: saving done")