56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
import yaml
|
|
import logging
|
|
import argparse
|
|
import sys
|
|
import socket
|
|
from os.path import exists
|
|
from sim_reader import SIMReader
|
|
from connection import ServerConnection
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
if __name__ == "__main__":
|
|
config = None
|
|
sim_readers = []
|
|
parser = argparse.ArgumentParser(description='Communicates with SIM cards using raw tty port')
|
|
parser.add_argument("--config", help="config file to use", type=str, default="config.yaml")
|
|
args = parser.parse_args()
|
|
|
|
# Parse config file
|
|
if not exists(args.config):
|
|
print("Config file not found")
|
|
exit(1)
|
|
try:
|
|
with open(args.config, "r") as config_file:
|
|
config = yaml.load(config_file, Loader=yaml.BaseLoader)
|
|
except Exception as err:
|
|
logging.error("Unable to read %s: %s" % (args.config, err))
|
|
sys.exit(1)
|
|
|
|
if not "instance" in config:
|
|
config["instance"] = {}
|
|
if not "name" in config["instance"]:
|
|
config["instance"]["name"] = socket.gethostname()
|
|
|
|
# Create a connection to SIM server
|
|
srv_connection = ServerConnection(
|
|
name=config["instance"]["name"],
|
|
addr=config["server"]["host"],
|
|
port=config["server"]["port"],
|
|
secret=config["server"]["secret"]
|
|
)
|
|
|
|
# Open SIM readers
|
|
for port in config['ports']:
|
|
sim = SIMReader(ttyPath=port)
|
|
if sim.ready:
|
|
print("Opened %s. ICCID: %s IMSI: %s" % (port, sim.iccid, sim.imsi))
|
|
srv_connection.send_register(
|
|
iccid=sim.iccid,
|
|
imsi=sim.imsi,
|
|
tty_port=port
|
|
)
|
|
sim.close()
|
|
|
|
|