class DeviceService(SNPService): def __init__(self, config): SNPService.__init__(self) self.config = config self.connecting_servers = {} self.cert_manager = CertManager("keys", "device_servers", self.config.name) def startService(self): def callb(res): for dev in res: fact = SNProtocolClientFactory(self) self.cert_manager.connect_to_server(dev[1], dev[2], fact) DB.get_device_servers().addCallback(callb) def type_fch(self, request, reqid, protocol): ip = protocol.transport.getPeer().host def callb(res): protocol.sendResponse(request, reqid) self.parent.field_updated(res, request["Field"]) DB.get_local_devid_from_remote(ip, request["DevId"]).addCallback(callb) def type_aur(self, request, reqid, protocol): # TODO: request to control server for device server pass pass def type_wel(self, request, reqid, protocol): def callb(res, ip): protocol.sendResponse({"Name": self.config.name}, reqid) if ip in self.connecting_servers: self.connecting_servers[ip].callback(res) ip = protocol.transport.getPeer().host port = protocol.transport.getPeer().port self.peers[ip] = protocol DB.update_devices(ip, port, request["Name"], request["Devices"]).addCallback(callb, ip) def get_device_fields(self, device_server, devid): return self.peers[device_server].sendRequest({"Type": "GDF", "DevId": devid}) def update_device_field(self, device_server, devid, field, value): return self.peers[device_server].sendRequest({"Type": "UDF", "DevId": devid, "Field": field, "Value": value}) def add_server(self, ip, port, pin): fact = SNProtocolClientFactory(self) self.cert_manager.connect_to_server(ip, port, fact) d = defer.Deferred() self.connecting_servers[ip] = d return d def clientConnectionFailed(self, connector, reason): ip = connector.getDestination().host if ip in self.connecting_servers: self.connecting_servers[ip].errback(Exception(ip))
class DeviceService(SNPService): def __init__(self, config): SNPService.__init__(self) self.config = config self.connecting_servers = {} self.cert_manager = CertManager("keys", "device_servers", self.config.name) def startService(self): def callb(res): for dev in res: fact = SNProtocolClientFactory(self) self.cert_manager.connect_to_server(dev[1], dev[2], fact) DB.get_device_servers().addCallback(callb) def type_fch(self, request, reqid, protocol): ip = protocol.transport.getPeer().host def callb(res): protocol.sendResponse(request, reqid) self.parent.field_updated(res, request["Field"]) DB.get_local_devid_from_remote(ip, request["DevId"]).addCallback(callb) def type_aur(self, request, reqid, protocol): # TODO: request to control server for device server pass pass def type_wel(self, request, reqid, protocol): def callb(res, ip): protocol.sendResponse({"Name": self.config.name}, reqid) if ip in self.connecting_servers: self.connecting_servers[ip].callback(res) ip = protocol.transport.getPeer().host port = protocol.transport.getPeer().port self.peers[ip] = protocol DB.update_devices(ip, port, request["Name"], request["Devices"]).addCallback(callb, ip) def get_device_fields(self, device_server, devid): return self.peers[device_server].sendRequest({ "Type": "GDF", "DevId": devid }) def update_device_field(self, device_server, devid, field, value): return self.peers[device_server].sendRequest({ "Type": "UDF", "DevId": devid, "Field": field, "Value": value }) def add_server(self, ip, port, pin): fact = SNProtocolClientFactory(self) self.cert_manager.connect_to_server(ip, port, fact) d = defer.Deferred() self.connecting_servers[ip] = d return d def clientConnectionFailed(self, connector, reason): ip = connector.getDestination().host if ip in self.connecting_servers: self.connecting_servers[ip].errback(Exception(ip))
class MainService(SNPService): def __init__(self): SNPService.__init__(self) self.config = Config.getconf() self.name = self.config.name self.port = int(self.config.server.Port) self.ip = self.config.server.IP self.tg = TelegramService(self.config.token) self.cert_manager = CertManager("keys", "skynet", self.name) def type_wel(self, request, reqid, protocol): protocol.sendResponse({"Type": "WEL", "Name": self.name, "Methods": []}, reqid) def type_cmt(self, request, reqid, protocol): pass def startService(self): from snp import create_self_signed_cert create_self_signed_cert("keys", self.config.name) fact = SNProtocolClientFactory(self) self.cert_manager.connect_to_server(self.ip, self.port, fact) self.tg.parent = self self.tg.startService() def connectionMade(self, protocol): ip = protocol.transport.getPeer().host self.peers[ip] = protocol def get_devices(self): def callb(res): ret = {} devices = res["Devices"] for device in devices: ret[int(device["ID"])] = device["Name"] return ret d = list(self.peers.values())[0].sendRequest({"Type": "GDL"}) d.addCallback(callb) return d def get_device_info(self, id): def callb(res): return res["Device"] d = list(self.peers.values())[0].sendRequest({"Type": "GDF", "DevId": id}) d.addCallback(callb) return d def get_scripts(self, password): def callb(res): scripts = {int(script["Id"]): Script.create_from_dict(script)for script in res["Scripts"]} return scripts d = list(self.peers.values())[0].sendRequest({"Type": "GSC", "Password": password}) d.addCallback(callb) return d def remove_script(self, id, password): d = list(self.peers.values())[0].sendRequest({"Type": "DSC", "ScriptId": id, "Password": password}) return d def update_field(self, dev_id, field, value): d = list(self.peers.values())[0].sendRequest({"Type": "UDF", "DevId": dev_id, "Field": field, "Value": value}) return d def create_script(self, script, password): d = list(self.peers.values())[0].sendRequest({"Type": "CSC", "Script": script.to_dict(), "Password": password}) return d def get_servers(self, password): def callb(res): return res["Servers"] d = list(self.peers.values())[0].sendRequest({"Type": "GSD", "Password": password}) d.addCallback(callb) return d def add_server(self, ip, port, pin, password): def callb(res): return res["Server"] d = list(self.peers.values())[0].sendRequest({"Type": "RSD", "Password": password, "IP": ip, "Port": port, "Pin": pin}) d.addCallback(callb) return d