示例#1
0
 ts = datetime.datetime.utcfromtimestamp(int(w)).isoformat('T')+"Z"
 t = c[4] # temp
 v = c[5] # volts
 ch = int(c[1]) # channel
 if not (ch in n):
     n[ch]=0
     volts[ch]=""
     temps[ch]=""
 temps[ch]+=string.join([ts,t],",") + "\r\n"
 volts[ch]+=string.join([ts,v],",")+ "\r\n"
 n[ch]+=1
 if n[ch]==MAX_DATAPOINTS:
     if not debug_mode:
         try:
             for ch in n:
                 cosm.submit_datapoints(feed,ch,key,temps[ch])
                 # Voltage datastream is 100+temp datasteam
                 cosm.submit_datapoints(feed,ch+100,key,volts[ch])
         except Exception, ex:
             log.error("Error sending to COSM: %s" % ex )
             normal_exit = False
             break
         try:
             write_watermark(WATERMARK_FILE % feed,w)
         except Exception, ex:
             log.error("Error writing watermark: %s" % ex )
             sys.exit(1)
     watermark = w
     temps={}
     volts={}
     n={}
                print csv_report
            else:
                # Write to file
                try:
                    data_file.write(csv_report)
                    data_file.flush()
                except IOError, ex:
                    # Error writing CSV is fatal
                    log.error("Error writing CSV file: %s" % ex)
                    sys.exit(1)
                # Send to COSM
                if cfg.has_key("cosm"):
                    try:
                        ts = datetime.datetime.utcfromtimestamp(int(local_time)).isoformat('T')+"Z"
                        cosm_report =string.join([ts,str(temp_c)],",") + "\r\n"
                        cosm.submit_datapoints(cosm_feed,cosm_datastream,cosm_key,cosm_report)
                    except Exception, ex:
                        # Error sending to COSM is non-fatal, but logged anyway
                        log.error("Error sending to COSM: %s" % ex )

            if sleep_time>0:
                time.sleep(sleep_time)
            else:
                break
    finally:
        if not debug_mode:
            data_file.close();

if __name__ == '__main__':
    main()
                except IOError, ex:
                    # Error writing CSV is fatal
                    log.error("Error writing CSV file: %s" % ex)
                    sys.exit(1)
                    
                # Send to COSM
                if cfg.has_key("cosm"):
                    data = {cosm_temp_datastream:temp_cs,
                            cosm_tstate_datastream:tstate,
                            cosm_fstate_datastream:fstate
                            }
                    ts = datetime.datetime.utcfromtimestamp(int(local_time)).isoformat('T')+"Z"
                    for ds in data:
                        try:
                            cosm_report =string.join([ts,str(data[ds])],",") + "\r\n"
                            cosm.submit_datapoints(cosm_feed,ds,cosm_key,cosm_report)
                        except Exception, ex:
                            # Error sending to COSM is non-fatal, but logged
                            log.warning("Error sending to COSM datastream %s. %s" % (ds,ex))

            if sleep_time>0:
                time.sleep(sleep_time)
            else:
                break
    finally:
        if not debug_mode:
            data_file.close();

if __name__ == '__main__':
    main()