{"id":16391764,"url":"https://github.com/williamfzc/whenconnect","last_synced_at":"2025-03-23T04:31:44.502Z","repository":{"id":55942213,"uuid":"148323048","full_name":"williamfzc/whenconnect","owner":"williamfzc","description":"when your android connected, do sth :)","archived":false,"fork":false,"pushed_at":"2020-12-05T12:37:12.000Z","size":36,"stargazers_count":12,"open_issues_count":0,"forks_count":6,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-18T17:56:38.161Z","etag":null,"topics":["adb","android","device","manager","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/williamfzc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-11T13:37:17.000Z","updated_at":"2023-10-05T14:46:29.000Z","dependencies_parsed_at":"2022-08-15T10:00:38.626Z","dependency_job_id":null,"html_url":"https://github.com/williamfzc/whenconnect","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/williamfzc%2Fwhenconnect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/williamfzc%2Fwhenconnect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/williamfzc%2Fwhenconnect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/williamfzc%2Fwhenconnect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/williamfzc","download_url":"https://codeload.github.com/williamfzc/whenconnect/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245056889,"owners_count":20553855,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["adb","android","device","manager","python"],"created_at":"2024-10-11T04:47:17.090Z","updated_at":"2025-03-23T04:31:44.206Z","avatar_url":"https://github.com/williamfzc.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# whenconnect\n\n[English README](README_en.md)\n\n[![Maintainability](https://api.codeclimate.com/v1/badges/c6e406c3416bbbcbd898/maintainability)](https://codeclimate.com/github/williamfzc/whenconnect/maintainability)\n[![PyPI version](https://badge.fury.io/py/whenconnect.svg)](https://badge.fury.io/py/whenconnect)\n[![Downloads](https://pepy.tech/badge/whenconnect)](https://pepy.tech/project/whenconnect)\n\n\u003e when your android connected, do sth :)\n\n## 目标\n\n提供一个简洁方便的方案以解决设备**连接与断开**时的监听工作，例如安装应用、启动应用、杀死进程，或是定制任何你希望的。\n\n\u003e 如果你在使用linux，你可能更喜欢 [ud4d](https://github.com/doringland/ud4d) :)\n\n## 使用\n\n### 基本用法\n\n如果你希望，在设备123456F成功连接电脑后执行函数A，你只需要：\n\n```python\nfrom whenconnect import when_connect\n\n\ndef A(device):\n    print('call function A', device)\n\n# 事件注册\nwhen_connect(device=['123456F'], do=A)\n```\n\n这样做之后，在你的程序执行时whenconnect将会同步检测123456F是否已经连接上；如果连接上，将把设备ID传入函数A并执行它：\n\n```bash\ncall function A 123456F\n```\n\n当然，你也可以选择响应所有设备：\n\n```python\nwhen_connect(device='any', do=A)\n```\n\n这样做之后，一旦新增了android设备都会执行函数A并传入连接上的设备id。\n\n同样的，whenconnect也支持当设备断开时的场景，你只需要：\n\n```python\nfrom whenconnect import when_disconnect\n\n\ndef A(device):\n    print('call function A', device)\n\n# 事件注册\nwhen_disconnect(device=['123456F'], do=A)\n```\n\n### 获取当前设备与任务\n\nwhenconnect也提供了API以便在程序任意位置获取到当前的设备与任务注册状态：\n\n```python\nfrom whenconnect import get_devices, get_current_task\n\n\ndevice_list = get_devices()\nprint(device_list)\n# ['123456F']\n\ntask_dict = get_current_task()\nprint(task_dict)\n# {'any': {'connect': {\u003cfunction normal_thing at 0x1033dad08\u003e}, 'disconnect': {\u003cfunction lose_connect at 0x1068d5b70\u003e}}, 'specific': {'123': {'connect': {\u003cfunction special_thing at 0x101dc8ea0\u003e}}, 'def456': {'connect': {\u003cfunction special_thing at 0x101dc8ea0\u003e}}}}\n```\n\n### 实际使用例子\n\n如果你只是单纯希望它单独作为一个长期的监听模块存在，只需要让你的程序保持工作即可：\n\n- 在末尾加入死循环\n- 嵌入到服务器\n- `...`\n\n基于whenconnect，你可以自由定制你需要的场景。例如，希望在设备插入后每五秒钟查看一次设备信息：\n\n```python\nfrom whenconnect import when_connect\nimport os\nimport threading\n\n\ndef check_device_info(device):\n    cmd = 'adb -s {} shell getprop ro.product.model'.format(device)\n    device_model = os.popen(cmd).read()\n    print(device_model)\n\n    global timer\n    timer = threading.Timer(5, lambda: check_device_info(device))\n    timer.start()\n\n# 事件注册\nwhen_connect(device='any', do=check_device_info)\n```\n\n## API\n\nwhenconnect只提供了为数不多的API：\n\n- 连接设备时\n- 断开设备时\n- 获取当前已连接的设备列表\n- 获取当前已注册的任务\n\n可直接阅读[api.py](whenconnect/api.py)了解实现细节，或参考[demo.py](demo.py)使用。\n\n## 安装\n\n```\npip install whenconnect\n```\n\n- Only tested on python3.\n- and ADB installed.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwilliamfzc%2Fwhenconnect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwilliamfzc%2Fwhenconnect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwilliamfzc%2Fwhenconnect/lists"}