Example #1
0
 def post(self, topic, data, wait=0, **kwargs):
     if MqttConnect.mqtt is None:
         self.rt.msg = "请先连接服务器"
     else:
         rs = ''
         for d in data:
             if ord(d) in [0xa0, 0xc2] or ord(d) > 128:
                 continue
             rs += d
         Send.ID = (Send.ID % 1000000000 + 1)
         data = rs
         try:
             data = json.loads(data)
         except Exception as e:
             self.rt.data = "error json:" + data
             return self.rt.json_dumps()
         if wait:
             data['id'] = str(Send.ID)
         MqttConnect.mqtt.publish(topic, payload=json.dumps(data))
         self.rt.msg = '发送成功'
         self.rt.data = '{}'
         MqttConnect.G[data['id']] = ""
         while wait > 0:
             if MqttConnect.G[data['id']]:
                 self.rt.data = MqttConnect.G[data['id']]
                 break
             wait -= 0.1
             time.sleep(0.1)
     return self.rt.json_dumps()
Example #2
0
 def addToken(self, params):
     try:
         params = json.loads(params)
         params.update(dict(token=app.config['TOKEN']))
         params = dumps(params)
     except:
         pass
     return params
Example #3
0
    def get_response(self, text):
        '''

        :param text:
        :return: 如请求结果能转为dict对象,则转化,不能则返回请求结果
        '''
        try:
            return json.loads(text)
        except:
            return text
Example #4
0
    def data_to_json(self, string):
        '''

        :param string:
        :return: json数据转为dict
        '''
        if isinstance(string, str):
            string = string.replace("'", '"').replace("\n", '')
            return json.loads(string)
        else:
            return string
Example #5
0
 def on_message(self, client, userdata, msg):
     #print('recv not json',msg.payload,self.callback)
     try:
         c = json.loads(msg.payload)
         if '_replay' in msg.topic:
             MqttConnect.G[c['id']] = json.dumps(c, indent=4)
     except Exception as e:
         print('recv not json', e, msg.payload)
     if self.callback:
         self.callback(msg.topic, msg.payload)
     else:
         web.clients.send(
             'bLinkHelp',
             dict(id=msg.topic, t=time.time(), i=msg.payload, o=''))
Example #6
0
 def run(self):
     if BaseWeb.db is None:
         return
     session = BaseWeb.db.session()
     while True:
         tasks = session.query(tb_task).filter(
             or_(
                 tb_task.statu == -2,
                 and_(tb_task.statu == 0, tb_task.runT < time.time() + 3,
                      tb_task.runT > time.time() - 3))).all()
         for task in tasks:
             try:
                 responce = requests.post(task.url,
                                          json=json.loads(task.param),
                                          timeout=1.5)
                 task.statu = 1
                 task.callBackParam = responce.content
             except Exception as e:
                 task.statu = -1
                 task.callBackParam = json.dumps(dict(error=str(e)))
         session.commit()
         time.sleep(0.5)