From 45aead62ad7b6c139a7ce508423ea8143a0c0582 Mon Sep 17 00:00:00 2001 From: Ondrej Mikle Date: Sun, 22 Jul 2018 19:19:58 +0200 Subject: [PATCH] SpaceAPI sftp uploader --- brmdoor_nfc.config.sample | 2 +- brmdoor_nfc_daemon.py | 31 +++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/brmdoor_nfc.config.sample b/brmdoor_nfc.config.sample index 761de85..233c1af 100644 --- a/brmdoor_nfc.config.sample +++ b/brmdoor_nfc.config.sample @@ -88,6 +88,6 @@ spaceapi_sftp_host = some.fqdn.com spaceapi_sftp_port = 22 spaceapi_sftp_username = brmdoor-web spaceapi_sftp_key = /path/to/key -spaceapi_dest_filen= /path/on/target/host/to/dest/file +spaceapi_dest_file = /path/on/target/host/to/dest/file spaceapi_template_file = /full/path/to/spaceapi_template.json diff --git a/brmdoor_nfc_daemon.py b/brmdoor_nfc_daemon.py index 57f2ba1..61b5635 100755 --- a/brmdoor_nfc_daemon.py +++ b/brmdoor_nfc_daemon.py @@ -1,6 +1,7 @@ #!/usr/bin/env python2 import sys +import os.path import logging import logging.handlers import time @@ -9,6 +10,8 @@ import threading import irc.client import ssl import Queue +import json +import tempfile from binascii import hexlify from functools import partial @@ -77,7 +80,7 @@ class BrmdoorConfig(object): self.sftpPort= self.config.getint("open_switch", "spaceapi_sftp_port") self.sftpUsername = self.config.get("open_switch", "spaceapi_sftp_username") self.sftpKey = self.config.get("open_switch", "spaceapi_sftp_key") - self.sftpDestFile = self.config.get("open_switch", "spaceapi_sftp_key") + self.sftpDestFile = self.config.get("open_switch", "spaceapi_dest_file") self.sftpTemplateFile = self.config.get("open_switch", "spaceapi_template_file") def convertLoglevel(self, levelString): @@ -369,7 +372,31 @@ class SpaceAPIUploader(object): """ def __init__(self, config): - pass + """ Create uploader with store settings, not yet connected.""" + self.config = config + + def upload(self, isOpen): + """ + Upload status via SFTP. Current timestamp will be added as last change. + + :param isOpen - whether space is opened. + :raises paramiko.ssh_exception.SSHException when upload fails (timeout, can't connect, host key mismatch...) + """ + import pysftp + dirname, targetFname = os.path.split(self.config.sftpDestFile) + + spaceApiJson = json.load(file("spaceapi_template.json")) + spaceApiJson["state"] = {"open": True, "lastchange": time.time()} + + with tempfile.NamedTemporaryFile() as tf: + json.dump(spaceApiJson, tf) + localFilename = tf.name + with pysftp.Connection(self.config.sftpHost, username=self.config.sftpUsername, port=self.config.sftpPort, + private_key=self.config.sftpKey) as sftp: + sftp.timeout = 30 + with sftp.cd(dirname): + sftp.put(localFilename, remotepath=targetFname) + class OpenSwitchThread(threading.Thread): """