def store_gps_coordinate(src, SNR, RSSI, seq, bc, lat, lgt, fxt, tdata, gwid): #------------------------------------------------------------ #open gateway_conf.json file #------------------------------------------------------------ gwconf_filename = "gateway_conf.json" f = open(os.path.expanduser(gwconf_filename),"r") lines = f.readlines() f.close() array = "" #get all the lines in a string for line in lines : array += line #change it into a python array gw_json_array = json.loads(array) try: _gw_lat = gw_json_array["gateway_conf"]["ref_latitude"] except KeyError: _gw_lat = "undef" try: _gw_long = gw_json_array["gateway_conf"]["ref_longitude"] except KeyError: _gw_long = "undef" try: gw_lat = float(_gw_lat) except ValueError: _gw_lat = "undef" try: gw_long = float(_gw_long) except ValueError: _gw_long = "undef" if (_gw_lat=="undef") or (_gw_long=="undef"): print 'GPS file: gw has no valid GPS coordinate' distance=-1 else: # gw pA = (gw_lat, gw_long) pB = (float(lat), float(lgt)) distance = haversine(pA, pB) #create the .csv file. First line is for the gateway's position #use for instance https://www.gpsvisualizer.com/ if not os.path.isfile(os.path.expanduser(_gps_csv)): f = open(os.path.expanduser(_gps_csv),"a") data = 'name,time,desc,symbol,latitude,longitude' f.write(data+'\n') data = 'GW,,gateway,wedge,'+_gw_lat+','+_gw_long f.write(data+'\n') f.close() #add the new position in the csv file f = open(os.path.expanduser(_gps_csv),"a") data = 'gps'+src+','+tdata+',"'+str(bc)+'('+str("%.3f" % distance)+'km)",googlemini,'+lat+','+lgt f.write(data+'\n') f.close() #open gps file to store the GPS coordinate and the calculated distance f = open(os.path.expanduser(_gps_file),"a") data = 'src '+src+' seq '+str(seq)+' bc '+str(bc)+' snr '+str(SNR)+' rssi '+str(RSSI)+' time '+tdata+' gw '+gwid+' fxt '+fxt+' lat '+lat+' lgt '+lgt+' distance '+str("%.4f" % distance) f.write(data+'\n') f.close() print 'GPS file: lat='+lat print 'GPS file: lgt='+lgt print 'GPS file: d='+"%.4f" % distance #update the list of remote GPS device #information from device older than key_GpsFile.active_interval_minutes will be marked not active update_gps_device(src, SNR, RSSI, seq, bc, lat, lgt, fxt, str("%.4f" % distance), tdata, gwid) if (key_GpsFile.SMS==True): try: gammurc_file=key_GpsFile.gammurc_file except AttributeError: gammurc_file="/home/pi/.gammurc" print 'GPS file: SMS is requested' #check Gammu configuration if (not libSMS.gammuCheck()): print 'GPS file: Gammu is not available' sys.exit() else: if (not libSMS.gammurcCheck(gammurc_file)): print 'GPS file: gammurc file is not available' sys.exit() if (libSMS.phoneConnection(gammurc_file, key_GpsFile.PIN) == None): print 'GPS file: Can not connect to cellular network' sys.exit() else: sm = libSMS.phoneConnection(gammurc_file, key_GpsFile.PIN) print "GPS file: Sending SMS" success = libSMS.send_sms(sm, data, key_GpsFile.contacts) if (success): print "GPS file: Sending SMS done"
print "Initializing gammu for SMS" else: print "overriding use_sms to false" _use_sms_alert = False if _use_sms_alert: if (libSMS.phoneConnection(gammurc_file, PIN) == None): print "overriding use_sms to false" print "Sending SMS failed" _use_sms_alert = False else: sm = libSMS.phoneConnection(gammurc_file, PIN) if _use_sms_alert : print "post_processing_gw.py sends SMS indicating that gateway has started post-processing stage..." success = libSMS.send_sms(sm, "Gateway "+_gwid+" has started post-processing stage", contact_sms) if (success): print "Sending SMS done" sys.stdout.flush() #------------------------------------------------------------ #for handling images #------------------------------------------------------------ #list of active nodes nodeL = deque([]) #association to get the file handler fileH = {} #association to get the image filename imageFilenameA = {} #association to get the image SN imgsnA= {} #association to get the image quality factor
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] arr = map(int,rdata.split(',')) bw=arr[0] cr=arr[1] sf=arr[2] #LoRaWAN packet if dst==256: src_str="%0.8X" % src else: src_str=str(src) if (src_str in key_SMS.source_list) or (len(key_SMS.source_list)==0): #this part depends on the syntax used by the end-device #we use: thingspeak_channel#thingspeak_field#TC/22.4/HU/85... #ex: ##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('#') 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', '') #sms data to be sent #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 sms_data = "SensorData Sensor"+src_str+" SNR "+str(SNR)+" RSSI "+str(RSSI)+" SN "+str(seq)+" BW "+str(bw)+" CR "+str(cr)+" SF "+str(sf)+" GWID "+gwid nomenclatures = [] # data to send data = [] data.append(data_array[0]) data.append(data_array[1]) 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 #add the nomemclatures and sensed values i = 0 while i < len(data)-2: sms_data += " "+nomenclatures[i]+" "+data[i+2] i += 1 # Send data to expected contacts success = False print("rcv msg to send via the SMS Service: "+sms_data) success = libSMS.send_sms(sm, sms_data, key_SMS.contacts) if (success): print "Sending SMS done" else: print "Source is not is source list, not sending with CloudSMS.py"
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] arr = map(int, rdata.split(',')) bw = arr[0] cr = arr[1] sf = arr[2] #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): # 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 BOTH fields is greater than 2 then we take them into account if len(data_array[0]) < 3 or len(data_array[1]) < 3: data_array[0] = '' 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', '') #sms data to be sent sms_data = "SensorData " + key_WAZIUP.sensor_name + src_str #start from the first nomenclature i = 2 while i < len(data_array) - 1: sms_data += " " + data_array[i] + " " + data_array[i + 1] i += 2 if data_array[0] == '': data_array[0] = key_WAZIUP.project_name if data_array[1] == '': data_array[1] = key_WAZIUP.service_path sms_data += " " + data_array[0] + " " + data_array[1] # Send data to expected contacts success = False print("rcv msg to send via the WAZIUP SMS Service: " + sms_data) success = libSMS.send_sms(sm, sms_data, key_WAZIUP_SMS.contacts) if (success): print "Sending SMS done" else: print "Source is not is source list, not sending with CloudWAZIUP_SMS.py"
def store_gps_coordinate(src, SNR, RSSI, seq, bc, lat, lgt, fxt, tdata, gwid): try: _gw_lat = gw_json_array["gateway_conf"]["ref_latitude"] except KeyError: _gw_lat = "undef" try: _gw_long = gw_json_array["gateway_conf"]["ref_longitude"] except KeyError: _gw_long = "undef" try: gw_lat = float(_gw_lat) except ValueError: _gw_lat = "undef" try: gw_long = float(_gw_long) except ValueError: _gw_long = "undef" if (_gw_lat == "undef") or (_gw_long == "undef"): print 'GPS file: gw has no valid GPS coordinate' distance = -1 else: # gw pA = (gw_lat, gw_long) pB = (float(lat), float(lgt)) distance = haversine(pA, pB) #open gps file to store the GPS coordinate and the calculated distance f = open(os.path.expanduser(_gps_file), "a") data = 'src ' + src + ' seq ' + str(seq) + ' bc ' + str( bc ) + ' snr ' + str(SNR) + ' rssi ' + str( RSSI ) + ' time ' + tdata + ' gw ' + gwid + ' fxt ' + fxt + ' lat ' + lat + ' lgt ' + lgt + ' distance ' + str( "%.4f" % distance) f.write(data + '\n') f.close() print 'GPS file: lat=' + lat print 'GPS file: lgt=' + lgt print 'GPS file: d=' + "%.4f" % distance #update the list of remote GPS device #information from device older than key_GpsFile.active_interval_minutes will be marked not active update_gps_device(src, SNR, RSSI, seq, bc, lat, lgt, fxt, str("%.4f" % distance), tdata, gwid) if (key_GpsFile.SMS == True): try: gammurc_file = key_GpsFile.gammurc_file except AttributeError: gammurc_file = "/home/pi/.gammurc" print 'GPS file: SMS is requested' #check Gammu configuration if (not libSMS.gammuCheck()): print 'GPS file: Gammu is not available' sys.exit() else: if (not libSMS.gammurcCheck(gammurc_file)): print 'GPS file: gammurc file is not available' sys.exit() if (libSMS.phoneConnection(gammurc_file, key_GpsFile.PIN) == None): print 'GPS file: Can not connect to cellular network' sys.exit() else: sm = libSMS.phoneConnection(gammurc_file, key_GpsFile.PIN) print "GPS file: Sending SMS" success = libSMS.send_sms(sm, data, key_GpsFile.contacts) if (success): print "GPS file: Sending SMS done"
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] arr = map(int, rdata.split(',')) bw = arr[0] cr = arr[1] sf = arr[2] if (str(src) in key_Orion.source_list) or (len(key_Orion.source_list) == 0): #this part depends on the syntax used by the end-device #we use: thingspeak_channel#thingspeak_field#TC/22.4/HU/85... #ex: ##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('#') 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 BOTH fields is greater than 2 then we take them into account if len(data_array[0]) < 3 or len(data_array[1]) < 3: data_array[0] = '' 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', '') #sms data to be sent sms_data = "SensorData " + key_Orion.sensor_name + str(src) #sms_data = "SRC#"+str(src)+"#RSSI#"+str(RSSI)+"#BW#"+str(bw)+"#CR#"+str(cr)+"#SF#"+str(sf)+"#GWID#"+gwid+"/"+data #sms_data = "SensorData Sensor"+str(src)+" RSSI "+str(RSSI)+" BW "+str(bw)+" CR "+str(cr)+" SF "+str(sf)+" GWID "+gwid+" " #start from the first nomenclature i = 2 while i < len(data_array) - 1: sms_data += " " + data_array[i] + " " + data_array[i + 1] i += 2 if data_array[0] == '': data_array[0] = key_Orion.project_name if data_array[1] == '': data_array[1] = key_Orion.service_path sms_data += " " + data_array[0] + " " + data_array[1] # Send data to expected contacts success = False if (not always_enabled): if (libSMS.internet_ON()): print('Internet is available, no need to use the SMS Service') sys.exit() else: print("rcv msg to send via the Orion WAZIUP SMS Service: " + sms_data) success = libSMS.send_sms(sm, sms_data, key_OrionWAZIUP_SMS.contacts) else: print("rcv msg to send via the Orion WAZIUP SMS Service: " + sms_data) success = libSMS.send_sms(sm, sms_data, key_OrionWAZIUP_SMS.contacts) if (success): print "Sending SMS done" else: print "Source is not is source list, not sending with CloudOrionWAZIUP_SMS.py"