Esempio n. 1
0
def color_change(request, _chipid, _channel, _newValue):
    channel = to_int(_channel)
    chipid = to_int(_chipid)
    newValue = to_int(_newValue)
    if channel not in [0, 1, 2]:
        return JsonResponse({'msg': 'CHANNEL ERROR.'}, status=400)
    if newValue > 8000:
        return JsonResponse({'msg': 'NEW VALUE too large.'}, status=400)
    if not has_dev_permission(chipid, appobj=request.cur_app):
        logger.warn(u'无操作权限 [%d], [%d]', chipid, request.cur_app.id)
        return JsonResponse({"msg": "无设备对应操作权限!"}, status=400)
    url = 'http://%s/_push_data?dev=%d&msgtype=%d' \
          % (settings.SOCK_SERVER, chipid, 0x26)
    msg = struct.pack(b'!cIc', chr(0x11), newValue, chr(channel))
    try:
        rsp = requests.post(url, data=msg, timeout=50)
        if rsp.status_code != 200:
            logger.warn(u'网关返回错误,数据可能推送失败')
            return JsonResponse({'msg': "网关返回错误"}, status=504)
    except requests.Timeout as _:
        logger.error(u'网关请求超时')
        return JsonResponse({'msg': "网关请求超时"}, status=504)
    except IOError as _:
        logger.error(u'请求失败')
        return JsonResponse({'msg': "请求网关失败"}, status=504)
    return JsonResponse({'msg': 'OK'})
Esempio n. 2
0
def whether_is_online(request, _chip):
    chipid = to_int(_chip)
    if not chipid:
        logger.warn('检测是否在线, CHIP参数必须为整形')
        return JsonResponse({"msg": "chipid arguments error."}, status=400)
    if not has_dev_permission(chipid, appobj=request.cur_app):
        logger.warn(u'无操作权限 [%d], [%d]', chipid, request.cur_app.id)
        return JsonResponse({"msg": "无设备对应操作权限!"}, status=400)
    url = 'http://%s/all_online' % settings.SOCK_SERVER
    try:
        rsp = requests.get(url, timeout=5)
        if rsp.status_code != 200:
            logger.warn('服务端错误')
            return JsonResponse({'msg': "服务端错误"}, status=502)
    except requests.Timeout as _:
        logger.warn('服务端超时')
        return JsonResponse({'msg': "服务端超时"}, status=504)
    found = False
    for devobj in rsp.json():
        if 'chipid' in devobj and devobj['chipid'] == chipid:
            found = True
    return JsonResponse({
        "result": "OK",
        "status": "online" if found else "offline"
    })
Esempio n. 3
0
def reboot_dev(request, _chip):
    chipid = to_int(_chip)
    if not has_dev_permission(chipid, appobj=request.cur_app):
        logger.warn(u'无操作权限 [%d], [%d]', chipid, request.cur_app.id)
        return JsonResponse({"msg": "无设备对应操作权限!"}, status=400)
    url = 'http://%s/_push_data?dev=%d&msgtype=%d' % (settings.SOCK_SERVER,
                                                      chipid, 0x12)
    try:
        rsp = requests.post(url, timeout=5)
        if rsp.status_code != 200:
            logger.warn('服务端错误')
            return JsonResponse({'msg': "服务端错误"}, status=502)
    except requests.Timeout as _:
        logger.warn('服务端超时')
        return JsonResponse({'msg': "服务端超时"}, status=504)
    return JsonResponse({"result": rsp.content[10:]})
Esempio n. 4
0
def get_gpio_status(request, _chipid):
    chipid = to_int(_chipid)
    if not has_dev_permission(chipid, appobj=request.cur_app):
        logger.warn(u'无操作权限 [%d], [%d]', chipid, request.cur_app.id)
        return JsonResponse({"msg": "无设备对应操作权限!"}, status=400)
    url = 'http://%s/_push_data?dev=%d&msgtype=%d' % (settings.SOCK_SERVER,
                                                      chipid, 0x20)
    try:
        rsp = requests.post(url, timeout=5)
        if rsp.status_code != 200:
            logger.warn('服务端错误')
            return JsonResponse({'msg': "服务端错误"}, status=502)
    except requests.Timeout as _:
        logger.warn('服务端超时')
        return JsonResponse({'msg': "服务端超时"}, status=504)
    espush_gpio_map = [0, 1, 2, 3, 4, 5, 9, 10, 12, 13, 14, 15]
    '''
    		{0, FUNC_GPIO0, PERIPHS_IO_MUX_GPIO0_U},
		{1, FUNC_GPIO1, PERIPHS_IO_MUX_U0TXD_U},	//同是串口tx口
		{2, FUNC_GPIO2, PERIPHS_IO_MUX_GPIO2_U},	//uart1 RX口
		{3, FUNC_GPIO3, PERIPHS_IO_MUX_U0RXD_U},	//串口RX口
		{4, FUNC_GPIO4, PERIPHS_IO_MUX_GPIO4_U},
		{5, FUNC_GPIO5, PERIPHS_IO_MUX_GPIO5_U},
		//9 ~ 10
		{9, FUNC_GPIO9, PERIPHS_IO_MUX_SD_DATA2_U},
		{10, FUNC_GPIO10, PERIPHS_IO_MUX_SD_DATA3_U},
		//12~15
		{12, FUNC_GPIO12, PERIPHS_IO_MUX_MTDI_U},
		{13, FUNC_GPIO13, PERIPHS_IO_MUX_MTCK_U},
		{14, FUNC_GPIO14, PERIPHS_IO_MUX_MTMS_U},
		{15, FUNC_GPIO15, PERIPHS_IO_MUX_MTDO_U},
    '''
    if len(rsp.content[10:]) != len(espush_gpio_map):
        return JsonResponse({'msg': '服务端返回了错误的数据。'}, status=500)
    gpio_status_list = rsp.content[10:]
    # {pin: 0, edge: 1}
    outRsp = []
    for pos, byteVal in enumerate(gpio_status_list):
        outRsp.append({
            'pin': espush_gpio_map[pos],
            'edge': 1 if byteVal == '\x01' else 0
        })
    return JsonResponse(outRsp, safe=False)
Esempio n. 5
0
def set_gpio_edge(request, chip, pin, edge):
    if edge not in ['1', '0']:
        logger.warn('电平态只能为0 或 1')
        return JsonResponse({"msg": "电平态只能为0 或 1"})
    if not has_dev_permission(chip, appobj=request.cur_app):
        logger.warn(u'无操作权限 [%d], [%d]', to_int(chip), request.cur_app.id)
        return JsonResponse({"msg": "无设备对应操作权限!"}, status=400)
    chipid = to_int(chip)
    url = 'http://%s/_push_data?dev=%d&msgtype=%d' % (settings.SOCK_SERVER,
                                                      chipid, 0x1E)
    body = chr(int(pin)) + chr(int(edge))
    try:
        rsp = requests.post(url, data=body, timeout=5)
        if rsp.status_code != 200:
            logger.warn('服务端错误')
            return JsonResponse({'msg': "服务端错误"}, status=502)
    except requests.Timeout as _:
        logger.warn('服务端超时')
        return JsonResponse({'msg': "服务端超时"}, status=504)
    return JsonResponse({"result": rsp.content[10:]})
Esempio n. 6
0
def rt_status(request, chip):
    func_key = request.GET.get("key")
    chipid = to_int(chip)
    if not func_key:
        logger.warn('key值为空,无法查询实时状态')
        return JsonResponse({"msg": "key值为空"}, status=400)
    if not has_dev_permission(chipid, appobj=request.cur_app):
        logger.warn(u'无操作权限 [%d], [%d]', chipid, request.cur_app.id)
        return JsonResponse({"msg": "无设备对应操作权限!"}, status=400)
    url = 'http://%s/_push_data?dev=%d&msgtype=%d' % (settings.SOCK_SERVER,
                                                      chipid, 0x1C)
    try:
        rsp = requests.post(url, data=func_key, timeout=5)
        if rsp.status_code != 200:
            logger.warn('服务端错误')
            return JsonResponse({'msg': "服务端错误"}, status=502)
    except requests.Timeout as _:
        logger.warn('服务端超时')
        return JsonResponse({'msg': "服务端超时"}, status=504)
    # return JsonResponse({"result": rsp.content[11:]})
    return HttpResponse(rsp.content[11:])
Esempio n. 7
0
def manual_refresh(request, chipid):
    chip = to_int(chipid)
    if not has_dev_permission(chip, appobj=request.cur_app):
        logger.warn(u'无操作权限 [%d], [%d]', chip, request.cur_app.id)
        return JsonResponse({"msg": "无设备对应操作权限!"}, status=400)
    url = 'http://%s/dev_refresh?chipid=%d' % (settings.SOCK_SERVER, chip)
    # 发出网络请求, 推送数据
    try:
        rsp = requests.post(url, timeout=8)
        if rsp.status_code != 200:
            logger.warn(u'网关返回错误,接口调用失败')
            return JsonResponse({"msg": "网关返回错误"}, status=504)
    except requests.Timeout as _:
        logger.error(u'网关请求超时')
        return JsonResponse({"msg": "网关请求超时"}, status=504)
    except IOError as _:
        logger.error(u'请求失败')
        return JsonResponse({"msg": "网关请求失败"}, status=504)
    if rsp.content == 'offline':
        logger.info(u'设备 [%d] 已离线', chip)
        return JsonResponse({"status": "offline", "msg": "OK"})
    logger.info(u'设备 [%d] 在线', chip)
    return JsonResponse({"status": "online", "msg": "OK"})
Esempio n. 8
0
def dev_push_msg(request):
    _chipid = request.REQUEST.get('devid')
    chipid = to_int(_chipid)
    if not chipid:
        logger.warn('设备芯片号需为整数')
        return JsonResponse({'msg': "设备芯片号需为整数"}, status=400)
    msg = request.REQUEST.get('message')
    msg = msg.encode("GBK")
    msgformat = request.REQUEST.get('format')
    if not format:
        logger.info(u'未选择指令类型,默认为文本')
        msgformat = u'MSG'
    if msgformat not in [u'MSG', u'HEX', u'AT', u'LUA', u'LUA_HEX', u'B64']:
        logger.info(u'指令类型错误')
        return JsonResponse({"msg": u"指令类型错误"}, status=400)
    if msgformat == u'HEX':
        if len(msg) % 2:
            logger.warn(u'十六进制原始数值长度需为偶数')
            return JsonResponse({"msg": u"十六进制原始数值长度需为偶数"}, status=400)
        try:
            msg = binascii.a2b_hex(msg)
        except Exception as _:
            logger.warn(u'十六进制数值错误')
            return JsonResponse({"msg": u"无法解析十六进制值"}, status=400)
    if msgformat == 'B64':
        msg = base64.b64decode(msg)
    if msgformat == u'AT':
        if len(msg) > 64 and ('\r' in msg or '\n' in msg):
            logger.info(u'AT指令长度不得超过64字节,且不得换行')
            return JsonResponse({"msg": u"AT指令长度不得超过64字节,且不得换行"}, status=400)
    if msgformat == u'LUA_HEX':
        msg = base64.b64decode(msg)
    if msgformat == u'AT':
        msgtype = 0x14
        msg += '\r\n'
    elif msgformat in [u'MSG', u'HEX', u'B64']:
        msgtype = 0x04
    elif msgformat == u'LUA':
        msgtype = 0x16
    elif msgformat == u'LUA_HEX':
        msgtype = 0x16
    # 判断是否有权限操作此设备
    if not has_dev_permission(chipid, appobj=request.cur_app):
        logger.warn(u'无操作权限 [%d], [%d]', chipid, request.cur_app.id)
        return JsonResponse({"msg": "无设备对应操作权限!"}, status=400)
    url = 'http://%s/_push_data?dev=%d&msgtype=%d'\
        % (settings.SOCK_SERVER, chipid, msgtype)
    try:
        rsp = requests.post(url, data=msg, timeout=50)
        if rsp.status_code != 200:
            logger.warn(u'网关返回错误,数据可能推送失败')
            return JsonResponse({'msg': "网关返回错误"}, status=504)
    except requests.Timeout as _:
        logger.error(u'网关请求超时')
        return JsonResponse({'msg': "网关请求超时"}, status=504)
    except IOError as _:
        logger.error(u'请求失败')
        return JsonResponse({'msg': "请求网关失败"}, status=504)
    # 新建数据库记录 以供浏览
    try:
        dev_db = Device.objects.get(chip=chipid, app=request.cur_app)
    except Device.DoesNotExist as _:
        dev_db = Device(chip=chipid, app=request.cur_app, stat="OK")
        dev_db.save()
    except Device.MultipleObjectsReturned as _:
        logger.error('服务器逻辑错误,存在相同记录 [%d] [%d]', chipid, request.cur_app.id)
        return JsonResponse({'msg': "网关返回错误"}, status=504)
    if isinstance(msg, unicode):
        msg = msg.encode('utf-8')
    record = Message(dev=dev_db,
                     app=request.cur_app,
                     msgtype='PUSH_DEV',
                     category='MSG',
                     create_time=datetime.datetime.now(),
                     msg=msg,
                     user=request.cur_user,
                     stat="OK")
    record.save()
    return JsonResponse({"msg": "OK"})