-
Notifications
You must be signed in to change notification settings - Fork 83
/
brewpiVersion.py
212 lines (183 loc) · 6.65 KB
/
brewpiVersion.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Copyright 2013 BrewPi
# This file is part of BrewPi.
# BrewPi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# BrewPi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with BrewPi. If not, see <http://www.gnu.org/licenses/>.
import simplejson as json
import sys
import time
from distutils.version import LooseVersion
from BrewPiUtil import asciiToUnicode
from serial import SerialException
import re
def getVersionFromSerial(bg_ser):
version = None
retries = 0
startTime = time.time()
bg_ser.writeln('n') # request version info
while retries < 10:
retry = True
while True: # read all lines from serial
line = bg_ser.read_line()
if line:
if line[0] == 'N':
data = line.strip('\n')[2:]
version_parsed = AvrInfo(data)
if version_parsed and version_parsed.version != "0.0.0":
retry = False
version = version_parsed
break
else:
break
if time.time() - startTime >= 30:
# try max 30 seconds
retry = False
if retry:
bg_ser.writeln('n') # request version info
retries += 1
time.sleep(1)
else:
break
return version
class AvrInfo:
""" Parses and stores the version and other compile-time details reported by the controller """
version = "v"
build = "n"
simulator = "y"
board = "b"
shield = "s"
log = "l"
commit = "c"
shield_revA = "revA"
shield_revC = "revC"
spark_shield_v1 = "V1"
spark_shield_v2 = "V2"
spark_shield_v3 = "V3"
shields = {1: shield_revA, 2: shield_revC, 3: spark_shield_v1, 4: spark_shield_v2, 5: spark_shield_v3}
board_leonardo = "leonardo"
board_standard = "uno"
board_mega = "mega"
board_spark_core = "core"
board_photon = "photon"
board_p1 = "p1"
boards = {'l': board_leonardo, 's': board_standard, 'm': board_mega, 'x': board_spark_core, 'y': board_photon, 'p': board_p1}
family_arduino = "Arduino"
family_spark = "Particle"
families = { board_leonardo: family_arduino,
board_standard: family_arduino,
board_mega: family_arduino,
board_spark_core: family_spark,
board_photon: family_spark,
board_p1: family_spark}
board_names = { board_leonardo: "Leonardo",
board_standard: "Uno",
board_mega: "Mega",
board_spark_core: "Core",
board_photon: "Photon",
board_p1: "p1"}
def __init__(self, s=None):
self.version = LooseVersion("0.0.0")
self.build = 0
self.commit = None
self.simulator = False
self.board = None
self.shield = None
self.log = 0
self.parse(s)
def parse(self, s):
if s is None or len(s) == 0:
pass
else:
s = s.strip()
if s[0] == '{':
self.parseJsonVersion(s)
else:
self.parseStringVersion(s)
def parseJsonVersion(self, s):
j = None
try:
j = json.loads(s)
except json.decoder.JSONDecodeError, e:
print >> sys.stderr, "JSON decode error: %s" % str(e)
print >> sys.stderr, "Could not parse version number: " + s
except UnicodeDecodeError, e:
print >> sys.stderr, "Unicode decode error: %s" % str(e)
print >> sys.stderr, "Could not parse version number: " + s
except TypeError, e:
print >> sys.stderr, "TypeError: %s" % str(e)
print >> sys.stderr, "Could not parse version number: " + s
self.family = None
self.board_name = None
if not j:
return
if AvrInfo.version in j:
self.parseStringVersion(j[AvrInfo.version])
if AvrInfo.simulator in j:
self.simulator = j[AvrInfo.simulator] == 1
if AvrInfo.board in j:
self.board = AvrInfo.boards.get(j[AvrInfo.board])
self.family = AvrInfo.families.get(self.board)
self.board_name = AvrInfo.board_names.get(self.board)
if AvrInfo.shield in j:
self.shield = AvrInfo.shields.get(j[AvrInfo.shield])
if AvrInfo.log in j:
self.log = j[AvrInfo.log]
if AvrInfo.build in j:
self.build = j[AvrInfo.build]
if AvrInfo.commit in j:
self.commit = j[AvrInfo.commit]
def parseStringVersion(self, s):
pattern = re.compile(r"^[0-9]+\.[0-9]+\.[0-9]+.*")
if pattern.match(s): # check for valid string
self.version = LooseVersion(s)
def toString(self):
if self.version:
return str(self.version)
else:
return "0.0.0"
def article(self, word):
if not word:
return "a" # in case word is not valid
firstLetter = word[0]
if firstLetter.lower() in 'aeiou':
return "an"
else:
return "a"
def toExtendedString(self):
string = "BrewPi v" + self.toString()
if self.commit:
string += ", running commit " + str(self.commit)
if self.build:
string += " build " + str(self.build)
if self.board:
string += ", running on "+ self.articleFullName()
if self.shield:
string += " with a " + str(self.shield) + " shield"
if(self.simulator):
string += ", running as simulator"
return string
def isNewer(self, versionString):
return self.version < LooseVersion(versionString)
def isEqual(self, versionString):
return self.version == LooseVersion(versionString)
def familyName(self):
family = AvrInfo.families.get(self.board)
if family == None:
family = "????"
return family
def boardName(self):
board = AvrInfo.board_names.get(self.board)
if board == None:
board = "????"
return board
def fullName(self):
return self.familyName() + " " + self.boardName()
def articleFullName(self):
return self.article(self.family) + " " + self.fullName()