def test_add_dhcp_host(self): """ Test add_dhcp_host action """ self.valid_data['networks'] = [{ 'name': 'network', 'dhcpserver': { 'hosts': [{ 'macaddress': 'address1' }] } }] gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'start', 'ok') gw.add_dhcp_host('network', {'macaddress': 'address2'}) assert gw.data['networks'] == [{ 'name': 'network', 'dhcpserver': { 'hosts': [{ 'macaddress': 'address1' }, { 'macaddress': 'address2' }] } }] gw._gateway_sal.configure_dhcp.assert_called_once_with() gw._gateway_sal.configure_cloudinit.assert_called_once_with()
def test_remove_network_name_exist(self): """ Test remove_network action if network with name doesnt exist """ gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'start', 'ok') gw.remove_network('network')
def test_remove_portforward_doesnt_exist(self): """ Test remove_portforward action if the portforward doesn't exist """ gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'start', 'ok') gw.remove_portforward('pf')
def test_fetch_data(self): input = {"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"base":"cmc stations","main":{"temp":279.68,"pressure":998,"humidity":87,"temp_min":278.15,"temp_max":281.15},"wind":{"speed":3.1,"deg":200},"rain":{"1h":0.25},"clouds":{"all":75},"dt":1452282912,"sys":{"type":1,"id":5091,"message":0.0111,"country":"GB","sunrise":1452240240,"sunset":1452269460},"id":2643743,"name":"London","cod":200} expected_output = {'name': 'London', 'weather_type': 'light rain', 'temp': 279.68} gateway = Gateway() output = gateway.fetch_data(input) self.assertEqual(expected_output, output)
def test_remove_dhcp_host_exception(self): """ Test remove_dhcp_host action raises exception """ with pytest.raises( RuntimeError, message= 'action should raise an error if configure_dhcp raises an exception' ): networks = [{ 'name': 'network', 'dhcpserver': { 'hosts': [{ 'macaddress': 'address1' }] } }] self.valid_data['networks'] = networks gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'start', 'ok') gw._gateway_sal.configure_dhcp.side_effect = RuntimeError gw.remove_dhcp_host('network', {'macaddress': 'address1'}) assert gw.data['networks'] == networks assert gw._gateway_sal.configure_dhcp.call_count == 2 assert gw._gateway_sal.configure_cloudinit.call_count == 2
def test_add_http_proxy_host_exists(self): """ Test add_http_proxy action if another proxy has the same host """ with pytest.raises( ValueError, message= 'action should raise an error if another http proxy with the same host exist' ): proxy = { 'host': 'host', 'destinations': ['destination'], 'types': ['http'], 'name': 'proxy' } proxy2 = { 'host': 'host', 'destinations': ['destination'], 'types': ['http'], 'name': 'proxy2' } self.valid_data['httpproxies'].append(proxy) gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'start', 'ok') gw.add_http_proxy(proxy2)
def __getDatasetsForReplica(): from gateway import Gateway gateway = 'cmip-gw.badc.rl.ac.uk' log.debug('Contacting remote Gateway: %s', gateway) g = Gateway('http://' + gateway ) return [ds['id'] for ds in g.listDatasets('cmip5') if ds['id'].startswith('cmip5.output1')]
def test_remove_http_proxy_doesnt_exist(self): """ Test remove_http_proxy action if proxy doesn't exist """ gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'start', 'ok') gw.remove_http_proxy('proxy')
def test_add_portforward_combination_exists_same_protocols(self): """ Test add_portforward action when another portforward with the same srcnetwork and srcport exists and have the same protocols """ with pytest.raises( ValueError, message= 'action should raise an error if another portforward with the same name exist' ): self.valid_data['portforwards'] = [{ 'name': 'pf2', 'dstip': '196.23.12.42', 'dstport': 22, 'srcnetwork': 'network', 'srcport': 22, 'protocols': ['tcp'] }] self.valid_data['networks'] = [{'name': 'network'}] gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'start', 'ok') portforward = { 'name': 'pf', 'dstip': '196.23.12.42', 'dstport': 22, 'srcnetwork': 'network', 'srcport': 22, 'protocols': ['tcp'] } gw.add_portforward(portforward)
def add_gateway_by_IMEI(): IMEI = request.args.get('IMEI') latest_info_file = open("./latest_info", "r") # 101,5001,1 info = latest_info_file.readline().strip().split(",") latest_info_file.close() ip = str(int(info[0]) + 1) vpn_ip = "172.16.0." + ip vpn_port = str(int(info[1]) + 1) server_ip = "smrtyan.cn" server_port = vpn_port num = str(int(info[2]) + 1) culiu = "culiu" + num gateway = Gateway(IMEI, vpn_ip, vpn_port, server_ip, server_port, culiu) # "867698045127654", "172.16.0.102", 5002, "smrtyan.cn", 5002, "culiu2" gateway.add() latest_info_file = open("./latest_info", "w") latest_info_file.write(ip + "," + vpn_port + "," + num) latest_info_file.close() # return the ./script/client_config.py file try: return send_file('./script/client_config.py', attachment_filename='client_config.py') except Exception as e: return str(e)
def test_add_portforward_combination_exists_different_protocols(self): """ Test add_portforward action when another portforward with the same srcnetwork and srcport exists and have different protocols """ portforward_one = { 'name': 'pf2', 'dstip': '196.23.12.42', 'dstport': 22, 'srcnetwork': 'network', 'srcport': 22, 'protocols': ['udp'] } self.valid_data['portforwards'] = [portforward_one] self.valid_data['networks'] = [{'name': 'network'}] gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'start', 'ok') portforward_two = { 'name': 'pf', 'dstip': '196.23.12.42', 'dstport': 22, 'srcnetwork': 'network', 'srcport': 22, 'protocols': ['tcp'] } gw.add_portforward(portforward_two) assert gw.data['portforwards'] == [portforward_one, portforward_two]
def test_add_dhcp_host_exception(self): """ Test add_dhcp_host action raises exception """ with pytest.raises( ValueError, message= 'action should raise an error if configure_dhcp raises an exception' ): self.valid_data['networks'] = [{ 'name': 'network', 'dhcpserver': { 'hosts': [{ 'macaddress': 'address1', 'hostname': 'one' }] } }] gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'start', 'ok') gw.add_dhcp_host('network', { 'macaddress': 'address2', 'hostname': 'one' }) assert gw.data['networks'] == [] assert gw._gateway_sal.configure_dhcp.call_count == 2 assert gw._gateway_sal.configure_cloudinit.call_count == 2
def test(): log.debug('Testing replica_db') from gateway import Gateway g = Gateway('http://albedo2.dkrz.de/esgcet') files = g.listFiles('cmip5.output1.MPI-M.MPI-ESM-LR.historical.mon.seaIce.OImon.r1i1p1') import re db = ReplicaDB('sqlite:///:memory:') #db = ReplicaDB('sqlite:///replica.db') print "File metadata:" print files[0] inserted = [] rejected = [] files_o = db.add_all(files, inserted=inserted, rejected=rejected, update=True) #, overwrite=True) log.debug('Ins: %s, rej: %s', len(inserted), len(rejected) ) print "File" print files_o[0] print "Query something..." f_dao = None for row in db.get(): if not f_dao: f_dao = row print row.path[:20], "...", row.name, len(row.endpoints), [ ep.name for ep in row.endpoints] db.close() print f_dao.getDRS().getId(), f_dao.getDRS().getDirectory(), f_dao.getDRS().getFilename() print f_dao.getMapFileEntry('/My_root_dir/') return locals()
class Gis(object): PROPS = [ { 'id':'1', 'name':u'服务器地址', 'field':'address'}, { 'id':'2', 'name': u'端口', 'field':'port'}, { 'id':'3','name': u'通信模式', 'field':'commtype'}, { 'id':'4','name': u'启用', 'field':'enabled' }, { 'id':'16383', 'name': u'驱动程序名', 'field':'driver' } ] def __init__(self): self.gw = Gateway() self.info = self.gw.get_config_root() self.gw_node = self.gw.get_node() def list(self): gis = etree.Element("GIS") module = self.gw_node.find('MODULE[@id="127"]') if module != None: tags = module.findall("TAG") for tag in tags: item = etree.SubElement('Item') for prop in self.PROPS: item.set(prop['field'],tag.find('PROP[id=' + prop['id'] + ']').text) return gis def write(self): self.gw.write(self.info)
def test_stop(self): """ Test uninstall action """ gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'start', 'ok') gw.stop() gw._gateway_sal.stop.called_once_with()
def subscribe(self, req:OrderRequest ): """ Subscribe tick data update of a specific gateway. """ gateway = Gateway(self.event_engine,req.symbol, req.exchange) gateway.generate_Tick()
def test_start(self): """ Test start action """ gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'install', 'ok') gw.install = MagicMock() gw.start() gw.install.assert_called_once_with()
def result(): if request.method == 'POST': lat = request.form['Latitude'] lon = request.form['Longitude'] url = 'http://api.openweathermap.org/data/2.5/weather?lat=' + str(lat) + '&lon=' + str(lon) + '&appid=ce0c5d404829241dc9b2ddbd3cc25a73' # TODO: fix this asap, can`t be like that, object will be created with every request gate = Gateway() data = gate.get_data(url) return render_template('layout.html',data = data)
def test_start_not_installed(self): """ Test start action, gateway isnt installed """ with pytest.raises( StateCheckError, message= 'actions should raise an error if gateway isn\'t installed'): gw = Gateway('gw', data=self.valid_data) gw.start()
def build_cross(): object_lst = pickle.load(open('./maps/cross00_object.p', 'rb')) # randomize intensity decay of objects for obj in object_lst: obj.decay = np.random.uniform() rect_idx_lst = pickle.load(open('./maps/cross00_rectidx.p', 'rb')) gate_lst = [Gateway(np.float32((10,10)), tx_power=100, ff_attenuation=1e-2), Gateway(np.float32((77,60)), tx_power=100, ff_attenuation=1e-2), Gateway(np.float32((74,7)), tx_power=100, ff_attenuation=1e-2)] return object_lst, rect_idx_lst, gate_lst
def test_add_network(self): """ Test add_network action """ network = {'name': 'network', 'type': 'default', 'id': 'id'} gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'start', 'ok') gw.add_network(network) assert gw.data['networks'] == [network] gw._gateway_sal.deploy.assert_called_once_with()
def test_remove_network_before_start(self): """ Test remove_network action if gateway isn't started """ with pytest.raises( StateCheckError, message= 'action should raise an error if another gateway isnt started' ): gw = Gateway('gw', data=self.valid_data) gw.remove_network('network')
def test_add_network_before_start(self): """ Test add_network action if gateway isn't started """ with pytest.raises( StateCheckError, message= 'action should raise an error if another network with the same type and id combination exists' ): network = {'name': 'network', 'type': 'default', 'id': 'id'} gw = Gateway('gw', data=self.valid_data) gw.add_network(network)
def test_install(self): """ Test install action """ gw = Gateway('gw', data=self.valid_data) gw_sal = MagicMock(zt_identity='zt_identity') gw._node_sal.primitives.from_dict.return_value = gw_sal gw.install() gw_sal.deploy.assert_called_once_with() assert gw.data['ztIdentity'] == 'zt_identity' gw.state.check('actions', 'install', 'ok') gw.state.check('actions', 'start', 'ok')
def test_remove_dhcp_host_name_doesnt_exists(self): """ Test remove_dhcp_host action if network with name doesnt exist """ with pytest.raises( LookupError, message= 'action should raise an error if network with this name doesnt exist' ): gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'start', 'ok') gw.remove_dhcp_host('network', {'macaddress': 'address2'})
def __getGatewayFiles(dataset): from gateway import Gateway gateway = 'cmip-gw.badc.rl.ac.uk' log.debug('Contacting remote Gateway: %s', gateway) g = Gateway('http://' + gateway) log.debug('getting files from %s', dataset) files = g.listFiles(dataset) if not files: raise Exception("No file found! Probably the test is outdated....") return files
def _mock_service(self, data, mock_capacity=True): self.service = Gateway(name='service', data=data) self.robotapi = MagicMock() self.vmservice = MagicMock() self.service.api.services.get = MagicMock(return_value=self.vmservice) self.publibcrobotapi = MagicMock() self.gateway = MagicMock() self.public_gateway = MagicMock() self.robotapi.services.get.return_value = self.gateway self.robotapi.services.find_or_create.return_value = self.gateway self.publibcrobotapi.services.get.return_value = self.public_gateway self.publibcrobotapi.services.find_or_create.return_value = self.public_gateway kwargs = {NODEID: self.robotapi} for url in PUBLIC_GW_ROBOTS: key = urlparse(url).netloc kwargs[key] = self.publibcrobotapi j.clients.zrobot.robots = kwargs capacity = MagicMock() self.mocknode = MagicMock(robot_address='url') capacity.api.GetCapacity.return_value = (self.mocknode, None) j.clients.grid_capacity.get.return_value = capacity # public gateay info self.public_gateway_info = { 'httpproxies': [], 'zerotierId': 'abcdef1234567890' } self.public_gateway.schedule_action.return_value.wait.return_value.result = self.public_gateway_info # gateway network self.gateway_info = { 'networks': [{ 'public': True, 'config': { 'cidr': '172.18.0.1/16' } }], 'portforwards': [] } self.gateway.schedule_action.return_value.wait.return_value.result = self.gateway_info # vm info self.vmservice.schedule_action.return_value.wait.return_value.result = { 'zerotier': { 'id': PRIVATEZT, 'ztClient': 'main', }, 'ztIdentity': 'abcdef:423423' }
def test_remove_http_proxy(self): """ Test remove_http_proxy action """ self.valid_data['httpproxies'] = [{ 'host': 'host', 'destinations': ['destination'], 'types': ['http'], 'name': 'proxy' }] gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'start', 'ok') gw.remove_http_proxy('proxy') assert gw.data['httpproxies'] == [] gw._gateway_sal.configure_http.assert_called_once_with()
def test_add_network_name_exist_same_combination(self): """ Test add_network action if network with the same type and id combination exists """ with pytest.raises( ValueError, message= 'action should raise an error if another network with the same type and id combination exists' ): network = {'name': 'network', 'type': 'default', 'id': 'id'} network_two = {'name': 'network2', 'type': 'default', 'id': 'id'} self.valid_data['networks'].append(network) gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'start', 'ok') gw.add_network(network_two)
def test_add_network_exception(self): """ Test add_network action raises exception """ with pytest.raises( RuntimeError, message= 'actions should raise an error deploy raises an exception'): network = {'name': 'network', 'type': 'default', 'id': 'id'} gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'start', 'ok') gw._gateway_sal.deploy.side_effect = RuntimeError gw.add_network(network) assert gw.data['networks'] == [] assert gw._gateway_sal.deploy.call_count == 2
def __init__(self): self.gw = Gateway() self.info = self.gw.get_config_root() self.gw_node = self.gw.get_node() self.gw_info = self.gw.get_info() self.extra = self.gw.extra self.rewrite = False
def UI_init(): global GW app.btn_rl1_bat.clicked.connect(ctr_R1_bat) app.btn_rl1_tat.clicked.connect(ctr_R1_tat) app.btn_rl2_bat.clicked.connect(ctr_R2_bat) app.btn_rl2_tat.clicked.connect(ctr_R2_tat) ports = serial.tools.list_ports.comports() check_device = '' if (len(ports) > 0): for port in ports: # print(port) if ("USB-SERIAL CH340" in str(port)): check_device = port.device break if (check_device != ''): GW = Gateway(CONSTANT.GW_NAME) # QMessageBox.information( # app, "KẾT NỐI THÀNH CÔNG", "CHÀO MỪNG BẠN ĐẾN VỚI VƯỜN CỦA NHUNG ĐẠI KA") app.lbl_com.setText("ĐÃ KẾT NỐI " + check_device) # GW.control_RL(1, 0) # GW.control_RL(2, 0) else: QMessageBox.critical(app, "LỖI KẾT NỐI", "KHÔNG ĐÚNG THIẾT BỊ") sys.exit() else: QMessageBox.critical(app, "LỖI KẾT NỐI", "KHÔNG CÓ COM NÀO ĐƯỢC KẾT NỐI") sys.exit()
def process(self): # Check to see if schedule file exists. If it does, change state to idle if Gateway.pingGateway(): self.fsm.setState(IdleState(self.fsm)) logging.info("Gateway ping succeeded") super().process()
def update_gateway_with_prefixlen(self, ipv4='', ipv4_prefixlen=0, ipv6='', ipv6_prefixlen=0, port_no=''): """ update the gateway information for Port object. """ port = self.ports[port_no] if port.gateway is None: port.gateway = Gateway(name=port.name, port_no=port.port_no, ipv4=ipv4, ipv4_prefixlen=ipv4_prefixlen, ipv6=ipv6, ipv6_prefixlen=ipv6_prefixlen) else: port.gateway.name = port.name port.gateway.ipv4 = netaddr.IPAddress(ipv4) port.gateway.ipv4_subnet = netaddr.IPNetwork(ipv4 + '/' + str(ipv4_prefixlen)) port.gateway.ipv6 = netaddr.IPAddress(ipv6) port.gateway.ipv6_subnet = netaddr.IPNetwork(ipv6 + '/' + str(ipv6_prefixlen)) port.gateway.port_no = port.port_no self.tbl.update_entry(subnet=port.gateway.ipv4_subnet, receive_port=port, metric=0, source="CONNECTED")
def __init__(self, itf, addr_list): #self._exo = exo self._itf = itf self._addr_list = addr_list # Create gateway object gatewayObject = Gateway()
def test_remove_portforward(self): """ Test remove_portforward action """ self.valid_data['portforwards'] = [{ 'name': 'pf', 'dstip': '196.23.12.42', 'dstport': 21, 'srcnetwork': 'network', 'srcport': 21, 'protocols': ['tcp'] }] gw = Gateway('gw', data=self.valid_data) gw.state.set('actions', 'start', 'ok') gw.remove_portforward('pf') assert gw.data['portforwards'] == []
def test_add_http_proxy_before_start(self): """ Test add_http_proxy action before gateway start """ with pytest.raises( StateCheckError, message= 'actions should raise an error if gateway isn\'t started'): gw = Gateway('gw', data=self.valid_data) proxy = { 'host': 'host', 'destinations': ['destination'], 'types': ['http'], 'name': 'proxy' } gw.add_http_proxy(proxy)
def on_login(self, p, data): [_, cmd, ver1, ver2, game_id, channel_id, session_key] = data if not game_id or not session_key: return self.request_fail(cmd, ERR_DATA_BROKEN) if ver1 != VERSION_MAIN: return self.request_fail(cmd, ERR_VERSION_ERR) if game_id != CONFIG.game_id: return self.request_fail(cmd, ERR_SYSTEM_ERR) info = online_model.get_by_key(session_key) if not info or not info.session_key: return self.request_fail(cmd, ERR_DATA_BROKEN) ugame = user_model.get(info.uid) if not ugame: return self.request_fail(cmd, ERR_SYSTEM_ERR) from gateway import Gateway gw = Gateway.share() old_player = gw.get_player(info.uid) if old_player: # 玩家已在线,直接将旧的socket清掉,并更新游戏数据,注意此时不返回任何数据,直接通过socket发送 p.stop() old_player.send([SERVICE_LOGIN, CMD_LOGIN, ERR_RE_LOGIN]) old_player.close() old_player.stop() old_player.set_socket(p.sock, p.address) old_player.start() old_player.set_ugame(ugame) old_player.send(self.request_ok(cmd, self.make_user_structure(old_player))) else: p.set_ugame(ugame) gw.save_player(p) return self.request_ok(cmd, self.make_user_structure(p))
def __init__(self, transport, subBroker=None): super().__init__(subBroker) self.endpointID = SerialID.integerToBytes(transport.portalNegotiation()) self.messageCount = -1 self.transport = transport self.cachedTransverse = {} self.responseWaitingQueue = {} self.defaultGateway = Gateway(self) self.listenTask = self.defaultGateway.activateListener()
def __init__(self, transport, messageNumberBuffer = 200): self.endpointID = transport.portalNegotiation() self.sharedCount = -1 self.messageCount = -1 self.transport = transport self.objectToSharedID = {} self.sharedIDToObject = {} self.exposedTransverse = {} self.cachedTransverse = {} self.responseWaitingQueue = {} self.defaultGateway = Gateway(self) self.listenTask = self.defaultGateway.activateListener()
def main(): utils.log('started with pid %d' % (os.getpid()), log_file) model_base.keep_redis_connect() gw = Gateway.share() gw.set_ports(CONFIG.ports) # 设置端口 gw.set_game_id(CONFIG.game_id) # 服务类型 gw.insert_service(ServiceGame.share()) # 插入游戏服务 gw.insert_service(ServiceLogin.share()) # 插入登陆相关的服务 gw.insert_service(ServiceSystem.share()) # 插入系统相关的服务 gw.insert_service(ServiceControl.share()) # 插入控制服务 gw.start_service() # 启动服务
class Router: """ Route is an object that defines a point-to-point connection between two end-points. It is responsible for handling: 1. Storage of incoming messages to the appropriate handlers. 2. Memory of outgoing messages so that the responses can be routed to the calling function. 3. Exposure of transverse objects by mapping to an object ID. 4. Assignation of object IDs to shared local objects. """ def __init__(self, transport, messageNumberBuffer = 200): self.endpointID = transport.portalNegotiation() self.sharedCount = -1 self.messageCount = -1 self.transport = transport self.objectToSharedID = {} self.sharedIDToObject = {} self.exposedTransverse = {} self.cachedTransverse = {} self.responseWaitingQueue = {} self.defaultGateway = Gateway(self) self.listenTask = self.defaultGateway.activateListener() @asyncio.coroutine def close(self): """ Closes the route and hangs up on the transport to let the other end know """ yield from self.transport.hangUp() yield from self.listenTask def generateMessageID(self): """ Assigns a message number to an outgoing message based upon an active window method. This is used to keep the numbers as low as possible to minimize transmission length. TODO: This doesn't actually work but the code is currently harmless """ self.messageCount += 1 return SerialID.integersToBytes(self.messageCount) def resolveMessageID(self, messageID): """ Checks whether the message ID exists in the current map, to see if a message has indeed been sent to the remote end. If it has then update the messageWindowLow if appropriate and return the response details. """ # Ensure that the message is a response to a message from this gateway if not messageID in self.responseWaitingQueue: # send a general error to the other-side if this message was unknown raise(UnknownMessageIDError(messageID)) # Return the resolution callback and the exception callback return self.responseWaitingQueue.pop(messageID) def waitForReply(self, messageID, decoder): """ Sets up a future which will be activated when the response to the outgoing message comes in. """ waiter = asyncio.Future() def resolve(byteStream): # Handle the argument marshalling response = decoder(byteStream) waiter.set_result(response) self.responseWaitingQueue[messageID] = resolve, waiter.set_exception return waiter def generateShareID(self): """ Generates an object ID in the form of a byte-string. Object IDs consist of the gateway tag plus a unique code for the object itself. This allows objects to be mapped to a particular end-point of the gateway. """ self.sharedCount += 1 return SerialID.integersToBytes(self.endpointID, self.sharedCount) def referenceObject(self, obj): """ Marks an object as a shared object and stores it in the shared object list for retrieval through an object reference. """ if isinstance(obj, ProxyObject): return obj.__shared_id__ if obj in self.objectToSharedID: return self.objectToSharedID[obj] shareID = self.generateShareID() self.sharedIDToObject[shareID] = obj self.objectToSharedID[obj] = shareID return shareID def dereferenceObject(self, arg, typ, gateway = None): """ Converts an incoming object reference into a python object. Local objects will be mapped to their local python object whereas remote objects will be wrapped in an object proxy. """ if arg in self.sharedIDToObject: arg = self.sharedIDToObject[arg] if not issubclass(type(arg), typ): raise(TypeError("%s was not of type %s as specified in the interface"%(arg, typ))) elif gateway is not None: arg = typ.getProxyClass()(gateway, arg) else: raise(UnknownObjectIDError(arg)) return arg def cacheTransverse(self, transverseID, objectID): """ caches a resolved transverse ID / object ID pair so that future resolutions do not have to go down the wire. """ self.cachedTransverse[transverseID] = objectID def getCachedTransverse(self, transverseID): """ tries to retrieve an object ID from the previously cached transverse ID. If that object had not been cached then raises a KeyError """ return self.cachedTransverse[transverseID] def resolveTransverseID(self, transverseID): """ Resolves a transverse object identifier into the objectID of the associated local object, if it has been exposed through this router). """ if not transverseID in self.exposedTransverse: # This gateway does not know about this transverse identifier raise (UnknownTransverseIDError(transverseID)) else: # return the exposed object associated with the transverse identifier return self.exposedTransverse[transverseID] def exposeCallImplementation(self, iface, func): """ Exposes a function call func that conforms to the interface iface """ uuid = iface.__transverse_id__ self.exposedTransverse[uuid] = self.referenceObject(ExposedCall(func, iface)) def exposeObjectImplementation(self, iface, cls): """ Exposes a python object that has been marked as an implementation of an object to the other side(s) of the transport. If the interface is marked as non-constructable, then no constructor method will be made available. """ for name, member in iface.__iface_members__.items(): # If the object has a constructor then expose the object class itself if name == "__constructor__": call = cls else: call = getattr(cls,name) self.exposeCallImplementation(member, call) def getBuffer(self): """ returns a buffer to write an outgoing packet into. """ return self.transport.startWrite() @asyncio.coroutine def flushBuffer(self, buf): """ Commits the write of a send buffer to the wire. """ yield from self.transport.commitWrite(buf) @asyncio.coroutine def poleTransport(self, recvHook, errHook): """ Continuously pole the transport whilst the gateway remains open this poling will be done in asychronous blocking mode so that it will not 'spin' whilst no incoming data is present. """ self.isOpen = True def doneCallback(fut): try: fut.result() except TransportClosed: self.isOpen = False self.transport.skip() except Exception as e: asyncio.get_event_loop().call_soon(errHook(e)) while self.isOpen: receivedStream = io.BytesIO((yield from self.transport.recv())) coro = asyncio.async(recvHook(receivedStream)) coro.add_done_callback(doneCallback)
def __init__(self, label): Gateway.__init__(self, 'Dell', 'Edge-5000', get_linux_version(), systemUUID().get_uuid(label), label, None, None, None, "HelixGateway")
def home(): url = 'http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=ce0c5d404829241dc9b2ddbd3cc25a73' # TODO: fix this asap, can`t be like that, object will be created with every request gate = Gateway() data = gate.get_data(url) return render_template('layout.html',data = data)
class Encoder(object): GW = Gateway() OBJECT_MAX = 65535 PROPS = [ { 'id':'1', 'name':u'名称', 'field':'name','unique':True, 'validate':'.+','long_name':u'编码器名称' }, { 'id':'2', 'name': u'设备地址', 'field':'address', 'unique':True, 'validate':'^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$'}, { 'id':'3','name': u'服务端口', 'field':'service_port', 'validate':'^\d+$', 'min':1,'max':65535 }, { 'id':'4','name': u'设备名称', 'field':'device_name' }, { 'id':'5','name': u'生产厂家', 'field':'vendor' }, { 'id':'6','name': u'设备类型', 'field':'device_type' }, { 'id':'7','name': u'GUID', 'field':'guid' }, { 'id':'8','name': u'PTZProtocal', 'field':'ptz_protocal' }, { 'id':'9','name': u'IO Addr', 'field':'io_addr', 'validate':'^\d+$', 'min':0, 'max':255, 'long_name':u'IO地址' }, { 'id':'10','name':u'音频端口', 'field':'audio_port', 'validate':'^\d+$', 'min':1, 'max':65535 }, { 'id':'11','name':u'连接模式', 'field':'connect_type' }, { 'id':'16383', 'name': u'驱动程序名', 'field':'driver' } ] VIDEO_PROPS = [ {'id':'1', 'name':u'名称', 'field':'name', 'default':'video0' }, { 'id':'2','name': u'编码类型', 'field':'encode_type' }, { 'id':'3','name': u'通道号', 'field':'channel' }, { 'id':'4','name': u'通讯模式', 'field':'com_method' }, { 'id':'5','name': u'源地址', 'field':'src_address', 'default':GW.get_info()['host'] }, { 'id':'6','name': u'源端口', 'field':'src_port' }, { 'id':'7','name': u'协议类型', 'field':'protocal_type' }, { 'id':'8','name': u'码率', 'field':'encode_rate' }, { 'id':'9','name': u'分辨率', 'field':'resolution' } ] AUDIO_PROPS = [ {'id':'1', 'name':u'名称', 'field':'name', 'default':'audio0' }, { 'id':'2','name': u'编码类型', 'field':'encode_type' }, { 'id':'3','name': u'通道号', 'field':'channel' }, { 'id':'4','name': u'通讯模式', 'field':'com_method' }, { 'id':'5','name': u'源地址', 'field':'src_address' }, { 'id':'6','name': u'源端口', 'field':'src_port' }, { 'id':'7','name': u'协议类型', 'field':'protocal_type' }, { 'id':'8','name': u'码率', 'field':'encode_rate' } ] SPOT_PROPS = [ {'id':'1', 'name':u'名称', 'field':'name' }, { 'id':'5','name': u'URL', 'field':'URL' }, ] def __init__(self): self.gw = Gateway() self.info = self.gw.get_config_root() self.gw_node = self.gw.get_node() self.gw_info = self.gw.get_info() self.extra = self.gw.extra self.rewrite = False def to_xml(self, encoder): encoder_id = encoder.get('id') en = etree.Element("Encoder", id=encoder_id) for prop in self.PROPS: value = encoder.xpath("PROP[@id='" + prop['id'] + "']")[0].text if value == None: value = '' en.set(prop['field'], value) en.set('device', str(get_device_index(en.get('device_name')))) return en def find_alias(self, id): nodes = self.extra.xpath('Stream[@id="' + id + '"]') if len(nodes) == 0: return None return nodes[0] def add_alias(self, id, alias): if alias.strip() == '': return None stream = etree.SubElement(self.extra.getroot(), 'Stream') stream.set('id', id) stream.set('status','0') stream.set('code', alias) return stream def update_alias(self, id, alias): if alias.strip() == '': self.remove_alias(id) return stream = self.find_alias(id) if stream == None: stream = self.add_alias(id, alias) else: stream.set('code', alias) def remove_alias(self, id): stream = self.find_alias(id) if stream != None: stream.getparent().remove(stream) def spot_to_xml(self, encoder, spot): id = spot.get('id') s = etree.Element('Spot') url = spot.xpath('PROP[@id="5"]')[0].text s.set('id', id) s.set('name', spot.xpath('PROP[@id="1"]')[0].text) s.set('global_id',re.search('(?<=sip:).*?(?=@)',url).group()) relay = spot.xpath('PROP[@id="6"]') if len(relay) == 0: relay = etree.SubElement(spot, "PROP", id="6", name=u"转发") relay.text = "false" self.rewrite = True else: relay = relay[0] s.set('relay', relay.text) alias = self.find_alias(id) if alias != None: s.set('alias', alias.get('code')) vnode = spot.xpath('PROP[@id="2"]')[0] g = re.compile('\d+') streamId = g.findall(vnode.text)[1] video = encoder.xpath("MODULE[@id='125']")[0].xpath('TAG[@id="' + streamId + '"]')[0] audio = encoder.xpath("MODULE[@id='126']")[0].xpath('TAG[@id="' + streamId + '"]')[0] for vp in self.VIDEO_PROPS: value = video.xpath("PROP[@id='" + vp['id'] + "']")[0].text if value == None: value = '' s.set('v_' + vp['field'],value) for ap in self.AUDIO_PROPS: value = audio.xpath("PROP[@id='" + ap['id'] + "']")[0].text if value == None: value = '' s.set('a_' + ap['field'],value) return s def list(self): capacity = "512"; if os.path.isfile('/etc/capacity'): file = open('/etc/capacity', 'r') line = file.readline() file.close() capacity = line.strip() spots = self.gw_node.xpath("MODULE[@id='1']")[0] encoders = etree.Element("Encoders", name=u'编码器', capacity=capacity) for encoder in self.info.xpath("OBJECT[@id!='0']"): encoder_id = encoder.get('id') en = self.to_xml(encoder) encoders.append(en) for vnode in spots.xpath('TAG/PROP[contains(text(),"/DEVICE' + encoder_id + '/VIDEO")]'): spot = vnode.getparent() en.append(self.spot_to_xml(encoder, spot)) if self.rewrite: self.write() return encoders def generate_seq(self, node, path): for i in range(1, self.OBJECT_MAX): if len(node.xpath(path + "[@id='" + str(i) + "']")) == 0: return str(i) def generate_url(self, id): return 'sip:' + id.strip() + '@' + self.gw_info['host'] + ':' + self.gw_info['port'] def generate_global_id(self, seq): r_id = to_bin('0', 7) gw_id = to_bin(self.gw_info['id'],7) type_id = to_bin('13', 4) spots = self.gw_node.xpath("MODULE[@id='1']")[0] spot_id = to_bin(seq, 14) return str(int(r_id + gw_id + type_id + spot_id, 2)) def generate_import_global_id(self, encoderId, spotId): r_id = to_bin(encoderId, 7) mg_id = to_bin('125',7) type_id = to_bin('13', 4) spot_id = to_bin(spotId, 14) return str(int(r_id + mg_id + type_id + spot_id, 2)) def add(self, props): seq = self.generate_seq(self.info, "OBJECT") encoder = etree.Element('OBJECT',id=seq) for prop in self.PROPS: p = etree.SubElement(encoder, 'PROP', name=prop['name'], id=prop['id']) if props.has_key(prop['field']) and props[prop['field']].strip() != '': p.text = props[prop['field']].strip() self.info.getroot().append(encoder) self.write() return self.to_xml(encoder) def update(self, props): id = props['id'] encoder = self.find(id) for prop in self.PROPS: for p in encoder.findall("PROP"): if props.has_key(prop['field']) and p.get('id') == prop['id']: p.text = props[prop['field']].strip() self.write() def find(self, id): encoder = self.info.xpath("OBJECT[@id='" + id + "']")[0] return encoder def find_spot(self, id): return self.gw_node.xpath('MODULE[@id="1"]')[0].xpath('TAG[@id="' + id + '"]')[0] def get_spot_ids(self, spot): vnode = spot.xpath('PROP[@id="2"]')[0] g = re.compile('\d+') return g.findall(vnode.text) def write(self): self.gw.write(self.info) def write_extra(self): self.gw.write_extra() def update_spot(self, props): id = props['id'] encoderId = props['encoder_id'] spot = self.find_spot(id) ids = self.get_spot_ids(spot) vId = ids[1] aId = ids[1] spot_name = spot.xpath('PROP[@id="1"]')[0] spot_name.text = props['name'].strip() relay = spot.xpath('PROP[@id="6"]')[0] if props.has_key('relay'): relay.text = props['relay'].strip() else: relay.text = '0' encoder = self.find(encoderId) videoStream = encoder.xpath('MODULE[@id="125"]')[0].xpath('TAG[@id="' + vId + '"]')[0] audioStream = encoder.xpath('MODULE[@id="126"]')[0].xpath('TAG[@id="' + aId + '"]')[0] for prop in self.VIDEO_PROPS: for p in videoStream.findall('PROP'): if props.has_key('v_' + prop['field']) and p.get('id') == prop['id']: p.text = props['v_' + prop['field']].strip() for prop in self.AUDIO_PROPS: for p in audioStream.findall('PROP'): if props.has_key('a_' + prop['field']) and p.get('id') == prop['id']: p.text = props['a_' + prop['field']].strip() self.write() if props.has_key('alias'): self.update_alias(id, props['alias'].strip()) self.write_extra() def add_spot(self, props): encoderId = props['encoder_id'] encoder = self.find(encoderId) videos = encoder.xpath('MODULE[@id="125"]') audios = encoder.xpath('MODULE[@id="126"]') coms = encoder.xpath('MODULE[@id="127"]') if len(videos) == 0: videos = etree.SubElement(encoder,'MODULE', id='125') audios = etree.SubElement(encoder,'MODULE', id='126') #coms = etree.SubElement(encoder,'MODULE', id='127') else: videos = videos[0] audios = audios[0] if len(coms) == 0: coms = etree.SubElement(encoder,'MODULE', id='127') else: coms = coms[0] seq = self.generate_seq(videos, "TAG") videoStream = etree.SubElement(videos, 'TAG', type='13', id=seq) audioStream = etree.SubElement(audios, 'TAG', type='13', id=seq) com = etree.SubElement(coms, 'TAG', type='13', id=seq) for prop in self.VIDEO_PROPS: p = etree.SubElement(videoStream, 'PROP', name=prop['name'], id=prop['id']) if props.has_key('v_' + prop['field']): p.text = props['v_' + prop['field']] elif prop.has_key('default'): p.text = prop['default'] for prop in self.AUDIO_PROPS: p = etree.SubElement(audioStream, 'PROP', name=prop['name'], id=prop['id']) if props.has_key('a_' + prop['field']): p.text = props['a_' + prop['field']] elif prop.has_key('default'): p.text = prop['default'] module1 = self.gw_node.xpath('MODULE[@id="1"]')[0] id = self.generate_seq(module1,"TAG") spot = etree.SubElement(module1, "TAG", type="13") spot.set('id',id) p11 = etree.SubElement(spot, "PROP", name=u"名称", id="1") p12 = etree.SubElement(spot, "PROP", name=u"VideoIn", id="2") p13 = etree.SubElement(spot, "PROP", name=u"AudioIn", id="3") p14 = etree.SubElement(spot, "PROP", name=u"运行状态", id="4") p15 = etree.SubElement(spot, "PROP", name=u"URL", id="5") p16 = etree.SubElement(spot, "PROP", name=u"转发", id="6") p11.text = props['name'].strip() p12.text = "/DEVICE" + encoderId + "/VIDEO" + seq p13.text = "/DEVICE" + encoderId + "/AUDIO" + seq p14.text = "0" p15.text = self.generate_url(self.generate_global_id(id)) if props.has_key('relay'): p16.text = props['relay'].strip() else: p16.text = '0' module2 = self.gw_node.xpath('MODULE[@id="2"]')[0] com = etree.SubElement(module2, "TAG", type="13", id=self.generate_seq(module2,"TAG")) p21 = etree.SubElement(com, "PROP", name=u"名称", id="1") p22 = etree.SubElement(com, "PROP", name=u"COM输入端口", id="2") p23 = etree.SubElement(com, "PROP", name=u"状态", id="3") p21.text = 'COM' + seq p22.text = '/DEVICE' + encoderId + '/COM' + seq p23.text = '0' self.write() if props.has_key('alias'): self.add_alias(id, props['alias'].strip()) self.write_extra() return self.spot_to_xml(encoder, spot) def remove(self, id): encoder = self.find(id) spots = self.gw_node.xpath("MODULE[@id='1']")[0] for vnode in spots.xpath('TAG/PROP[contains(text(),"/DEVICE' + id + '/VIDEO")]'): spot = vnode.getparent() self.remove_alias(spot.get('id')) spots.remove(spot) coms = self.gw_node.xpath("MODULE[@id='2']")[0] for cnode in coms.xpath('TAG/PROP[contains(text(),"/DEVICE' + id + '/COM")]'): com = cnode.getparent() coms.remove(com) encoder.getparent().remove(encoder) self.write() self.write_extra() def remove_spot(self, id): spot = self.find_spot(id) ids = self.get_spot_ids(spot) com_module = self.gw_node.xpath('MODULE[@id="2"]')[0] com = com_module.xpath('TAG/PROP[text()="/DEVICE' + ids[0] + '/COM' + ids[1] + '"]') if len(com) == 1: com_module.remove(com[0].getparent()) encoder = self.find(ids[0]) vmodule = encoder.xpath('MODULE[@id="125"]')[0] amodule = encoder.xpath('MODULE[@id="126"]')[0] cmodule = encoder.xpath('MODULE[@id="127"]') vmodule.remove(vmodule.xpath('TAG[@id="' + ids[1] + '"]')[0]) amodule.remove(amodule.xpath('TAG[@id="' + ids[1] + '"]')[0]) if len(cmodule) > 0: cmodule = cmodule[0] tags = cmodule.xpath('TAG[@id="' + ids[1] + '"]') if len(tags) > 0: cmodule.remove(tags[0]) spot.getparent().remove(spot) self.write() self.remove_alias(id) self.write_extra() def import_spots(self, id, xml, ids): encoder = self.find(id) i_xml = etree.fromstring(xml) i_gw = i_xml.xpath("/LIBRARY/OBJECT[@id='0']")[0] i_module = i_gw.xpath("MODULE[@id='1']")[0] new_spots = [] for spot_id in ids: i_spot = i_module.xpath("TAG[@id='" + spot_id + "']")[0] vnode = i_spot.xpath('PROP[@id="2"]')[0] urlnode = i_spot.xpath('PROP[@id="5"]')[0] g = re.compile('\d+') i_ids = g.findall(vnode.text) i_encoder = i_xml.xpath("OBJECT[@id='" + i_ids[0] + "']")[0] i_video = i_encoder.xpath("MODULE[@id='125']")[0].xpath('TAG[@id="' + i_ids[1] + '"]')[0] i_audio = i_encoder.xpath("MODULE[@id='126']")[0].xpath('TAG[@id="' + i_ids[1] + '"]')[0] i_com = i_encoder.xpath("MODULE[@id='127']") if len(i_com) > 0: icom = i_com[0].xpath('TAG[@id="' + i_ids[1] + '"]') if len(i_com) == 0: i_com = etree.Element("TAG") i_com.set("type","13") else: i_com = i_com[0][0] else: i_com = etree.Element("TAG") i_com.set("type","13") videos = encoder.xpath('MODULE[@id="125"]') audios = encoder.xpath('MODULE[@id="126"]') coms = encoder.xpath('MODULE[@id="127"]') if len(videos) == 0: videos = etree.SubElement(encoder,'MODULE', id='125') audios = etree.SubElement(encoder,'MODULE', id='126') coms = etree.SubElement(encoder,'MODULE', id='127') else: videos = videos[0] audios = audios[0] if len(coms) == 0: coms = etree.SubElement(encoder,'MODULE', id='127') else: coms = coms[0] seq = self.generate_seq(videos, "TAG") videoStream = etree.SubElement(videos, 'TAG', type='13', id=seq) audioStream = etree.SubElement(audios, 'TAG', type='13', id=seq) com = etree.SubElement(coms, 'TAG', type='13', id=seq) for prop in i_video.getchildren(): videoStream.append(prop) for prop in i_audio.getchildren(): audioStream.append(prop) for prop in i_com.getchildren(): com.append(prop) module1 = self.gw_node.xpath('MODULE[@id="1"]')[0] new_spot_id = self.generate_seq(module1,"TAG") spot = etree.SubElement(module1, "TAG", type="13") spot.set('id',new_spot_id) p11 = etree.SubElement(spot, "PROP", name=u"名称", id="1") p12 = etree.SubElement(spot, "PROP", name=u"VideoIn", id="2") p13 = etree.SubElement(spot, "PROP", name=u"AudioIn", id="3") p14 = etree.SubElement(spot, "PROP", name=u"运行状态", id="4") p15 = etree.SubElement(spot, "PROP", name=u"URL", id="5") p16 = etree.SubElement(spot, "PROP", name=u"转发", id="6") p11.text = i_spot.xpath('PROP[@id="1"]')[0].text p12.text = "/DEVICE" + id + "/VIDEO" + seq p13.text = "/DEVICE" + id + "/AUDIO" + seq p14.text = "0" p15.text = self.generate_url(self.generate_global_id(new_spot_id)) p16.text = "true" module2 = self.gw_node.xpath('MODULE[@id="2"]')[0] com = etree.SubElement(module2, "TAG", type="13", id=self.generate_seq(module2,"TAG")) p21 = etree.SubElement(com, "PROP", name=u"名称", id="1") p22 = etree.SubElement(com, "PROP", name=u"COM输入端口", id="2") p23 = etree.SubElement(com, "PROP", name=u"状态", id="3") p21.text = 'COM' + seq p22.text = '/DEVICE' + id + '/COM' + seq p23.text = '0' #i_global_id = self.generate_import_global_id(i_ids[0],i_ids[1]) i_global_id = urlnode.text.split('@')[0].split(':')[1] self.add_alias(new_spot_id, i_global_id) new_spots.append(spot) self.write() self.write_extra() en = self.to_xml(encoder) for spot in new_spots: en.append(self.spot_to_xml(encoder, spot)) return en
class Spot(object): OBJECT_MAX = 127 OBJECT_PROPS = [ {"id": "1", "name": u"名称", "field": "name", "unique": True, "validate": ".+", "long_name": u"监控点名称"}, { "id": "2", "name": u"设备地址", "field": "address", "unique": True, "validate": "^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$", }, {"id": "3", "name": u"服务端口", "field": "service_port", "validate": "^\d+$", "min": 1, "max": 65535}, {"id": "4", "name": u"设备名称", "field": "device_name"}, {"id": "5", "name": u"生产厂家", "field": "vendor"}, {"id": "6", "name": u"设备类型", "field": "device_type"}, {"id": "7", "name": u"GUID", "field": "guid"}, {"id": "8", "name": u"PTZProtocal", "field": "ptz_protocal"}, { "id": "9", "name": u"IO Addr", "field": "io_addr", "validate": "^\d+$", "min": 0, "max": 255, "long_name": u"IO地址", }, {"id": "10", "name": u"音频端口", "field": "audio_port", "validate": "^\d+$", "min": 1, "max": 65535}, {"id": "16383", "name": u"驱动程序名", "field": "driver"}, {"id": "", "name": u"标识", "field": "unique_id", "unique": True, "validate": "^\w+$"}, {"id": "", "name": u"在线", "field": "online"}, ] MODULE125_PROPS = [ {"id": "1", "name": u"名称", "field": "name"}, {"id": "2", "name": u"编码类型", "field": "encode_type"}, {"id": "3", "name": u"通道号", "field": "channel"}, {"id": "4", "name": u"通讯模式", "field": "com_method"}, {"id": "5", "name": u"源地址", "field": "src_address"}, {"id": "6", "name": u"源端口", "field": "src_port"}, {"id": "7", "name": u"协议类型", "field": "protocal_type"}, {"id": "8", "name": u"码率", "field": "encode_rate"}, {"id": "9", "name": u"分辨率", "field": "resolution"}, ] MODULE126_PROPS = [ {"id": "1", "name": u"名称", "field": "name"}, {"id": "2", "name": u"编码类型", "field": "encode_type"}, {"id": "3", "name": u"通道号", "field": "channel"}, {"id": "4", "name": u"通讯模式", "field": "com_method"}, {"id": "5", "name": u"源地址", "field": "src_address"}, {"id": "6", "name": u"源端口", "field": "src_port"}, {"id": "7", "name": u"协议类型", "field": "protocal_type"}, {"id": "8", "name": u"码率", "field": "encode_rate"}, ] gw_info = Gateway().get_info() def __init__(self): self.gw = Gateway() self.info = self.gw.get_config_root() self.gw_node = self.gw.get_node() def short_list(self): module = self.gw_node.xpath("MODULE[@id='1']")[0] spot_list = [] state = open("/tmp/gateway.state") states = state.read().split("\n") state.close() pending = open("/tmp/online") pendings = pending.read().split("\n") pending.close() for tag in module.getchildren(): spot_attrs = tag.getchildren() spot = {"name": spot_attrs[0].text, "global_id": spot_attrs[4].text, "video_in": spot_attrs[2].text} spot["id"] = re.search("(?<=sip:).*?(?=@)", spot["global_id"]).group() spot["global_id"] = ( "sip:" + spot["id"] + "@" + self.gw.get_info()["host"] + ":" + self.gw.get_info()["port"] ) spot["online"] = str(((spot["id"] + "=1" in states) or (spot["id"] + "=1" in pendings)) + 0) spot_list.append(spot) return spot_list def get_onlines(self): spot_list = [] state = open("/tmp/gateway.state") states = state.read().split("\n") state.close() pending = open("/tmp/online") pendings = pending.read().split("\n") pending.close() for s in states: s = s.split("=") if len(s) == 2 and s[1] == "1": spot_list.append(s[0]) for s in pendings: s = s.split("=") if len(s) == 2 and s[1] == "1" and s[0] not in spot_list: spot_list.append(s[0]) return ",".join(spot_list) def short_list1(self): module = self.gw_node.xpath("MODULE[@id='1']")[0] spot_list = [] gis = open("/tmp/gis") gis_array = gis.read().split("\n") gis.close() for tag in module.getchildren(): spot_attrs = tag.getchildren() spot = {"name": spot_attrs[0].text.encode("utf-8"), "global_id": spot_attrs[4].text} # spot['id'] = re.search('(?<=sip:).*?(?=@)',spot['global_id']).group() spot["global_id"] = ( "sip:" + spot["id"] + "@" + self.gw.get_info()["host"] + ":" + self.gw.get_info()["port"] ) spot["gis"] = 0 for gisinf in gis_array: if gisinf.startswith(spot["id"] + "="): spot["gis"] = gisinf spot_list.append(spot) return spot_list def getGis(self, id): gis = open("/tmp/gis") gis_array = gis.read().split("\n") gis.close() for gisinf in gis_array: if gisinf.startswith(id + "="): return gisinf return "" # def xml_list(self): # module = self.gw_node.xpath("MODULE")[0] # spots = etree.Element('Spots') # for tag in module.getchildren(): # spot_attrs = tag.getchildren() # spot = etree.SubElement(spots, 'Spot', id=tag.get('id')) # spot.set('name',spot_attrs[0].text) # return spots def xml_list(self): list = self.short_list() spots = etree.Element("Spots") spots.set("gateway", self.gw_info["id"]) for item in list: spot = etree.SubElement(spots, "Spot") for key in item.keys(): spot.set(key, item[key]) return spots def all(self, ip=""): if ip == "": ip = netconf.get_ip() state = open("/tmp/gateway.state") states = state.read().split("\n") state.close() module = self.gw_node.xpath("MODULE[@id='1']")[0] spots = etree.Element("Spots") for tag in module.getchildren(): spot_attrs = tag.getchildren() spot = etree.SubElement(spots, "Spot", id=tag.get("id")) spot.set("name", spot_attrs[0].text) gid = re.search("(?<=sip:).*?(?=@)", spot_attrs[4].text).group() spot.set("url", gid + "@" + ip + ":" + self.gw.get_info()["port"]) spot.set("state", str((gid + "=1" in states) + 0)) return spots def list(self): spot_list = self.short_list() for spot in spot_list: spot["unique_id"] = spot["id"] seq = re.search("\d+", spot["video_in"]).group() obj = self.info.xpath("OBJECT[@id='" + seq + "']")[0] spot["seq"] = seq for prop in obj.getchildren(): for p in Spot.OBJECT_PROPS: if prop.get("id") == p["id"]: spot[p["field"]] = prop.text return spot_list @staticmethod def generate_seq(node, path): for i in range(1, Spot.OBJECT_MAX): if len(node.xpath(path + "[@id='" + str(i) + "']")) == 0: return str(i) @staticmethod def generate_url(id): return "sip:" + id.strip() + "@" + Spot.gw_info["host"] + ":" + Spot.gw_info["port"] @staticmethod def log_xml(node): return etree.tostring(node, encoding="UTF-8", pretty_print=True) def is_unique(self, key, value, id): for spot in self.list(): if id != spot["id"] and spot[key] == value: return False return True def validate_props(self, props): messages = {} for key, value in props.items(): for p in Spot.OBJECT_PROPS: if key == p["field"]: long_name = p["name"] if p.has_key("long_name"): long_name = p["long_name"] if p.has_key("validate") and not re.match(p["validate"], value.strip()): # 验证未通过 messages["error_" + key] = u"请填写正确的" + long_name continue if p.has_key("min") and (int(value.strip()) < p["min"] or int(value.strip()) > p["max"]): # 范围不符合要求 messages["error_" + key] = u"请填写正确的" + long_name continue if p.has_key("unique") and not self.is_unique(key, value, props["id"]): # 重复唯一 messages["error_" + key] = long_name + u"重复" return messages def f(self): return self.info.xpath('OBJECT/MODULE/TAG/PROP[contains(text(),"sip:")]') def update(self, props): messages = self.validate_props(props) if len(messages) > 0: # 有字段验证没通过 return {"spot": props, "messages": messages} id = props["id"] tag = self.info.xpath('OBJECT/MODULE/TAG/PROP[contains(text(),"sip:' + id + '@")]')[0] tag = tag.getparent() object = self.find(id)["node"] tag_props = tag.getchildren() object_props = object.getchildren() tag_props[0].text = props["name"].strip() tag_props[4].text = Spot.generate_url(props["unique_id"]) for prop in object_props: for p in Spot.OBJECT_PROPS: if p["id"] == prop.get("id") and props.has_key(p["field"]): prop.text = props[p["field"]].strip() self.write() return {"spot": props, "messages": {"success": True}} def add(self, props): messages = self.validate_props(props) if len(messages) > 0: # 有字段验证没通过 return {"spot": props, "messages": messages} seq = Spot.generate_seq(self.info, "OBJECT") module1 = self.gw_node.xpath('MODULE[@id="1"]')[0] tag1 = etree.SubElement(module1, "TAG", type="13", id=Spot.generate_seq(module1, "TAG")) p11 = etree.SubElement(tag1, "PROP", name=u"名称", id="1") p12 = etree.SubElement(tag1, "PROP", name=u"VideoIn", id="2") p13 = etree.SubElement(tag1, "PROP", name=u"AudioIn", id="3") p14 = etree.SubElement(tag1, "PROP", name=u"运行状态", id="4") p15 = etree.SubElement(tag1, "PROP", name=u"URL", id="5") p11.text = props["name"].strip() p12.text = "/DEVICE" + seq + "/VIDEO1" p13.text = "/DEVICE" + seq + "/AUDIO1" p14.text = "0" p15.text = Spot.generate_url(props["unique_id"]) module2 = self.gw_node.xpath('MODULE[@id="2"]')[0] tag2 = etree.SubElement(module2, "TAG", type="13", id=Spot.generate_seq(module2, "TAG")) p21 = etree.SubElement(tag2, "PROP", name=u"名称", id="1") p22 = etree.SubElement(tag2, "PROP", name=u"COM输入端口", id="2") p23 = etree.SubElement(tag2, "PROP", name=u"状态", id="3") p21.text = "COM" + seq p22.text = "/DEVICE" + seq + "/COM1" p23.text = "0" object = etree.Element("OBJECT", id=seq) for prop in Spot.OBJECT_PROPS: p = etree.SubElement(object, "PROP", name=prop["name"], id=prop["id"]) if props.has_key(prop["field"]) and props[prop["field"]].strip() != "": p.text = props[prop["field"]].strip() module125 = etree.SubElement(object, "MODULE", id="125") tag125 = etree.SubElement(module125, "TAG", type="13", id="1") module126 = etree.SubElement(object, "MODULE", id="126") tag126 = etree.SubElement(module126, "TAG", type="13", id="1") module127 = etree.SubElement(object, "MODULE", id="127") tag127 = etree.SubElement(module127, "TAG", type="13", id="1") for prop in Spot.MODULE125_PROPS: p = etree.SubElement(tag125, "PROP", name=prop["name"], id=prop["id"]) for prop in Spot.MODULE126_PROPS: p = etree.SubElement(tag126, "PROP", name=prop["name"], id=prop["id"]) self.info.getroot().append(object) self.write() return {"spot": props, "messages": {"success": True}} def write(self): self.gw.write(self.info) def find(self, unique_id): spot = {} for s in self.list(): if s["id"] == unique_id: spot = s break if spot == {}: return {"info": spot, "node": None} object = self.info.xpath('OBJECT[@id="' + spot["seq"] + '"]')[0] for prop in object.getchildren(): for p in Spot.OBJECT_PROPS: if prop.get("id") == p["id"]: spot[p["field"]] = prop.text return {"info": spot, "node": object} def get_gateway(self): return self.gw def remove(self, id_list): module1 = self.gw_node.xpath('MODULE[@id="1"]')[0] module2 = self.gw_node.xpath('MODULE[@id="2"]')[0] for id in id_list: found = self.find(id) self.info.getroot().remove(found["node"]) module1.remove(module1.xpath('TAG/PROP[contains(text(),"sip:' + id + '@")]')[0].getparent()) module2.remove(module2.xpath('TAG[PROP="COM' + found["info"]["seq"] + '"]')[0]) self.write()
class TestGateway(unittest.TestCase): def setUp(self): self.message_handler = DummyMessageHandler() self.gw = Gateway(self.message_handler) self.gw.start() def tearDown(self): self.gw.stop() self.gw.join() def test_noNodesAfterStartup(self): self.assertEqual(len(self.gw.get_connected_nodes()), 0) def test_Register(self): led = Device("name", self.message_handler) self.assertEqual(led.connect(), True) time.sleep(0.1) connected_nodes = self.gw.get_connected_nodes() self.assertEqual(len(connected_nodes), 1) node = connected_nodes[led.node_id] self.assertEqual(node.name, led.name) self.assertEqual(node.registered, True) last_seen = node.last_seen led._send_status() time.sleep(0.1) connected_nodes = self.gw.get_connected_nodes() self.assertEqual(len(connected_nodes), 1) node = connected_nodes[led.node_id] self.assertEqual(node.name, led.name) self.assertEqual(node.registered, True) self.assertNotEqual(node.last_seen, last_seen) # calling connect again should change nothing self.assertEqual(led.connect(), True) connected_nodes = self.gw.get_connected_nodes() self.assertEqual(len(connected_nodes), 1) def test_sendUnknownMessage(self): header = MessageHeader(node_id = 1, group_id = 1, wants_ack = False) m = Message(99, header) self.message_handler.write_message_from_device(m) self.assertEqual(len(self.gw.get_connected_nodes()), 0) # wait so the gateway can finish processing time.sleep(0.01) def test_sendACKForUnknownMessage(self): dev = Device("name", self.message_handler) self.assertEqual(dev.connect(), True) header = MessageHeader(node_id = dev.node_id, group_id = dev.group_id, wants_ack = False) m = ACKMessage(header) self.message_handler.write_message_from_device(m) time.sleep(0.1) self.assertEqual(len(self.gw.get_connected_nodes()), 1) def test_sendACKForUnknownNode(self): header = MessageHeader(node_id = 1, group_id = 1, wants_ack = False) m = ACKMessage(header) self.message_handler.write_message_from_device(m) self.assertEqual(len(self.gw.get_connected_nodes()), 0) # wait so the gateway can finish processing time.sleep(0.1) def test_moreThan30Nodes(self): for i in range(0, 30): dev = Device('%s' % i, self.message_handler) if i == 29: # too many nodes registered self.assertEqual(dev.connect(), False) else: self.assertEqual(dev.connect(), True) time.sleep(0.1) connected_nodes = self.gw.get_connected_nodes() self.assertEqual(len(connected_nodes), 29) def test_sendRegisterMessageWithoutACKRequest(self): header = MessageHeader(node_id = 1, group_id = 1, wants_ack = False) m = RegisterMessage(header) self.message_handler.write_message_from_device(m) time.sleep(0.1) self.assertEqual(len(self.gw.get_connected_nodes()), 1) def test_sendRegisterMessageWithWrongNodeId(self): header = MessageHeader(node_id = 5, group_id = 1, wants_ack = False) m = RegisterMessage(header) self.message_handler.write_message_from_device(m) time.sleep(0.1) self.assertEqual(len(self.gw.get_connected_nodes()), 0) def test_sendStatusForUnknownNode(self): header = MessageHeader(node_id = 5, group_id = 1, wants_ack = False) m = StatusMessage(header, name = 'dev name') self.message_handler.write_message_from_device(m) self.assertEqual(len(self.gw.get_connected_nodes()), 0) # wait so the gateway can finish processing time.sleep(0.1) def test_Register_lostRegisterResponse(self): token = random.random() dev = Device('test dev', self.message_handler) header = MessageHeader(node_id = 1, group_id = 1, wants_ack = False) p = RegisterMessage(header, name = dev.name, token = token) self.message_handler.write_message_from_device(p) # ignore RegisterResponseMessage for now m = dev._incoming_messages.get(True, 1) # the gateway should already list the node as not registered connected_nodes = self.gw.get_connected_nodes() self.assertEqual(len(connected_nodes), 1) self.assertEqual(connected_nodes[m.new_node_id].name, dev.name) self.assertEqual(connected_nodes[m.new_node_id].registered, False) # write a register message again, let's assume the RegisterResponse # Message was lost self.message_handler.write_message_from_device(p) m2 = dev._incoming_messages.get(True, 1) self.assertEqual(m.token, m2.token) self.assertEqual(m.new_node_id, m2.new_node_id) # the gateway should still list the node as not registered connected_nodes = self.gw.get_connected_nodes() self.assertEqual(len(connected_nodes), 1) self.assertEqual(connected_nodes[m.new_node_id].name, dev.name) self.assertEqual(connected_nodes[m.new_node_id].registered, False)
def __init__(self, label): Gateway.__init__(self, 'Intel', 'Galileo-DK300', get_linux_version(), systemUUID().get_uuid(label), label, None, None, None, "HelixGateway")
def _report_data(self, msg_id, statkey, timestamps, values): # Post data onto cloud_provider solution return Gateway._report_data(self, msg_id, statkey, timestamps, values)
def __init__(self, res_name): Gateway.__init__(self, 'Intel', 'Galileo-DK50', '', res_name)
def setUp(self): self.message_handler = DummyMessageHandler() self.gw = Gateway(self.message_handler) self.gw.start()
def __init__(self): self.gw = Gateway() self.info = self.gw.get_config_root() self.gw_node = self.gw.get_node()
from models import Session, Node, Message from sqlalchemy import desc import string import json import thread from hueDiscovery import * from hueBridge import HueBridge # Init logger logger = logging.getLogger("server") # The Gateway message_handler = SerialMessageHandler() #message_handler = DummyMessageHandler() gw = Gateway(message_handler) def stopIt(): gw.stop() gw.join() # Hue bridge hb = HueBridge() # Webserver @get('/nodes/') def get_list_nodes(): s = Session() res = s.query(Node) nodes = []
def before_session_end(self, *args): from gateway import Gateway gw = Gateway.share() gw.on_connection_end(self)
class Router(ObjectBroker): """ Route is an object that defines a point-to-point connection between two end-points. It is responsible for handling: 1. Storage of incoming messages to the appropriate handlers. 2. Memory of outgoing messages so that the responses can be routed to the calling function. 3. Exposure of transverse objects by mapping to an object ID. 4. Assignation of object IDs to shared local objects. """ def __init__(self, transport, subBroker=None): super().__init__(subBroker) self.endpointID = SerialID.integerToBytes(transport.portalNegotiation()) self.messageCount = -1 self.transport = transport self.cachedTransverse = {} self.responseWaitingQueue = {} self.defaultGateway = Gateway(self) self.listenTask = self.defaultGateway.activateListener() @asyncio.coroutine def close(self): """ Closes the route and hangs up on the transport to let the other end know """ yield from self.transport.hangUp() yield from self.listenTask def resolveTransverseID(self, transverseID): """ Resolves a transverse object identifier into the objectID of the associated local object, if it has been exposed through this router). """ # return the exposed object associated with the transverse identifier return self.endpointID, self.transverseToReference(transverseID) def generateMessageID(self): """ Assigns a message number to an outgoing message based upon an active window method. This is used to keep the numbers as low as possible to minimize transmission length. TODO: This doesn't actually work but the code is currently harmless """ self.messageCount += 1 return SerialID.integerToBytes(self.messageCount) def resolveMessageID(self, messageID): """ Checks whether the message ID exists in the current map, to see if a message has indeed been sent to the remote end. If it has then update the messageWindowLow if appropriate and return the response details. """ # Ensure that the message is a response to a message from this gateway if not messageID in self.responseWaitingQueue: # send a general error to the other-side if this message was unknown raise (UnknownMessageIDError(messageID)) # Return the resolution callback and the exception callback return self.responseWaitingQueue.pop(messageID) def referenceObject(self, obj): """ Marks an object as a shared object and stores it in the shared object list for retrieval through an object reference. """ if isinstance(obj, ProxyObject): return obj.__shared_id__ else: return self.endpointID, self.objectToReference(obj) def dereferenceObject(self, arg, typ, gateway=None): """ Converts an incoming object reference into a python object. Local objects will be mapped to their local python object whereas remote objects will be wrapped in an object proxy. """ endpointID, objectID = arg if endpointID == self.endpointID: obj = self.referenceToObject(objectID) if not isinstance(obj, typ): raise (ReferenceTypeMismatchError(type(obj), typ)) elif gateway is not None: obj = typ.getProxyClass()(gateway, arg) else: raise (UnknownObjectIDError(arg)) return obj def waitForReply(self, messageID, gateway, decoder): """ Sets up a future which will be activated when the response to the outgoing message comes in. """ waiter = asyncio.Future() def resolve(byteStream): # Handle the argument marshalling response = decoder(gateway, byteStream) waiter.set_result(response) self.responseWaitingQueue[messageID] = resolve, waiter.set_exception return waiter def cacheTransverse(self, transverseID, objectID): """ caches a resolved transverse ID / object ID pair so that future resolutions do not have to go down the wire. """ self.cachedTransverse[transverseID] = objectID def getCachedTransverse(self, transverseID): """ tries to retrieve an object ID from the previously cached transverse ID. If that object had not been cached then raises a KeyError """ return self.cachedTransverse[transverseID] def getBuffer(self): """ returns a buffer to write an outgoing packet into. """ return self.transport.startWrite() @asyncio.coroutine def flushBuffer(self, buf): """ Commits the write of a send buffer to the wire. """ yield from self.transport.commitWrite(buf) @asyncio.coroutine def poleTransport(self, recvHook, errHook): """ Continuously pole the transport whilst the gateway remains open this poling will be done in asychronous blocking mode so that it will not 'spin' whilst no incoming data is present. """ def doneCallback(fut): try: fut.result() except Exception as e: asyncio.get_event_loop().run_until_complete(errHook(e)) try: while True: receivedStream = io.BytesIO((yield from self.transport.recv())) coro = asyncio.async(recvHook(receivedStream)) coro.add_done_callback(doneCallback) except TransportClosed: pass
def home(): url = 'http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295cca9337e1e96b00e0' # TODO: fix this asap, can`t be like that, object will be created with every request gate = Gateway() return render_template('layout.html', data=gate.get_data(url))
def connectionLost(self, reason): Gateway.connectionLost(self, reason) reactor.stop()
def on_line_received(self, message): print 'line received: ', message from gateway import Gateway gw = Gateway.share() gw.on_line_received(self, message)