def initLogger(): log_folder = os.path.expanduser(parseConfig()['log_folder']) log_file = '%s/node.log' % log_folder os.system('mkdir %s' % log_folder) logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
def sqlResultToDict(table, query): serial = parseConfig()['serial'] sqlResult = getSqlResult(query) # Turn sql result into a dict with the column names names = getColNames(table) result = [] for row in sqlResult: result.append(dict(list(zip(names, row)))) return result
def setup(): global plcmngr master_plc_ip, tx_port, rx_port = parseConfig("serverconfig.json") plcmngr = PlcMasterManager(master_plc_ip, tx_port, rx_port) plcmngr.start() atexit.register(lambda: cleanup(plcmngr))
def printLogFolder(): log_folder = parseConfig()['log_folder'] print(os.path.expanduser(log_folder))
from config_parser import parseConfig import os import sqlite3 # Use expanduser to expand ~ to the user's home directory DB_FILENAME = os.path.expanduser(parseConfig()['db_filename']) def main(): # Start the database fresh if os.path.exists(DB_FILENAME): os.remove(DB_FILENAME) # Connect to db. Creates new file if no db found conn = sqlite3.connect(DB_FILENAME) c = conn.cursor() c.execute( '''CREATE table sensor_data ( timestamp integer PRIMARY KEY, value integer );''') c.execute( '''CREATE table node_data ( serial integer PRIMARY KEY, ip_address text, is_master integer, is_broker integer, is_device integer, is_sensor integer,
data = self._sockmngr_rx.recv() if data: response = self._parseInput(data) if response: self._sockmngr_tx.send(response) time.sleep(0.05) def start(self): self._sockmngr_tx.start() self._sockmngr_rx.start() self._running.set() self._input_thread = threading.Thread(target=self._inputReaderLoop) self._input_thread.start() self._root.mainloop() def stop(self): self._running.clear() if self._input_thread is not None and self._input_thread.isAlive(): self._input_thread.join() self._sockmngr_rx.stop() self._sockmngr_tx.stop() self._root.destroy() if __name__ == "__main__": server = "localhost" _, rx_port, tx_port = parseConfig("serverconfig.json") dummyplc = DummyMasterPlc(server, tx_port, rx_port) dummyplc.start()
def isMaster(serial=None): if serial is None: serial = parseConfig()['serial'] return getBoolCol(serial, 'is_master')
def getOwnInteractions(): serial = parseConfig()['serial'] query = '''SELECT * FROM interactions WHERE target_serial IS %d''' % serial return sqlResultToDict('interactions', query)