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): |
| 39 | print "Ready for Programming: Insert card now (or CTRL-C to cancel)" |
| 40 | self.sl.wait_for_card(newcardonly=not first) |
| 41 | |
| 42 | def error(self): |
| 43 | print "Programming failed: Remove card from reader" |
| 44 | print "" |
| 45 | |
| 46 | def done(self): |
| 47 | print "Programming successful: Remove card from reader" |
| 48 | print "" |
| 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): |
| 58 | print "Card handler Config-file: " + str(config_file) |
| 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): |
| 66 | print "" |
| 67 | print "Card handler output:" |
| 68 | print "---------------------8<---------------------" |
| 69 | stdout = out[0].strip() |
| 70 | if len(stdout) > 0: |
| 71 | print "stdout:" |
| 72 | print stdout |
| 73 | stderr = out[1].strip() |
| 74 | if len(stderr) > 0: |
| 75 | print "stderr:" |
| 76 | print stderr |
| 77 | print "---------------------8<---------------------" |
| 78 | print "" |
| 79 | |
| 80 | def __exec_cmd(self, command): |
| 81 | print "Card handler Commandline: " + str(command) |
| 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: |
| 91 | print "" |
| 92 | print "Error: Card handler failure! (rc=" + str(rc) + ")" |
| 93 | sys.exit(rc) |
| 94 | |
| 95 | def get(self, first = False): |
| 96 | print "Ready for Programming: Transporting card into the reader-bay..." |
| 97 | self.__exec_cmd(self.cmds['get']) |
| 98 | self.sl.connect() |
| 99 | |
| 100 | def error(self): |
| 101 | print "Programming failed: Transporting card to the error-bin..." |
| 102 | self.__exec_cmd(self.cmds['error']) |
| 103 | print "" |
| 104 | |
| 105 | def done(self): |
| 106 | print "Programming successful: Transporting card into the collector bin..." |
| 107 | self.__exec_cmd(self.cmds['done']) |
| 108 | print "" |