{"id":18355883,"url":"https://github.com/anduin2017/sleepagent","last_synced_at":"2025-04-13T16:42:38.858Z","repository":{"id":226375771,"uuid":"768157829","full_name":"Anduin2017/SleepAgent","owner":"Anduin2017","description":null,"archived":false,"fork":false,"pushed_at":"2025-03-27T15:27:25.000Z","size":1207,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-10T12:26:14.434Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Anduin2017.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-06T15:18:35.000Z","updated_at":"2025-03-27T15:38:04.000Z","dependencies_parsed_at":"2024-03-17T05:38:20.154Z","dependency_job_id":"9e5cefce-644a-44bb-b3ff-4733ea1d29a4","html_url":"https://github.com/Anduin2017/SleepAgent","commit_stats":null,"previous_names":["anduin2017/sleepagent"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anduin2017%2FSleepAgent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anduin2017%2FSleepAgent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anduin2017%2FSleepAgent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anduin2017%2FSleepAgent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Anduin2017","download_url":"https://codeload.github.com/Anduin2017/SleepAgent/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248747420,"owners_count":21155448,"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":[],"created_at":"2024-11-05T22:08:15.340Z","updated_at":"2025-04-13T16:42:38.850Z","avatar_url":"https://github.com/Anduin2017.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sleep Agent\n\n[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://gitlab.aiursoft.cn/anduin/sleepAgent/-/blob/master/LICENSE)\n[![Pipeline stat](https://gitlab.aiursoft.cn/anduin/SleepAgent/badges/master/pipeline.svg)](https://gitlab.aiursoft.cn/anduin/sleepAgent/-/pipelines)\n[![Test Coverage](https://gitlab.aiursoft.cn/anduin/sleepAgent/badges/master/coverage.svg)](https://gitlab.aiursoft.cn/anduin/SleepAgent/-/pipelines)\n[![ManHours](https://manhours.aiursoft.cn/r/gitlab.aiursoft.cn/anduin/SleepAgent.svg)](https://gitlab.aiursoft.cn/anduin/sleepAgent/-/commits/master?ref_type=heads)\n[![Website](https://img.shields.io/website?url=https%3A%2F%2Fhealth.aiursoft.cn%2F)](https://health.aiursoft.cn)\n[![Docker](https://img.shields.io/docker/pulls/anduin2019/sleepagent.svg)](https://hub.docker.com/r/anduin2019/sleepagent)\n\nSmart wristbands are basic electronic products that many people own nowadays. They have a wide variety of sensors that can collect a large amount of interesting data, such as sleep patterns, physical activity, and cardiovascular health. This data often has high analytical and development value.\n\nFor example, you can automatically activate the \"do not disturb\" mode on your phone after falling asleep and turn it off when you wake up. Or you can monitor your heart rate changes to control a treadmill for steady-state running. Or you can monitor blood oxygen saturation and send an alert when abnormalities occur.\n\n![grafana](./assets/grafana.png)\n\nSo, it is very necessary to expand your smart wristband. And this repository provides a solution:\n\nIt supports the Amazfit GTR 4 wristband, running a background service in the wristband that scans the data of various sensors every 5 minutes and sends it to a custom HTTP server. Similarly, this repository also provides the HTTP data cache server.\n\nUsing this solution, you can perfectly retrieve your health data using HTTP API, use it for further expansion and development, or export it to Prometheus.\n\n# Server side\n\n## Run in Ubuntu\n\nThe following script will install\\update this app on your Ubuntu server. Supports Ubuntu 22.04.\n\nOn your Ubuntu server, run the following command:\n\n```bash\ncurl -sL https://gitlab.aiursoft.cn/anduin/sleepagent/-/raw/master/install.sh | sudo bash\n```\n\nOf course it is suggested that append a custom port number to the command:\n\n```bash\ncurl -sL https://gitlab.aiursoft.cn/anduin/sleepagent/-/raw/master/install.sh | sudo bash -s 8080\n```\n\nIt will install the app as a systemd service, and start it automatically. Binary files will be located at `/opt/apps`. Service files will be located at `/etc/systemd/system`.\n\n## Run manually\n\nRequirements about how to run\n\n1. Install [.NET 9 SDK](http://dot.net/) and [Node.js](https://nodejs.org/).\n2. Execute `npm install` at `wwwroot` folder to install the dependencies.\n3. Execute `dotnet run` to run the app.\n4. Use your browser to view [http://localhost:5000](http://localhost:5000).\n\n## Run in Microsoft Visual Studio\n\n1. Open the `.sln` file in the project path.\n2. Press `F5` to run the app.\n\n## Run in Docker\n\nFirst, install Docker [here](https://docs.docker.com/get-docker/).\n\nThen run the following commands in a Linux shell:\n\n```bash\nimage=anduin2019/sleepagent\nappName=sleepagent\nsudo docker pull $image\nsudo docker run -d --name $appName --restart unless-stopped -p 5000:5000 -v /var/www/$appName:/data $image\n```\n\nThat will start a web server at `http://localhost:5000` and you can test the app.\n\nThe docker image has the following context:\n\n| Properties  | Value                            |\n|-------------|----------------------------------|\n| Image       | anduin2019/sleepagent            |\n| Ports       | 5000                             |\n| Binary path | /app                             |\n| Data path   | /data                            |\n| Config path | /data/appsettings.json           |\n\n# Sleep Agent full deployment\n\n## Step 1 - Buy an Amazfit GTR 4\n\nOf course you need to own a ZEPP watch to use this solution. Please buy the Amazfit GTR 4.\n\n[京东](https://item.jd.com/100037094209.html)\n\nCosts around 1000 RMB.\n\n## Step 2 - Enable the developer mode\n\nYou need to enable the developer mode on your watch.\n\nGo to `Settings` -\u003e `About`, then tap the logo 10 times.\n\n![enable developer mode](./assets/dev.png)\n\nNow you can see the developer mode.\n\n## Step 3 - Deploy the server (Optional if you want to use our server)\n\nOf course, you need to deploy the server to store the sleep data.\n\nBuy or deploy a Ubuntu 22.04 server. Deploy with any approaches above.\n\n* Deploy with a single script\n* Deploy manually\n* Deploy with Docker\n\nAnd get the server's endpoint. For example, `http://your-server.com`.\n\nThis will be the endpoint that the app will send the sleep data to.\n\n## Step 4 - Change the endpoint in the app\n\nYou need to edit, build the app and install it on your phone.\n\nFirst, install Node.js and NPM on your computer.\n\n```bash\n# Assuming you are using Windows\nwinget install -e --id Node.js\n```\n\nThen, install the ZEUS CLI tool:\n\n```bash\nnpm i @zeppos/zeus-cli -g\n```\n\nNow change the endpoint to your own server's endpoint in the `./src/Anduin.SleepAgent.ClockApp/app-service/background_service.js` file.\n\n```typescript\nconst endPoint = \"http://your-server.com/api/metrics/send\"\n```\n\nIf you want to use our server, you can skip this step, or keep the value:\n\n```typescript\nconst endPoint = \"https://health.aiursoft.cn/api/metrics/send\"\n```\n\nThe app will send the sleep data to this endpoint.\n\n## Step 5 - Build the app\n\nNow, build the app:\n\n```bash\ncd ./src/Anduin.SleepAgent.ClockApp\nnpm install\nzeus preview\n```\n\nNow you will see a QR code.\n\n![shown-qr](./assets/qr.png)\n\n## Step 6 - Install the app to your watch\n\nOpen the ZEPP app on your phone, and scan the QR code.\n\n![install](./assets/install.jpg)\n\nNow you can see the app on your watch's app list.\n\n## Step 7 - Configure the app to send the data\n\nOpen the app on your watch, click the start button, and the app will start to send the sleep data to your server.\n\n![app-running](./assets/running.png)\n\nAnd you must disable the `Enable Sleep Mode` feature in the clock: `Sleep` -\u003e `Settings` -\u003e `Sleep Plan` -\u003e `Enable Sleep Mode`. This is because the app will not work when the watch is in sleep mode.\n\nAnd **NEVER** enter sleep mode for your watch!!! Because the app will stop working when the watch is in sleep mode.\n\n## Step 8 - View the data on the server\n\nWait 5-10 minutes, and you can view the metrics data on your server.\n\nOpen your browser and navigate to `http://your-server.com/api/metrics/all`.\n\nIf you are using our server, you can navigate to `https://health.aiursoft.cn/api/metrics/all`.\n\nYou will see the users list which has sent the sleep data to your server.\n\n```json\n[\n    \"SomeUser\",\n    \"SomeOtherUser\"\n]\n```\n\nTo view one user's data, navigate to `/api/metrics/query?nick-name=SomeUser`.\n\nYou may see:\n\n```json\n// View https://docs.zepp.com/docs/reference/device-app-api/newAPI/sensor/Sleep/ for more information\n{\n    \"user\": {\n        \"age\": 30,\n        \"height\": 1.80, // Meters\n        \"weight\": 75.0, // KG\n        \"gender\": 0, // 0 is male, 1 is female\n        \"nickName\": \"UserName\",\n        \"region\": \"cn\",\n        \"birth\": {\n            \"year\": 1990,\n            \"month\": 1,\n            \"day\": 1\n        }\n    },\n    \"device\": {\n        \"width\": 466,\n        \"height\": 466,\n        \"screenShape\": 1,\n        \"deviceName\": \"Amazfit GTR 4\",\n        \"keyNumber\": 2,\n        \"keyType\": \"normal_21\",\n        \"deviceSource\": 7930222,\n        \"deviceColor\": 3,\n        \"productId\": 121,\n        \"productVer\": 256,\n        \"skuId\": 256\n    },\n    \"heartRateLast\": 85, // BPM\n    \"heartRateResting\": 65, // BPM\n    \"heartRateSummary\": {\n        \"maximum\": {\n            \"time\": 1708437033, // Unix timestamp\n            \"time_zone\": 0, // GMT+0\n            \"hr_value\": 154 // BPM\n        }\n    },\n    \"battery\": 99, // Percentage\n    \"bloodOxygen\": {\n        \"value\": 97, // Percentage\n        \"time\": 1708433574, // Unix timestamp\n        \"retCode\": 2 // 2: Success, other: Failed\n    },\n    \"bloodOxygenLastFewHour\": [],\n    \"calorie\": 247, // Kcal\n    \"calorieT\": 800, // Kcal\n    \"distance\": 3391, // Meters\n    \"fatBurning\": 39,\n    \"fatBurningT\": 30,\n    \"paiDay\": 2,\n    \"paiWeek\": 155,\n    \"sleepInfo\": {\n        \"score\": 93,\n        \"startTime\": 1186, // Minutes from 00:00\n        \"endTime\": 1648, // Minutes from 00:00\n        \"deepTime\": 103, // Minutes\n        \"totalTime\": 463 // Minutes\n    },\n    \"sleepStgList\": {\n        \"WAKE_STAGE\": 7,\n        \"REM_STAGE\": 8,\n        \"LIGHT_STAGE\": 4,\n        \"DEEP_STAGE\": 5\n    },\n    \"SleepingStatus\": 0, // 0: Awake, 1: Sleeping\n    \"stands\": 5, // Times\n    \"standsT\": 12, // Times\n    \"steps\": 4461, // Steps\n    \"stepsT\": 6000, // Steps\n    \"stress\": {\n        \"value\": 49,\n        \"time\": 1708439460\n    },\n    \"isWearing\": 1 // 0: not wearing, 1: wearing, 2: in motion, 3: not sure\n}\n```\n\nNow you can save the JSON or teleport it to other monitoring systems like Prometheus or Grafana.\n\n## Step 9 - Scrap the data with Prometheus\n\nYou can scrap the data with Prometheus.\n\nFirst, you need to create a new Prometheus server. You can start it with Docker:\n\n```bash\nsudo docker run -d --name prometheus -p 9090:9090 -v /var/www/prometheus:/etc/prometheus prom/prometheus\n```\n\nThen, edit the `prometheus.yml` file:\n\n```yaml\nglobal:\n  scrape_interval: 300s\n\nscrape_configs:\n  - job_name: health\n    scheme: https\n    static_configs:\n      - targets: [health.aiursoft.cn]\n    metrics_path: /api/metrics/metric\n    params:\n      nick-name: ['AnduinXiaomi'] # Replace with your own user name\n    scrape_interval: 300s\n    honor_labels: true\n```\n\nHere I'm using the `health.aiursoft.cn` server, and I want to scrap the data of the user `AnduinXiaomi` as an example. You need to replace the server and user name with your own.\n\nThen, restart the Prometheus server:\n\n```bash\nsudo docker restart prometheus\n```\n\nNow open the Prometheus server at `http://localhost:9090`, and you can see the metrics data.\n\n## Step 10 - Visualize the data with Grafana\n\nYou can visualize the data with Grafana.\n\nFirst, you need to create a new Grafana server. You can start it with Docker-compose.\n\n```bash\nsudo docker-compose up -d\n```\n\nThis command will start 3 docker containers:\n\n* SleepAgent: The server that stores the sleep data. `http://localhost:5000`\n* Prometheus: The server that scraps the sleep data. `http://localhost:9090`\n* Grafana: The server that visualizes the sleep data. `http://localhost:3000`\n\n![docker-compose](./assets/docker-compose.png)\n\nEdit your clock app's settings, and change the endpoint to `http://YOUR_BOX:5000/api/metrics/send`.\n\nThen, open the Grafana server at `http://localhost:3000`, login with account: `admin`, password: `admin`.\n\nYou will see the dashboard.\n\n![grafana](./assets/grafana.png)\n\n## How to contribute\n\nThere are many ways to contribute to the project: logging bugs, submitting pull requests, reporting issues, and creating suggestions.\n\nEven if you with push rights on the repository, you should create a personal fork and create feature branches there when you need them. This keeps the main repository clean and your workflow cruft out of sight.\n\nWe're also interested in your feedback on the future of this project. You can submit a suggestion or feature request through the issue tracker. To make this process more effective, we're asking that these include more information to help define them more clearly.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanduin2017%2Fsleepagent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanduin2017%2Fsleepagent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanduin2017%2Fsleepagent/lists"}