2018-02-11 01:38:38 +00:00
|
|
|
#!/usr/bin/env python3
|
2018-02-08 00:15:58 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-02-11 01:38:38 +00:00
|
|
|
"""Generic update check request."""
|
|
|
|
|
2018-02-08 00:15:58 +00:00
|
|
|
from collections import OrderedDict
|
2018-02-11 01:38:38 +00:00
|
|
|
|
2018-02-10 01:37:10 +00:00
|
|
|
from .. import devices
|
|
|
|
from .tclrequest import TclRequest
|
|
|
|
from .tclresult import CheckResult
|
2018-02-08 00:15:58 +00:00
|
|
|
|
2018-02-11 01:38:38 +00:00
|
|
|
|
2018-02-08 00:15:58 +00:00
|
|
|
class CheckRequest(TclRequest):
|
2018-02-11 01:38:38 +00:00
|
|
|
"""Generic update check request."""
|
|
|
|
|
2018-02-08 00:15:58 +00:00
|
|
|
def __init__(self, device: devices.Device):
|
2018-02-11 01:38:38 +00:00
|
|
|
"""Populate variables.."""
|
2018-02-08 00:15:58 +00:00
|
|
|
super().__init__()
|
|
|
|
self.uri = "/check.php"
|
|
|
|
self.method = "GET"
|
|
|
|
self.device = device
|
2018-02-11 01:38:38 +00:00
|
|
|
|
2018-02-08 00:15:58 +00:00
|
|
|
def get_headers(self):
|
2018-02-11 01:38:38 +00:00
|
|
|
"""Return request headers."""
|
|
|
|
return {"User-Agent": self.device.uagent}
|
2018-02-08 00:15:58 +00:00
|
|
|
|
|
|
|
def get_params(self):
|
2018-02-11 01:38:38 +00:00
|
|
|
"""Return request parameters."""
|
2018-02-08 00:15:58 +00:00
|
|
|
params = OrderedDict()
|
|
|
|
params["id"] = self.device.imei
|
|
|
|
params["curef"] = self.device.curef
|
|
|
|
params["fv"] = self.device.fwver
|
|
|
|
params["mode"] = self.device.mode
|
|
|
|
params["type"] = self.device.type
|
|
|
|
params["cltp"] = self.device.cltp
|
|
|
|
params["cktp"] = self.device.cktp
|
|
|
|
params["rtd"] = self.device.rtd
|
|
|
|
params["chnl"] = self.device.chnl
|
|
|
|
#params["osvs"] = self.device.osvs
|
|
|
|
#params["ckot"] = self.device.ckot
|
|
|
|
return params
|
|
|
|
|
|
|
|
def is_done(self, http_status: int, contents: str) -> bool:
|
2018-02-11 01:38:38 +00:00
|
|
|
"""Handle request result."""
|
2018-02-08 00:15:58 +00:00
|
|
|
ok_states = {
|
|
|
|
204: "No update available.",
|
|
|
|
404: "No data for requested CUREF/FV combination.",
|
|
|
|
}
|
|
|
|
if http_status == 200:
|
2018-02-08 23:12:46 +00:00
|
|
|
self.response = contents
|
2018-02-10 01:37:10 +00:00
|
|
|
self.result = CheckResult(contents)
|
2018-02-08 00:15:58 +00:00
|
|
|
self.success = True
|
|
|
|
return True
|
|
|
|
elif http_status in ok_states:
|
|
|
|
self.error = ok_states[http_status]
|
|
|
|
self.success = False
|
|
|
|
return True
|
|
|
|
elif http_status not in [500, 502, 503]:
|
|
|
|
# Errors OTHER than 500, 502 or 503 are probably
|
|
|
|
# errors where we don't need to retry
|
2018-02-11 01:38:38 +00:00
|
|
|
self.error = "HTTP {}.".format(http_status)
|
2018-02-08 00:15:58 +00:00
|
|
|
self.success = False
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Check requests have 4 possible outcomes:
|
|
|
|
# 1. HTTP 200 with XML data - our desired info
|
|
|
|
# 2. HTTP 204 - means: no newer update available
|
|
|
|
# 3. HTTP 404 - means: invalid device or firmware version
|
|
|
|
# 4. anything else: server problem (esp. 500, 502, 503)
|