From 34574ad57a5878ed9bdf8ea21e1266c12313e26b Mon Sep 17 00:00:00 2001 From: Sangbum Kim Date: Sun, 24 Sep 2017 23:28:38 +0900 Subject: [PATCH] air initial script added --- .gitignore | 139 +++++++++++++++++++++++++++++++++++++++++++++++ PKGBUILD | 38 +++++++++++++ air.service | 25 +++++++++ main.py | 49 +++++++++++++++++ requirements.txt | 3 + 5 files changed, 254 insertions(+) create mode 100644 .gitignore create mode 100644 PKGBUILD create mode 100644 air.service create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85339b4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,139 @@ +# use glob syntax +syntax: glob + + +# Packages # +############ +# it's better to unpack these files and commit the raw source +# git has its own built in compression methods +*.7z +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip +*.apk +*.ipa +*.mgl + +# Logs and databases # +###################### +*.log +*.sql +*.sqlite + +# OS generated files # +###################### +._.DS_Store +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +Icon? +ehthumbs.db +Thumbs.db + +#Ignore files build by Visual Studio +# Compiled source # +################### +*.com +*.class +*.dll +*.o +*.so +*.obj +*.exe +*.pdb +*.user +*.aps +*.pch +*.vspscc +*_i.c +*_p.c +*.ncb +*.suo +*.tlb +*.tlh +*.bak +*.cache +*.ilk + +[Bb]uild[Ll]og.* +*.[Pp]ublish.xml +*.lib +*.sbr +*.scc +*.sdf +*.opensdf +[Bb]in +[Dd]ebug*/ +obj/ +[Rr]elease*/ +_ReSharper*/ +[Tt]est[Rr]esult* +ipch/ +doc/ +moglue/settings/__init__.py +########daniel added +.orig +.hg* +*.tmproj +*.ser +#vim +*.swn +*.swo +*.swp +*~ +tags +.rvmrc +*.rdb +#vimend + +#python +*.py[cod] +#python packages +*.egg +*.egg-info +dist +build +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg +lib +lib64 +MANIFEST + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox +nosetests.xml + +# Translations +*.mo + +# Mr Developer +.mr.developer.cfg +.project +.pydevproject +.idea + +#misc +*.pid + + + +/*.iml +/out +/venv +.history +.vscode \ No newline at end of file diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 0000000..7ed6cf6 --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,38 @@ +# Maintainer: surefire@cryptomile.net +pkgname=air_monitor +pkgver=0+1f41c24+ +pkgrel=1 +pkgdesc="Air monitering Service" +arch=('i686' 'x86_64') +url="https://amuz.es/src/infra/air" +license=('GPL') +conflicts=('air') +provides=('air') +replaces=('air') +options=('!buildflags') +depends=('python') +makedepends=('python' 'git') +source=('git+https://amuz.es/src/infra/air.git') +md5sums=('SKIP') + + +pkgver() { + cd "${srcdir}/${pkgname}" + printf '%s+%s+%s' \ + $(sed -e 's,/,+,g; s, ,,g' templates/.VERSION) \ + $(git rev-list --count HEAD...$(git log --pretty=format:%H -n 1 -- templates/.VERSION)) \ + $(git rev-parse --short HEAD) \ + ; +} + +package() { + mkdir -p "${pkgdir}/opt/air" + + cd "srcdir/${pkgname}" + git archive master | tar -x -C "$pkgdir/opt/air" + cd "${pkgdir}/opt/air" + python -m venv --clear --symlinks venv + "${pkgdir}/opt/air/bin/pip" install -r requirements.txt + + install -Dm0644 -t "$pkgdir/usr/lib/systemd/system" "$srcdir/${pkgname}/air.service" +} \ No newline at end of file diff --git a/air.service b/air.service new file mode 100644 index 0000000..1542bed --- /dev/null +++ b/air.service @@ -0,0 +1,25 @@ +[Unit] +Description=Air monitering Service +After=syslog.target + +[Service] +WorkingDirectory=/opt/air +ExecStart=/opt/air/venv/bin/python run.py +TimeoutStartSec=5 +User=nobody +Group=nobody +Restart=on-failure +KillSignal=SIGQUIT +Type=notify +StandardError=syslog +NotifyAccess=all +PrivateDevices=yes +PrivateTmp=yes +ProtectSystem=full +ReadWriteDirectories=/opt/air +#ProtectHome=yes +NoNewPrivileges=yes + + +[Install] +WantedBy=multi-user.target diff --git a/main.py b/main.py new file mode 100644 index 0000000..452be6d --- /dev/null +++ b/main.py @@ -0,0 +1,49 @@ +from mirobo.airpurifier import AirPurifier, OperationMode, LedBrightness +from collections import namedtuple +from datetime import datetime +import time +from influxdb import InfluxDBClient +import json +from systemd import daemon + + +class Message(namedtuple('Message', ('measurement', 'time', 'tags', 'fields'))): + __slots__ = () + + +class Atmosphere(namedtuple('Atmosphere', ('purifier_activated', 'purifier_fan_rpm', 'temperature', 'aqi', 'humidity'))): + __slots__ = () + + def __new__(cls, status): + return super(Atmosphere, cls).__new__(cls, + status.power == 'on', + int(status.motor_speed), + float(status.temperature), + int(status.aqi), + status.humidity / 100.0 + ) + + +def getStat(dev): + while True: + time.sleep(1) + yield time.time(), Atmosphere(dev.status()) + + +if __name__ == '__main__': + try: + metric = InfluxDBClient('db', database='core', + use_udp=True, udp_port=8089) + + dev = AirPurifier('10.0.1.13', '7e538c9fb31f6f6bfcc3354f46c421ca') + tags = { + 'source': 'mi_air_2', + 'position': 'center_room', + } + daemon.notify('READY=1') + for at, info in getStat(dev): + message = Message('air', int(at*1000000000), tags, info._asdict()) + print(json.dumps([message._asdict()])) + metric.write_points([message._asdict()]) + finally: + daemon.notify('STOPPING=1') diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2d387f4 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +https://github.com/rytilahti/python-mirobo/archive/master.zip +influxdb +git+https://github.com/systemd/python-systemd.git#egg=systemd \ No newline at end of file