Esempio n. 1
0
def main(ldata, pdata, rdata, tdata, gwid):

    # 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]

    #compute the MD5 digest (hash) from the clear gw id provided by post_processing_gw.py
    global gw_id_md5
    gw_id_md5 = md5.new(gwid).hexdigest()

    #LoRaWAN packet
    if dst == 256:
        src_str = "%0.8X" % src
    else:
        src_str = str(src)

    if (src_str in key_Orion.source_list) or (len(key_Orion.source_list) == 0):

        #remove any space in the message as we use '/' as the delimiter
        #any space characters will introduce error in the json structure and then the curl command will fail
        ldata = ldata.replace(' ', '')

        # 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... to indicate a Fiware-Service and Fiware-ServicePath
        # or simply 22.4 in which case, the nomemclature will be DEF

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

        # no separator
        if nsharp == 0:
            # will use default Fiware-Service and Fiware-ServicePath
            data = ['', '']

            # 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:
                # insert '' to indicate default Fiware-Service
                # as we assume that the only parameter indicate the Fiware-ServicePath
                data_array.insert(0, '')
                # if the length is greater than 2
                if len(data_array[1]) < 3:
                    data_array[1] = ''

            # we have 2 separators
            if nsharp == 2:
                # if the length of a fields is greater than 2 then we take it into account
                if len(data_array[0]) < 3:
                    data_array[0] = ''
                if len(data_array[1]) < 3:
                    data_array[1] = ''

            # get number of '/' separator on data_array[2]
            # because ldata may contain '/' as Fiware-ServicePath name
            nslash = data_array[2].count('/')

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

            # at the end data_array contains
            # ["Fiware-Service", "Fiware-ServicePath", "s1", s1value, "s2", s2value, ...]

        # 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', '')

        nomenclatures = []
        # data to send
        data = []
        data.append(data_array[0])  #Fiware-service (if '' default)
        data.append(data_array[1])  #Fiware-servicePath (if '' default)

        if nslash == 0:
            # old syntax without nomenclature key, so insert only one key
            # we use DEF
            nomenclatures.append("DEF")
            data.append(data_array[2])
        else:
            # completing nomenclatures and data
            i = 2
            while i < len(data_array) - 1:
                nomenclatures.append(data_array[i])
                data.append(data_array[i + 1])
                i += 2

        connected = test_network_available()

        #if we got a response from the server, send the data to it
        if (connected):
            print("Orion: uploading")
            #here we append the device's address to get for instance Sensor2
            #if packet come from a LoRaWAN device with 4-byte devAddr then we will have for instance Sensor01020304
            #where the devAddr is expressed in hex format
            send_data(data, key_Orion.sensor_name + src_str, nomenclatures,
                      tdata)
        else:
            print("Orion: not uploading")

            if (CloudNoInternet_enabled):
                print("Using CloudNoInternet")
                from CloudNoInternet import store_internet_pending
                # we call store_internet_pending to store the message for future upload
                store_internet_pending(ldata, pdata, rdata, tdata, gwid)

        # update connection_failure value
        global connection_failure
        connection_failure = not connected

        # upload data to Orion
        #here we append the device's address to get for instance Sensor2
        #if packet come from a LoRaWAN device with 4-byte devAddr then we will have for instance Sensor01020304
        #where the devAddr is expressed in hex format
        #Orion_uploadData(nomenclatures, data, key_Orion.sensor_name+src_str, tdata)
    else:
        print "Source is not is source list, not sending with CloudOrion.py"
Esempio n. 2
0
def main(ldata, pdata, rdata, tdata, gwid):

	# 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]
	
	#remove microsecond for WAZIUP cloud
	#from 2019-05-14T14:53:10.241191+02:00 to 2019-05-14T14:53:10Z
	dt = parser.parse(tdata)
	tdata = dt.replace(microsecond=0,tzinfo=None).isoformat()+"Z"	
	
	#compute the MD5 digest (hash) from the clear gw id provided by post_processing_gw.py
	global gw_id_md5
	gw_id_md5=md5.new(gwid).hexdigest()
	
	#LoRaWAN packet
	if dst==256:
		src_str="0x%0.8X" % src
	else:
		src_str=str(src)	

	if (src_str in key_WAZIUP.source_list) or (len(key_WAZIUP.source_list)==0):

		#remove any space in the message as we use '/' as the delimiter
		#any space characters will introduce error in the json structure and then the curl command will fail
		ldata=ldata.replace(' ', '')
			
		# 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... to indicate a project_name and a service_path
		# or simply 22.4 in which case, the nomemclature will be DEF
		 		
		# get number of '#' separator
		nsharp=ldata.count('#')
		nslash=0
				
		# no separator
		if nsharp==0:
			# will use default project_name and service_path
			data=['','']

			# 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:
				# insert '' to indicate default project name
				# as we assume that the only parameter indicate the service_path
				data_array.insert(0,'');
				# if the length is greater than 2
				if len(data_array[1])<3:
					data_array[1]=''	

			# we have 2 separators
			if nsharp==2:
				# if the length of a fields is greater than 2 then we take it into account
				if len(data_array[0])<3: 
					data_array[0]=''				
				if len(data_array[1])<3:
					data_array[1]=''
									
			# get number of '/' separator on data_array[2]
			# because ldata may contain '/' in service_path name
			nslash = data_array[2].count('/')
	
			# then reconstruct data_array
			data_array=[data_array[0],data_array[1]]+re.split("/", data_array[2])
				
			# at the end data_array contains
			# ["project_name", "service_path", "s1", s1value, "s2", s2value, ...]
		
		# 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', '')	
																		
		nomenclatures = []
		# data to send
		data = []
		data.append(data_array[0]) #project_name (if '' default)
		data.append(data_array[1]) #service_path (if '' default)
		
		if nslash==0:
			# old syntax without nomenclature key, so insert only one key
			# we use DEF
			nomenclatures.append("DEF")
			data.append(data_array[2])
		else:
			# completing nomenclatures and data
			i=2
			while i < len(data_array)-1 :
				nomenclatures.append(data_array[i])
				data.append(data_array[i+1])
				i += 2

		connected = test_network_available()
	
		#if we got a response from the server, send the data to it	
		if (connected):
			print("CloudWAZIUP: uploading")
			#here we append the device's address to get for instance Sensor2
			#if packet come from a LoRaWAN device with 4-byte devAddr then we will have for instance Sensor01020304
			#where the devAddr is expressed in hex format
			
			append_gw_str=''
	
			if key_WAZIUP.organization_name=='':
				key_WAZIUP.organization_name='ORG'
		
			#default organization name or default username?
			if key_WAZIUP.organization_name=='ORG' or key_WAZIUP.username=='guest':
				#we append the md5 hash to the sensor name: ORG_Sensor2_4b13a223f24d3dba5403c2727fa92e62
				append_gw_str='_'+gw_id_md5	
				
			#key_WAZIUP.service_path=key_WAZIUP.organization_name+key_WAZIUP.service_tree
			#the provided parameter would be the entity name, i.e. UPPA-TESTS_Sensor2
			#here: UPPA is the organization_name, -TESTS is the servire_tree
			
			#if organization name is ORG (default) or username is guest (default) then we append the gw id md5 hash
			
			send_data(data, key_WAZIUP.service_path+'_'+key_WAZIUP.sensor_name+src_str+append_gw_str, nomenclatures, tdata)
		else:
			print("CloudWAZIUP: not uploading")
			
			if (CloudNoInternet_enabled):
				print("Using CloudNoInternet")
				from CloudNoInternet import store_internet_pending
				# we call store_internet_pending to store the message for future upload
				store_internet_pending(ldata, pdata, rdata, tdata, gwid)
			
		# update connection_failure value
		global connection_failure
		connection_failure = not connected
			
	else:
		print "Source is not is source list, not sending with CloudWAZIUP.py"