1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
|
3
|
|
|
from collections import OrderedDict |
4
|
|
|
from .. import devices |
5
|
|
|
from .tclrequest import TclRequest |
6
|
|
|
from .tclresult import CheckResult |
7
|
|
|
|
8
|
|
|
class CheckRequest(TclRequest): |
|
|
|
|
9
|
|
|
def __init__(self, device: devices.Device): |
|
|
|
|
10
|
|
|
super().__init__() |
11
|
|
|
self.uri = "/check.php" |
12
|
|
|
self.method = "GET" |
13
|
|
|
self.device = device |
14
|
|
|
|
|
|
|
|
15
|
|
|
def get_headers(self): |
16
|
|
|
return {"User-Agent": self.device.ua} |
17
|
|
|
|
18
|
|
|
def get_params(self): |
19
|
|
|
params = OrderedDict() |
20
|
|
|
params["id"] = self.device.imei |
21
|
|
|
params["curef"] = self.device.curef |
22
|
|
|
params["fv"] = self.device.fwver |
23
|
|
|
params["mode"] = self.device.mode |
24
|
|
|
params["type"] = self.device.type |
25
|
|
|
params["cltp"] = self.device.cltp |
26
|
|
|
params["cktp"] = self.device.cktp |
27
|
|
|
params["rtd"] = self.device.rtd |
28
|
|
|
params["chnl"] = self.device.chnl |
29
|
|
|
#params["osvs"] = self.device.osvs |
30
|
|
|
#params["ckot"] = self.device.ckot |
31
|
|
|
return params |
32
|
|
|
|
33
|
|
|
def is_done(self, http_status: int, contents: str) -> bool: |
34
|
|
|
ok_states = { |
35
|
|
|
204: "No update available.", |
36
|
|
|
404: "No data for requested CUREF/FV combination.", |
37
|
|
|
} |
38
|
|
|
if http_status == 200: |
39
|
|
|
self.response = contents |
40
|
|
|
self.result = CheckResult(contents) |
41
|
|
|
self.success = True |
42
|
|
|
return True |
43
|
|
|
elif http_status in ok_states: |
44
|
|
|
self.error = ok_states[http_status] |
45
|
|
|
self.success = False |
46
|
|
|
return True |
47
|
|
|
elif http_status not in [500, 502, 503]: |
48
|
|
|
# Errors OTHER than 500, 502 or 503 are probably |
49
|
|
|
# errors where we don't need to retry |
50
|
|
|
self.error ="HTTP {}.".format(http_status) |
|
|
|
|
51
|
|
|
self.success = False |
52
|
|
|
return True |
53
|
|
|
return False |
54
|
|
|
|
55
|
|
|
# Check requests have 4 possible outcomes: |
56
|
|
|
# 1. HTTP 200 with XML data - our desired info |
57
|
|
|
# 2. HTTP 204 - means: no newer update available |
58
|
|
|
# 3. HTTP 404 - means: invalid device or firmware version |
59
|
|
|
# 4. anything else: server problem (esp. 500, 502, 503) |
60
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.