Ejemplo n.º 1
0
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.path = os.path.split(sys.argv[0])[0]
        self.data_dir = os.path.join(self.path, DATADIR)
        self.cfg = config.Load(os.path.join(self.data_dir, CONFIG))
        self.grid(sticky=tk.NSEW)
        self.idle_calls = 0
        self.charts = []
        self.focus_chart = None
        self.q = multiprocessing.Queue(maxsize=QUEUE_SIZE)
        self.status_q = multiprocessing.Queue(0)
        self.next_color = 0
        self.running = 1
        self.proc = None
        self.state = App.SETUP
        self.parent_conn, self.child_conn = multiprocessing.Pipe()
        self.man_span = 0
        self.scaling = config.ScaleInfo()
        self.spans = [0] * SPAN_MAX  # map range slider to range value
        self.time_diff = time.time() - utils.timer()  # see module docstring
        top = self.winfo_toplevel()
        top.geometry(self.cfg.geometry)
        try:
            top.wm_iconbitmap('electroscope.ico')
        except:
            pass
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.scale_min = (SAMPLESCALE_MIN, 0, FREQSCALE_MIN)
        self.scale_max = (SAMPLESCALE_MAX, 0, FREQSCALE_MAX)
        self.plot_sizes = [SAMPLESCALE_MAX, 0, FREQSCALE_MAX]

        # the layout is 3 rows:
        self.rowconfigure(0, weight=1)
        self.rowconfigure(1, weight=1000)
        self.rowconfigure(2, weight=1)
        # row 1 is constructed as 2 columns, with all the expansion
        # assigned to the graphs
        self.columnconfigure(0, weight=1000)
        self.columnconfigure(1, weight=1)

        # construct the widgets - note that these member functions know where
        # they belong in the grid, so they are sensitive to grid layout changes
        self.create_menu(self)
        self.create_controls(self)
        self.create_graphs(self)
        self.create_button_row(self)

        # connect event handlers
        top.protocol("WM_DELETE_WINDOW", self.on_quit)
        pubsub.subscribe('status', self.set_status)
        pubsub.subscribe('focus2', self.on_focus)

        # start a process to receive and buffer data
        self.start_source(self.cfg.sources)

        self.on_timer()
        # start the code that processes and displays the data
        self.after_idle(self.on_plot_type)
        self.after_idle(self.read_queue)
Ejemplo n.º 2
0
    def __init__(self, Loader_Log):

        # Try to load the network config file
        # if we cant start the cli
        try:
            with open('/conf/network.json', 'r') as f:
                Network_Config = ujson.load(f)
        # start the cli on Error
        except:
            print("Starting Dobby Loader - Version: " + str(Version))
            print("Missing network config starting CLI")
            # start the cli, it will trigger a reboot after getting a wifi config
            # self.CLI()
            import cli
            cli.Run(self)

        # Default log level = 1 aka Info
        self.Log_Level = 1
        # Log queue to hold logs untill we can pass it on to base
        self.Log_Queue = Loader_Log

        # Log relies on this to check if we need to blink on errors
        # So needs to be set before log is used the first time
        self.Indicator = None

        # Create self.wlan0 reference
        self.wlan0 = network.WLAN(network.STA_IF)
        # Activate self.wlan0 regardless if it is no not
        self.wlan0.active(True)

        # System header added in front of log messages
        self.System_Header = "/Unconfigured"

        # Var to hold stat of MQTT Connection
        self.MQTT_State = 'init'

        # Used to download modules aka mqtt broker
        self.Server = Network_Config["MQTT Broker"]

        self.Hostname = Network_Config["Hostname"]

        # Log start of script
        self.Log(1, "System", "Starting Dobby Base - Version: " + str(Version))

        # Create MQTT Object
        import umqttsimple as MQTT
        self.MQTT = MQTT

        # Import config
        import config as dConfig

        # Load device config
        Device_Config = dConfig.Load('device')

        # Change log level if set in config
        self.Log_Level = Device_Config.get('Log Level', 1)
        # Change system header
        self.System_Header = Device_Config['System Header']

        ## Holds all loaded modules
        self.Modules = {}
        # Holds loaded System Modules like WirePusher if enabeled
        self.Sys_Modules = {}

        # List of push messages that failed to send
        # we retry when online again
        self.Push_Queue = []

        # Change CPU frequancy if requested
        # if Config.get('CPU_16', False) == True:
        #     machine.freq(160000000)
        #     self.Log(1, 'System', 'CPU frequancy set to: 16MHz')

        # MQTT Connection status
        self.MQTT_State = 'init'
        self.MQTT_Reconnect_At = 0
        self.MQTT_Subscribe_To = []
        self.MQTT_Client = None
        self.MQTT_Ping_At = utime.ticks_ms()

        # Subscribe to Commands topic
        self.MQTT_Subscribe(self.Peripherals_Topic("Commands"))

        # Var to indicate of we have published the ip we got when wifi connected
        self.Published_Boot = False

        # ++++++++++++++++++++++++++++++++++++++++ MQTT ++++++++++++++++++++++++++++++++++++++++
        # Remember to add something raondom after the hostname so the borker see a new connecton
        # Check if we got a user and pass for mqtt
        # Generate Unique Post Hostname
        Post_Hostname = str(uos.urandom(1)[0] % 1000)
        # Log event
        self.Log(0, 'MQTT',
                 'Using hostname: ' + self.Hostname + "-" + Post_Hostname)
        # Stores messages so we can act on them in MQTT Loop
        ## List containing Topic and payload
        ## [[<Topic>, <Payload>]]
        self.MQTT_Incomming_Queue = []
        # Create MQTT Client
        self.MQTT_Client = self.MQTT.MQTTClient(
            self.Hostname + "-" + Post_Hostname, self.Server,
            int(Device_Config.get('MQTT Port', 1883)),
            Device_Config.get('MQTT Username', None),
            Device_Config.get('MQTT Password', None))
        # Set last will
        self.MQTT_Client.set_last_will(
            self.System_Header + "/" + self.Hostname + "/Log/Will",
            "Disconnected")

        # try to connect to mqtt
        self.MQTT_Connect()

        # Device_Config is no loger needed so will delete it to free up space
        del Device_Config

        # ++++++++++++++++++++++++++++++++++++++++ Setup peripherals ++++++++++++++++++++++++++++++++++++++++
        # ++++++++++++++++++++++++++++++++++++++++ Setup peripherals ++++++++++++++++++++++++++++++++++++++++
        # ++++++++++++++++++++++++++++++++++++++++ Setup peripherals ++++++++++++++++++++++++++++++++++++++++
        # Variable for Pin Monitor
        # Remember to pass base aka self
        import pinmonitor
        self.Pin_Monitor = pinmonitor.Init(self)

        # Loop over config names in /conf and import matching modules
        ## Get config names
        Config_List = uos.listdir('/conf')
        # Remove device.json since we dont want to use that again
        Config_List.remove('device.json')
        Config_List.remove('network.json')

        ## Loop over names in config
        for Name in Config_List:

            # Strip .json from name
            Name = Name.replace('.json', '')

            # DHT has to be in capital or else it will try to load the micropython module called dht
            if Name == 'dht':
                Name = 'DHT'

            # Import the config
            Config = dConfig.Load(Name)
            Error = None

            # Load config is False if no config is found
            if Config is not False:
                try:
                    # Try to import
                    Module = __import__(Name)
                    # Store objevt in self.Modules
                    # Pass config and get perifical object
                    # Remember to pass Shared aka self so we can log in Button and use dobby variables
                    self.Modules[Name] = Module.Init(self, Config)

                except (AttributeError, TypeError, SyntaxError, KeyError) as e:
                    Error = str(e)
                # Incompatible mpy file
                # except ValueError:
                #     # remove module
                #     uos.remove('/lib/dobby/' + Name.replace('.json', '.mpy'))
                #     # Log event
                #     Error = "Removed Module: " + Name.replace('.json', '') + " due to incompatibility"
                except MemoryError:
                    Error = 'Not enough free memory. Free memory: ' + str(
                        gc.mem_free())
                except self.Error as e:
                    Error = str(e)

                # No errors on import in creation of module object
                else:
                    # Log event
                    self.Log(0, "System", "Module loaded: " + str(Name))

                finally:
                    # Check if Module import ok
                    if Error != None:
                        # remove module from self.Modules if added
                        try:
                            del self.Modules[Name.replace('.json', '')]
                        except:
                            pass
                        # Dont remove config on "Missing module" we are trying to download it when we get wlan0 up
                        if Error.startswith("Missing module: ") != True:
                            # Log event
                            self.Log(
                                4, "System", "Unable to load module: " +
                                str(Name) + " - Error: " + Error)
                            # Remove config file
                            uos.remove("/conf/" + Name + '.json')
                            # Remove config file
                            uos.remove("/lib/" + Name + '.mpy')
                            # Log removal of config file
                            self.Log(
                                1, "System",
                                "Removed config file and module for: " + Name)
            else:
                self.Log(0, 'System', "Invalid config: " + Name)

        # Activate indicator if 'LED' owned by Indicator aka not used for something else
        # on esp32 its IO2
        # on esp8266 its D4
        if self.Pin_Monitor.Get_Owner(self.Pin_Monitor.LED) == 'Indicator':
            # check if indicator is already imported and init
            if self.Sys_Modules.get('indicator', None) == None:
                # Import Indicator
                import indicator
                # Init Indicator and store in Sys_Modules to enable loop
                self.Sys_Modules['indicator'] = indicator.Init(
                    self, {"System": {
                        "Pin": "LED"
                    }})
                # Create the indicator object
                self.Indicator = self.Sys_Modules['indicator'].Peripherals[
                    "System"]
            # indicator already imported to add an object in stead
            else:
                # Create the indicator object
                self.Indicator = self.Sys_Modules['indicator'].Add(
                    "System", {"Pin": "LED"})
                # On add the indicator will be blinked 3 times
                # will use this as boot blink

            # add wifi infication if disconnected
            if self.wlan0.isconnected() == False:
                self.Indicator.Add("WiFi", 4, "0.5s", "0.75s", True)

        else:
            # Log we could not enable indicator led
            self.Log(0, 'System/Indicator',
                     "LED Pin in use, cannot enable system indicator")

        # Remove dconfig again since we are done with it
        del dConfig

        # Boot done message
        self.Log(
            0, 'System', 'Dobby - Initialization compleate - Free memory: ' +
            str(gc.mem_free()))
Ejemplo n.º 3
0
import os
import requests
import telegram
import time
import config
from telegram import Update, Message
from telegram.ext import Updater
from telegram.ext import CommandHandler
import random
import templates

config.Load()

TELEGRAM_TOKEN: str = config.data['telegram']['token']
TELEGRAM_USERS: dict = config.data['telegram']['users']


def add_new_user(id, name, status='user'):
    TELEGRAM_USERS[str(id)] = {
        'id': id,
        'name': name,
        'status': status,
        'mute': False
    }
    config.Save()


def get_user(uid):
    if isinstance(uid, Update):
        uid = uid.message.from_user.id
    uid = str(uid)
Ejemplo n.º 4
0
    def __init__(self, Dobby):

        # Dict to hold config
        Config = {}
        # Try to load network.json to 'Config'
        # if failes create an empthy dict
        try:
            # Try to load config
            Config = dConfig.Load('network')
        except:
            # Create empthy dict on failure
            Config = {}

        # print cli init message
        print("Starting cli")
        print("Please configure:")
        print("   Hostname")
        print("   WiFi SSID")
        print("   WiFi Password")
        print("   MQTT Broker")
        print("")

        # start eternal loop while getting user
        while True:
            try:
                User_Entry = input("Dobby CLI: ")
            except (EOFError, KeyboardInterrupt):
                # If we got interrupted exit
                print("   Leaving CLI")
                sys.exit()

            # Check if the 200 pound gorilla pressed enter without entering a command
            if User_Entry == "":
                continue

            # ---------------------------------------- Help ----------------------------------------
            elif User_Entry.lower() == "help":
                print()
                print("Avalible commands:")
                print()
                print("   boot - boot the device")
                print("   json - past device config as a json string")
                print("   show - shows the loaded config")
                print("   module list - lists all installed modules")
                print("   module delete - delete a installed module")
                print(
                    "   set <config> <to> - sets 'Hostname', 'WiFi SSID', 'WiFi Password', 'MQTT Broker'"
                )
                print()

            # ---------------------------------------- boot ----------------------------------------
            # reboots the device
            # if required settings is give
            # saves config to fs first
            elif User_Entry.lower() == "boot":
                # check if config is ok
                try:
                    dConfig.Check(Config, [
                        'Hostname', 'WiFi SSID', 'WiFi Password', 'MQTT Broker'
                    ])
                except Dobby.Error as e:
                    print("   Missing config: " + str(e))
                    continue

                # save config to file
                if dConfig.Save(Config, 'network') == False:
                    print("   Unable to save config")
                    continue

                # Log event
                print("   Config saved rebooting")
                # Reboot the device
                machine.reset()

            # ---------------------------------------- set ----------------------------------------
            # prints the current config
            elif User_Entry.lower() == "set":

                # List of options user can change
                Config_Options = [
                    'Hostname', 'WiFi SSID', 'WiFi Password', 'MQTT Broker',
                    'Log Level'
                ]

                # print list with number in front
                for i in range(len(Config_Options)):
                    print("   " + str(i) + " - " + str(Config_Options[i]))

                # Get config option number from user
                try:
                    print()
                    print("Press CTRL + C to cancle")
                    print()
                    Selected_Option = input("Please select config to change: ")
                    # Convert selection to int
                    Selected_Option = int(Selected_Option)
                    # check config exists in list
                    Config_Options[Selected_Option]
                except (EOFError, KeyboardInterrupt):
                    print("   canceled")
                except (ValueError, IndexError):
                    print("   invalid config selected: " +
                          str(Selected_Option))
                else:

                    # Get config option number from user
                    try:
                        print()
                        New_Value = input("Please enter new value for " +
                                          Config_Options[Selected_Option] +
                                          ": ")
                    except (EOFError, KeyboardInterrupt):
                        print("   canceled")
                    else:
                        # change config value
                        Config[Config_Options[Selected_Option]] = New_Value
                        # Log event
                        print("   Config: " +
                              str(Config_Options[Selected_Option]) +
                              " set to: " + str(New_Value))

            # ---------------------------------------- show ----------------------------------------
            # prints the current config
            elif User_Entry.lower() == "show":
                print("Current config:")
                for Key in [
                        'Hostname', 'WiFi SSID', 'WiFi Password',
                        'MQTT Broker', 'Log Level'
                ]:
                    print("   " + Key + ": " +
                          Config.get(Key, "Not configured"))

            # ---------------------------------------- json ----------------------------------------
            # past device config as a json string
            elif User_Entry.lower() == "json":
                # Get json string from user
                json_Config = input("Please paste device config json string: ")

                # Try to parse json
                try:
                    # parse json to config dict
                    json_Config = ujson.loads(json_Config)
                # Error in json string
                except ValueError:
                    print()
                    print("   Invalid json string")
                    print()
                    continue
                # Json loaded ok, check if we got the needed config
                else:
                    Check_List = [
                        'Hostname', 'WiFi SSID', 'WiFi Password', 'MQTT Broker'
                    ]
                    try:
                        dConfig.Check(json_Config, Check_List)
                    except Dobby.Error as e:
                        # Log error
                        print("   Missing config entries: " + str(e))
                        continue
                    # if we got a large config for some reason only take what we need aka keys in the check list
                    for Key in Check_List:
                        # Save value from json_Config in Config var
                        Config[Key] = json_Config[Key]
                    # Log Event
                    print("   json config ok")
                    continue

            # ---------------------------------------- module list ----------------------------------------
            # lists all modules
            elif User_Entry.lower() == "module list":
                # print to give distance to above
                print()
                # get list of libs
                Lib_List = uos.listdir('lib')
                # print list with number in front
                for i in range(len(Lib_List)):
                    print("   " + str(i) + " - " + str(Lib_List[i]))
                # Print to give space to command below
                print()

            # ---------------------------------------- module delete ----------------------------------------
            # lists all modules with number in front and lets the user deside what to delete
            # ctrl + c to cancle
            elif User_Entry.lower() == "module delete":
                # print to give distance to above
                print()

                # get list of libs
                Lib_List = uos.listdir('lib')

                # print list with number in front
                for i in range(len(Lib_List)):
                    print("   " + str(i) + " - " + str(Lib_List[i]))

                # Get user input
                try:
                    print()
                    print("Press CTRL + C to cancle")
                    print()
                    User_Entry = input("Select module to delete: ")
                except (EOFError, KeyboardInterrupt):
                    print("   canceled")
                else:
                    try:
                        # delete selected lib aka module
                        uos.remove('lib/' + Lib_List[int(User_Entry)])
                    except TypeError:
                        print("   invalid module selected: " + User_Entry)
                    else:
                        print("   deleted module: " +
                              Lib_List[int(User_Entry)])

            # ---------------------------------------- Unknown command ----------------------------------------
            else:
                print("unknown command: " + User_Entry)
Ejemplo n.º 5
0
sys.path.insert(0, 'preprocess/')
sys.path.insert(0, 'models/')
sys.path.insert(0, 'layers/')
from sklearn.metrics import log_loss, classification_report
import Generate_train_test2
import config
from caffenet import CaffeNet
from keras.applications.imagenet_utils import decode_predictions
import numpy as np
from load_cifar10 import load_cifar10_data

if __name__ == '__main__':
    finetune = True
    img_rows, img_cols = 227, 227  # Resolution of inputs
    channel = 3
    batch_size, nb_epoch, num_classes, save_weights = config.Load()
    # Load Cifar10 data. Please implement your own load_data() module for your own dataset
    X_train, Y_train, X_valid, Y_valid = load_cifar10_data(img_rows, img_cols)

    model = CaffeNet(weights='caffenet_weights_th.h5', classes=num_classes)

    # Start Fine-tuning
    if finetune:
        model.fit(
            X_train,
            Y_train,
            batch_size=batch_size,
            nb_epoch=nb_epoch,
            shuffle=True,
            verbose=1,
            validation_data=(X_valid, Y_valid),