Created
June 7, 2018 08:47
-
-
Save kimseler14/92505bf8ff3577c46175a4b1cb8f3b8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*-coding:utf-8-*- | |
import socket | |
import sys | |
import RPi.GPIO as GPIO | |
from thread import * | |
import datetime | |
import random | |
import requests | |
import os | |
GPIO.setwarnings(False) | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(37,GPIO.OUT) | |
GPIO.setup(35,GPIO.OUT) | |
GPIO.setup(33,GPIO.OUT) | |
GPIO.setup(31,GPIO.OUT) | |
host = '' | |
port = 8220 | |
address = (host, port) | |
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
server_socket.bind(address) | |
server_socket.listen(5) | |
numbOfConn = 4 | |
addressList = [] | |
clients = set() | |
greetings = ['merhaba', 'selam', 'hi', 'hey'] | |
questions = ['nasılsın', 'nabersin'] | |
responses = ['tamam', 'iyiyim'] | |
database={ | |
'robot':'merhaba,efendim size nasıl yardımcı olabilirim', | |
'adın':'robot', | |
'adın ne ':'adım robot', | |
'selam robot':'merhaba,efendim size nasıl yardımcı olabilirim', | |
'numaranı söyle':'kırkaltı,sıfır üç,onaltı,sıfır,yirmidört', | |
'benim için ne yapabilirsin':'bir çok şey yapabilirim' | |
} | |
print ("dinleniyor . . .") | |
def chatboat(data): | |
if data in database: | |
print(database[data]) | |
sclient(database[data]) | |
elif data in questions: | |
random_response = random.choice(responses) | |
print(random_response) | |
sclient(random_response) | |
elif data in greetings: | |
random_greeting = random.choice(greetings) | |
print(random_greeting) | |
sclient(random_greeting) | |
elif 'ışık aç'in data or 'led aç' in data: | |
sclient("ışık açıldı") | |
#os.system("flite -t 'ışık aç'") | |
GPIO.output(37,False) | |
print("ışık açıldı") | |
elif 'ışık kapat' in data or 'led kapat' in data: | |
sclient("ışık kapat") | |
#os.system("flite -t 'ışık kapat") | |
GPIO.output(37,True) | |
print("ışık kapandı") | |
elif 'saat' in data: | |
now = datetime.datetime.now() | |
time=str(now.hour)+str("saat")+str(now.minute)+str("dakika") | |
print(time) | |
#os.system("flite -t '"+ time+"'") | |
sclient(time) | |
elif 'tarih'in data: | |
now = datetime.datetime.now() | |
date=str("%s/%s/%s" % (now.month,now.day,now.year)) | |
print(date) | |
#os.system("flite -t '"+date+"'") | |
sclient(date) | |
else: | |
conn.send("anlamadım, tekrar et") | |
add_data = open("newdata.txt", 'a') | |
add_data.write("\n") | |
add_data.write(data) | |
add_data.close() | |
def sclient(mess): | |
for c in clients: | |
try: | |
c.send(mess) | |
except: | |
c.close() | |
def clientthread(conn,addressList): | |
while True: | |
output = conn.recv(2048); | |
if output.strip() == "disconnect": | |
conn.close() | |
sys.exit("Received disconnect message. Shutting down.") | |
conn.send("connection loss") | |
elif output: | |
print ("Message received from client:") | |
data=str(output).lower() | |
print (data) | |
print("Reply from the server:") | |
chatboat(data) | |
while True: | |
conn, address = server_socket.accept() | |
print ("Connected to client at ", address) | |
clients.add(conn) | |
start_new_thread(clientthread,(conn,addressList)) | |
conn.close() | |
sock.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment