def main(): try: # mart start os.system("date > /var/log/iot/edge-start.log") # test for admin if not xsys.is_admin(): print "Run program as admin: sudo ./start-ttybots.py" exit(1) # keep going if not start_ports(): xsys.log_error("UnableToStartPorts!") exit(1) # start scanning rxtx procs while True: try: print "--- start process main ----" xsys.trace1("MAIN PID: %s" % os.getpid()) for p in PROCS: xsys.trace1("\tSUB PID: %s" % p.pid) time.sleep(MAIN_DELAY) except Exception as ex: xsys.log_error(ex.message) except Exception as ex: # if shit goes bad! os.system("echo -e '\n+ + + +' >> /var/log/iot/edge.log") os.system("date >> /var/log/iot/edge.log") os.system("echo '%s' >> /var/log/iot/edge.log" % ex.message)
def start_ports(): try: global PROCS xsys.trace1("\n- - - starting ports - - -") # for each port for dev in PORTS: try: if not check_tty_dev(dev): continue # start process name = dev.replace("/dev/", "") p = mp.Process(name=name, target=ttybot.main, args=("d", dev, None)) p.start() xsys.trace2("PID:", "\t%s" % p.pid) PROCS.append(p) time.sleep(0.480) except Exception as x: xsys.log_error(x.message) # save to file new on each start & return safe_pids() return True except Exception as x: xsys.log_error(x.message) return False
def insert_report(edgeid, collid, line): cur = None conn = None try: conn = get_opened_conn() # get db id for edgeid + collid db_rid = __get_coll_id(conn, edgeid, collid) # error getting row id if db_rid is None: xsys.log_error("DbNotFound: %s; %s;" % (edgeid, collid)) return # get args from msg body dtsbucket = int(time.time() / xconf.DTS_BUCKET_15M) # collid, dtsbucket, pin, pinval args = get_query_args(line) buff = js.dumps(args) # build json string json = "{\"pins\": %s}" % buff qry = "insert into collector_reads values(%i, %i, '%s');" qargs = (db_rid, dtsbucket, json) cur = conn.cursor() cur.execute(qry % qargs) # this should be fixed later on; maybe auto commit; conn.commit() except psycopg2.Error as e: print e finally: # the end & close if cur is not None: cur.close() if conn is not None: conn.close()
def __get_coll_id_from_db(conn, edgeid, collid): rid = None cur = None try: tsql = "select rid from collectors c where c.edge_id = '%s' and c.collector_id = '%s';" sql = tsql % (edgeid, collid) xsys.trace2("sql: ", sql) # open cursor cur = conn.cursor() cur.execute(sql) row = cur.fetchone() rid = int(row[0]) except Exception as ex: xsys.log_error(ex.message) finally: # close cursor if cur is not None: cur.close() return rid
xsys.trace1("\tSUB PID: %s" % p.pid) time.sleep(MAIN_DELAY) except Exception as ex: xsys.log_error(ex.message) except Exception as ex: # if shit goes bad! os.system("echo -e '\n+ + + +' >> /var/log/iot/edge.log") os.system("date >> /var/log/iot/edge.log") os.system("echo '%s' >> /var/log/iot/edge.log" % ex.message) def safe_pids(): pidsfile = "logs/pids.log" if os.path.exists(pidsfile): os.unlink(pidsfile) with open(pidsfile, "w") as f: f.write("*%s\n" % os.getpid()) for proc in PROCS: f.write("**%s\n" % proc.pid) # end if __name__ == "__main__": # start main try: setproctitle.setproctitle("ttybots/START") except Exception as x: xsys.log_error(x.message) # start main main()