Example #1
0
def closeAll():
    if request.method == 'GET':
        jwt = get_jwt_identity()
        print(jwt)
        if jwt:
            cx = sqlite3.connect(DB)
            cur = cx.cursor()
            sql = 'UPDATE device SET close = 1'
            cur.execute(sql)
            cx.commit()
            cur.close()
            cx.close()
            myLogger.info('closing all!')
            return jsonify({"code": 200, "msg": "ok"})
        return jsonify(common.falseReturn('', 'need jwt_token'))
    return jsonify(common.falseReturn('', 'wrong method'))
Example #2
0
def clearDevice():
    if request.method == 'GET':
        try:
            ip = request.args.get('ip').encode('ascii')
            name = request.args.get('name').encode('ascii')
            print 'clearing device ', name
            if 1:
                myLogger.info('clearing device: {0}'.format(name))
                cx = sqlite3.connect(DB)
                cur = cx.cursor()
                sql = 'DELETE FROM device where ip = "{0}"'.format(ip)
                cur.execute(sql)
                cx.commit()
                cur.close()
                cx.close()
                return json.dumps({"code": 200, "msg": "ok"})
        except Exception, e:
            myLogger.error(e)
            return json.dumps({"code": 500, "msg": "InternalError"})
Example #3
0
def register():
    if request.method == 'GET':
        name = request.args.get('name').encode('ascii')
        ip = request.args.get('ip').encode('ascii')
        mac = request.args.get('mac').encode('ascii')
        cx = sqlite3.connect(DB)
        cur = cx.cursor()
        if checkDevice(ip, mac):
            return json.dumps({'code': 500, 'msg': 'device already exist!'})
        else:
            sql = 'INSERT INTO device VALUES (null,"{0}","{1}","{2}",0)'.format(
                ip, name, mac)
            cur.execute(sql)
            cx.commit()
            cur.close()
            cx.close()
            print 'register device: ', name, ip, mac
            myLogger.info('register device: {0} {1} {2}'.format(ip, name, mac))
            return json.dumps({"code": 200, "msg": "register ok"})
    return json.dumps({"code": 500, "msg": 'wrong method'})
Example #4
0
def wakeDevice():
    if request.method == 'GET':
        try:
            ip = request.args.get('ip').encode('ascii')
            name = request.args.get('name').encode('ascii')
            raw_mac = request.args.get('mac').encode('ascii')
            mac = wakeOnLan.format_mac(raw_mac)
            send_data = wakeOnLan.create_magic_packet(mac)
            wakeOnLan.send_magic_packet(send_data)
            cx = sqlite3.connect(DB)
            cur = cx.cursor()
            sql = 'UPDATE device SET close = 0 where ip = "{0}"'.format(ip)
            cur.execute(sql)
            cx.commit()
            cur.close()
            cx.close()
            myLogger.info('waking device {0}'.format(name))
            return json.dumps({"code": 200, "mac": mac})
        except Exception, e:
            print e
            myLogger.error(e)
            return json.dumps({"code": 500, "msg": str(e)})