{"id":15163274,"url":"https://github.com/fedulovivan/mhz19-go","last_synced_at":"2026-01-20T07:32:54.243Z","repository":{"id":254823748,"uuid":"847641477","full_name":"fedulovivan/mhz19-go","owner":"fedulovivan","description":"Backend for the home automation server, written on golang","archived":false,"fork":false,"pushed_at":"2025-06-21T21:09:16.000Z","size":3674,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-21T22:19:42.005Z","etag":null,"topics":["golang","home-automation","mqtt","smart-home","sqlite","zigbee"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":false,"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/fedulovivan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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,"zenodo":null}},"created_at":"2024-08-26T09:02:32.000Z","updated_at":"2025-06-21T21:09:19.000Z","dependencies_parsed_at":"2024-09-17T11:40:09.094Z","dependency_job_id":"c518692a-7144-4763-8560-4c5884c497e3","html_url":"https://github.com/fedulovivan/mhz19-go","commit_stats":{"total_commits":86,"total_committers":1,"mean_commits":86.0,"dds":0.0,"last_synced_commit":"cd013345c12cee1a3d1bc1aa283c84d6a543f3f7"},"previous_names":["fedulovivan/mhz19-go"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/fedulovivan/mhz19-go","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fedulovivan%2Fmhz19-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fedulovivan%2Fmhz19-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fedulovivan%2Fmhz19-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fedulovivan%2Fmhz19-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fedulovivan","download_url":"https://codeload.github.com/fedulovivan/mhz19-go/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fedulovivan%2Fmhz19-go/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28598158,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-20T02:08:49.799Z","status":"ssl_error","status_checked_at":"2026-01-20T02:08:44.148Z","response_time":117,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["golang","home-automation","mqtt","smart-home","sqlite","zigbee"],"created_at":"2024-09-27T02:22:20.110Z","updated_at":"2026-01-20T07:32:54.237Z","avatar_url":"https://github.com/fedulovivan.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"### Project «mhz19-go»\n\nThis project is a backend for the home automation server, written on Golang. Evolution of another myne project [mhz19-next](https://github.com/fedulovivan/mhz19-next) which was a typescript-based. \n\n[![Go Report Card](https://goreportcard.com/badge/github.com/fedulovivan/mhz19-go)](https://goreportcard.com/report/github.com/fedulovivan/mhz19-go)\n\n### Real use cases\n\n- Switch smart ceiling light on/off upon receiving message from smart wall switch\n- Automatically switch storage room light upon receiving message from movement sensor\n- Automatically switch storage room ventilation, when movement sensor reports a human presense and room door sensor reports it is closed\n- Play alert sound, notify owner via telegram and cut off home water supply upon receiving message from one of water leakage sensors\n- Notify owner then some guarded door (equipped with smart sensor) was opened/closed and user is not at home\n\nSee full list of configured rules: [user rules](https://github.com/fedulovivan/mhz19-go/tree/main/assets/rules/user) and [system rules](https://github.com/fedulovivan/mhz19-go/tree/main/assets/rules/system)\n\n### Project goal\n\n- Create pure offline, vendor agnostic and fully controlled local home automation server\n- Bring project to modern stack and learn golang while project migration from typescript\n\n### Applied best practices\n\n- Source code organisation folows SOLID/clean-architecture recommendations\n- DI to enable better UTs coverage\n- Folders layout in accordance with [project-layout](https://github.com/golang-standards/project-layout)\n- With unit tests, race tests and code coverage stats enabled\n- Sql schema and basic data migrations support\n- Load tests for the REST API\n- Race tests\n- Collecting prometheus metrics, configured prometheus and dashboard in grafana \n- Makefile for the common developer tasks and docker-compose file to deploy app with additional tooling (prometheus, grafana)\n- Backlog with TODOs\n\n### Architecture\n\n- Channel providers which collect messages from different channels and devices into unified stream. Supported channels are mqtt, telegram, dns-sd, sonoff (TBD) and yeelight (TBD).\n- History storage which persist received messages in sqlite db\n- Versatile mapping rules Engine which defines how application should respond to received messages\n- Actions executor which executes one or more actions in respond to received message\n- REST API layer to manage application: create rules, read device messages history, read registered devices\n- Telegram as a channel to delivery various notifications and alerts and remote controll\n- Docker compose to deploy entire server, which consists of this [backend](https://github.com/fedulovivan/mhz19-go), frontend (TBD), [device-pinger](https://github.com/fedulovivan/device-pinger) service, [eclipse mosquitto](https://mosquitto.org/) as mqtt message broker, [zigbee2mqtt](https://www.zigbee2mqtt.io/) zigbee bridge, metrics database [prometheus](https://prometheus.io/) and [grafana](https://grafana.com/) for metrics visualization.\n\n### Unified message structure\n\nNo matter which channel was used to receive a message, or which certain device has emitted that message, we pack every message into unified strusture to be handled by Engine.\n- Channel type - mqtt, telegram, dns-sd, sonoff, yeelight\n- Device class - zigbee device, zigbee bridge, device-pinger, valves-manipulator, telegram-bot, sonoff diy-plug device, yeelight device\n- Device id - unique device identifier, specific for the certain device class. e.g. zigbee ieee device address 0x00158d0004244bda or device IP\n- Payload - the message itself as a json. untyped, specific for the certain device and channel. e.g. zigbee wall switch message may look like `{\"battery\":100,\"action\":\"single_left\"}` or telegram-bot message as `{\"Text\":\"/open-valves\"}`\n- Timestamp - a time when message was received by the server, usefull when reading and visualizing historical data\n\n### Used technologies\n\n- [golang](https://go.dev/) as main app language\n- [sqlite3](https://www.sqlite.org/) with [go-sqlite3](github.com/mattn/go-sqlite3) client as a persistent storage\n- [ozzo-routing](github.com/go-ozzo/ozzo-routing/v2) http routing\n- [godotenv](github.com/joho/godotenv) and [go-envconfig](github.com/sethvargo/go-envconfig) as configuration layer\n- [eclipse paho](github.com/eclipse/paho.mqtt.golang) as mqtt client\n- [docker](https://www.docker.com/) for containerization\n- [Makefile](./blob/main/Makefile) for developer routine automation\n- [telegram bot api](https://core.telegram.org/bots/api) with [client](https://github.com/go-telegram-bot-api/telegram-bot-api) for the notifications and remote management\n- [dnssd](https://github.com/brutella/dnssd) as mdns client (sonoff smart devices discovery)\n- [gabs](https://github.com/Jeffail/gabs) as json querier\n\n### Screenshots\n\nDashboard\n![demo-03.png](assets/demo-03.png)\nConsole\n![demo-02.png](assets/demo-02.png)\n\n### Migrations, schema version validation\n\n- `make migrate-reset` - execute all down migrations and then all up migrations, basically reset schema to its default empty state\n- `make migrate-down` - execute all down migrations\n- `make migrate-up` - execute all up migrations\n- `make migrate-up-single` - run certain migration up\n- `make migrate-down-single` - run certain migration down\n- `make migrate-dump` - create current schema dump\n\n### Load tests\n\n- `make api-load-rules-read`\n- `make api-load-rules-write`\n- `make api-load-push-message-write`\n\n### Development\n\n- create db and run all migrations `make migrate-up` or reset to inital state with `make migrate-reset`\n- create config file from sample `cp .env.sample .env`\n- optionally: run tests with `make test`\n- run application `make run`\n\n### Deploying production instance\n\n- create db and run all migrations `make migrate-up`\n- create config file from sample `cp .env.sample .env`\n- build image with `make docker-build`\n- create and run container `make docker-up`\n- optionally: check logs with `make docker-logs`\n\n### Initial data seeding (provisioning)\n\n`DIR=devices make seed`\n`DIR=rules/system make seed`\n`DIR=rules/user make seed`\n\n### Upgrade\n\nstep 1: pull changes\n`git pull`\n\nstep 2: run migration using existing running instance of backend container (docker-migrate takes local sql file and executes it inside container)\n`DB_REV=07 make docker-migrate-up-single`\n\nstep 3: run update\n`make update`\n\n### Tools required for the development on the bare host\n\n- go `wget https://go.dev/dl/go1.23.0.linux-amd64.tar.gz`, `rm -rf /usr/local/go \u0026\u0026 tar -C /usr/local -xzf go1.23.0.linux-amd64.tar.gz`, `export PATH=$PATH:/usr/local/go/bin`\n- golangci-lint https://golangci-lint.run/welcome/install `curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.60.3`\n- gcc `sudo apt install gcc`\n- delve `go install -v github.com/go-delve/delve/cmd/dlv@latest`\n- sqlite3 `sudo apt-get install sqlite3`\n- oha `brew install oha`\n\n### Usefull commands\n\nrun and view logs for selected module only `make run 2\u003e\u00261 \u003e/dev/null | grep \"engine\"`\nscp log `scp ivanf@192.168.88.188:/home/ivanf/Projects/mhz19-go/log.txt ~/Desktop`\nhtop hotkeys:\n    - t - toggle tree view\n    - Shift + k - toglle showing kernel processes\n    - p - toggle showing full paths\n\nmeasure on/off time for sonoff relay\nhttps://stackoverflow.com/questions/18215389/how-do-i-measure-request-and-response-times-at-once-using-curl\n`curl -w \"@assets/curl-format.txt\" -v -d '{\"data\":{\"switch\":\"off\"}}' http://192.168.88.60:8081/zeroconf/switch`\n\n### Profiling\n\n- run cpu and memory benchmark for single unit test and save two profiles accordingly\n`make bench`\n- open cpu profile in pprof\n`make pprof-cpu`\n- open memory profile in pprof\n`make pprof-mem`\n- inside pprof: see top N records\n`top`\n- inside pprof: open top N graph in browser\n`web`\n- inside pprof: see memory allocation for certain function\n`list Test31`\n- collect 10s cpu profile from running app\n`curl --location 'http://localhost:7070/debug/pprof/profile?seconds=10' \u003e back_cpu.prof`\n- collect 10s head allocations profile from running app\n`curl --location 'http://localhost:7070/debug/pprof/heap?seconds=10' \u003e back_heap.prof`\n- open heap snapshot in text format in browser\n`http://localhost:7070/debug/pprof/heap?debug=1`\n- open local cpu profile in pprof web version\n`go tool pprof -http=:7272 back_cpu.prof`\n- open remote heap profile in local pprof web version\n`go tool pprof -http=:7272 http://192.168.88.188:7070/debug/pprof/heap`\n- open remote cpu profile in local pprof web version\n`go tool pprof -http=:7272 http://192.168.88.188:7070/debug/pprof/profile`\n- list of available profilers\n`http://localhost:7070/debug/pprof`\n\n### Database schema\n\n- **rules** - TBD\n- **rule_conditions** - TBD\n- **rule_actions** - TBD\n- **rule_condition_or_action_arguments** - TBD\n- **rule_action_argument_mappings** - TBD\n- **condition_functions** - TBD\n- **action_functions** - TBD\n- **device_classes** - TBD\n- **channel_types** - TBD\n- **devices** - TBD\n- **messages** - TBD\n- **schema_version** - since v1, TBD","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffedulovivan%2Fmhz19-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffedulovivan%2Fmhz19-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffedulovivan%2Fmhz19-go/lists"}