1
0
Fork 0

air initial script added

This commit is contained in:
Sangbum Kim 2017-09-24 23:28:38 +09:00
commit 34574ad57a
5 changed files with 254 additions and 0 deletions

139
.gitignore vendored Normal file
View File

@ -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

38
PKGBUILD Normal file
View File

@ -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"
}

25
air.service Normal file
View File

@ -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

49
main.py Normal file
View File

@ -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')

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
https://github.com/rytilahti/python-mirobo/archive/master.zip
influxdb
git+https://github.com/systemd/python-systemd.git#egg=systemd