forked from digidotcom/transport_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_sim.py
101 lines (80 loc) · 2.76 KB
/
read_sim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/local/bin/python
############################################################################
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2017 Digi International Inc. All Rights Reserved.
#
############################################################################
"""
Application prints a statement if the current, active SIM ICCID is the same
or different as the previous ICCID.
Flow: Read the active SIM on boot to determine if it is different from
the previous boot. On first boot, it will report that it is the
first boot up and tracked the current SIM by ICCID.
Options: When a non-matching ICCID is found, an alert, data point or some
other action should be added. The placeholder is marked as:
---- DO SOMETHING HERE ---
Usage: The script should be uploaded and then can be run manually via the
command-line (CLI), as:
> python read_sim.py
It should updated with an appropriate action and then set to run
on boot.
"""
import os
import sarcli
import sys
SIM_FILE = "iccid.txt"
MODEM_STAT = "modemstat ?"
def cli(command):
cli = sarcli.open()
cli.write(command)
response = cli.read()
cli.close()
return response
def parse_modemStat(modemstat):
iccid = ""
string_match = "ICCID:"
lines = modemstat.split('\r\n')
for line in lines:
if string_match in line:
iccid = line.strip('\r\n').replace(string_match, '').strip()
break
return iccid
def get_iccid():
resp = cli(MODEM_STAT)
return parse_modemStat(resp)
def sim_file_exists():
return os.path.isfile(SIM_FILE)
def read_sim_file():
"""Opens the SIM_FILE and return the ICCID"""
sim = ""
if sim_file_exists():
with(open(SIM_FILE, 'r')) as f:
sim = f.read()
sim = sim.strip()
return sim
def write_sim_file(iccid):
with(open(SIM_FILE, 'w')) as f:
f.write(iccid)
if __name__ == "__main__":
if not sim_file_exists():
print "First run, no SIM file exists"
write_sim_file(get_iccid())
else:
print "Checking SIM information"
modem_stat = cli(MODEM_STAT)
current_sim = parse_modemStat(modem_stat)
print "SIM number %s" % (current_sim,)
sim = read_sim_file()
print "Previous SIM %s" % (sim,)
if sim != current_sim:
print "SIM is different than previous!"
# ---- DO SOMETHING HERE ----
# send datapoint here
else:
print "SIM ICCIDs match"
write_sim_file(current_sim)
sys.exit()