|
1 |
| -#!/usr/bin/env python3 |
| 1 | +#!/usr/bin/env python2 |
2 | 2 | # coding:utf-8
|
3 | 3 | import socket
|
4 | 4 | import serial
|
5 | 5 | import time
|
| 6 | +import logging |
| 7 | +import logging.handlers |
6 | 8 | import re
|
| 9 | +import RPi.GPIO as GPIO |
7 | 10 |
|
8 | 11 |
|
9 |
| -class MycobotServer: |
| 12 | +def get_logger(name): |
| 13 | + logger = logging.getLogger(name) |
| 14 | + logger.setLevel(logging.DEBUG) |
| 15 | + |
| 16 | + LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s" |
| 17 | + #DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p" |
| 18 | + |
| 19 | + formatter = logging.Formatter(LOG_FORMAT) |
| 20 | + # console = logging.StreamHandler() |
| 21 | + # console.setFormatter(formatter) |
| 22 | + |
| 23 | + save = logging.handlers.RotatingFileHandler("/home/ubuntu/mycobot_server.log", maxBytes=10485760, backupCount=1) |
| 24 | + save.setFormatter(formatter) |
| 25 | + |
| 26 | + logger.addHandler(save) |
| 27 | + # logger.addHandler(console) |
| 28 | + return logger |
| 29 | + |
| 30 | +class MycobotServer(object): |
10 | 31 |
|
11 | 32 | def __init__(self, host, port):
|
| 33 | + GPIO.setwarnings(False) |
| 34 | + self.logger = get_logger("AS") |
12 | 35 | self.mc = None
|
13 | 36 | self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
14 |
| - self.s.bind((host, port)) |
15 |
| - print("Binding succeeded!") |
| 37 | + self.s.bind((host,port)) |
| 38 | + print "Binding succeeded!" |
16 | 39 | self.s.listen(1)
|
17 | 40 | self.connect()
|
18 | 41 |
|
19 | 42 | def connect(self):
|
20 | 43 | try:
|
21 | 44 | while True:
|
| 45 | + print "waiting connect!------------------" |
22 | 46 | conn, addr = self.s.accept()
|
23 | 47 | port_baud = []
|
24 | 48 | while True:
|
25 | 49 | try:
|
| 50 | + print "waiting data--------" |
26 | 51 | data = conn.recv(1024)
|
27 | 52 | command = data.decode('utf-8')
|
28 | 53 | if data.decode('utf-8') == "":
|
29 |
| - print("client dsiconnect!") |
| 54 | + print("close dsiconnect!") |
30 | 55 | break
|
31 |
| - res = b'None' |
32 |
| - command = command.replace(" ", "") |
33 |
| - |
34 |
| - if len(port_baud) < 3: |
35 |
| - |
| 56 | + res = b'1' |
| 57 | + command = command.replace(" ","") |
| 58 | + if len(port_baud)<3: |
| 59 | + |
36 | 60 | port_baud.append(command)
|
37 |
| - if len(port_baud) == 3: |
38 |
| - print(port_baud) |
39 |
| - self.mc = serial.Serial( |
40 |
| - port_baud[0], port_baud[1], timeout=float(port_baud[1])) |
| 61 | + if len(port_baud)==3: |
| 62 | + self.mc = serial.Serial(port_baud[0],port_baud[1],timeout=float(port_baud[1])) |
41 | 63 | port_baud.append(1)
|
42 | 64 | else:
|
| 65 | + self.logger.info(command) |
43 | 66 | command = self.re_data_2(command)
|
| 67 | + if command[3] == 170: |
| 68 | + if command[4] == 0: |
| 69 | + GPIO.setmode(GPIO.BCM) |
| 70 | + else: |
| 71 | + GPIO.setmode(GPIO.BOARD) |
| 72 | + elif command[3] == 171: |
| 73 | + if command[5]: |
| 74 | + GPIO.setup(command[4],GPIO.OUT) |
| 75 | + else: |
| 76 | + GPIO.setup(command[4],GPIO.IN) |
| 77 | + |
| 78 | + elif command[3] == 172: |
| 79 | + GPIO.output(command[4],command[5]) |
| 80 | + |
| 81 | + elif command[3] == 173: |
| 82 | + res = bytes(GPIO.input(command[4])) |
| 83 | + |
44 | 84 | self.write(command)
|
45 | 85 | res = self.read()
|
46 |
| - |
| 86 | + if res == None: |
| 87 | + res = b'1' |
47 | 88 | conn.sendall(res)
|
48 | 89 | except Exception as e:
|
| 90 | + self.logger.error(str(e)) |
49 | 91 | conn.sendall(str.encode("ERROR:"+str(e)))
|
50 |
| - break |
51 |
| - except: |
| 92 | + break |
| 93 | + except Exception as e: |
| 94 | + self.logger.error(str(e)) |
52 | 95 | conn.close()
|
53 | 96 |
|
54 |
| - def write(self, command): |
| 97 | + def write(self,command): |
55 | 98 | self.mc.write(command)
|
56 | 99 | self.mc.flush()
|
57 | 100 | time.sleep(0.05)
|
58 | 101 |
|
59 | 102 | def read(self):
|
60 | 103 | data = None
|
61 |
| - if self.mc.inWaiting() > 0: |
| 104 | + if self.mc.inWaiting()>0: |
62 | 105 | data = self.mc.read(self.mc.inWaiting())
|
63 | 106 | return data
|
64 | 107 |
|
65 | 108 | def re_data_2(self, command):
|
66 | 109 | r2 = re.compile(r'[[](.*?)[]]')
|
67 | 110 | data_str = re.findall(r2, command)[0]
|
68 |
| - data_list = [int(i) for i in data_str.split(',')] |
| 111 | + data_list = data_str.split(",") |
| 112 | + data_list = [int(i) for i in data_list] |
69 | 113 | return data_list
|
70 | 114 |
|
71 |
| - |
72 | 115 | if __name__ == "__main__":
|
73 | 116 | HOST = socket.gethostbyname(socket.gethostname())
|
74 | 117 | PORT = 9000
|
75 |
| - MycobotServer(HOST, PORT) |
| 118 | + MycobotServer(HOST,PORT) |
0 commit comments