Пример #1
0
    def __init__(self, buffer_size=60, batch_size=1440, n_clusters=81):

        # User setting
        self.user = "******"
        self.flora = "carnation"

        # Database manager
        self.db_manager = DbManager()

        # Async lock
        self.lock = Lock()

        # MQTT client
        self.mqtt = mqtt.Messenger(self.read_input)

        # ML Setting
        self.n_clusters = n_clusters
        self.batch_size = batch_size
        self.row_count = 0
        self.kmeans = self._load_kmeans_model()
        self.mean_setting = self._obtain_optimal_condition()

        # Raw data temp storage
        self.buffer_size = buffer_size
        self.buffer = np.zeros(shape=(self.buffer_size, 4))
        self.buffer_count = 0
        self.cur_min = None  # current minute count
        self.cur_hour = None

        # Average data temp storage
        self.average = [[], [], [], []]

        # Processed data temp storage
        self.data = np.zeros(shape=(self.batch_size, 4))
        self.label = np.ndarray(shape=(self.batch_size, ), dtype=str)
Пример #2
0
 def runProgram(self):
     # try:
     self.output = Output(sleepChar=self.sleepChar,
                          sleepLine=self.sleepLine)
     self.write = self.output.write
     self.writeln = self.output.writeln
     self.canWrite = self.output.canWrite
     self.printHello()
     if not self.tryConnectToRemoteServer():
         print "Closing.. bye."
     self.dbManager = DbManager(self.output)
     self.tryConnectDB()
     self.mainBOT()
     self.newEntry()
Пример #3
0
class Server(object):
    def __init__(self, buffer_size=60, batch_size=1440, n_clusters=81):

        # User setting
        self.user = "******"
        self.flora = "carnation"

        # Database manager
        self.db_manager = DbManager()

        # Async lock
        self.lock = Lock()

        # MQTT client
        self.mqtt = mqtt.Messenger(self.read_input)

        # ML Setting
        self.n_clusters = n_clusters
        self.batch_size = batch_size
        self.row_count = 0
        self.kmeans = self._load_kmeans_model()
        self.mean_setting = self._obtain_optimal_condition()

        # Raw data temp storage
        self.buffer_size = buffer_size
        self.buffer = np.zeros(shape=(self.buffer_size, 4))
        self.buffer_count = 0
        self.cur_min = None  # current minute count
        self.cur_hour = None

        # Average data temp storage
        self.average = [[], [], [], []]

        # Processed data temp storage
        self.data = np.zeros(shape=(self.batch_size, 4))
        self.label = np.ndarray(shape=(self.batch_size, ), dtype=str)

    def _obtain_optimal_condition(self):
        optimal = self.db_manager.get_optimal_setting(self.flora)
        return [
            (optimal["co2"]["min"] + optimal["co2"]["max"]) / 2,
            (optimal["tvoc"]["min"] + optimal["tvoc"]["max"]) / 2,
            (optimal["humidity"]["min"] + optimal["humidity"]["max"]) / 2,
            (optimal["temperature"]["min"] + optimal["temperature"]["max"]) /
            2,
        ]

    def _load_kmeans_model(self):
        '''load or initialise training model'''
        return MiniBatchKMeansTrainer(n_clusters=self.n_clusters,
                                      resolution=2,
                                      batch_size=self.batch_size)

    def _find_average_setting(self):
        ''' find the average of current cluster centroids '''
        return np.average(self.kmeans.kmeans.cluster_centers_, axis=0)

    def read_input(self, topic, message):
        '''processes subscribed message'''
        if topic == self.mqtt.topic + "/history_request":  # if history data is requested
            history = self.db_manager.get_user_history(
                self.user, self.flora)  # obtain history from database
            if history == None:
                self.mqtt.client.publish(self.mqtt.topic + "/history",
                                         "No history")
            else:
                # send history data to mobile application
                if len(history) < 20:
                    for j in range(0, len(history)):
                        self.mqtt.client.publish(
                            self.mqtt.topic + "/history",
                            json.dumps(history[j]["value"]))
                else:
                    for j in range(0, 20):
                        self.mqtt.client.publish(
                            self.mqtt.topic + "/history",
                            json.dumps(history[j]["value"]))

        elif topic == self.mqtt.topic + "/raw_data":  # if raw data comes from device
            corrupted_data = False
            try:
                # try to parse the json data to see whether there is error
                co2 = message["co2"]
                organic = message["organic"]
                humidity = message["humidity"]
                temperature = message["temperature"]
                time = message["time"]
            except KeyError:
                corrupted_data = True  # if there is error, there is no further processing
            if not corrupted_data:
                self.check_control(co2, organic, humidity, temperature)
                date_time = datetime.datetime.strptime(
                    message["time"], "%Y-%m-%d %H:%M:%S")  # parse time stamp
                if self.cur_min == None:
                    self.cur_min = date_time.minute
                    self.cur_hour = date_time.hour
                if self.cur_min == date_time.minute:
                    # if time stamp has not progressed into a new minute
                    self.lock.acquire()
                    try:
                        # store input data into buffer
                        self.update_buffer(co2, organic, humidity, temperature)
                        self.buffer_count = self.buffer_count + 1
                    except Exception as e:
                        print(e)
                    finally:
                        self.lock.release()
                else:
                    # if a new minute has come
                    self.lock.acquire()
                    try:
                        check_valid = self.last_nonzero(
                            self.buffer, axis=0,
                            invalid_val=-1)  # find the length of buffered data
                        average = self.buffer[:check_valid[0]].mean(
                            axis=0)  # compute average from buffered data
                        msg_dict = {
                            "co2": co2,
                            "organic": organic,
                            "humidity": humidity,
                            "temperature": temperature,
                            "time": time
                        }
                        self.average[co2_index].append(co2)
                        self.average[organic_index].append(organic)
                        self.average[humidity_index].append(humidity)
                        self.average[temperature_index].append(temperature)
                        self.average.append(msg_dict)
                        self.mqtt.client.publish(
                            self.mqtt.topic + "/average", json.dumps(msg_dict)
                        )  # send average data to mobile device for display
                        self.update_data(average, date_time)

                        # actual_implementation
                        if self.cur_hour != date_time.hour:
                            # if a new hour has come, averaged history is stored to database
                            self.write_to_database(date_time)

                    except Exception as e:
                        print(e)
                    finally:

                        # clear buffer
                        self.row_count = self.row_count + 1
                        self.buffer_count = 0
                        self.buffer.fill(0)
                        self.cur_min = date_time.minute
                        self.cur_hour = date_time.hour
                        self.lock.release()

                    if self.row_count >= self.batch_size:
                        # if number of averages equal to batch size, start async training
                        self.row_count = 0
                        thread = Thread(target=self.train)
                        thread.run()

    def check_control(self, co2, organic, humidity, temperature):
        ''' compare the centroid of current input readings with optimal centroid 
            and write control signal accordingly '''

        # use current input for prediction
        data = np.array([[co2, organic, humidity, temperature]])
        data = self.kmeans.kmeans.predict(data)
        decision = []
        update_required = False

        # if the indicator value of predicted centroid is different from that of optimal
        # control is needed
        for i in range(0, 4):

            if data[0, i] < self.mean_setting[i]:
                decision.append(-1)
                update_required = True
            elif data[0, i] > self.mean_setting[i]:
                decision.append(1)
                update_required = True
            else:
                decision.append(0)

        if update_required:
            # if control is needed, notify client device for control
            self.mqtt.client.publish(self.mqtt.topic + "/control",
                                     json.dumps(decision))

    def write_to_database(self, date_time):
        ''' update history to database '''
        try:
            # try to find out whether existing history is available
            # if not, create new history object
            history = self.db_manager.get_user_history(self.user, self.flora)
        except Exception:
            history = []

        # format new entry
        entry = {
            "co2":
            round(
                sum(self.average[co2_index]) // len(self.average[co2_index]),
                0),
            "organic":
            round(
                sum(self.average[organic_index]) //
                len(self.average[organic_index]), 0),
            "humidity":
            round(
                sum(self.average[humidity_index]) /
                len(self.average[humidity_index]), 1),
            "temperature":
            round(
                sum(self.average[temperature_index]) /
                len(self.average[temperature_index]), 1),
            "time":
            date_time.strftime("%Y-%m-%d %H"),
        }
        update = {"value": entry}

        # insert new history to the front of past history
        history.insert(0, update)
        self.db_manager.write_user_history(self.user, self.flora, history)

    def update_buffer(self, co2, organic, humidity, temperature):
        '''update buffered data for per-second readings'''
        self.buffer[self.buffer_count, co2_index] = co2
        self.buffer[self.buffer_count, organic_index] = organic
        self.buffer[self.buffer_count, humidity_index] = humidity
        self.buffer[self.buffer_count, temperature_index] = temperature

    def update_data(self, data, date_time):
        '''update average of readings per minute to prepare for classification'''
        self.data[self.row_count, co2_index] = data[co2_index]
        self.data[self.row_count, organic_index] = data[organic_index]
        self.data[self.row_count, humidity_index] = data[humidity_index]
        self.data[self.row_count, temperature_index] = data[temperature_index]
        self.label[self.row_count] = date_time.strftime("%Y-%m-%d %H:%M")

    def last_nonzero(self, arr, axis=0, invalid_val=-1):
        '''find the index of the last nonzero element in nparray for computation of average'''
        # masking array for non-zero contents
        mask = arr != 0
        val = arr.shape[axis] - np.flip(mask, axis=axis).argmax(axis=axis) - 1
        return np.where(
            mask.any(axis=axis), val,
            invalid_val)  # return the position of the last non_zero element

    def start_server(self):
        ''' start server '''
        self.mqtt.client_init()
        self.mqtt.run()

    def train(self):
        ''' asynchronous training '''
        self.lock.acquire()
        try:
            ''' deep copy current raw data '''
            raw = self.data.copy()
        finally:
            self.lock.release()
        self.kmeans.partial_fit(raw)
        self.mean_setting = self._find_average_setting()
Пример #4
0
from aiogram import Bot, Dispatcher, executor, types
from aiogram.utils.markdown import bold, code, italic, text
from aiogram.types import ParseMode
from os import remove as f_remove

from dbManager import DbManager, check_course_group
from actions import Actions

from telethon import TelegramClient, sync
from telethon.tl.types import InputMessagesFilterDocument

bot = Bot(token=config.API_TOKEN)
dp = Dispatcher(bot)

db = DbManager('db.db')
logging.basicConfig(
    level=logging.INFO,
    format=u'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
act = Actions()
AGENT_ID = config.AGENT_ID

# telethon settings
entity = config.entity
api_id = config.api_id
api_hash = config.api_hash
phone = config.phone
chat_id = config.chat_id
client = TelegramClient(entity, api_id, api_hash)

Пример #5
0
        session_tmp.close()


# Record the message from each running device
mqtt_client.set_uplink_callback(uplink_callback)


mqtt_client.start()

mqtt_client.connect()

# Manage the loop connexion to the broker
# MQTT client is based on paho.mqtt.client: We add loop_forever method in ttn lib (ttnmqtt.py) which wraps paho.mqtt.client.loop_forever()
# mqtt_client.loop_forever()

database_manager = DbManager()

print("get 5 last messages: ")
result = database_manager.get_messages()
for row in result:
    print("id: ", row.id, "name device:", row.dev_id, "payload",
          row.payload, "timestamp: ", row.release_date)

print("get messages for today: ")
result = database_manager.get_messages_from(date.today())
for row in result:
    print("id: ", row.id, "name device:", row.dev_id, "payload",
          row.payload, "timestamp: ", row.release_date)

print("get messages between two day: ")
result = database_manager.get_messages_between(
Пример #6
0
import os
import config
import asyncio
import logging
import hashlib
from aiogram import Bot

from dbManager import DbManager

bot = Bot(token=config.API_TOKEN)
MY_ID = 231905851
db = DbManager('db.db')

logging.basicConfig(
    format=
    u'%(filename)s [ LINE:%(lineno)+3s ]#%(levelname)+8s [%(asctime)s]  %(message)s',
    level=logging.INFO)

DOCS_PATH = "G:\\Проектная деятельность\\upload2"
course = 2
group = 9
semester = 2


async def uploadMediaFiles():
    for discipline_name in os.listdir(DOCS_PATH):
        for dir_name in os.listdir(os.path.join(DOCS_PATH, discipline_name)):
            for fname in os.listdir(
                    os.path.join(DOCS_PATH, discipline_name, dir_name)):
                if db.file_exists(fname, discipline_name, dir_name):
                    logging.info(
Пример #7
0
class SBProg:

    timers = {}
    timersTime = {}
    startSessionTime = ""

    PATH_TO_SERVER = Settings.PATH_TO_SERVER
    RECEIVER = Settings.RECEIVER

    MAX_NUM_ERRORS = 5

    def __init__(self, sleepChar=None, sleepLine=None, isTest=False):
        self.isTest = isTest
        self.sleepChar = sleepChar
        self.sleepLine = sleepLine

    def runProgram(self):
        # try:
        self.output = Output(sleepChar=self.sleepChar,
                             sleepLine=self.sleepLine)
        self.write = self.output.write
        self.writeln = self.output.writeln
        self.canWrite = self.output.canWrite
        self.printHello()
        if not self.tryConnectToRemoteServer():
            print "Closing.. bye."
        self.dbManager = DbManager(self.output)
        self.tryConnectDB()
        self.mainBOT()
        self.newEntry()
        # except Exception, e:
        # 	self.write("Global Error.\n")
        # 	self.write(str(e) + "\n")

    def printHello(self):
        print """$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n\
$$$$$$$$$$$$$$$$$$$$$$$$$$   WELCOME SOCIAL BOT   $$$$$$$$$$$$$$$$$$$$$$$$$$\n\
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n"""

    def tryConnectToRemoteServer(self):
        "Look for the remote server"
        sys.stdout.write("Trying connecting to server online.. ")
        resp = self.post_request({"action": "server_alive"})
        if resp != None:
            print "ok"
            return True
        else:
            return False

    def tryConnectDB(self):
        "Look for database"
        sys.stdout.write("Look for database (" + self.dbManager.dbName +
                         ").. ")
        if (not os.path.exists(self.dbManager.dbName)):
            print "not in path"
            self.dbManager.initDB()
        else:
            print "already in path!"
            self.dbManager.initDB()

    def mainBOT(self):
        print "Initializing the BOT:"
        self.startSessionTime = datetime.datetime.fromtimestamp(
            float(int(time.time()))).strftime('%H:%M:%S %d/%m')
        print "Get data from online server:"
        self.accounts = Accounts(self)
        print "Get data from online server complete!"
        self.updateStatistics(firstTime=True)
        if self.isTest:
            self.testConnectedBlogs()
        print "Initialization finished! Run the blogs!"

    def newEntry(self):
        while True:
            entry = raw_input("\n" + self.output.startSimble)
            if entry in ["quit", "exit"]:
                self.closing_operations()
                break
            elif entry in ["help", "info"]:
                self.printHelpCmd()
            elif (entry != "") and (entry.split()[0] in ["clear", "Clear"]):
                self.accounts.clearDB4blog(entry)
            elif (entry != "") and (entry.split()[0] in ["log", "Log"]):
                self.accounts.log(entry)
            elif (entry != "") and (entry.split()[0]
                                    in ["changeSpeed", "speed", "cs"]):
                self.output.changeSpeed(entry)
            elif (entry != "") and (entry.split()[0] in ["run", "Run"]):
                # self.accounts.runBlogs(entry)
                print "Running Blogs!"
                t = threading.Thread(target=self.accounts.runBlogs,
                                     args=(entry, )).start()
            elif (entry != "") and (entry.split()[0] in ["stop", "Stop"]):
                self.accounts.stopBlogs(entry)
            elif (entry != "") and (entry.split()[0] == "copy"):
                self.copyBlog(entry)
            else:
                print "Unknown command '" + entry + "'"

    def logResults(self):
        self.canWrite = True
        print "\nLogging results.."
        while not raw_input() in ['q', 'Q']:
            pass
        self.canWrite = False

    def printHelpCmd(self):
        "Print list of available commands"
        prevCanWrite = self.canWrite
        self.canWrite = True
        print "List of commands:"
        print "   - 'help': for list of instructions"
        print "   - 'clean': clean directory"
        print "   - 'f': fast print"
        print "   - 'changeSpeed': for changing printing text speed"
        print "   - 'copy blog_to_copy my_blog': for copy an entire blog"
        print "   - 'run': for run a/all blog(s)"
        print "   - 'stop': for stop a/all blog(s)"
        print "   - 'quit': for quit"
        prevCanWrite = self.canWrite

    def closing_operations(self):
        self.canWrite = True
        print "Terminating program."
        self.accounts.closingOperations()
        try:
            self.timers["update"].cancel()
        except KeyError, msg:
            pass
        self.updateStatistics()
        resp = self.post_request({
            "action":
            "closing_operations",
            "stop_session_time":
            datetime.datetime.fromtimestamp(float(int(
                time.time()))).strftime('%H:%M:%S %d/%m')
        })
        print "   Bye!\n"
Пример #8
0
# -*- coding: utf-8 -*-
import falcon
import os
from receipe import Receipe
from dbManager import DbManager
from dotenv import load_dotenv

#Creamos una instancia de la API proporcionada por el framework
#falcon, ejecutable con WSGI.
api = falcon.API()
#Leemos las variables de entorno necesarias para establecer la
#conexión con la base de datos.
load_dotenv()
#Creamos una instancia del manejador de la base de datos, para
#la BD y colección indicadas como parámetros.
dbReceipesManager = DbManager(os.getenv("DB_RECEIPES"), 'ReceipesDB',
                              'receipes')
#Creamos la instancia del recurso para la gestión de recetas, pasándole
#como parámetro la instancia del manejador de la BD con el objetivo
#de respetar la 'single source of truth' con la inyección de dependencias.
receipes = Receipe(dbReceipesManager)
#Definimos la ruta 'receipes', sobre la que se ejecutarán los request
#definidos en el recurso de la clase 'Receipe' creado.
api.add_route('/receipes', receipes)
Пример #9
0
            return book

    ''' Update a given book '''

    @books_ns.doc('Update_book')
    @books_ns.marshal_with(bookModel)
    def put(self, id):
        return daoBook.update(id, api.payload)

    ''' Delete a given book '''

    @books_ns.doc('Delete_book')
    def delete(self, id):
        daoBook.delete(id)


if __name__ == '__main__':
    arg = parser.parse_args()
    if arg.test:
        print('test mode')
    else:
        print('no test')

    print("Running with dbName = %s" % (arg.dbName))

    managerDb = DbManager(arg.dbName)
    initDao()
    if arg.test:
        managerDb.createDb('test.sql')
    app.run(host='127.0.0.1', debug=True, port=5000)