def data(self,timerange=None,level=None,datavar={},lon=None,lat=None): try: # send data (temperature for example) non retained if lon is not None and lat is not None: lonlat="%d,%d" % (nint(lon*100000),nint(lat*100000)) else: lonlat=self.lonlat for key,val in datavar.iteritems(): rc=self.publish(self.prefix+"/"+self.ident+"/"+lonlat+"/"+self.network+"/"+ timerange+"/"+level+"/"+key, payload=dumps(val), qos=1, retain=False ) if rc != mqtt.MQTT_ERR_SUCCESS: raise Exception("publish data",rc) #rc = self.mqttc.loop() #if rc != mqtt.MQTT_ERR_SUCCESS: # raise Exception("loop",rc) except Exception as inst: self.error(inst)
def get(self, lon, lat): nlat = nint(lat) nlon = nint(lon) if (self.lat != nlat or self.lon != nlon): #try: #print "execute select" self.cur.execute( 'select geoid_height from geoid_height where longitude=? and latitude=?', (nint(lon), nint(lat))) self.height = self.cur.fetchone() #except ProgrammingError: # # here we are in a diffrent thread ! new con and cur # # ProgrammingError: SQLite objects created in a thread can only be used in that same thread. # # The object was created in thread id 1100526960 and this is thread id 1074954032 # # print "ProgrammingError: SQLite objects created in a thread can only be used in that same thread." # print "recover with new connect" # not needed with check_same_thread=False # # con = sqlite3.connect(db) # cur = self.con.cursor() # cur.execute('select geoid_height from geoid_height where longitude=? and latitude=?',(nint(lon),nint(lat))) # self.height=self.cur.fetchone() # con.close() if self.height is not None: self.height = self.height[0] self.lat = nlat self.lon = nlon return self.height
def get(self, lon, lat): nlat = nint(lat) nlon = nint(lon) if self.lat != nlat or self.lon != nlon: # try: # print "execute select" self.cur.execute( "select geoid_height from geoid_height where longitude=? and latitude=?", (nint(lon), nint(lat)) ) self.height = self.cur.fetchone() # except ProgrammingError: # # here we are in a diffrent thread ! new con and cur # # ProgrammingError: SQLite objects created in a thread can only be used in that same thread. # # The object was created in thread id 1100526960 and this is thread id 1074954032 # # print "ProgrammingError: SQLite objects created in a thread can only be used in that same thread." # print "recover with new connect" # not needed with check_same_thread=False # # con = sqlite3.connect(db) # cur = self.con.cursor() # cur.execute('select geoid_height from geoid_height where longitude=? and latitude=?',(nint(lon),nint(lat))) # self.height=self.cur.fetchone() # con.close() if self.height is not None: self.height = self.height[0] self.lat = nlat self.lon = nlon return self.height
def ana(self, anavar={}, lon=None, lat=None): try: if lon is not None and lat is not None: lonlat = "%d,%d" % (nint(lon * 100000), nint(lat * 100000)) else: lonlat = self.lonlat # mando dati di anagrafica # retained only if the station is fixed retain = self.prefix != "mobile" for key, val in anavar.iteritems(): rc = self.publish(self.prefix + "/" + self.ident + "/" + lonlat + "/" + self.network + "/-,-,-/-,-,-,-/" + key, payload=dumps(val), qos=1, retain=retain) if rc != mqtt.MQTT_ERR_SUCCESS: raise Exception("publish ana", rc) except Exception as inst: self.error(inst)
def get(self): if (time.time() - self._timing) < (self._delay/1000.): return None if (time.time() - self._timing > MAXDELAYTIME/1000.): return None temperature,pressure=self.sensor.get_values() return {"B12101":nint(temperature*100.+27315.),"B10004":nint(pressure/10.)}
def on_location(self, **kwargs): ''' callback for new GPS location ''' # self.gps_location = '\n'.join([ # '{}={}'.format(k, v) for k, v in kwargs.items()]) self.lat=nint(kwargs["lat"]*100000)/100000. self.lon=nint(kwargs["lon"]*100000)/100000. try: self.height=int(kwargs["altitude"])-int(self.geo.get(self.lon,self.lat)) except: print "ERROR getting height on msl" self.height=None self.gpsfix=True self.call_on_location(lon=self.lon,lat=self.lat,height=self.height,gpsfix=self.gpsfix)
def ana(self,anavar={},lon=None,lat=None): try: if lon is not None and lat is not None: lonlat="%d,%d" % (nint(lon*100000),nint(lat*100000)) else: lonlat=self.lonlat # mando dati di anagrafica retained for key,val in anavar.iteritems(): rc=self.publish(self.prefix+"/"+self.ident+"/"+lonlat+"/"+self.network+"/-,-,-/-,-,-,-/"+key, payload=dumps(val), qos=1,retain=True) if rc != mqtt.MQTT_ERR_SUCCESS: raise Exception("publish ana",rc) except Exception as inst: self.error(inst)
def getvalues(self): # set one shot (OS) bit self.bus.write_word_data(self.address,0x01,self.resolution | 0x1 | 0x80) # sleep ? the time to take a single temperature conversion time.sleep(.5) d = self.bus.read_word_data(self.address,0x0) #print d d=[d >> i & 0xff for i in (8,0)] #print d count = ((d[1] << 4) | (d[0] >> 4)) & 0xFFF return {"B12101":nint(signInteger(count, 12)*6.25 + 27315.)}
def get(self): if (time.time() - self._timing) < (self._delay/1000.): return None if (time.time() - self._timing > MAXDELAYTIME/1000.): return None d = self.bus.read_word_data(self.address,0x0) #print d d=[d >> i & 0xff for i in (8,0)] #print d count = ((d[1] << 4) | (d[0] >> 4)) & 0xFFF return {"B12101":nint(signInteger(count, 12)*6.25 + 27315.)}
def getvalues(self): # set one shot (OS) bit self.bus.write_word_data(self.address, 0x01, self.resolution | 0x1 | 0x80) # sleep ? the time to take a single temperature conversion time.sleep(.5) d = self.bus.read_word_data(self.address, 0x0) #print d d = [d >> i & 0xff for i in (8, 0)] #print d count = ((d[1] << 4) | (d[0] >> 4)) & 0xFFF return {"B12101": nint(signInteger(count, 12) * 6.25 + 27315.)}
def get(self): if (time.time() - self._timing) < (self._delay/1000.): return None if (time.time() - self._timing > MAXDELAYTIME/1000.): return None # This, technically, sends an incorrect command. This issues an additional # measurement request, which causes the sensor to make another reading. As # the write is built into this, there is no delay and thus the result is # considered stale. The result it returns, however, is from moments ago so # it's fine. # This is our: i2cget -y 1 0x27 4 w val = self.bus.read_i2c_block_data(self.address,0,4) status = (val[0] & 0xc0) >> 6 humid = float(((val[0] & 0x3f) << 8) + val[1])/0x3ffe*100. temp = float((val[2] << 6) + ((val[3] & 0xfc) >> 2))/0x3ffe*165.-40. #good status will be 00 but it's 1 return {"B12101":nint(temp*100.+27315.),"B13003":nint(humid)}
def __init__(self,ident="-",lon=None,lat=None,network="generic",host="localhost",port=1883,username=None,password=None,timeout=60,logfunc=log_stdout,clientid="",prefix="test",maintprefix="test"): self.ident=ident self.lonlat="%d,%d" % (nint(lon*100000),nint(lat*100000)) self.network=network self.host=host self.port=port self.username=username self.password=password self.timeout=timeout self.log = logfunc self.prefix=prefix self.maintprefix=maintprefix self.connected=False self.mid=-1 self.loop_started=False # If you want to use a specific client id, use # mqttc = mosquitto.Mosquitto("client-id") # but note that the client id must be unique on the broker. Leaving the client # id parameter empty will generate a random id for you. #self.mqttc = mosquitto.Mosquitto(clientid) self.mqttc = mqtt.Client(clientid) self.mqttc.on_message = self.on_message self.mqttc.on_connect = self.on_connect self.mqttc.on_disconnect = self.on_disconnect self.mqttc.on_publish = self.on_publish self.mqttc.on_subscribe = self.on_subscribe # Uncomment to enable debug messages self.mqttc.on_log = self.on_log # Imposto le credenziali if (not self.username is None): self.mqttc.username_pw_set(self.username,self.password) #self.mqttc.max_inflight_messages_set(1) # mando stato di connessione della stazione con segnalazione di sconnessione gestita male com will self.mqttc.will_set(self.maintprefix+"/"+self.ident+"/"+self.lonlat+"/"+self.network+"/-,-,-/-,-,-,-/B01213", payload=dumps({"v": "error01"}), qos=1, retain=True) try: print "start connect" #self.mqttc.connect_async(self.host,self.port,self.timeout) rc=self.mqttc.connect(self.host,self.port,self.timeout) print "end connect" if rc != mqtt.MQTT_ERR_SUCCESS: raise Exception("connect",rc) rc=self.mqttc.publish(self.maintprefix+"/"+self.ident+"/"+self.lonlat+"/"+self.network+"/-,-,-/-,-,-,-/B01213", payload=dumps({ "v": "conn"}), qos=1,retain=True) if rc[0] != mqtt.MQTT_ERR_SUCCESS: raise Exception("publish status",rc) self.log("publish maint message mid: "+str(rc[1])) except Exception as inst: self.error(inst)
def __init__(self,ident="-",lon=None,lat=None,network="generic",host="localhost",port=1883,username=None,password=None,timeout=60,logfunc=log_stdout,clientid="",prefix="test",maintprefix="test"): self.ident=ident self.lonlat="%d,%d" % (nint(lon*100000),nint(lat*100000)) self.network=network self.host=host self.port=port self.username=username self.password=password self.timeout=timeout self.log = logfunc self.prefix=prefix self.maintprefix=maintprefix self.connected=False self.mid=-1 self.loop_started=False # If you want to use a specific client id, use # mqttc = mosquitto.Mosquitto("client-id") # but note that the client id must be unique on the broker. Leaving the client # id parameter empty will generate a random id for you. #self.mqttc = mosquitto.Mosquitto(clientid) self.mqttc = mqtt.Client(clientid) self.mqttc.on_message = self.on_message self.mqttc.on_connect = self.on_connect self.mqttc.on_disconnect = self.on_disconnect self.mqttc.on_publish = self.on_publish self.mqttc.on_subscribe = self.on_subscribe # Uncomment to enable debug messages self.mqttc.on_log = self.on_log # Imposto le credenziali if (not self.username is None): self.mqttc.username_pw_set(self.username,self.password) #self.mqttc.max_inflight_messages_set(1) # retained only if the station is fixed retain = self.maintprefix != "mobile" # mando stato di connessione della stazione con segnalazione di sconnessione gestita male com will self.mqttc.will_set(self.maintprefix+"/"+self.ident+"/"+self.lonlat+"/"+self.network+"/-,-,-/-,-,-,-/B01213", payload=dumps({"v": "error01"}), qos=1, retain=retain) try: print "start connect" #self.mqttc.connect_async(self.host,self.port,self.timeout) rc=self.mqttc.connect(self.host,self.port,self.timeout) print "end connect" if rc != mqtt.MQTT_ERR_SUCCESS: raise Exception("connect",rc) # retained only if the station is fixed retain = self.maintprefix != "mobile" rc=self.mqttc.publish(self.maintprefix+"/"+self.ident+"/"+self.lonlat+"/"+self.network+"/-,-,-/-,-,-,-/B01213", payload=dumps({ "v": "conn"}), qos=1,retain=retain) if rc[0] != mqtt.MQTT_ERR_SUCCESS: raise Exception("publish status",rc) self.log("publish maint message mid: "+str(rc[1])) except Exception as inst: self.error(inst)