def insertDataRainboImpactData(request): html_template = 'insertdata/rainbodataform.html' form = RainboImpactForm(request.POST) # A form bound to the POST data if request.method == 'POST': # If the form has been submitted... if form.is_valid(): # All validation rules pass geom = form.cleaned_data['geom'] lon = geom['coordinates'][0] lat = geom['coordinates'][1] dt = datetime.utcnow().replace(microsecond=0) ident = request.user.username datavar = {} value = form.cleaned_data['impact_detected'] if form.cleaned_data[ 'impact_detected'] != "" else "" if (value != ""): datavar["B20203"] = {"t": dt, "v": str(value)} if (len(datavar) > 0): try: user = rmap.settings.mqttuser password = rmap.settings.mqttpassword prefix = rmap.settings.topicreport network = "mobile" slug = form.cleaned_data['coordinate_slug'] print "<", slug, ">", "prefix:", prefix mqtt = rmapmqtt(ident=ident, lon=lon, lat=lat, network=network, host="localhost", port=1883, prefix=prefix, maintprefix=prefix, username=user, password=password) mqtt.data(timerange="254,0,0", level="1,-,-,-", datavar=datavar) mqtt.disconnect() form = RainboImpactForm() # An unbound Rainbo form except Exception as e: return render(request, html_template, { 'form': form, "error": True }) return render(request, html_template, {'form': form}) else: return render(request, html_template, { 'form': form, "invalid": True }) else: return render(request, html_template, {'form': form})
def insertDataManualData(request): if request.method == 'POST': # If the form has been submitted... #stationlat=None #stationlon=None stationform = StationForm(request.user.get_username(),request.POST) # A form bound to the POST data nominatimform = NominatimForm(request.POST) # A form bound to the POST data form = ManualForm(request.POST) # A form bound to the POST data if stationform.is_valid(): # All validation rules pass slug=stationform.cleaned_data['station_slug'] if slug: station=StationMetadata.objects.get(ident__username=request.user.username,slug=slug) #stationlat=station.lat #stationlon=station.lon request.POST['geom']= str(Point(station.lon,station.lat)) request.POST['coordinate_slug']= slug return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,'nominatimform':nominatimform}) else: stationform = StationForm(request.user.get_username()) return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,'nominatimform':nominatimform,"invalid":True}) if nominatimform.is_valid(): # All validation rules pass address=nominatimform.cleaned_data['address'] if address: nom = Nominatim(base_url="http://nominatim.openstreetmap.org") result=nom.query(address,limit=1,countrycodes="IT") if len(result) >= 1: lat= result[0]["lat"] lon= result[0]["lon"] address= result[0]["display_name"] request.POST['geom']= str(Point(float(lon),float(lat))) request.POST['address']= address return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,'nominatimform':nominatimform}) else: nominatimform = NominatimForm() return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,'nominatimform':nominatimform,"invalid":True}) if form.is_valid(): # All validation rules pass geom=form.cleaned_data['geom'] lon=geom['coordinates'][0] lat=geom['coordinates'][1] dt=datetime.utcnow().replace(microsecond=0) ident=request.user.username #if (not stationlat is None): # if (stationlat != lat): # stationform = StationForm(request.user.get_username()) # return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,"invalid":True}) #if (not stationlon is None): # if (stationlon != lon): # stationform = StationForm(request.user.get_username()) # return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,"invalid":True}) datavar={} value=form.cleaned_data['presentweather'] if (value != ""): datavar["B20003"]={"t": dt,"v": str(value)} value=form.cleaned_data['snow_height'] if (not value is None): value=float(value*10.) datavar["B13013"]={"t": dt,"v": str(value)} value=form.cleaned_data['visibility'] if (not value is None): value=float(value/10.) datavar["B20001"]={"t": dt,"v": str(value)} print datavar if (len(datavar)>0): try: user=rmap.settings.mqttuser password=rmap.settings.mqttpassword slug=form.cleaned_data['coordinate_slug'] if (slug): prefix="rmap" else: prefix="mobile" print "<",slug,">","prefix:",prefix mqtt=rmapmqtt(ident=ident,lon=lon,lat=lat,network="rmap",host="localhost",port=1883,prefix=prefix,maintprefix=prefix,username=user,password=password) mqtt.data(timerange="254,0,0",level="1,-,-,-",datavar=datavar) mqtt.disconnect() form = ManualForm() # An unbound form except: return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,'nominatimform':nominatimform,"error":True}) return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,'nominatimform':nominatimform}) else: print "invalid form" form = ManualForm() # An unbound form return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,'nominatimform':nominatimform,"invalid":True}) else: stationform = StationForm(request.user.get_username()) # An unbound form nominatimform = NominatimForm() # An unbound form form = ManualForm() # An unbound form return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,'nominatimform':nominatimform})
def insertDataManualData(request): if request.method == 'POST': # If the form has been submitted... #stationlat=None #stationlon=None stationform = StationForm(request.user.get_username(),request.POST) # A form bound to the POST data nominatimform = NominatimForm(request.POST) # A form bound to the POST data form = ManualForm(request.POST) # A form bound to the POST data if stationform.is_valid(): # All validation rules pass slug=stationform.cleaned_data['station_slug'] if slug: station=StationMetadata.objects.get(ident__username=request.user.username,slug=slug) #stationlat=station.lat #stationlon=station.lon POST=request.POST.copy() POST['geom']= str(Point(station.lon,station.lat)) POST['coordinate_slug']= slug stationform = StationForm(request.user.get_username(),POST) # A form bound to the new data form = ManualForm(POST) # A form bound to the new data return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,'nominatimform':nominatimform}) else: stationform = StationForm(request.user.get_username()) return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,'nominatimform':nominatimform,"invalid":True}) if nominatimform.is_valid(): # All validation rules pass address=nominatimform.cleaned_data['address'] if address: nom = Nominatim(base_url="http://nominatim.openstreetmap.org",referer=get_current_site(request)) result=nom.query(address,limit=1,countrycodes="IT") if result is not None: if len(result) >= 1: lat= result[0]["lat"] lon= result[0]["lon"] address= result[0]["display_name"] POST=request.POST.copy() POST['geom']= str(Point(float(lon),float(lat))) POST['address']= address nominatimform = NominatimForm(POST) # A form bound to the new data stationform = StationForm(request.user.get_username(),POST) # A form bound to the new data form = ManualForm(POST) # A form bound to the new data return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,'nominatimform':nominatimform}) else: nominatimform = NominatimForm() return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,'nominatimform':nominatimform,"invalid":True}) if form.is_valid(): # All validation rules pass geom=form.cleaned_data['geom'] lon=geom['coordinates'][0] lat=geom['coordinates'][1] dt=datetime.utcnow().replace(microsecond=0) ident=request.user.username #if (not stationlat is None): # if (stationlat != lat): # stationform = StationForm(request.user.get_username()) # return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,"invalid":True}) #if (not stationlon is None): # if (stationlon != lon): # stationform = StationForm(request.user.get_username()) # return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,"invalid":True}) datavar={} value=form.cleaned_data['presentweather'] if (value != ""): datavar["B20003"]={"t": dt,"v": str(value)} value=form.cleaned_data['snow_height'] if (not value is None): value=int(value*10) datavar["B13013"]={"t": dt,"v": str(value)} value=form.cleaned_data['visibility'] if (not value is None): value=int(value/10) datavar["B20001"]={"t": dt,"v": str(value)} print "datavar:",datavar if (len(datavar)>0): try: user=rmap.settings.mqttuser password=rmap.settings.mqttpassword prefix=rmap.settings.topicreport slug=form.cleaned_data['coordinate_slug'] if (slug): network="fixed" else: network="mobile" print "<",slug,">","prefix:",prefix mqtt=rmapmqtt(ident=ident,lon=lon,lat=lat,network=network,host="localhost",port=1883,prefix=prefix,maintprefix=prefix,username=user,password=password) mqtt.data(timerange="254,0,0",level="1,-,-,-",datavar=datavar) mqtt.disconnect() form = ManualForm() # An unbound form except: return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,'nominatimform':nominatimform,"error":True}) return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,'nominatimform':nominatimform}) else: print "invalid form" form = ManualForm() # An unbound form return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,'nominatimform':nominatimform,"invalid":True}) else: stationform = StationForm(request.user.get_username()) # An unbound form nominatimform = NominatimForm() # An unbound form form = ManualForm() # An unbound form return render(request, 'insertdata/manualdataform.html',{'form': form,'stationform':stationform,'nominatimform':nominatimform})
def on_message(self, mosq, userdata, msg): # JSON: try and load the JSON string from payload try: # "report/digiteco/1208611,4389056/fixed/0,0,900/103,2000,-,-/B12101 {"v":null,"t":"2019-03-16T08:15:00"}" # "report/+/+/+/+/+" topics = msg.topic.split("/") prefix = topics[0] ident = topics[1] lonlat = topics[2] network = topics[3] st = json.loads(msg.payload.decode()) dt = st.get("t") try: logging.info("try to decode with table d") d = st["d"] timerange = topics[4] level = topics[5] bcodes = rmap_core.dtable[str(d)] timeranges = [] levels = [] for bcode in bcodes: timeranges.append(timerange) levels.append(level) except: try: logging.info("Error; try to decode with table e") e = st["e"] numtemplate = int(e) #if numtemplate > 0 and numtemplate < len(rmap_core.ttntemplate): mytemplate = rmap_core.ttntemplate[numtemplate] bcodes = [] timeranges = [] levels = [] for bcode, param in list(mytemplate.items()): bcodes.append(bcode) timeranges.append(param["timerange"]) levels.append(param["level"]) except: logging.error("skip message: %s : %s" % (msg.topic, msg.payload)) return logging.info( "ident=%s username=%s password=%s lonlat=%s network=fixed host=localhost prefix=sample maintprefix=maint" % (ident, rmap.settings.mqttuser, "fakepassword", lonlat)) mqtt = rmapmqtt.rmapmqtt(ident=ident, username=rmap.settings.mqttuser, password=rmap.settings.mqttpassword, lonlat=lonlat, network=network, host="rmap.cc", prefix=prefix, maintprefix="maint", logfunc=logging.debug, qos=0) # attention qos 0 for fast publish dindex = 0 for val in st["p"]: bcode = bcodes[dindex] timerange = timeranges[dindex] level = levels[dindex] datavar = {bcode: {"t": dt, "v": val}} logging.info("timerange=%s level=%s bcode=%s val=%d" % (timerange, level, bcode, val)) mqtt.data(timerange=timerange, level=level, datavar=datavar) dindex += 1 mqtt.disconnect() except Exception as exception: logging.error( "Topic %s error decoding or publishing; payload: [%s]" % (msg.topic, msg.payload)) logging.error('Exception occured: ' + str(exception)) logging.error(traceback.format_exc()) # if some exception occour here, ask to terminate #self.terminateevent.exit_gracefully() finally: return
def on_message(self,mosq, userdata, msg): now = int(time.time()) # Find out how to handle the topic in this message: slurp through our map # this is not needed if all things are right; I cannot receive topics that I have not subscribed for t in self.map: if paho.topic_matches_sub(t, msg.topic): # print "%s matches MAP(%s) => %s" % (msg.topic, t, self.map[t]) (user, slug) = self.map[t] # JSON: try and load the JSON string from payload try: st = json.loads(msg.payload.decode()) metadata=st["metadata"] #remove string part after second ( 2017-12-22T09:52:30.245940879Z ) mytime=metadata.pop("time",time.strftime("%Y-%m-%dT%H:%M:%S",time.gmtime(now))).split(".")[0] dt=datetime.datetime.strptime(mytime,"%Y-%m-%dT%H:%M:%S") payload=base64.b64decode(st["payload_raw"]) #print "payload: ",payload logging.debug("hex: %s" % binascii.hexlify(payload)) nbits=len(binascii.hexlify(payload))*4 template=int(binascii.hexlify(payload),16) #temp=int(binascii.hexlify(payload),16) #template=0 #for i in xrange(0,nbits): # if (testBit(temp,i)!=0): # template=setBit(template,nbits-i-1) logging.debug("int: %d" %template) logging.debug("bynary: {0:b}".format(template)) #print "bynary:",bin(template) nbit=8 start=nbits-nbit numtemplate=bitextract(template,start,nbit) # TEMPLATE NUMBER 1 if numtemplate > 0 and numtemplate < len(rmap_core.ttntemplate): try: #close django connection to DB to be sure we have a new active connection handler try: connection.close() except Exception as e: print(("django connection close error",e)) mystation=StationMetadata.objects.get(slug=slug,ident__username=user) except ObjectDoesNotExist : logging.error("StationMetadata matching query does not exist") return if not mystation.active: logging.error("disactivated station: %s %s ; do nothing!" % (slug,user) ) return else: logging.error("Unknown template %d " % numtemplate) return except Exception as exception: # log and retry on exception logging.error('Exception occured: ' + str(exception)) logging.error(traceback.format_exc()) logging.error("error decoding message: skip it and do nothing!") #raise return try: #print "ident=",user,"username="******"password="******"lon=",mystation.lon,"lat=",mystation.lat,"network=","fixed","host=","rmap.cc","prefix=","sample","maintprefix=","maint" logging.info("ident=%s username=%s password=%s lon=%f lat=%f network=fixed host=localhost prefix=sample maintprefix=maint" % (user,rmap.settings.mqttuser,"fakepassword",mystation.lon,mystation.lat)) mqtt=rmapmqtt.rmapmqtt(ident=user,username=rmap.settings.mqttuser,password=rmap.settings.mqttpassword,lon=mystation.lon,lat=mystation.lat,network="fixed",host="localhost",prefix="sample",maintprefix="maint",logfunc=logging.debug) mytemplate=rmap_core.ttntemplate[numtemplate] for bcode,param in list(mytemplate.items()): nbit=param["nbit"] start-=nbit bval=bitextract(template, start, nbit) if (bval != ((1 << nbit) - 1)): #val=(bval+param["offset"])/float(param["scale"]) val=bval+param["offset"] datavar={bcode:{"t": dt,"v": val}} #print "datavar=",datavar logging.info("timerange=%s level=%s bcode=%s val=%d" % (param["timerange"],param["level"],bcode,val)) mqtt.data(timerange=param["timerange"],level=param["level"],datavar=datavar) mqtt.disconnect() #if mqtt.mqttc._sock: # mqtt.mqttc._sock.close() #if mqtt.mqttc._sockpairW: # mqtt.mqttc._sockpairW.close() #if mqtt.mqttc._sockpairR: # mqtt.mqttc._sockpairR.close() except Exception as exception: logging.error("Topic %s error decoding or publishing; payload: [%s]" % (msg.topic, msg.payload)) logging.error('Exception occured: ' + str(exception)) logging.error(traceback.format_exc()) # if some exception occour here, ask to terminate; if not the thread will stall forever self.terminateevent.set() finally: return logging.error("Message topic do not match any topics that I have subcribed [%s]", t) # somethings go wrong here; I cannot receive topics that I have not subscribed self.terminateevent.set()
def main(): signal.signal(signal.SIGTERM, cleanup) signal.signal(signal.SIGINT, cleanup) os.environ['DJANGO_SETTINGS_MODULE'] = 'rmap.settings' import django django.setup() django.utils.translation.activate("it") data = weatherlink_status() lon = float(data["station_longitude"]) lat = float(data["station_latitude"]) rmap = rmapmqtt(ident=RMAPUSER, lon=lon, lat=lat, host=host, network="fixed", username=RMAPUSER, password=RMAPPASSWORD, prefix="sample", maintprefix="maint") rmap.loop_start() rmap.connect() anavar = { "B01019": { "v": data["user_city"] }, # data["user_state"] # data["user_country"] "B07030": { "v": float(data["station_elevation_m"]) } } rmap.ana(anavar) time.sleep(5) #rmap.loop() while True: try: data = weatherlink_get() reptime = datetime.datetime.utcfromtimestamp( email.utils.mktime_tz( email.utils.parsedate_tz(data["observation_time_rfc822"]))) print("connect status: ", rmap.connected) timerange = "254,0,0" # dati istantanei level = "103,2000,-,-" # 2m dal suolo datavar = { "B12101": { "t": reptime, "v": float(data["temp_c"]) + 273.15 }, "B13003": { "t": reptime, "v": float(data["relative_humidity"]) } } rmap.data(timerange, level, datavar) level = "1,0,-,-" # surface datavar = { "B07004": { "t": reptime, "v": float(data["pressure_mb"]) * 100. } } rmap.data(timerange, level, datavar) level = "103,10000,-,-" # 2m dal suolo datavar = { "B11002": { "t": reptime, "v": float(data["wind_kt"]) / 1.94 }, "B11001": { "t": reptime, "v": float(data["wind_degrees"]) } } rmap.data(timerange, level, datavar) time.sleep(20) #rmap.loop() except: while True: try: print("terminated with error") rmap.disconnect() rmap.loop_stop() #raise rmap = rmapmqtt(ident=RMAPUSER, lon=lon, lat=lat, host=host, network="fixed", username=RMAPUSER, password=RMAPPASSWORD, prefix="sample", maintprefix="maint") rmap.loop_start() rmap.connect() rmap.ana(anavar) time.sleep(5) break except: print("error reconneting") time.sleep(20)