Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | """ pySim: card handler utilities |
| 5 | """ |
| 6 | |
| 7 | # |
| 8 | # (C) 2019 by Sysmocom s.f.m.c. GmbH |
| 9 | # All Rights Reserved |
| 10 | # |
| 11 | # This program is free software: you can redistribute it and/or modify |
| 12 | # it under the terms of the GNU General Public License as published by |
| 13 | # the Free Software Foundation, either version 2 of the License, or |
| 14 | # (at your option) any later version. |
| 15 | # |
| 16 | # This program is distributed in the hope that it will be useful, |
| 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | # GNU General Public License for more details. |
| 20 | # |
| 21 | # You should have received a copy of the GNU General Public License |
| 22 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | # |
| 24 | |
| 25 | |
| 26 | import subprocess |
| 27 | import sys |
| 28 | import yaml |
| 29 | |
| 30 | # Manual card handler: User is prompted to insert/remove card from the reader. |
| 31 | class card_handler: |
| 32 | |
| 33 | sl = None |
| 34 | |
| 35 | def __init__(self, sl): |
| 36 | self.sl = sl |
| 37 | |
| 38 | def get(self, first = False): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 39 | print("Ready for Programming: Insert card now (or CTRL-C to cancel)") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 40 | self.sl.wait_for_card(newcardonly=not first) |
| 41 | |
| 42 | def error(self): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 43 | print("Programming failed: Remove card from reader") |
| 44 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 45 | |
| 46 | def done(self): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 47 | print("Programming successful: Remove card from reader") |
| 48 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 49 | |
| 50 | # Automatic card handler: A machine is used to handle the cards. |
| 51 | class card_handler_auto: |
| 52 | |
| 53 | sl = None |
| 54 | cmds = None |
| 55 | verbose = True |
| 56 | |
| 57 | def __init__(self, sl, config_file): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 58 | print("Card handler Config-file: " + str(config_file)) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 59 | self.sl = sl |
| 60 | with open(config_file) as cfg: |
| 61 | self.cmds = yaml.load(cfg, Loader=yaml.FullLoader) |
| 62 | |
| 63 | self.verbose = (self.cmds.get('verbose') == True) |
| 64 | |
| 65 | def __print_outout(self,out): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 66 | print("") |
| 67 | print("Card handler output:") |
| 68 | print("---------------------8<---------------------") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 69 | stdout = out[0].strip() |
| 70 | if len(stdout) > 0: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 71 | print("stdout:") |
| 72 | print(stdout) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 73 | stderr = out[1].strip() |
| 74 | if len(stderr) > 0: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 75 | print("stderr:") |
| 76 | print(stderr) |
| 77 | print("---------------------8<---------------------") |
| 78 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 79 | |
| 80 | def __exec_cmd(self, command): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 81 | print("Card handler Commandline: " + str(command)) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 82 | |
| 83 | proc = subprocess.Popen([command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 84 | out = proc.communicate() |
| 85 | rc = proc.returncode |
| 86 | |
| 87 | if rc != 0 or self.verbose: |
| 88 | self.__print_outout(out) |
| 89 | |
| 90 | if rc != 0: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 91 | print("") |
| 92 | print("Error: Card handler failure! (rc=" + str(rc) + ")") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 93 | sys.exit(rc) |
| 94 | |
| 95 | def get(self, first = False): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 96 | print("Ready for Programming: Transporting card into the reader-bay...") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 97 | self.__exec_cmd(self.cmds['get']) |
| 98 | self.sl.connect() |
| 99 | |
| 100 | def error(self): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 101 | print("Programming failed: Transporting card to the error-bin...") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 102 | self.__exec_cmd(self.cmds['error']) |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 103 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 104 | |
| 105 | def done(self): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 106 | print("Programming successful: Transporting card into the collector bin...") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 107 | self.__exec_cmd(self.cmds['done']) |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 108 | print("") |