Exemplo n.º 1
0
#!/home/zz/.pyenv/shims/python

import os
import time
from optparse import OptionParser
from cab.utils.c_log import init_log
from cab.utils.machine_info import get_machine_id
from cab.db.dbm import get_db_instance

log = init_log("change_config")

NOT_SHOW_LIST = ()


def change_config(key, value):
    try:
        db = get_db_instance()
        sql = "SELECT id_ai, key, value FROM machine_config;"
        configs = [
            itm for itm in db.cursor.execute(sql)
            if itm[1] not in NOT_SHOW_LIST
        ]
        configs = sorted(configs, key=lambda x: x[1])
        list_all = True
        if key:
            keys = [itm[1] for itm in configs]
            if key in keys:
                list_all = False
                select_id = keys.index(key)
                confirm = input(
                    "Are your sure to change config(%s) to %s? y/N: " %
Exemplo n.º 2
0
import sqlite3
import os
import traceback

from cab.utils.machine_info import get_config, get_cur_time
from cab.utils.c_log import init_log
from cab.utils.utils import make_dirs

__all__ = ["get_db_instance"]

log = init_log("db")


def get_db_instance():
    return DB()


class DB(object):
    def __init__(self, ckc_db_path=None, sync_db_path=None):
        self.config = get_config("ckc")
        self.ckc_db_path = ckc_db_path if ckc_db_path else self.config.get(
            "db", "ckc_db")
        self.sync_db_path = sync_db_path if sync_db_path else self.config.get(
            "db", "sync_db")
        make_dirs(os.path.dirname(self.ckc_db_path))
        make_dirs(os.path.dirname(self.sync_db_path))

        self._open_db()

    def __del__(self):
        self._close_db()
Exemplo n.º 3
0
import socket
import time
import os
import traceback
import select
import threading

from cab.utils.c_log import init_log

_log = init_log("client", count=1)


class Client(object):
    # TODO:

    def __init__(self, serv_addr, serv_port, log=None):
        self.lock = threading.Lock()
        self.serv_addr = serv_addr
        self.serv_port = serv_port
        self.sock = None
        self.connected = False
        self.log = log if log else _log
        try:
            self.connect(timeout=1)
        except Exception as e:
            pass

    def _connect(self):
        try:
            if self.sock:
                self.close()
Exemplo n.º 4
0
import asyncore
import traceback
import sys
import socket
from cab.utils.c_log import init_log
from cab.utils.utils import run_in_thread

__all__ = ["Server", "run_server", "ClientHandler"]

log = init_log("server", count=1)


class Server(asyncore.dispatcher):
    def __init__(self, address, client=None):
        asyncore.dispatcher.__init__(self)
        self.client = ClientHandler if not client else client
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind(address)
        self.address = self.socket.getsockname()
        log.info('binding to %s, %s' % (address[0], address[1]))
        self.listen(5)

    def handle_accept(self):
        # Called when a client connects to our socket
        client_info = self.accept()
        if client_info is not None:
            log.info('handle_accept:  %s, %s' %
                     (client_info[0], client_info[1]))
            self.client(client_info[0], client_info[1])
Exemplo n.º 5
0
import pexpect
import site
site.getsitepackages()
sys.path.append("/usr/share/hplip")

from base import device, status, utils, module
from prnt import cups

from cab.utils.console import embed
from cab.utils.utils import get_mimetype, run_cmd
from cab.utils.c_log import init_log
from cab.prts.prt_exceptions import (PrtSetupError, DeviceNotFoundError,
                                     PrtPrintError)
# from cab.prts import office

log = init_log("prt")


class HpPrinter():
    def __init__(self, device_uri=None, name=None):
        self.name = None
        self.device_uri = None
        self.dev = None

        hp_printers = device.getSupportedCUPSPrinters(["hp"])
        for printer in hp_printers:
            if (device_uri is None and name is None) or \
                    (device_uri and device_uri == printer.device_uri and name and printer.name == name):
                self.name = printer.name
                self.device_uri = printer.device_uri
                break
Exemplo n.º 6
0
from cab.services.protocol import (Protocol, Request, Reply, HeartBeat,
                                   CommunicateException, ProtocolException,
                                   UserException, NoMethodException,
                                   MAX_MESSAGE_LENGTH, MSG_TYPE_REQUEST)
from cab.utils.client import Client
from cab.utils.c_log import init_log
from cab.utils.utils import run_in_thread
from cab.utils.machine_info import get_config, get_machine_id

r2c_server = get_config("ckc").get("server", "r2c_server")
r2c_port = get_config("ckc").getint("server", "r2c_port")

cab_host = get_config("ckc").get("server", "cab_server")
cab_port = get_config("ckc").getint("server", "cab_port")

log = init_log("call_cab")


class CallCab(threading.Thread):
    def __init__(self, call_module_path):
        super(CallCab, self).__init__()
        self.lock = threading.Lock()
        self.call_module_path = call_module_path
        self.stop = threading.Event()
        self.remote_cli = Client(r2c_server, r2c_port)
        # self.remote_cli = Client("127.0.0.1", 1507)

    def _send_heardbeat(self):
        r = HeartBeat(get_machine_id())
        data = Protocol().heart_to_raw(r)
        self.send_to_remote(data)
Exemplo n.º 7
0
import os
import re
try:
    import simplejson as json
except ImportError:
    import json
import sqlite3
from cab.utils.c_log import init_log
from cab.utils.machine_info import get_machine_id, \
    get_cur_time, get_config, \
    get_machine_version
from cab.utils.utils import get_db_uuid
from cab.db.dbm import DB


log = init_log("db_verify", count=1)


def escape_algorithm(algorithm):
    return algorithm.replace("&", "&amp;").replace("<", "&lt;").\
        replace('"', "&quot;").replace("'", "&apos;")

# =============================================================================
# Class SchemaTools
# -----------------------------------------------------------------------------


SQL_DIVIDER = '--------------------'


class SchemaTools():
Exemplo n.º 8
0
from cab.utils.console import embed
from cab.utils.c_log import init_log
from cab.utils.machine_info import get_config, get_machine_id
from cab.services.protocol import (Protocol, Request, HeartBeat, Reply,
                                   MSG_TYPE_REPLY, CommunicateException,
                                   ProtocolException, UserException,
                                   NoMethodException, MAX_MESSAGE_LENGTH,
                                   MSG_TYPE_REQUEST)

__all__ = ["call_once", "CallServer"]

c2r_server = get_config("ckc").get("server", "c2r_server")
c2r_port = get_config("ckc").getint("server", "c2r_port")
print("%s %s" % (c2r_server, c2r_port))

log = init_log("server_api")


def call_once(func, params=None, timeout=60):
    cli = Client(c2r_server, c2r_port)
    r = Request(func, params)
    _id, data = Protocol().request_to_raw(r)
    cli.send(data)
    cli.recv(80960)
    cli.close()

    return


class CallServer(threading.Thread):
    def __init__(self):
Exemplo n.º 9
0
from cab.services import code
from cab.utils.machine_info import get_config
from cab.utils.server import Server, run_server, ClientHandler
from cab.services.protocol import (Protocol, Request,
                                   Reply,
                                   CommunicateException,
                                   ProtocolException,
                                   UserException,
                                   NoMethodException,
                                   MAX_MESSAGE_LENGTH,
                                   MSG_TYPE_REQUEST)

from cab.utils.c_log import init_log

log = init_log("remote_server", count=1)

c2r_server = get_config("ckc").get("server", "c2r_server") 
c2r_port = get_config("ckc").getint("server", "c2r_port") 

class ApiClient(ClientHandler):

    def __init__(self, sock, address):
        super(ApiClient, self).__init__(sock, address)

    def handle_read(self):
        self.on_recv()

    def recvall(self, size, timeout=60):
        s = size
        data = b""
Exemplo n.º 10
0
import requests
try:
    import simplejson as json
except ImportError:
    import json
from cab.utils.machine_info import (get_config, get_machine_id,
                                    get_machine_type,
                                    get_machine_uuid,
                                    get_hw_addr)
from cab.utils.c_log import init_log
from cab.utils.console import embed

log = init_log("web_api")

WEB_SERVER = get_config("ckc").get("server", "web_server")


def _http_call(api, data, files=None, jsonly=True):
    result = {"status": "error", "info": ""}
    url = "%s%s" % (WEB_SERVER, api)
    if jsonly:
        d = json.dumps(data)
    else:
        d = data
    try:
        if files is None:
            res = requests.post(url, data=d)
        else:
            res = requests.post(url, data=d, files=files)

        log.info("%s %s" % (res.url, d))
Exemplo n.º 11
0
    import simplejson as json
except ImportError:
    import json
import sys
import copy
import struct
import random
import time
import select
import serial
import threading
import traceback
from cab.utils.c_log import init_log
from cab.utils.console import embed

log = init_log("dc_cab")

BOUDRATE = 9600

PACK_STX = '\x8A'
PACK_ETX = '\x9B'

IND_STX = 0
IND_ADDR = 1
IND_DOOR = 2
IND_CMD = 3
IND_ST = 3
IND_BCC = 4

CMD_OPEN_DOOR = '\x11'
Exemplo n.º 12
0
import threading
import os
from cab.utils.utils import (get_ui_state, run_in_thread)
from cab.utils.c_log import init_log
from cab.utils.machine_info import get_config
from cab.db.dbm import get_db_instance
from math import log as math_log
from cab import PRJ_DIR

__all__ = ["OmxPlayer"]

config = get_config("ckc")
video_path = config.get("videos", "path")
db = get_db_instance()

log = init_log("video")

wx_width = 865

screen_width = 1920
screen_height = 1080

pro = float(1920) / float(1080)
mini_omx_h = int((screen_width - wx_width) / pro)
mini_omx_y = int((screen_height - mini_omx_h) / 2)


def count_omx_vol(vol_percent):
    try:
        float_percent = float(vol_percent) / 100
    except Exception as e:
Exemplo n.º 13
0
                             extern_if,
                             play_video,
                             download_file,
                             get_udisk,
                             get_udisk_path)

from cab.prts.prt_exceptions import PrtError
from cab.utils.machine_info import (get_machine_id,
                                    get_machine_type,
                                    get_config,
                                    get_hw_addr,
                                    set_machine_id)
from cab.ext.dc_door import DCDoor, SDCDoor


log = init_log("ctl")

cab_port = get_config("ckc").getint("server", "cab_port")


class Controler(object):
    def __init__(self):
        super(Controler, self).__init__()
        self.log = log
        self.test = get_config("ckc").get("main", "test") == "false"
        self.call_cab = CallCab("")
        self._stop_event = threading.Event()
        self.job_queue = queue.Queue()
        signal.signal(signal.SIGINT, self.exit_gracefully)
        signal.signal(signal.SIGTERM, self.exit_gracefully)
        self.door = DCDoor() if not self.test else SDCDoor()
Exemplo n.º 14
0
import time
import os
import threading
import pudb

from prnt import cups
from base import device

from cab.utils.c_log import init_log
from cab.utils.utils import run_cmd
from cab.db.db_pool import DB_POOL
from cab.prts.hp.hp_prt import HpPrinter
from cab.prts.prt_exceptions import PrtSetupError
from cab.prts import office

log = init_log("prt_manager")


def wait_job_done(job, timeout=60 * 5):
    #TODO: need op
    start = time.time()
    while time.time() - start < timeout:
        jobs = cups.getJobs()
        if job.id not in [j.id for j in jobs]:
            completed_jobs = cups.getJobs(completed=1)
            for completed_job in completed_jobs[::-1]:
                if completed_job.id == job.id:
                    try:
                        err_msg = cups.getPrintJobErrorLog(job.id)
                        log.info("job_id: %s, msg: %s" %
                                 (completed_job.id, err_msg))
Exemplo n.º 15
0
from cab.api.server_api import call_once
from cab.utils.c_log import init_log
from cab.utils.machine_info import (get_machine_home, get_machine_server,
                                    get_machine_type, get_machine_id,
                                    get_hw_addr, get_config)

try:
    SERVER = get_machine_server("app", "conn.cereson.cn")
except Exception:
    SERVER = "conn.cereson.cn"

HOME_PATH = get_machine_home()
PROGRESS = os.path.join(HOME_PATH, "var/progress")
print(PROGRESS)

log = init_log("machine_init")
TRY_TIMES = 3


class KioskInit():
    '''
    when the device power on, this py will be excetued once
    '''
    def __init__(self):
        self.config = get_config("ckc")

        try:
            self.machine_id = get_machine_id().upper()
        except Exception:
            self.machine_id = ""
        log.debug("old machine_id:%s" % self.machine_id)
Exemplo n.º 16
0
import traceback
import subprocess
import time
import os

from cab.utils.c_log import init_log
from cab.utils.utils import (get_udisk, get_root_pwd, get_udisk_path,
                             play_video)

log = init_log("usb_monitor")

wifi_conf = "wifi.ini"
sys_wifi_conf = "/etc/wpa_supplicant/wpa_supplicant.conf"


def usb_monitor():
    udisk_exist = False
    while True:
        try:
            udisk_paths = get_udisk_path()
            if not udisk_paths:
                udisk_exist = False
            elif udisk_exist is False:
                for disk_path in udisk_paths:
                    user_conf = os.path.join(disk_path, wifi_conf)
                    if os.path.isfile(user_conf):
                        if set_wiif_config(user_conf, sys_wifi_conf) == 0:
                            play_video("wifi_config_success.mp3")
                        else:
                            play_video("wifi_config_failed.mp3")
                    else: