@@ -1,3 +1,4 @@  | 
            ||
| 1 | 
                +*.py[cod]  | 
            |
| 1 | 2 | 
                .idea/  | 
            
| 2 | 3 | 
                .DS_Store  | 
            
| 3 | 4 | 
                minipai2.db  | 
            
                @@ -7,14 +7,17 @@ import re  | 
            ||
| 7 | 7 | 
                import sqlite3  | 
            
| 8 | 8 | 
                import stat  | 
            
| 9 | 9 | 
                import time  | 
            
| 10 | 
                -from functools import wraps  | 
            |
| 11 | 10 | 
                 | 
            
| 12 | 11 | 
                from tornado.httpserver import HTTPServer  | 
            
| 13 | 12 | 
                from tornado.ioloop import IOLoop  | 
            
| 14 | 
                -from tornado.log import app_log as logger  | 
            |
| 15 | 13 | 
                from tornado.options import define, options  | 
            
| 16 | 14 | 
                from tornado.web import Application, RequestHandler, StaticFileHandler  | 
            
| 17 | 15 | 
                 | 
            
| 16 | 
                +from utils.boxinfo import get_box_status  | 
            |
| 17 | 
                +from utils.logit import log_request_arguments, logit  | 
            |
| 18 | 
                +from utils.sqlite import (CREATE_INDEX1, CREATE_INDEX2, CREATE_TABLE_STMT, DELETE_RECORD_STMT, INSERT_RECORD_STMT,  | 
            |
| 19 | 
                + SELECT_MAX_PHOTO_ID_STMT, SELECT_ORIGIN_PATH_STMT)  | 
            |
| 20 | 
                +  | 
            |
| 18 | 21 | 
                 | 
            
| 19 | 22 | 
                 define('host', default='127.0.0.1', help='run on the given host', type=str)
               | 
            
| 20 | 23 | 
                 define('port', default=8001, help='run on the given port', type=int)
               | 
            
                @@ -23,29 +26,6 @@ options.parse_command_line()  | 
            ||
| 23 | 26 | 
                 | 
            
| 24 | 27 | 
                ROOT_PATH = '/home/work/minipai2'  | 
            
| 25 | 28 | 
                 | 
            
| 26 | 
                -# Create Table SQL  | 
            |
| 27 | 
                -CREATE_TABLE_STMT = """  | 
            |
| 28 | 
                - CREATE TABLE IF NOT EXISTS photoinfo (  | 
            |
| 29 | 
                - id integer primary key,  | 
            |
| 30 | 
                - lensman varchar(20),  | 
            |
| 31 | 
                - session varchar(20),  | 
            |
| 32 | 
                - photo_id varchar(13),  | 
            |
| 33 | 
                - photo_name varchar(255),  | 
            |
| 34 | 
                - thumb_path varchar(255),  | 
            |
| 35 | 
                - origin_path varchar(255)  | 
            |
| 36 | 
                -);"""  | 
            |
| 37 | 
                -# Create Index SQL  | 
            |
| 38 | 
                -CREATE_INDEX1 = 'CREATE INDEX IF NOT EXISTS idx_lensman ON photoinfo (lensman);'  | 
            |
| 39 | 
                -CREATE_INDEX2 = 'CREATE INDEX IF NOT EXISTS idx_session ON photoinfo (session);'  | 
            |
| 40 | 
                -# Insert Record SQL  | 
            |
| 41 | 
                -INSERT_RECORD_STMT = 'INSERT INTO photoinfo VALUES (NULL, ?, ?, ?, ?, ?, ?);'  | 
            |
| 42 | 
                -# Delete Record SQL  | 
            |
| 43 | 
                -DELETE_RECORD_STMT = 'DELETE FROM photoinfo WHERE lensman = ? and session = ? and name = ?;'  | 
            |
| 44 | 
                -# Query Max(photo_id) SQL  | 
            |
| 45 | 
                -SELECT_MAX_PHOTO_ID_STMT = 'SELECT MAX(photo_id) FROM photoinfo WHERE lensman = ? and session = ?;'  | 
            |
| 46 | 
                -# Query Origin Path SQL  | 
            |
| 47 | 
                -SELECT_ORIGIN_PATH_STMT = 'SELECT origin_path FROM photoinfo WHERE lensman = ? and photo_id = ?;'  | 
            |
| 48 | 
                -  | 
            |
| 49 | 29 | 
                 | 
            
| 50 | 30 | 
                 conn = sqlite3.connect('minipai2.db')
               | 
            
| 51 | 31 | 
                cur = conn.cursor()  | 
            
                @@ -58,21 +38,6 @@ cur.execute(CREATE_INDEX2)  | 
            ||
| 58 | 38 | 
                conn.commit()  | 
            
| 59 | 39 | 
                 | 
            
| 60 | 40 | 
                 | 
            
| 61 | 
                -# Logging Some Vars  | 
            |
| 62 | 
                -def logit(self, content, key='content'):  | 
            |
| 63 | 
                - uri = self.request.uri if hasattr(self, 'request') else self  | 
            |
| 64 | 
                -    logger.info('uri=%s&%s=%s', uri, key, content)
               | 
            |
| 65 | 
                -  | 
            |
| 66 | 
                -  | 
            |
| 67 | 
                -# Logging Request Arguments  | 
            |
| 68 | 
                -def log_request_arguments(func):  | 
            |
| 69 | 
                - @wraps(func)  | 
            |
| 70 | 
                - def returned_wrapper(self, *args, **kwargs):  | 
            |
| 71 | 
                - logit(self, self.request.arguments, key='arguments')  | 
            |
| 72 | 
                - return func(self, *args, **kwargs)  | 
            |
| 73 | 
                - return returned_wrapper  | 
            |
| 74 | 
                -  | 
            |
| 75 | 
                -  | 
            |
| 76 | 41 | 
                # FILE OPERATE  | 
            
| 77 | 42 | 
                def silent_makdirs(path):  | 
            
| 78 | 43 | 
                try:  | 
            
                @@ -315,10 +280,7 @@ class BoxInfoHandler(RequestHandler):  | 
            ||
| 315 | 280 | 
                'status': 200,  | 
            
| 316 | 281 | 
                             'data': {
               | 
            
| 317 | 282 | 
                'no.': 'paiai000001',  | 
            
| 318 | 
                -                'status': {
               | 
            |
| 319 | 
                - 'code': '200',  | 
            |
| 320 | 
                - 'msg': u'正常',  | 
            |
| 321 | 
                - }  | 
            |
| 283 | 
                + 'status': get_box_status(),  | 
            |
| 322 | 284 | 
                }  | 
            
| 323 | 285 | 
                })  | 
            
| 324 | 286 | 
                 | 
            
                @@ -0,0 +1,35 @@  | 
            ||
| 1 | 
                +# -*- coding: utf-8 -*-  | 
            |
| 2 | 
                +  | 
            |
| 3 | 
                +  | 
            |
| 4 | 
                +STATUS_CODE_MSG = {
               | 
            |
| 5 | 
                + '101': u'相机连接异常或者没有接入相机',  | 
            |
| 6 | 
                + '201': u'目录监控初始化异常',  | 
            |
| 7 | 
                + '202': u'目录监控失败',  | 
            |
| 8 | 
                + '203': u'目录监控失败',  | 
            |
| 9 | 
                +}  | 
            |
| 10 | 
                +  | 
            |
| 11 | 
                +  | 
            |
| 12 | 
                +DETECT_LOGS = (  | 
            |
| 13 | 
                +    # ('LOG FILE PATH', 'VALID STATUS CODE')
               | 
            |
| 14 | 
                +    ('/tmp/getpic.log', '100'),
               | 
            |
| 15 | 
                +    ('/tmp/main.log', '200')
               | 
            |
| 16 | 
                +)  | 
            |
| 17 | 
                +  | 
            |
| 18 | 
                +  | 
            |
| 19 | 
                +def get_box_status():  | 
            |
| 20 | 
                + for path, valid_code in DETECT_LOGS:  | 
            |
| 21 | 
                + try:  | 
            |
| 22 | 
                + with open(path, 'r') as f:  | 
            |
| 23 | 
                + code = f.read().strip()  | 
            |
| 24 | 
                + if code != valid_code:  | 
            |
| 25 | 
                +                    return {
               | 
            |
| 26 | 
                + 'code': code,  | 
            |
| 27 | 
                + 'msg': STATUS_CODE_MSG.get(code, u''),  | 
            |
| 28 | 
                + }  | 
            |
| 29 | 
                + except IOError:  | 
            |
| 30 | 
                + # LOG FILE NOT EXIST  | 
            |
| 31 | 
                + pass  | 
            |
| 32 | 
                +    return {
               | 
            |
| 33 | 
                + 'code': '200',  | 
            |
| 34 | 
                + 'msg': u'正常',  | 
            |
| 35 | 
                + }  | 
            
                @@ -0,0 +1,25 @@  | 
            ||
| 1 | 
                +# -*- coding: utf-8 -*-  | 
            |
| 2 | 
                +  | 
            |
| 3 | 
                +from functools import wraps  | 
            |
| 4 | 
                +  | 
            |
| 5 | 
                +from tornado.log import app_log as logger  | 
            |
| 6 | 
                +  | 
            |
| 7 | 
                +  | 
            |
| 8 | 
                +DEBUG = False  | 
            |
| 9 | 
                +  | 
            |
| 10 | 
                +  | 
            |
| 11 | 
                +# Logging Some Vars  | 
            |
| 12 | 
                +def logit(self, content, key='content'):  | 
            |
| 13 | 
                + if not DEBUG:  | 
            |
| 14 | 
                + return  | 
            |
| 15 | 
                + uri = self.request.uri if hasattr(self, 'request') else self  | 
            |
| 16 | 
                +    logger.info('uri=%s&%s=%s', uri, key, content)
               | 
            |
| 17 | 
                +  | 
            |
| 18 | 
                +  | 
            |
| 19 | 
                +# Logging Request Arguments  | 
            |
| 20 | 
                +def log_request_arguments(func):  | 
            |
| 21 | 
                + @wraps(func)  | 
            |
| 22 | 
                + def wrapper(self, *args, **kwargs):  | 
            |
| 23 | 
                + logit(self, self.request.arguments, key='arguments')  | 
            |
| 24 | 
                + return func(self, *args, **kwargs)  | 
            |
| 25 | 
                + return wrapper  | 
            
                @@ -0,0 +1,24 @@  | 
            ||
| 1 | 
                +# -*- coding: utf-8 -*-  | 
            |
| 2 | 
                +  | 
            |
| 3 | 
                +# Create Table SQL  | 
            |
| 4 | 
                +CREATE_TABLE_STMT = """  | 
            |
| 5 | 
                + CREATE TABLE IF NOT EXISTS photoinfo (  | 
            |
| 6 | 
                + id integer primary key,  | 
            |
| 7 | 
                + lensman varchar(20),  | 
            |
| 8 | 
                + session varchar(20),  | 
            |
| 9 | 
                + photo_id varchar(13),  | 
            |
| 10 | 
                + photo_name varchar(255),  | 
            |
| 11 | 
                + thumb_path varchar(255),  | 
            |
| 12 | 
                + origin_path varchar(255)  | 
            |
| 13 | 
                +);"""  | 
            |
| 14 | 
                +# Create Index SQL  | 
            |
| 15 | 
                +CREATE_INDEX1 = 'CREATE INDEX IF NOT EXISTS idx_lensman ON photoinfo (lensman);'  | 
            |
| 16 | 
                +CREATE_INDEX2 = 'CREATE INDEX IF NOT EXISTS idx_session ON photoinfo (session);'  | 
            |
| 17 | 
                +# Insert Record SQL  | 
            |
| 18 | 
                +INSERT_RECORD_STMT = 'INSERT INTO photoinfo VALUES (NULL, ?, ?, ?, ?, ?, ?);'  | 
            |
| 19 | 
                +# Delete Record SQL  | 
            |
| 20 | 
                +DELETE_RECORD_STMT = 'DELETE FROM photoinfo WHERE lensman = ? and session = ? and name = ?;'  | 
            |
| 21 | 
                +# Query Max(photo_id) SQL  | 
            |
| 22 | 
                +SELECT_MAX_PHOTO_ID_STMT = 'SELECT MAX(photo_id) FROM photoinfo WHERE lensman = ? and session = ?;'  | 
            |
| 23 | 
                +# Query Origin Path SQL  | 
            |
| 24 | 
                +SELECT_ORIGIN_PATH_STMT = 'SELECT origin_path FROM photoinfo WHERE lensman = ? and photo_id = ?;'  |