Beispiel #1
0
def create_app_from_server(apps, info):
    global temp_id
    lock.acquire()
    for appliance in apps:
        if appliance.get_id() == info["id"]:
            print("duplicate ids of appliances.")
            lock.release()
            return -1
    app = Appliance(info["id"], info["name"], info["voltage"], info["current"])
    apps.append(app)
    save_apps(apps)
    lock.release()
    return 0
Beispiel #2
0
def get_apps(apps):
    global temp_id
    with open(file_name, 'r') as file:
        info = file.readlines()
        for line in info:
            line = eval(line)
            if line[0] == 0:
                app = ArduinoLED(line[0], ser, line[1], line[2], line[3],
                                 line[4])
            else:
                app = Appliance(line[0], line[1], line[2], line[3], line[4])
            temp_id = line[0] + 1
            apps.append(app)
Beispiel #3
0
def create(appliance):
    appliance = Appliance(appliance.get("pin", None),
                          appliance.get("label", None))
    if PinList.has_value(
            appliance.get_pin()) and appliance.get_label() is not None:
        APPLIANCES.append(appliance)
        return appliance.__dict__, 201
    else:
        abort(406, "Unable to save")
Beispiel #4
0
def create_app(apps, info):
    global temp_id
    lock.acquire()
    for appliance in apps:
        if appliance.get_name() == info["name"]:
            print("duplicate name of appliances.")
            lock.release()
            return -1
    if "id" not in info.keys():
        info["id"] = temp_id
    app = Appliance(info["id"], info["name"], info["voltage"], info["current"])
    temp_id += 1
    apps.append(app)
    save_apps(apps)
    lock.release()
    return 0
Beispiel #5
0
 async def get_appliance(self, app_id):
     status, app, err = await self.__app_db.get_appliance(app_id)
     if status != 200:
         return status, app, err
     app = Appliance(**app)
     status, app.containers, err = await self.__contr_mgr.get_containers(
         appliance=app_id)
     if app.data_persistence:
         _, local_vols, _ = await self.__vol_mgr.get_local_volumes(
             appliance=app_id)
         _, global_vols, _ = await self.__vol_mgr.get_global_volumes_by_appliance(
             app_id)
         app.data_persistence.volumes = local_vols + global_vols
     if len(app.containers) == 0 \
         and (not app.data_persistence or len(app.data_persistence.volumes) == 0):
         await self.__app_db.delete_appliance(app_id)
         return 404, None, "Appliance '%s' is not found" % app_id
     return 200, app, None
# print(json.dumps(devices, indent=2))

snapshot = {}
# Gather config snapshot of each device
for i in range(numDevices):
    deviceInfo = devices[i]
    deviceType = deviceInfo['model'][0:2]
    # Call appropriate function depending on device type
    if deviceType == "MR":
        device = Wireless(deviceInfo['serial'], networkID, apiKey)
        identifier = "MR:" + deviceInfo['serial']
        snapshot[identifier] = device.collectInfo()
    elif deviceType == "MS":
        device = Switch(deviceInfo['serial'], networkID, apiKey)
        identifier = "MS:" + deviceInfo['serial']
        snapshot[identifier] = device.collectInfo()
    elif deviceType == "MX":
        device = Appliance(deviceInfo['serial'], networkID, orgID, apiKey)
        identifier = "MX:" + deviceInfo['serial']
        snapshot[identifier] = device.collectInfo()

device = TotalNetwork(networkID, apiKey)
identifier = "Network"
#snapshot = {}
snapshot[identifier] = device.collectInfo()

with open("snapshot.json", "w") as outfile:
    json.dump(snapshot, outfile, indent=2)

# end of create snapshot code
Beispiel #7
0
from appliance import Appliance
from random import choice

app = Appliance('heater')
print app.upgrade('BSI172E')
Beispiel #8
0
import tkinter as tk
from tkinter import ttk
from script import Database
from appliance import Appliance
from main import Root
import tkinter.messagebox
import os
import datetime
import  random

database = Database()
appliance = Appliance()

date= datetime.datetime.now().date()
LARGE_FONT=(" verdana", 12)
class Energyaudit(tk.Tk):

    def __init__(self, *args, **kwargs):
        super(Energyaudit, self, *args, **kwargs).__init__()
        tk.Tk.iconbitmap(self, default="energyaudit.ico")
        tk.Tk.title(self,string="ENERGY AUDIT")
        container = tk.Frame(self)
        container.grid(row=0, column=0)
        container.grid_rowconfigure(0,weight=1)
        container.grid_columnconfigure( 0, weight=1)

        self.frames = {}

        for F in (StartPage, LoginPage, RegisterPage, Main, Sizing, Solarmodule, Battery, Chargecontroller, Inverter, Cost):
            frame = F(container,self)
            self.frames[F] = frame
Beispiel #9
0
from pinlist import PinList
from appliance import Appliance

# 3rd party modules
from flask import make_response, abort
import time
import json

switch1 = Appliance(4, "Lamp")
switch = Appliance(2, "Pump")

APPLIANCES = [switch, switch1]


def read_all():
    """
    This function responds to a request for /api/switches
    with the complete lists of switches
    :return:        json string of list of switches
    """
    print(len(APPLIANCES))
    return {"appliances": list(appliance.__dict__ for appliance in APPLIANCES)}


def read_one(id):
    appliance = findById(id)
    if appliance == False:
        abort(404, "Unable to find record".format(id=id))
    return appliance.__dict__

Beispiel #10
0
organizations = dashboard.organizations.getOrganizations()
networks = dashboard.organizations.getOrganizationNetworks(
    organizations[0]['id'])
networkID = networks[0]['id']
orgID = organizations[0]['id']

data = {}

with open('snapshot.json') as json_file:
    data = json.load(json_file)


for device in data:
    ss = data[device]
    deviceType = device[0:2]
    serial = device[3:]
    if deviceType == "MR":
        device = Wireless(serial, networkID, apiKey, ss)
        device.pushInfo()
    elif deviceType == "MS":
        device = Switch(serial, networkID, apiKey, ss)
        device.pushInfo()
    elif deviceType == "MX":
        device = Appliance(serial, networkID, orgID, apiKey, ss)
        device.pushInfo()
    else:
        dev = TotalNetwork(networkID, apiKey, ss)
        dev.pushInfo()

# end of push code
Beispiel #11
0
    async def create_appliance(self, data):

        vol_mgr = self.__vol_mgr

        def validate_volume_mounts(app, vols_existed, vols_declared):
            all_vols = set(vols_existed) | set(vols_declared)
            vols_to_mount = set([
                pv.src for c in app.containers for pv in c.persistent_volumes
            ])
            return list(vols_to_mount - all_vols)

        async def update_global_volumes(global_vols, app_id):
            for gpv in global_vols:
                gpv.subscribe(app_id)
            resps = await multi(
                [vol_mgr.update_volume(gpv) for gpv in global_vols])
            for status, _, err in resps:
                if status != 200:
                    self.logger.error(err)

        def set_container_volume_scope(contrs, vols):
            vols = {v.id: v for v in vols}
            for c in contrs:
                for pv in c.persistent_volumes:
                    if pv.src in vols:
                        pv.scope = vols[pv.src].scope

        status, app, _ = await self.get_appliance(data['id'])
        if status == 200 and len(app.containers) > 0:
            return 409, None, "Appliance '%s' already exists" % data['id']
        status, app, err = Appliance.parse(data)
        if status != 200:
            self.logger.error(err)
            return status, None, err

        # create persistent volumes if any
        dp = app.data_persistence
        if dp:
            resps = await multi([
                vol_mgr.get_local_volume(app.id, v.id)
                for v in dp.local_volumes
            ] + [vol_mgr.get_global_volume(v.id) for v in dp.global_volumes])
            vols_existed = set(
                [v.id for status, v, _ in resps if status == 200])
            vols_declared = set([v.id for v in dp.volumes])
            invalid_vols = validate_volume_mounts(app, vols_existed,
                                                  vols_declared)
            if len(invalid_vols) > 0:
                await self._clean_up_incomplete_appliance(app.id)
                return 400, None, 'Invalid persistent volume(s): %s' % invalid_vols
            global_vols = [
                v for _, v, _ in resps
                if v and v.scope == volume.VolumeScope.GLOBAL
            ]
            if len(vols_existed) < len(dp.volumes):
                resps = await multi([
                    vol_mgr.create_volume(v.to_save()) for v in dp.volumes
                    if v.id not in vols_existed
                ])
                for status, v, err in resps:
                    if status != 201:
                        self.logger.error(err)
                        await self._clean_up_incomplete_appliance(app.id)
                        return status, None, err
                    if v.scope == volume.VolumeScope.GLOBAL:
                        global_vols += v,
            await update_global_volumes(global_vols, app.id)
            set_container_volume_scope(app.containers, dp.volumes)

        # create containers
        resps = await multi([
            self.__contr_mgr.create_container(c.to_save())
            for c in app.containers
        ])
        for status, _, err in resps:
            if status != 201:
                self.logger.error(err)
                await self._clean_up_incomplete_appliance(app.id)
                return status, None, err

        status, _, err = await self.save_appliance(app)
        if status != 200:
            self.logger.error(err)
            await self._clean_up_incomplete_appliance(app.id)
            return status, None, err
        scheduler = self._get_scheduler(app.scheduler)
        self.logger.info('Appliance %s uses %s' %
                         (app.id, scheduler.__class__.__name__))
        ApplianceScheduleExecutor(app.id, scheduler).start()
        return 201, app, None
Beispiel #12
0
 async def get_appliances(self, **filters):
     return 200, [
         Appliance(**app) async for app in self.__app_col.find(filters)
     ], None