def fsys_export(self,pin): if(pin<408 or pin>415): p.warn("fsys export error, pin "+str(pin)+" out of range (408-415)") else: try: f = open("/sys/class/gpio/export","w") f.write(str(pin)) f.close() except: p.warn("Failed to set pin "+str(pin)+" as output")
def get(self,pin): if(rpi_support): return int(GPIO.input(pin)) elif(fsys_support and pin>=408 and pin<=415): try: gpiopin = "gpio%s" % (str(pin)) f = open("/sys/class/gpio/"+gpiopin+"/direction","w") f.write("in") f.close() f = open("/sys/class/gpio/"+gpiopin+"/value","r") val=f.read() f.close() #rint("read from pin "+str(pin)+" returned "+str(int(val))) return int(val) except: p.warn("failed to read from pin "+str(pin)) return int(0)
def set(self,pin,level): if(rpi_support): if(level==0): p.rint("setting pin "+str(pin)+" weak low","g") GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) else: p.rint("setting pin "+str(pin)+" high","g") GPIO.setup(pin, GPIO.OUT) GPIO.output(pin,1) elif(fsys_support and pin>=408 and pin<=415): try: gpiopin = "gpio%s" % (str(pin)) f = open("/sys/class/gpio/"+gpiopin+"/direction","w") f.write("out") f.close() f = open("/sys/class/gpio/"+gpiopin+"/value","w") if(level==0): f.write("0") else: f.write("1") f.close() except: p.warn("failed to write to pin "+str(pin))
def get_data(self, mid): try: self.connect() #rint("try:") with self.connection.cursor() as cursor: # Read a single record #rint("get_data gen req:") req = "SELECT COUNT(*) FROM m2m WHERE mid= %s" cursor.execute(req, str(mid)) result_count = cursor.fetchone() #rint(result) #rint(result) if(result_count["COUNT(*)"] == 1): req = "SELECT * FROM m2m WHERE mid= %s" cursor.execute(req, str(mid)) result = cursor.fetchone() # area is just an id, lets replace it with a name and som location data req2 = "SELECT area, longitude, latitude FROM area_state WHERE id= %s" cursor.execute(req2, str(result["area"])) result2 = cursor.fetchone() result["area_id"]=result["area"] result["latitude"]=result2["latitude"] result["longitude"]=result2["longitude"] result["area"]=result2["area"] else: result = -1 p.warn("SQL get data for ->%s<- did not return 1 line but %s"%(str(mid),str(result_count["COUNT(*)"]))) #rint(result) except: self.he() result = -2 self.close() return result
def handle (client, addr): lock = threading.Lock() busy=1 while 1: try: rList, wList, xList = select([client.conn], [client.conn], [client.conn], 3) except Exception as n: p.warn("S_m2m Select() return an exception for connection "+str(addr)+":"+str(n)) disconnect(client) break if(busy==0): time.sleep(0.03) busy=0 ######################## SEND ########################### if(len(wList)>0): try: if(len(client.sendq)>0): busy=1 payload=client.sendq[0] client.sendq.remove(payload) msg= bytearray() # add payload for d in bytearray(payload): msg.append(d) client.conn.send(msg) p.rint("m2m send "+str(len(msg))+" bytes","w") except Exception as n: print("exception!!!") print(n) disconnect(client) ######################## SEND ########################### ####################### RECEIVE ########################## if(len(rList)>0): busy=1 lt="" try: #print(addr,end="") #print("server_m2m: rList has "+str(len(rList))+" Elements") lt="start _handleData()" res = recv_data(client, MAX_MSG_SIZE) if(res<0): disconnect(client) except Exception as n: print("exception!!!") print(n) print("") print("except, our status ",end="") print(lt,end="") print(" sys:",end="") print(sys.exc_info()[0]) print("") disconnect(client) ####################### RECEIVE ########################## ######################## ERROR ########################## if(len(xList)>0): busy=1 try: print("exception!!!") if client.conn: client.conn.close() try: for callb in callback_con: callb("disconnect",client) except: break; pass try: clients.remove(client) except: break; pass except: pass break; ######################## ERROR ########################## ######################## MAINTAINANCE ########################## if(time.time()-client.debug_ts>1): client.debug_ts=time.time() ######################## MAINTAINANCE ########################## p.rint("[S_m2m "+time.strftime("%H:%M:%S")+"] -> Client closed:"+str(addr),"l")