+def sendcmd(zpush: Any, wsmsg: Dict[str, Any]) -> Dict[str, Any]:
+ imei = wsmsg.pop("imei", None)
+ cmd = wsmsg.pop("type", None)
+ if imei is None or cmd is None:
+ log.info("Unhandled message %s %s %s", cmd, imei, wsmsg)
+ return {
+ "type": "cmdresult",
+ "imei": imei,
+ "result": "Did not get imei or cmd",
+ }
+ pmod = fetchpmod(imei)
+ if pmod is None:
+ log.info("Uknown type of recipient for %s %s %s", cmd, imei, wsmsg)
+ return {
+ "type": "cmdresult",
+ "imei": imei,
+ "result": "Type of the terminal is unknown",
+ }
+ tmsg = common.make_response(pmod, cmd, imei, **wsmsg)
+ if tmsg is None:
+ log.info("Could not make packet for %s %s %s", cmd, imei, wsmsg)
+ return {
+ "type": "cmdresult",
+ "imei": imei,
+ "result": f"{cmd} unimplemented for terminal protocol {pmod}",
+ }
+ resp = Resp(imei=imei, when=time(), packet=tmsg.packed)
+ log.debug("Response: %s", resp)
+ zpush.send(resp.packed)
+ return {
+ "type": "cmdresult",
+ "imei": imei,
+ "result": f"{cmd} sent to {imei}",
+ }
+
+