class ThreadReception(threading.Thread): """objet thread gérant la réception des messages""" def __init__(self, conn): threading.Thread.__init__(self) self.connexion = conn # réf. du socket de connexion self.onNewMessage=EventHook() self.onError = EventHook() def run(self): try : while 1: message_recu = self.connexion.recv(1024) print "*" + message_recu + "*" self.onNewMessage.fire(self.bytes_to_number(message_recu)) except : print "Client arrêté. Connexion interrompue." self.onError.fire() def bytes_to_number(self,b): # if Python2.x b = map(ord, b) res = 0 for i in range(4): res += b[i] << (i*8) return res
def __init__(self, robot): self.robot = robot self.OnTooClose = EventHook() self.OnAllClear = EventHook() self.OnStop = EventHook() self.Running = True threading.Thread.__init__ ( self )
class ThreadEmission(threading.Thread): """objet thread gérant l'émission des messages""" def __init__(self, conn): threading.Thread.__init__(self) self.connexion = conn # réf. du socket de connexion self.onError = EventHook() def run(self): try : #length = os.path.getsize(config.nomImage) #self.connexion.send(self.convert_to_bytes(length)) if os.path.exists(config.nomImage): traitement = 0 self.connexion.send(self.convert_to_bytes(traitement)) emeteur = config.identifiantCam self.connexion.send(self.convert_to_bytes(emeteur)) length = os.path.getsize(config.nomImage) self.connexion.send(self.convert_to_bytes(length)) reply = open(config.nomImage, 'rb').read() self.connexion.sendall(reply) #self.onError.clear() except : print "Erreur dans l envoi du fichier" + str(sys.exc_info()[0]) self.onError.fire() def convert_to_bytes(self,no): result = bytearray() result.append(no & 255) for i in range(3): no = no >> 8 result.append(no & 255) return result
class webcamPi(): def __init__(self,fps,nameImg,numeroCam): self.isRecording = False self.fps = fps self.nameImg = nameImg self.onNewImage=EventHook() self.numeroCam=numeroCam def startRecord(self): # Wait for modprobe try : camera = picamera.PiCamera() time.sleep(2) camera.led = False self.isRecording =True while self.isRecording: # Grab one frame from camera camera.capture(self.nameImg) self.onNewImage.fire() time.sleep( ( 1.0 / self.fps) ) finally : # self.onNewImage.clear() camera.close() print "stop recording" def stopRecord(self): #TODO proteger la variable !! self.isRecording =False
class ProximitySensor(Sensor): def __init__(self, warning_distance): Sensor.__init__(self) self.warning_distance = warning_distance self.onProximity = EventHook() def check_proximity(self): if self.data < self.warning_distance: self.onProximity.fire()
def __init__(self, ip: str = "127.0.0.1", port: int = 8888): self.UDP_IP: str = ip self.UDP_PORT: int = port self.sock: socket = socket.socket(socket.AF_INET, # Internet socket.SOCK_DGRAM) # UDP self.sock.bind((self.UDP_IP, self.UDP_PORT)) self.msg: dict = None self.on_update: EventHook = EventHook() self.stop: bool = False self.listen_UDP: threading.Thread = None
class webcam(): def __init__(self,fps,nameImg,numeroCam): self.isRecording = False self.fps = fps self.nameImg = nameImg self.onNewImage=EventHook() self.numeroCam=numeroCam def startRecord(self): # Wait for modprobe time.sleep(2) capture = cv.CaptureFromCAM(self.numeroCam) text_font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, .5, .5, 0.0, 1, cv.CV_AA ) text_coord = ( 5, 15 ) text_color = cv.CV_RGB(255,255,255) self.isRecording =True try : while self.isRecording: # Grab one frame from camera frame = cv.QueryFrame(capture) cv.PutText( frame,"PORT : "+ time.strftime('%d/%m/%y %H:%M',time.localtime()), text_coord, text_font, text_color ) cv.SaveImage(self.nameImg,frame) self.onNewImage.fire() time.sleep( ( 1.0 / self.fps) ) finally : # self.onNewImage.clear() print "stop recording" def stopRecord(self): #TODO proteger la variable !! self.isRecording =False
def __init__(self, ip_address, port, notify): self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.listening = False self.thingspeak_client = None self.ip_address = ip_address self.port = port self._basement_state = 0 self._basement_hallway_state = 0 self._living_room_state = 0 self.message_received = EventHook() self.notify = notify self.callback = None self.connect()
class RobotSenses(threading.Thread): def __init__(self, robot): self.robot = robot self.OnTooClose = EventHook() self.OnAllClear = EventHook() self.OnStop = EventHook() self.Running = True threading.Thread.__init__ ( self ) def CheckIsObstructionPresent(self, distanceFromObstacle, obstructionPresent): if (distanceFromObstacle < MIN_DRIVE_FORWARDS_DISTANCE and obstructionPresent == False): print("obstructionPresent") obstructionPresent = True self.OnTooClose.fire() return obstructionPresent def CheckIsObstructionClear(self, distanceFromObstacle, obstructionPresent): if (distanceFromObstacle > MIN_STOP_TURNING_DISTANCE and obstructionPresent == True): print("no obstructionPresent") obstructionPresent = False self.OnAllClear.fire() return obstructionPresent def IsStopButtonPressed(self): print("IsStopButtonPressed") pressed = self.robot.IsButtonPressed() print("IsStopButtonPressed: " + str(pressed)) if (pressed == 1): print("StopButtonPressed") self.Running = 0 self.OnStop.fire() def run ( self ): obstructionPresent =False while(self.Running==1): sleep(REFRESH_FREQUENCY) distanceFromObstacle = self.robot.getDistanceFromObstacle() print("distanceFromObstacle:" + str(distanceFromObstacle)) obstructionPresent = self.CheckIsObstructionPresent(distanceFromObstacle, obstructionPresent) obstructionPresent = self.CheckIsObstructionClear(distanceFromObstacle, obstructionPresent) self.IsStopButtonPressed()
def __init__(self, warning_distance): Sensor.__init__(self) self.warning_distance = warning_distance self.onProximity = EventHook()
def __init__(self, conn): threading.Thread.__init__(self) self.connexion = conn # réf. du socket de connexion self.onNewMessage=EventHook() self.onError = EventHook()
def __init__(self,fps,nameImg,numeroCam): self.isRecording = False self.fps = fps self.nameImg = nameImg self.onNewImage=EventHook() self.numeroCam=numeroCam
class ArduinoClient(object): def __init__(self, ip_address, port, notify): self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.listening = False self.thingspeak_client = None self.ip_address = ip_address self.port = port self._basement_state = 0 self._basement_hallway_state = 0 self._living_room_state = 0 self.message_received = EventHook() self.notify = notify self.callback = None self.connect() def set_motion_detect_callback(self, callback): self.callback = callback def connect(self): try: self.socket.connect((self.ip_address, self.port)) self.socket.send('Thank you for connecting\n') self.start_listener_thread() self.notify("Arduino: Connected") logging.info("Arduino: Connected") except: self.notify("Arduino: Not found, giving up") logging.warning("Arduino: Not found, giving up") def start_listener_thread(self): listener = Thread(target=self.listener_thread) self.listening = True listener.start() def listener_thread(self): msg = '' while self.listening: msg = msg + self.socket.recv(1024) if msg.endswith('\n'): logging.debug("Arduino: Message received - " + msg) self.parse_message(msg) self.message_received.fire(msg) msg = '' def parse_message(self, message): j = json.loads(message) d = dict() if j.has_key('Living Room'): d['field5'] = j['Living Room'] if self._living_room_state != j['Living Room']: self._living_room_state = j['Living Room'] self.change_detected('Living Room', j['Living Room']) if j.has_key('Basement Hallway'): d['field6'] = j['Basement Hallway'] if self._basement_hallway_state != j['Basement Hallway']: self._basement_hallway_state = j['Basement Hallway'] self.change_detected('Basement Hallway', j['Basement Hallway']) if j.has_key('Basement'): d['field7'] = j['Basement'] if self._basement_state != j['Basement']: self._basement_state = j['Basement'] self.change_detected('Basement', j['Basement']) if self.thingspeak_client: self.thingspeak_client.write(d) def change_detected(self, location, state): if state == 1 and self.callback: self.callback(location)