Beispiel #1
0
def bind(msg):
    wx_device_id = msg.device_id
    open_id = msg.open_id
    wx_wechatoriginalid = msg.device_type
    userinfo = getuserinfo(open_id)
    userinfo = json.dumps(userinfo)
    url = FOGPATH + '/wechat/bind/'
    data = {
        "wx_device_id": wx_device_id,
        "originalid": wx_wechatoriginalid,
        "open_id": open_id,
        "userinfo": userinfo
    }
    response = requests.post(url, data=data, verify=False)
    fog_response = json.loads(response.text)
    if not Enduser.objects.filter(open_id=open_id):
        en = Enduser()
        en.fog_enduser_id = fog_response['data']['fog_enduser_id']
        en.open_id = open_id
        en.fog_product_id = fog_response['data']['fog_product_id']
        en.save()
    try:
        device = Device.objects.get(wx_device_id=wx_device_id)
        device.note = fog_response['data']['devicename']
        device.fog_product_id = fog_response['data']['fog_product_id']
        device.save()
    except Device.DoesNotExist:
        device = Device()
        device.note = fog_response['data']['devicename']
        device.fog_product_id = fog_response['data']['fog_product_id']
        device.fog_device_id = fog_response['data']['fog_device_id']
        device.wx_device_id = wx_device_id
        device.save()
    return json.loads(response.text)
Beispiel #2
0
 def post(self, request):
     try:
         de = Device.objects.get(
             fog_device_id=request.data['fog_device_id'])
         de.wx_device_id = request.data['wx_device_id']
         de.ai_device_id = request.data['ai_device_id']
         note = getdeviceidfromfog(request.data['wx_device_id'])
         if note['meta']['code'] == 0:
             de.note = note['data']['wx_device_alias']
             de.fog_product_id = note['data']['fog_product_id']
             de.save()
         else:
             data = requests.query_params
             meta = {'code': 27081, 'message': note['meta']['message']}
             return Response({
                 'meta': meta,
                 'data': data
             },
                             status=status.HTTP_200_OK)
     except Device.DoesNotExist:
         de = Device()
         de.wx_device_id = request.data['wx_device_id']
         de.ai_device_id = request.data['ai_device_id']
         note = getdeviceidfromfog(requests.data['wx_device_id'])
         if note['meta']['code'] == 0:
             de.note = note['data']['wx_device_alias']
             de.fog_product_id = note['data']['fog_product_id']
             de.save()
         else:
             data = requests.query_params
             meta = {'code': 27081, 'message': note['meta']['message']}
             return Response({
                 'meta': meta,
                 'data': data
             },
                             status=status.HTTP_200_OK)
     data = self.serializer_class(de).data
     meta = {'code': 0, 'message': 'successful'}
     return Response({
         'meta': meta,
         'data': data
     },
                     status=status.HTTP_200_OK)