简体   繁体   中英

python Paho client MQTT : writing to multiple files

Initialize a MQTT client, connect to the MQTT broker on a specific topic, for each message received on that topic execute a function call to check if the length of an attribute in message is greater than 200, if yes then find the Gyro meter reading: GyroX & save to file ensuring that the file size is kept less than say 400MB if not then open a new file and start writing to it.

Referring post: https://stackoverflow.com/a/54306598/12097046

I want to write to multiple files ie post incoming json messages to different files based on size instead of just one file. how to do it? any help is appreciated

file_name='/tmp/gyro_256'+"_"+timestr+".csv"
def on_message(client, userdata, message):
  y = json.loads(message.payload)
  v = (len(y['sec_data']))
  p = int(v)
  if p >= 200:
          d = (y["sec_data"][10]["GyroX"])
           with open(file_name,'a+') as f:
                    f.write(d + "\n")
client = mqttClient.Client("123")               #create new instance
client.username_pw_set(user, password=password)    #set username and 
client.on_connect= on_connect                      #attach function to 
  
client.on_message= on_message                      #attach function to 
   
client.connect(broker_address,port,100) #connect
client.subscribe("tes1") #subscribe
client.loop_start() #then keep listening forever
if int(os.path.getsize(file_name)) > 47216840 :
     client.loop_stop()
     timestr = time.strftime("%Y%m%d%H%M%S")
     file_name = '/vol/vol_HDB/data/gyro_256'+"_"+timestr+".csv"
client.loop_start()

None of the code after the fist call to client.loop_start() will ever be run because that call blocks forever.

If you want to change the file name you will have to do the file size test in the on_message callback.

def on_message(client, userdata, message):
  global filename
  y = json.loads(message.payload)
  v = (len(y['sec_data']))
  p = int(v)

  if int(os.path.getsize(file_name)) > 47216840 :
     timestr = time.strftime("%Y%m%d%H%M%S")
     file_name = '/vol/vol_HDB/data/gyro_256'+"_"+timestr+".csv"

  if p >= 200:
    d = (y["sec_data"][10]["GyroX"])
    with open(file_name,'a+') as f:
      f.write(d + "\n")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM