Example #1
0
    def __init__(self, lora):
        self.lora = lora
        self.is_updating = False
        self.version_file = '/flash/OTA_INFO.py'
        self.update_version = '0.0.0'
        self.update_time = -1
        self.resp_received = False
        self.update_in_progress = False
        self.operation_timeout = 10
        self.max_send = 5
        self.listen_before_sec = uos.urandom(1)[0] % 180
        self.updates_check_period = 6 * 3600

        self.mcAddr = None
        self.mcNwkSKey = None
        self.mcAppSKey = None

        self.patch = ''
        self.file_to_patch = None
        self.patch_list = dict()
        self.checksum_failure = False
        self.device_mainfest = None

        self._exit = False
        _thread.start_new_thread(self._thread_proc, ())

        self.inactivity_timeout = 120
        self.wdt = Watchdog()

        self.lora.init(self.process_message)
def main(args):
    """
    Read args passed to script, load tokens and run watchdog.

    Keyword arguments:
    :param args:    arguments parsed by argparse ArgumentParser

    :return:        returns status code 0 on successful completion

    """
    jenkins_server = args.jenkins_server.strip()
    jenkins_user = args.jenkins_user.strip()
    jenkins_token = open(args.jenkins_token).read().replace('\n', '').strip()
    msteams_url = open(args.msteams_url).read().replace('\n', '').strip()
    github_credentials = args.github_credentials
    github_org = args.github_org
    github_project = args.github_project
    ci_job = args.ci_job.strip()
    watchdog_job = args.watchdog_job.strip()
    quiet = args.quiet

    wd = Watchdog(jenkins_token=jenkins_token,
                  jenkins_server=jenkins_server,
                  jenkins_user=jenkins_user,
                  github_credentials=github_credentials,
                  git_org=github_org,
                  git_project=github_project,
                  msteams_url=msteams_url,
                  ci_job_name=ci_job,
                  watchdog_job_name=watchdog_job)
    wd.run(quiet=quiet)

    return 0
Example #3
0
class Oracle(object):
    def __init__(self):

        # Connect to the Gazepoint tracker.
        self.gp = GazePoint()

        # Establish an event bus.
        self.events = Events()

        # Set up the watchdog...
        try:
            os.remove('request.oracle')
        except:
            pass
        target_files = ['request.oracle']
        self.watchdog = Watchdog(target_files)
        self.watchdog.events.on_change += self.scan

    def scan(self, target_file):

        # Load oracle request.
        scan = Vessel(target_file).scan

        # Start the scan.
        scan = self.gp.collect(scan)
        os.remove('request.oracle')
        try:
            db.update(scan)
        except:
            print('Cannot save to database.')

    def kill():
        self.watchdog.kill()
        self.gp.kill()
Example #4
0
def start_watchdog(update, context):
    job_queue = context.job_queue
    query = context.user_data['query']
    chat = update.effective_chat

    w = Watchdog(query, chat)
    w.start_job(job_queue)
    context.user_data['watchdogs'].append(w)
    return
Example #5
0
    async def listen(self, channels: list[str], callbacks: dict[str,
                                                                Callable]):
        self.__watchdog = Watchdog(HEARTBEAT_TIMEOUT)
        self.__invoke_lock = asyncio.Lock()
        await self.__connect()
        if self.api_key is not None:
            await self.__auth()

        assert 'heartbeat' not in channels
        channels.append('heartbeat')
        callbacks['heartbeat'] = self.__on_heartbeat
        await self.__subscribe(channels, callbacks)
        await self.__watchdog.loop()
        self.__connection.close()
    def __init__(self, args):
        """
        1. Bootstrap dependencies
            a. test gps
            b. check for SDR
        2. Start srsue with config file
            a. watch srsue for crashes and restart
        3. Start watchdog daemon
        4. Start web daemon
        """
        self.threads = []
        self.subprocs = []
        self.debug = args.debug
        self.disable_gps = args.disable_gps
        self.disable_wigle = args.disable_wigle
        self.web_only = args.web_only
        self.config_fp = 'config.ini'
        self.config = args.config = configparser.ConfigParser()
        self.config.read(self.config_fp)
        self.earfcn_list = []
        signal.signal(signal.SIGINT, self.signal_handler)
        signal.signal(signal.SIGTERM, self.signal_handler)

        if args.project_name:
            self.project_name = args.project_name
        else:
            self.project_name = self.config['general']['default_project']
            args.project_name = self.config['general']['default_project']

        if self.project_name not in self.config:
            self.config[self.project_name] = {}

        # GPSD settings
        self.gpsd_args = {
            'host': self.config['gpsd']['host'],
            'port': int(self.config['gpsd']['port'])
        }

        # Set up logging
        self.logger = args.logger = verboselogs.VerboseLogger(
            "crocodile-hunter")
        fmt = f"\b * %(asctime)s {self.project_name} - %(levelname)s %(message)s"
        if (self.debug):
            log_level = "DEBUG"
        else:
            log_level = "VERBOSE"
        coloredlogs.install(level=log_level, fmt=fmt, datefmt='%H:%M:%S')

        self.watchdog = Watchdog(args)
Example #7
0
    def __init__(self):

        # Connect to the Gazepoint tracker.
        self.gp = GazePoint()

        # Establish an event bus.
        self.events = Events()

        # Set up the watchdog...
        try:
            os.remove('request.oracle')
        except:
            pass
        target_files = ['request.oracle']
        self.watchdog = Watchdog(target_files)
        self.watchdog.events.on_change += self.scan
Example #8
0
 def build(self):
     # Build this app!
     try:
         os.remove('status.oracle')  # remove oracle status file...
     except:
         pass
     self.patients = db.all('patients')
     self.watchdog = Watchdog(['status.oracle'])
     self.watchdog.events.on_change += self.update_status
     signal.signal(signal.SIGINT, self.kill_threads)
Example #9
0
 def __init__(self):
     super(iAltar, self).__init__()
     self.name = "iAltarPlayer"
     print("starting: %s" % self.name)
     self.searchType = Specs().s['defaultSearchType']
     print("%s: default search type: %s" % (self.name, self.searchType))
     Watchdog().add(self)
     if Hosts().getLocalAttr("hasServer"):
         Server().register({'Search': self.setSearchType})
     self.queue = Queue.Queue()
Example #10
0
def main():

    import sys
    sys.modules.clear()

    from button_handler import Button
    from pins import Pins

    # Init device button IRQ:
    Pins.button_pin.irq(Button().irq_handler)

    from context import Context

    context = Context()

    import wifi
    wifi.init(context)
    del sys.modules['wifi']

    _RTC_KEY_RUN = 'run'
    _RUN_WEB_SERVER = 'web-server'
    _RUN_SOFT_OTA = 'soft-ota'

    from rtc import get_rtc_value
    if get_rtc_value(_RTC_KEY_RUN) == _RUN_WEB_SERVER:
        print('start webserver')

        from rtc import update_rtc_dict
        update_rtc_dict(data={_RTC_KEY_RUN:
                              _RUN_SOFT_OTA})  # run OTA client on next boot

        del update_rtc_dict
        del get_rtc_value
        del sys.modules['rtc']

        # init Watchdog timer
        from watchdog import Watchdog
        context.watchdog = Watchdog(context)

        from webswitch import WebServer

        WebServer(context=context, version=__version__).run()
    else:
        print('start "soft" OTA')
        Pins.power_led.off()
        from rtc import update_rtc_dict
        update_rtc_dict(data={_RTC_KEY_RUN:
                              _RUN_WEB_SERVER})  # run web server on next boot
        from ota_client import SoftOtaUpdate

        SoftOtaUpdate().run()

    from reset import ResetDevice
    ResetDevice(reason='unknown').reset()
Example #11
0
    def __init__(self, start_bg_thread=True):
        self.dbc = pymongo.MongoClient()

        self.registration_col1 = self.dbc.db.registration_col1
        self.ranking = self.dbc.db.ranking
        self.config_col = self.dbc.db.config
        self.raw_data = self.dbc.db.raw_data
        self.snapshots_col_rooms = self.dbc.db.snapshots_col_rooms
        self.snapshots_col_appliances = self.dbc.db.snapshots_col_appliances
        self.DRL_recommendations = self.dbc.db.DRL_recommendations
        self.snapshots_col_users = self.dbc.db.snapshots_col_users
        self.web_registration = self.dbc.db.web_registration
        self.web_comfort_feedback = self.dbc.db.web_comfort_feedback
        self.web_rec_feedback = self.dbc.db.web_rec_feedback

        self.snapshots_parameters = self.dbc.db.snapshots_parameters
        self._latestSuccessShot = 0

        self._ReadConfigs()
        self.watchdog = Watchdog(self.WATCHDOG_TIMEOUT_USER,
                                 self.WATCHDOG_TIMEOUT_APPLIANCE)
        ## Data Structure Init: bipartite graph between rooms and appls
        ## TODO: Add a web interface to update config in db, and pull new config into memory.

        self._ConstructInMemoryGraph()
        ## Construct bipartite graph.
        # self._accumulator()
        #self._GracefulReloadGraph()
        ## Read appliance values from database; TODO: occupants location
        #self._HardcodeValues()

        # self.watchdogInit()

        self.recommender = recommenderSystem()
        if start_bg_thread:
            self.startDaemon()

        self.snapshots_col_rooms = self.dbc.db.snapshots_col_rooms
        self.snapshots_col_appliances = self.dbc.db.snapshots_col_appliances
        self.snapshots_col_users = self.dbc.db.snapshots_col_users
Example #12
0
def scrape():
    scraper = LinkedinScraper()
    scraper.login(os.environ['LINKEDIN_USER'], os.environ['LINKEDIN_PW'])
    gatherer = Gatherer(scraper)
    watchdog = Watchdog(gatherer)

    job_list = [
        'data intern', 'machine learning intern', 'software intern',
        'python intern'
    ]

    location_list = [
        'Vancouver',
        'New York',
        'San Francisco Bay Area',
        'Toronto',
    ]

    for job in job_list:
        for location in location_list:
            print('gathering for', job, location)
            watchdog.monitor_gather(job, location)

    for job in job_list:
        print('gathering for', job, 'Worldwide')
        watchdog.monitor_gather(job, 'Worldwide')

    return 'Done'
Example #13
0
    def run(self):
        print("%s starting" % self.name)
        afiles = []
        (minTime, maxTime) = Specs().s["displayTimeRange"]
        lastImageDir = ""
        imageIndex = 0
        splash = Specs().s['splashImg']
        print("displaying f:%s" % splash)
        Display().image(splash)
        ts = None
        while True:
            Watchdog().feed(self)
            try:
                path = self.queue.get(timeout=ts)
            except Queue.Empty:
                path = lastImageDir

            if path == "__stop__":
                print("%s is stopping" % self.name)
                break

            print("%s: path %s lastImageDir %s" %
                  (self.name, path, lastImageDir))
            if path != lastImageDir:
                print("%s reseting imageIndex" % self.name)
                imageIndex = 0
                lastImageDir = path
                if len(afiles) == 0:
                    print("empty image file. waiting")
                    ts = 1
                    continue
            afiles = glob.glob(path + "/*.jpg")
            numFiles = len(afiles)
            if numFiles == 0:
                print("%s directory empty!!" % self.name)
                print("displaying f:%s" % splash)
                Display().image(splash)
                ts = 1
                continue
            print("imageIndex %d len afiles %d" % (imageIndex, numFiles))
            if imageIndex >= numFiles:
                print("resetting imageIndex")
                imageIndex = 0
            f = afiles[imageIndex]
            imageIndex += 1
            print("displaying f:%s" % f)
            if Display().image(f) == False:
                print "skipping bad images:%s" % f
                ts = .1
                continue
            ts = (random.random() * (maxTime - minTime)) + minTime
            print("next display %f" % ts)
Example #14
0
 def __init__(self):
     super(ImageHandler, self).__init__()
     self.name = "ImageHandler"
     print("starting: %s" % self.name)
     Watchdog().add(self)
     self.currentId = None
     self.queue = Queue.Queue()
     if Hosts().getLocalAttr('hasServer'):
         Server().register({
             'AddImage': self.addImage,
             'RmCacheDir': self.rmCacheDir,
             'SetImageDir': self.setImageDir,
             'ClearCache': self.clearCache
         })
Example #15
0
def main():

    log.info('starting ' + c.DRIVER_NAME)

    tty = parse_cmdline_args()
    sensor = ImtSiRs485Sensor(tty)

    hw_ver, fw_ver = sensor.identify()

    # add hw and fw version to the signals, they are mandatory
    signals = c.SIGNALS + [
        DbusSignal('/HardwareVersion', hw_ver),
        DbusSignal('/FirmwareVersion', fw_ver)
    ]

    service_name = c.SERVICE_NAME + '.' + tty
    watchdog_timeout = (c.UPDATE_INTERVAL + c.SERIAL_TIMEOUT) * 2

    # starting watchdog here, because with VeDbusServiceAsync we are going multi-threaded
    with Watchdog(watchdog_timeout) as watchdog, VeDbusServiceAsync(
            service_name, signals) as dbus:

        settings_for_tty = get_settings_for_tty(tty)
        settings = SettingsDevice(dbus.dbusconn, settings_for_tty, None)
        _is_subsensor_present = partial(is_subsensor_present, settings)

        # only the modbus signals are updated, the others are const
        modbus_signals = [s for s in signals if isinstance(s, ModbusSignal)]

        while True:

            watchdog.alive()  # signal the watchdog that we are still alive

            registers = sensor.read_modbus_registers()

            for s in modbus_signals:
                value = registers[s.reg_no]
                if s.signed and value & 0x8000:
                    value -= 0x10000
                value *= s.gain

                if _is_subsensor_present(s.dbus_path, value):
                    dbus[s.dbus_path] = value
                else:
                    dbus[s.dbus_path] = None

            log.debug('iteration completed, sleeping for ' +
                      str(c.UPDATE_INTERVAL) + ' seconds')
            sleep(c.UPDATE_INTERVAL)
    def __init__(self):

        # Set up socket connections/event bus.
        self.events = Events()

        # Boot up datastore/etc.
        self.valid_keys = ['t', 't_sys', 'v', 'filtered']
        self.datastore = DataStore()
        self.session_id = None
        self.is_recording = False
        self.is_broadcasting = True
        self.counter = 0

        # Configure filtering.
        self.allowed_channels = [0, 1]
        self.freq_cutoff = 10
        self.filter_order = 6
        self.downsample_rate = 32
        self.zi = {}
        self.channel_data = {}
        self.buffers_to_watch = []
        for chn in self.allowed_channels:
            self.zi[chn] = np.zeros(self.filter_order)
            self.channel_data[chn] = []
            self.buffers_to_watch.append('buffer-{:02d}.dat'.format(chn))
        self.buffers_to_watch.append('oracle_status.dat')

        # Listen for buffer changes:
        self.watchdog = Watchdog(self.buffers_to_watch)

        # Set up threading to handle i/o in background.
        self.q = Queue()
        nb_workers = 1
        target = self.data_received
        for _ in range(nb_workers):
            worker = Thread(target=target, args=(self.q, ), daemon=True)
            worker.start()

        # Listen for data from the server.
        self.watchdog.events.on_change += self.push_to_queue
        self.last_time = time()
Example #17
0
if __name__ == '__main__':
    import time
    import signal
    import sys

    from watchdog import Watchdog as Watchdog
    from digital_output import DigitalOutput as DigitalOutput

    def signal_handler(sig, frame):
        myOutput.cleanup()
        sys.exit(0)

    signal.signal(signal.SIGINT, signal_handler)
    print('Ctrl+C to exit')

    with Watchdog() as watchdog:
        watchdog.enable(wait=True)
        myOutput = DigitalOutput(0)
        myOutput.enable()
        increment = 0.1
        delay = 1.0
        while True:
            if delay > 3.0 or delay < 1.0:
                increment = -increment

            if myOutput.is_on():
                watchdog.update(0)
                myOutput.off()
                state = 'off'
            elif myOutput.is_off():
                watchdog.update(1)
    elif data['type'] == 'order':
        task['type'] = 'add_order_or_watchdog'
    elif data['type'] == 'clear_order':
        task['type'] = 'clear_order'
    elif data['type'] == 'cost':
        task['type'] = 'receive_cost'
    elif data['type'] == 'acknowledge_order':
        task['type'] = 'acknowledge_order'
    add_task(task)


#elevator.run(MY_ID, add_task)
elevator = Elevator(MY_ID, add_task)
elevator.run()
#watchdog.run(add_task)
watchdog = Watchdog(add_task)
watchdog.run()
network.run(add_task_from_message)


def order_watcher():
    global ordersAndCosts
    while True:
        current_time = int(time.time())
        popList = []
        for element in ordersAndCosts:
            if element['timestamp'] + ORDER_WATCHER_LIMIT < current_time:
                #print(f'Costs: {element}')
                if len(element['costs']) > 0:
                    lowest_cost = 1000
                    for costElement in element['costs']:
Example #19
0
class CrocodileHunter():
    def __init__(self, args):
        """
        1. Bootstrap dependencies
            a. test gps
            b. check for SDR
        2. Start srsue with config file
            a. watch srsue for crashes and restart
        3. Start watchdog daemon
        4. Start web daemon
        """
        self.threads = []
        self.subprocs = []
        self.debug = args.debug
        self.disable_gps = args.disable_gps
        self.disable_wigle = args.disable_wigle
        self.web_only = args.web_only
        self.config_fp = 'config.ini'
        self.config = args.config = configparser.ConfigParser(inline_comment_prefixes=('#', ';'))
        self.config.read(self.config_fp)
        self.earfcn_list = []
        signal.signal(signal.SIGINT, self.signal_handler)
        signal.signal(signal.SIGTERM, self.signal_handler)

        if args.project_name:
            self.project_name = args.project_name
        else:
            self.project_name = self.config['general']['default_project']
            args.project_name = self.config['general']['default_project']

        if self.project_name not in self.config:
            self.config[self.project_name] = {}

        # GPSD settings
        self.gpsd_args = {
            'host': self.config['gpsd']['host'],
            'port': int(self.config['gpsd']['port'])
        }

        # Set up logging
        self.logger = args.logger = verboselogs.VerboseLogger("crocodile-hunter")
        fmt=f"\b * %(asctime)s {self.project_name} - %(levelname)s %(message)s"
        if(self.debug):
            log_level = "DEBUG"
        else:
            log_level = "VERBOSE"
        coloredlogs.install(level=log_level, fmt=fmt, datefmt='%H:%M:%S')

        self.watchdog = Watchdog(args)

    def start(self):
        self.logger.info(f"starting crocodile hunter project: {self.project_name}")
        """
        if not self.debug:
            spn = Thread(target=self.show_spinner)
            spn.start()
            self.threads.append(spn)
            """

        #Start web ui dæmon
        webui = Webui(self.watchdog)
        webui.start_daemon()

        if self.web_only:
            return

        # Bootstrap srsUE dependencies
        args = "./bootstrap.sh"
        proc = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

        for stdout_line in iter(proc.stdout.readline, b""):
            self.logger.info(stdout_line.decode("utf-8").rstrip())
        proc.stdout.close()
        return_code = proc.wait()

        if return_code:
            self.logger.critical("Bootstrapping failed")
            self.cleanup(True)

        # Test GPSD
        if self.disable_gps:
            self.logger.info("GPS disabled. Skipping test.")
        else:
            try:
                gpsd.connect(**self.gpsd_args)
            except (ConnectionRefusedError, ConnectionResetError, TimeoutError):
                raise RuntimeError("Connection to GPSD failed. Please ensure GPSD is set up as " \
                    "described in the \"Configuring GPSD\" section of README.md and that " \
                    "it's running.")
            except gaierror:
                raise RuntimeError(f"Couldn't resolve GPSD hostname: {self.gpsd_args['host']}")
            packet = gpsd.get_current()
            if packet.mode > 1:
                self.logger.info("GPS fix detected")
            else:
                self.logger.info("No GPS fix detected. Waiting for fix.")
                packet = gpsd.get_current()
                tries = 1
                while packet.mode < 2:
                    if tries < 10:
                        self.logger.debug("Try %s waiting for fix.")
                        packet = gpsd.get_current()
                        tries += 1
                    else:
                        self.logger.critical("No GPS fix detected. Exiting.")
                        exit(1)

        #Start watchdog dæmon
        self.watchdog.start_daemon()

        # Monitor and restart srsUE if it crashes
        self.update_earfcn_list()
        proc = self.start_srslte()
        self.subprocs.append(proc)
        monitor = Thread(target=self.monitor_srslte, args=(proc,))
        monitor.start()
        self.threads.append(monitor)

    def signal_handler(self, sig, frame):
        """ Exit gracefully on SIGINT """
        self.logger.critical("You pressed Ctrl+C!")
        self.cleanup()

    def update_earfcn_list(self):
        """ call to wigle to update the local EARFCN list or use a statically configured list """
        if 'earfcns' in self.config[self.project_name]:
            self.earfcn_list = map(int, self.config[self.project_name]['earfcns'].split(','))
        else:
            if self.disable_wigle:
                self.logger.critical("Wigle is disabled so we cant fetch an EARFCN list, either " \
                    "run again with wigle enabled or copy an EARFCN list from another project in " \
                    "config.ini")
                self.cleanup()
            self.logger.warning("Getting earcn list for the first time, this might take a while")
            gps = self.watchdog.get_gps()
            try:
                self.earfcn_list = self.watchdog.wigle.earfcn_search(gps.lat, gps.lon, 0.05)
            except WigleError as e:
                self.logger.error(e)
                self.logger.critical("Wigle couldn't fetch an EARFCN list for your area. Please " \
                                     "specify EARFCN list manually in config.ini")
                self.cleanup()
            self.config[self.project_name]['earfcns'] = ",".join(map(str, self.earfcn_list))
        self.logger.notice(f"Using earfcn list {self.config[self.project_name]['earfcns']}")

    def start_srslte(self):
        # TODO Intelligently handle srsUE output (e.g. press a key to view output or something,
        #      maybe in another window)
        """ Start srsUE """
        earfcns = ",".join(map(str, self.earfcn_list))
        self.logger.info(f"EARFCNS: {earfcns}")
        self.logger.info(f"Running srsUE")
        proc = Popen([
            "./srsLTE/build/lib/examples/cell_measurement", "-z", earfcns],
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT)
        self.logger.success(f"srsUE started with pid {proc.pid}")
        return proc

    def monitor_srslte(self, proc):
        """ Monitor for crashes and restart srsLTE"""
        # TODO
        global EXIT
        out = []
        sleep(1)
        nbsr = NBSR(proc.stdout)
        line = ''
        while not EXIT:
            line = nbsr.readline(int(self.config["general"]["crash_timeout"]))
            if line is not None:
                self.logger.debug(line.decode("ascii").rstrip())
            out.append(line)

            if proc.poll() is not None or line is None:
                self.logger.warning(f"srsUE has exited unexpectedly")
                try:
                    self.logger.warning(f"It's dying words were: {out[-2].decode('ascii').rstrip()}")
                except IndexError as e:
                    pass
                proc.kill()
                self.update_earfcn_list()
                proc = self.start_srslte()
                self.monitor_srslte(proc)

    def show_spinner(self):
        """ show a spinning cursor """
        global EXIT
        spinner = itertools.cycle(['-', '/', '|', '\\'])
        while not EXIT:
            sys.stdout.write(next(spinner))   # write the next character
            sys.stdout.flush()                # flush stdout buffer (actual character display)
            sys.stdout.write('\b')            # erase the last written char
            sleep(0.1)

    def cleanup(self, error=False, error_message=None):
        """ Gracefully exit when program is quit """
        global EXIT

        if error_message is not None:
            self.logger.error(error_message)

        self.logger.error(f"Exiting...")

        with open(self.config_fp, 'w') as cf:
            self.config.write(cf)

        EXIT = True
        for thread in self.threads:
            thread.join()
        for proc in self.subprocs:
            proc.kill()
        self.watchdog.shutdown()
        self.logger.success(f"See you space cowboy...")
        os._exit(int(error))
Example #20
0
def watchdog(device_address):
    Watchdog(int(device_address)).start()
Example #21
0
 #listen for when to stop
 stop_subscriber = rospy.Subscriber(STOP_TOPIC, Empty, onStopRequestReceived); 
 
 #listen for user-drawn shapes
 shape_subscriber = rospy.Subscriber(USER_DRAWN_SHAPES_TOPIC, Path, userShapePreprocessor); 
     
 #initialise display manager for shapes (manages positioning of shapes)
 from display_manager.srv import *
 print('Waiting for display manager services to become available');
 rospy.wait_for_service('clear_all_shapes');
 
 rospy.sleep(2.0);   #Allow some time for the subscribers to do their thing, 
                     #or the first message will be missed (eg. first traj on tablet, first clear request locally)
 
 from watchdog import Watchdog #TODO: Make a ROS server so that *everyone* can access the connection statuses
 tabletWatchdog = Watchdog('watchdog_clear/tablet', 0.4);
 robotWatchdog = Watchdog('watchdog_clear/robot', 0.8);
 
 if(naoConnected):
     from naoqi import ALBroker, ALProxy
     #start speech (ROS isn't working..)
     port = 9559;
     myBroker = ALBroker("myBroker", #I'm not sure that pyrobots doesn't already have one of these open called NAOqi?
         "0.0.0.0",   # listen to anyone
         0,           # find a free port and use it
         NAO_IP,      # parent broker IP
         port)        # parent broker port
     textToSpeech = ALProxy("ALTextToSpeech", NAO_IP, port)   
     textToSpeech.setLanguage(naoLanguage)
     #textToSpeech.setVolume(1.0);
     if(naoWriting):
Example #22
0
    def work(cls, jobs, session, taskconf):

        if taskconf.ssh_identity:
            sshkey = ssh.PrivateKey(taskconf.ssh_identity)
        else:
            sshkey = ssh.TempPrivateKey()

        def status(msg):
            timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
            session.logs.manager.write("%s :: session %d %s\n" % (timestamp, session.id, msg))

        status("(pid %d)" % os.getpid())
        print >> session.logs.manager

        class CaughtSignal(CloudWorker.Terminated):
            pass

        def terminate(sig, f):
            signal.signal(sig, signal.SIG_IGN)
            sigs = dict([ ( getattr(signal, attr), attr) 
                            for attr in dir(signal) if attr.startswith("SIG") ])

            raise CaughtSignal("caught %s termination signal" % sigs[sig], sig)

        exception = None
        while True:
            watchdog = Watchdog(session.logs.manager, session.paths.workers, taskconf)

            signal.signal(signal.SIGINT, terminate)
            signal.signal(signal.SIGTERM, terminate)

            executor = None

            work_started = time.time()
            try:
                executor = CloudExecutor(session.logs, taskconf, sshkey)
                for job in jobs:
                    executor(job)

                executor.join()
                executor_results = executor.results

            except Exception, exception:
                if isinstance(exception, CaughtSignal):
                    print >> session.logs.manager,  "# " + str(exception[0])

                elif not isinstance(exception, (CloudWorker.Error, CloudWorker.Terminated)):
                    traceback.print_exc(file=session.logs.manager)

                if executor:
                    executor.stop()
                    executor_results = executor.results
                else:
                    executor_results = []

            watchdog.terminate()
            watchdog.join()

            session.jobs.update(jobs, executor_results)

            if len(session.jobs.pending) != 0 and executor_results and exception is None:
                print >> session.logs.manager
                status("(auto-resuming) %d pending jobs remaining" % len(session.jobs.pending))
                print >> session.logs.manager

                jobs = list(session.jobs.pending)
                if taskconf.split > len(jobs):
                    taskconf.split = len(jobs)
            else:
                break
Example #23
0
class LoraOTA:

    MSG_HEADER = b'$OTA'
    MSG_TAIL = b'*'

    UPDATE_INFO_MSG = 1
    UPDATE_INFO_REPLY = 2

    MULTICAST_KEY_REQ = 3
    MULTICAST_KEY_REPLY = 4

    LISTENING_MSG = 5
    LISTENING_REPLY = 6

    UPDATE_TYPE_FNAME = 7
    UPDATE_TYPE_PATCH = 8
    UPDATE_TYPE_CHECKSUM = 9

    DELETE_FILE_MSG = 10
    MANIFEST_MSG = 11

    def __init__(self, lora):
        self.lora = lora
        self.is_updating = False
        self.version_file = '/flash/OTA_INFO.py'
        self.update_version = '0.0.0'
        self.update_time = -1
        self.resp_received = False
        self.update_in_progress = False
        self.operation_timeout = 10
        self.max_send = 5
        self.listen_before_sec = uos.urandom(1)[0] % 180
        self.updates_check_period = 6 * 3600

        self.mcAddr = None
        self.mcNwkSKey = None
        self.mcAppSKey = None

        self.patch = ''
        self.file_to_patch = None
        self.patch_list = dict()
        self.checksum_failure = False
        self.device_mainfest = None

        self._exit = False
        _thread.start_new_thread(self._thread_proc, ())

        self.inactivity_timeout = 120
        self.wdt = Watchdog()

        self.lora.init(self.process_message)

    def stop(self):
        self.lora.stop()
        self._exit = True

    def _thread_proc(self):
        updates_check_time = utime.time()
        self.device_mainfest = self.create_device_manifest()

        while not self._exit:
            if utime.time() > updates_check_time and self.update_time < 0:
                self.synch_request(self.check_firmware_updates)
                updates_check_time = utime.time() + self.updates_check_period

            if self.update_time > 0 and not self.update_in_progress:
                if self.update_time - utime.time() < self.listen_before_sec:
                    self.update_in_progress = True
                    self.updating_proc()

            if self.update_failed():
                print('Update failed: No data received')
                machine.reset()

            utime.sleep(2)

    def updating_proc(self):
        self.synch_request(self.get_mulitcast_keys)

        if self.mcAddr is not None:
            mulitcast_auth = (self.mcAddr, self.mcNwkSKey, self.mcAppSKey)
            self.lora.change_to_multicast_mode(mulitcast_auth)

            wdt_timeout = self.listen_before_sec + self.inactivity_timeout
            self.wdt.enable(wdt_timeout)

            self.synch_request(self.send_listening_msg)
        else:
            self.reset_update_params()

    def create_device_manifest(self):

        manifest = dict()
        manifest["delete"] = 0
        manifest["update"] = 0
        manifest["new"] = 0

        return manifest

    def reset_update_params(self):
        self.mcAddr = None
        self.mcNwkSKey = None
        self.mcAppSKey = None

        self.update_in_progress = False
        self.update_time = -1
        self.update_version = '0.0.0'

    def get_mulitcast_keys(self):
        msg = bytearray()
        msg.extend(self.MSG_HEADER)
        msg.extend(b',' + str(self.MULTICAST_KEY_REQ).encode())
        msg.extend(b',' + self.MSG_TAIL)

        self.lora.send(msg)

    def synch_request(self, func):
        attempt_num = 0
        self.resp_received = False

        while attempt_num < self.max_send and not self.resp_received:
            func()

            count_10ms = 0
            while (count_10ms <= self.operation_timeout * 100
                   and not self.resp_received):
                count_10ms += 1
                utime.sleep(0.01)

            attempt_num += 1

    def check_firmware_updates(self):
        msg = bytearray()
        msg.extend(self.MSG_HEADER)
        msg.extend(b',' + str(self.UPDATE_INFO_MSG).encode())

        version = self.get_current_version().encode()
        msg.extend(b',' + version)
        msg.extend(b',' + self.MSG_TAIL)

        self.lora.send(msg)
        print("Lora OTA: Request for info sent")

    def get_current_version(self):
        version = '0.0.0'
        if self.file_exists(self.version_file):
            with open(self.version_file, 'r') as fh:
                version = fh.read().rstrip("\r\n\s")
        else:
            self._write_version_info(version)

        print("Version: {}", version)

        return version

    def send_listening_msg(self):
        msg = bytearray()
        msg.extend(self.MSG_HEADER)
        msg.extend(b',' + str(self.LISTENING_MSG).encode())
        msg.extend(b',' + self.MSG_TAIL)

        self.lora.send(msg)

    def _write_version_info(self, version):
        try:
            with open(self.version_file, 'w+') as fh:
                fh.write(version)
        except Exception as e:
            print("Exception creating OTA version file")

    def file_exists(self, file_path):
        exists = False
        try:
            if uos.stat(file_path)[6] > 0:
                exists = True
        except Exception as e:
            exists = False
        return exists

    def get_msg_type(self, msg):
        msg_type = -1
        try:
            msg_type = int(msg.split(",")[1])
        except Exception as ex:
            print("Exception getting message type")

        return msg_type

    def sync_clock(self, epoc):
        try:
            rtc = RTC()
            rtc.init(utime.gmtime(epoc))
        except Exception as ex:
            print("Exception setting system data/time: {}".format(ex))
            return False

        return True

    def parse_update_info_reply(self, msg):
        self.resp_received = True

        try:
            token_msg = msg.split(",")
            need_updating = int(token_msg[2])
            if need_updating:
                self.update_version = token_msg[3]
                self.update_time = int(token_msg[5])

            if utime.time() < 1550000000:
                self.sync_clock(int(token_msg[4]))

        except Exception as ex:
            print("Exception getting update information: {}".format(ex))
            return False

        return True

    def parse_multicast_keys(self, msg):

        try:
            token_msg = msg.split(",")
            print(token_msg)

            if len(token_msg[2]) > 0:
                self.mcAddr = token_msg[2]
                self.mcNwkSKey = token_msg[3]
                self.mcAppSKey = token_msg[4]

            print("mcAddr: {}, mcNwkSKey: {}, mcAppSKey: {}".format(
                self.mcAddr, self.mcNwkSKey, self.mcAppSKey))

            self.resp_received = True
        except Exception as ex:
            print("Exception getting multicast keys: {}".format(ex))
            return False

        return True

    def parse_listening_reply(self, msg):
        self.resp_received = True

    def _data_start_idx(self, msg):
        # Find first index
        i = msg.find(",")

        #Find second index
        return msg.find(",", i + 1)

    def _data_stop_idx(self, msg):
        return msg.rfind(",")

    def get_msg_data(self, msg):
        data = None
        try:
            start_idx = self._data_start_idx(msg) + 1
            stop_idx = self._data_stop_idx(msg)
            data = msg[start_idx:stop_idx]
        except Exception as ex:
            print("Exception getting msg data: {}".format(ex))
        return data

    def process_patch_msg(self, msg):
        partial_patch = self.get_msg_data(msg)

        if partial_patch:
            self.patch += partial_patch

    def verify_patch(self, patch, received_checksum):
        h = uhashlib.sha1()
        h.update(patch)
        checksum = ubinascii.hexlify(h.digest()).decode()
        print("Computed checksum: {}".format(checksum))
        print("Received checksum: {}".format(received_checksum))

        if checksum != received_checksum:
            self.checksum_failure = True
            return False

        return True

    def process_checksum_msg(self, msg):
        checksum = self.get_msg_data(msg)
        verified = self.verify_patch(self.patch, checksum)
        if verified:
            self.patch_list[self.file_to_patch] = self.patch

        self.file_to_patch = None
        self.patch = ''

    def backup_file(self, filename):
        bak_path = "{}.bak".format(filename)

        # Delete previous backup if it exists
        try:
            uos.remove(bak_path)
        except OSError:
            pass  # There isnt a previous backup

        # Backup current file
        uos.rename(filename, bak_path)

    def process_delete_msg(self, msg):
        filename = self.get_msg_data(msg)

        if self.file_exists('/flash/' + filename):
            self.backup_file('/flash/' + filename)
            self.device_mainfest["delete"] += 1

    def get_tmp_filename(self, filename):
        idx = filename.rfind(".")
        return filename[:idx + 1] + "tmp"

    def _read_file(self, filename):

        try:
            with open('/flash/' + filename, 'r') as fh:
                return fh.read()
        except Exception as ex:
            print("Error reading file: {}".format(ex))

        return None

    def backup_file(self, filename):
        bak_path = "{}.bak".format(filename)

        # Delete previous backup if it exists
        try:
            uos.remove(bak_path)
        except OSError:
            pass  # There isnt a previous backup

        # Backup current file
        uos.rename(filename, bak_path)

    def _write_to_file(self, filename, text):
        tmp_file = self.get_tmp_filename('/flash/' + filename)

        try:
            with open(tmp_file, 'w+') as fh:
                fh.write(text)
        except Exception as ex:
            print("Error writing to file: {}".format(ex))
            return False

        if self.file_exists('/flash/' + filename):
            self.backup_file('/flash/' + filename)
        uos.rename(tmp_file, '/flash/' + filename)

        return True

    def apply_patches(self):
        for key, value in self.patch_list.items():
            self.dmp = dmp_module.diff_match_patch()
            self.patch_list = self.dmp.patch_fromText(value)

            to_patch = ''
            print('Updating file: {}'.format(key))
            if self.file_exists('/flash/' + key):
                to_patch = self._read_file(key)

            patched_text, success = self.dmp.patch_apply(
                self.patch_list, to_patch)
            if False in success:
                return False

            if not self._write_to_file(key, patched_text):
                return False

        return True

    @staticmethod
    def find_backups():
        backups = []
        for file in uos.listdir("/flash"):
            if file.endswith(".bak"):
                backups.append(file)
        return backups

    @staticmethod
    def revert():
        backup_list = LoraOTA.find_backups()
        for backup in backup_list:
            idx = backup.find('.bak')
            new_filename = backup[:idx]
            uos.rename(backup, new_filename)
        print('Error: Reverting to old firmware')
        machine.reset()

    def manifest_failure(self, msg):

        try:
            start_idx = msg.find("{")
            stop_idx = msg.find("}")

            recv_manifest = json.loads(msg[start_idx:stop_idx])

            print("Received manifest: {}".format(recv_manifest))
            print("Actual manifest: {}".format(self.device_mainfest))

            if (recv_manifest["update"] != self.device_mainfest["update"]) or \
               (recv_manifest["new"] != self.device_mainfest["new"]) or \
               (recv_manifest["delete"] != self.device_mainfest["delete"]):
                return True
        except Exception as ex:
            print("Error in manifest: {}".format(ex))
            return True

        return False

    def process_manifest_msg(self, msg):

        if self.manifest_failure(msg):
            print('Manifest failure: Discarding update ...')
            self.reset_update_params()
        if self.checksum_failure:
            print('Failed checksum: Discarding update ...')
            self.reset_update_params()
        elif not self.apply_patches():
            LoraOTA.revert()
        else:
            print('Update Success: Restarting .... ')
            self._write_version_info(self.update_version)
            machine.reset()

    def process_filename_msg(self, msg):
        self.file_to_patch = self.get_msg_data(msg)

        if self.file_exists('/flash/' + self.file_to_patch):
            self.device_mainfest["update"] += 1
            print("Update file: {}".format(self.file_to_patch))
        else:
            self.device_mainfest["new"] += 1
            print("Create new file: {}".format(self.file_to_patch))

        self.wdt.enable(self.inactivity_timeout)

    def update_failed(self):
        return self.wdt.update_failed()

    def process_message(self, msg):
        self.wdt.ack()

        msg_type = self.get_msg_type(msg)
        if msg_type == self.UPDATE_INFO_REPLY:
            self.parse_update_info_reply(msg)
        elif msg_type == self.MULTICAST_KEY_REPLY:
            self.parse_multicast_keys(msg)
        elif msg_type == self.LISTENING_REPLY:
            self.parse_listening_reply(msg)
        elif msg_type == self.UPDATE_TYPE_FNAME:
            self.process_filename_msg(msg)
        elif msg_type == self.UPDATE_TYPE_PATCH:
            self.process_patch_msg(msg)
        elif msg_type == self.UPDATE_TYPE_CHECKSUM:
            self.process_checksum_msg(msg)
        elif msg_type == self.DELETE_FILE_MSG:
            self.process_delete_msg(msg)
        elif msg_type == self.MANIFEST_MSG:
            self.process_manifest_msg(msg)
Example #24
0
async def on_message(message):
    if message.author == client.user:
        return

################################################
#           Modules start here!

    if message.content.startswith('$flip '):

        print("replied to a message, $flip")
        Product = message.content[6:].lower()

        Send = flip(Product)

        color = random.randint(0, 0xffffff)
        embed = discord.Embed(title="$flip " + Product, color=color)
        embed.set_author(
            name="BazaarViewer",
            icon_url=
            "https://cdn.discordapp.com/attachments/703719065977487390/708392264086585364/yeeeeeeeeee.png"
        )

        i = 0
        for x in Send:
            embed.add_field(name="#" + str(i + 1) + ":",
                            value=Send[i],
                            inline=False)
            i += 1
        strColor = str(color)
        embed.set_footer(text="Color: " + strColor)
        await message.channel.send(embed=embed)

    elif message.content.startswith('$commands'):
        color = random.randint(0, 0xffffff)
        embed = discord.Embed(title="Every command", color=color)
        embed.set_author(
            name="BazaarViewer",
            icon_url=
            "https://cdn.discordapp.com/attachments/703719065977487390/708392264086585364/yeeeeeeeeee.png"
        )
        embed.add_field(name="$commands",
                        value="Show every command.",
                        inline=False)
        embed.add_field(
            name="$flip (item)",
            value=
            "This will give you information about one item, like the NPC buy and sell price, and also the bazaar prices.",
            inline=False)
        embed.add_field(name="$merchantflip",
                        value="Shows the top products to flip.",
                        inline=False)
        embed.add_field(name="$everymerchantflip",
                        value="Sends you a DM with every products to flip.",
                        inline=False)
        embed.add_field(
            name="$pricedifference",
            value=
            "This will give you the items from the bazaar with the biggest buy-sell gap.",
            inline=False)
        embed.add_field(name="$request (item)",
                        value="If a item isnt supported yet, do this.",
                        inline=False)
        embed.add_field(
            name="$notify",
            value=
            "Using these two commands, you can toggle your notifications for events.",
            inline=False)
        embed.add_field(
            name="$eflip",
            value=
            "This will show you the top 5 items, you can buy from the bazaar, craft to the enchanted versions and resell them.",
            inline=False)
        embed.add_field(name="$bestlog",
                        value="Shows the woodtype, thats worth the most atm.",
                        inline=False)
        #embed.add_field(name="$minions", value="You type in the details, and I say you how much you would make with that minion setup **code by e56, ported to a discord-bot by me.**", inline=False)
        embed.add_field(
            name="$skyscore",
            value=
            "(BETA) That shows you the SkyScore of a product. The score gets calculated from the money you can make flipping it, and the buy/sell volume.",
            inline=False)
        embed.add_field(
            name="$reverseflip",
            value=
            "This shows you items, that you can buy at the Bazaar and sell to an NPC for profit. Not much, but easy.",
            inline=False)
        embed.add_field(
            name="$watchdog",
            value=
            "Everyone loves the watchdog message, right? Now you can get it on discord!",
            inline=False)
        strColor = str(color)
        embed.set_footer(text="Color: " + strColor)
        await message.channel.send(embed=embed)
        print("answered to $commands")

    elif message.content.startswith('$merchantflip'):
        print("replied to a message, $merchantflip!")

        sortedtoplist = MerchantFlip()
        color = random.randint(0, 0xffffff)
        embed = discord.Embed(title="How much can you make?", color=color)
        embed.set_author(
            name="BazaarViewer",
            icon_url=
            "https://cdn.discordapp.com/attachments/703719065977487390/708392264086585364/yeeeeeeeeee.png"
        )
        i = 0
        for x in sortedtoplist:
            embed.add_field(name="#" + str(i + 1) + ":",
                            value=sortedtoplist[i],
                            inline=False)
            i += 1
            if i == 4:
                break
        strColor = str(color)
        embed.set_footer(text="Color: " + strColor)
        await message.channel.send(embed=embed)

        sortedtoplist = []
        AllProfit = 0

    elif message.content.startswith('$request'):
        Product = message.content[9:].lower()
        Request(Product)
        await message.channel.send(
            "Ok, <@585935919224324117> has to add this. I wrote it to a text file."
        )

    elif message.content.startswith('$eflip'):

        sortedtoplist = eFlip()
        #I love try and except
        color = random.randint(0, 0xffffff)
        embed = discord.Embed(
            title="Nice!",
            description="Look up first, if you have the right collection!",
            color=color)
        embed.set_author(
            name="BazaarViewer",
            icon_url=
            "https://cdn.discordapp.com/attachments/703719065977487390/708392264086585364/yeeeeeeeeee.png"
        )

        try:
            embed.add_field(name="#1:", value=sortedtoplist[0], inline=False)
            try:
                embed.add_field(name="#2:",
                                value=sortedtoplist[1],
                                inline=False)
                try:
                    embed.add_field(name="#3:",
                                    value=sortedtoplist[2],
                                    inline=False)
                    try:
                        embed.add_field(name="#4:",
                                        value=sortedtoplist[3],
                                        inline=False)
                        try:
                            embed.add_field(name="#5:",
                                            value=sortedtoplist[4],
                                            inline=False)
                        except:
                            a = 0
                    except:
                        a = 0
                except:
                    a = 0
            except:
                a = 0
        except:
            embed.add_field(name="No items found",
                            value="Cant find any items :/",
                            inline=False)

        strColor = str(color)
        embed.set_footer(text="Color: " + strColor)
        await message.channel.send(embed=embed)
        print("replied to a message, e-flip!")
        toplist = []

    elif message.content.startswith('$reverseflip'):

        print("replied to a message!")
        sortedtoplist = ReverseFlip()
        color = random.randint(0, 0xffffff)
        embed = discord.Embed(title="Gettin that cash", color=color)
        embed.set_author(
            name="BazaarViewer",
            icon_url=
            "https://cdn.discordapp.com/attachments/703719065977487390/708392264086585364/yeeeeeeeeee.png"
        )
        i = 0
        for x in sortedtoplist:
            embed.add_field(name="#" + str(i + 1) + ":",
                            value=sortedtoplist[i],
                            inline=False)
            if i == 4:
                break
            i += 1

        await message.channel.send(embed=embed)

    elif message.content.startswith('$bestlog'):
        print("answered to BestLogs")

        #print(BestLogs())
        sortedtoplist = BestLogs()
        color = random.randint(0, 0xffffff)
        embed = discord.Embed(
            title="For how much can you sell each log?",
            description="You can sell every log for 2$ to the NPC.",
            color=color)
        embed.set_author(
            name="BazaarViewer",
            icon_url=
            "https://cdn.discordapp.com/attachments/703719065977487390/708392264086585364/yeeeeeeeeee.png"
        )
        i = 0
        for x in sortedtoplist:
            embed.add_field(name="#" + str(i + 1) + ":",
                            value=sortedtoplist[i],
                            inline=False)
            i += 1
        strColor = str(color)
        embed.set_footer(text="Color: " + strColor)
        await message.channel.send(embed=embed)
        sortedtoplist = []

    elif message.content.startswith("$pricedifference"):

        sortedtoplist = PriceDifference()
        color = random.randint(0, 0xffffff)
        embed = discord.Embed(title="Dont lose your money!", color=color)
        embed.set_author(
            name="BazaarViewer",
            icon_url=
            "https://cdn.discordapp.com/attachments/703719065977487390/708392264086585364/yeeeeeeeeee.png"
        )
        i = 0
        for x in sortedtoplist:
            embed.add_field(name="#" + str(i + 1) + ":",
                            value=sortedtoplist[i],
                            inline=False)
            if i == 4:
                break
            i += 1
        strColor = str(color)
        embed.set_footer(text="Color: " + strColor)
        await message.channel.send(embed=embed)

        toplist = []
        sortedtoplist = []

    elif message.content.startswith('$everymerchantflip'):
        sortedtoplist = MerchantFlip()
        a = 1
        b = 0
        color = random.randint(0, 0xffffff)
        embed = discord.Embed(title="Gettin that cash", color=color)
        embed.set_author(
            name="BazaarViewer",
            icon_url=
            "https://cdn.discordapp.com/attachments/703719065977487390/708392264086585364/yeeeeeeeeee.png"
        )
        i = 0
        for x in sortedtoplist:
            embed.add_field(name="#" + str(i + 1) + ":",
                            value=sortedtoplist[i],
                            inline=False)
            i += 1
        member = message.author
        channel = await member.create_dm()
        strColor = str(color)
        embed.set_footer(text="Color: " + strColor)
        await channel.send(embed=embed)
        await message.channel.send(
            "Ok, you should have gotten a DM with the prices.")

    elif message.content.startswith('$notifyme '):

        print("replied to a message, $notifyme")
        ############getting the product and the sender
        Person = message.author.id
        Product = message.content[10:].lower()
        #is this a existing product?
        if NormalName(Product) == "error":
            await message.channel.send(
                "Couldnt find the item " + Product +
                ". If you think it should be added, request it with $request "
                + Product + ".")
            #it is!
        else:
            time.sleep(1)
            #getting the buy/sell data:
            await message.channel.send(
                "Do you want to wait for the sell or buy price of the product to be a specific number? Enter buy or sell. You have 20 seconds."
            )
            buysell = await client.wait_for('message', timeout=20)
            if buysell.content == "sell" or buysell.content == "buy":
                buysell = buysell.content.lower()
                #nice!
                # when do you want to be notified?
                await message.channel.send(
                    "Ok, when do you want to be notified for the product " +
                    Product +
                    "? Please enter just a number. You have 20 seconds.")
                msg = await client.wait_for('message', timeout=20)
                try:
                    time.sleep(1)
                    Price = float(msg.content)
                    strPrice = str(Price)
                    await message.channel.send(
                        'Ok, you will be notified if the ' + buysell +
                        ' price of ' + Product + " is under " + strPrice +
                        "$.")
                    NewNotify(Product, Person, Price, buysell)
                except:
                    await message.channel.send(
                        "ERROR: Pleace just enter a number")
                    NewNotify(Product, Person, Price, buysell)

            else:
                await message.channel.send(
                    "ERROR: Please just say buy or sell.")

    elif message.content.startswith('$skyscore'):
        sortedtoplist = Score()
        color = random.randint(0, 0xffffff)
        embed = discord.Embed(title="Double check everything, still beta",
                              color=color)
        embed.set_author(
            name="BazaarViewer",
            icon_url=
            "https://cdn.discordapp.com/attachments/703719065977487390/708392264086585364/yeeeeeeeeee.png"
        )
        i = 0
        for x in sortedtoplist:
            embed.add_field(name="#" + str(i + 1) + ":",
                            value=sortedtoplist[i],
                            inline=False)
            if i == 4:
                break
            i += 1
        strColor = str(color)
        embed.set_footer(text="Color: " + strColor)
        await message.channel.send(embed=embed)

    elif message.content.startswith('$watchdog'):
        await message.channel.send(Watchdog())
Example #25
0
            if prediction is None:
                action = functools.partial(graphics_utils.draw_xcentered_text, text='No match', height=100)

            else:
                action = functools.partial(graphics_utils.draw_xcentered_text, text=prediction, height=100)

                email = face_identifier.get_email_for_name(prediction)
                match_action(email, timed_frame_modify)

            timed_frame_modify.add_modification(action, 3, 'nametext', exclusive=True)

        frame_counter += 1

        if Wakeup.are_we_awake():
            for frame_worker in awake_frame_workers:
                frame = frame_worker.process_frame(frame)

            # display the frame, potentially break the loop
            cv2.imshow('Video', frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            cv2.imshow('Video', frame)

if __name__ == "__main__":
    watchdog = Watchdog(action=Wakeup.run_wakeup_command)
    watchdog.start_watchdog()
    try:
        main_loop(watchdog)
    finally:
        watchdog.stop_watchdog()
Example #26
0
class DBMgr(object):
    def _GetConfigValue(self, key):
        try:
            ret = self.config_col.find_one({"_id": key})
            return ret["value"]
        except:
            return None

    def _SetConfigValue(self, key, value):
        self.config_col.replace_one({"_id": key}, {"value": value}, True)

    def _ReadConfigs(self):
        print("Setting up configs...")
        self.ROOM_DEFINITION = self._GetConfigValue("ROOM_DEFINITION")
        self.APPLIANCE_DEFINITION = self._GetConfigValue(
            "APPLIANCE_DEFINITION")
        self.SAMPLING_TIMEOUT_SHORTEST = self._GetConfigValue(
            "SAMPLING_TIMEOUT_SHORTEST")
        self.SAMPLING_TIMEOUT_LONGEST = self._GetConfigValue(
            "SAMPLING_TIMEOUT_LONGEST")
        self.WATCHDOG_TIMEOUT_USER = self._GetConfigValue(
            "WATCHDOG_TIMEOUT_USER")
        self.WATCHDOG_TIMEOUT_APPLIANCE = self._GetConfigValue(
            "WATCHDOG_TIMEOUT_APPLIANCE")

    def _ConstructInMemoryGraph(self):
        self.list_of_PM_values = {}
        self.list_of_temp_values = {}
        self.list_of_light_values = {}
        self.list_of_rooms = {}
        self.list_of_appliances = {}
        self.location_of_users = {}
        self.list_of_users = {}

        for room in self.ROOM_DEFINITION:
            room["PM"] = {}
            room["appliances"] = []
            room["users"] = []
            room["occupancy"] = 0
            self.list_of_rooms[room["id"]] = room

        for appliance in self.APPLIANCE_DEFINITION:
            appliance["value"] = 0
            appliance["total_users"] = 0
            appliance["rooms"].sort()
            self.list_of_appliances[appliance["id"]] = appliance
            for roomID in appliance["rooms"]:
                self.list_of_rooms[roomID]["appliances"] += [appliance["id"]]

        for room in self.ROOM_DEFINITION:
            self.list_of_rooms[room["id"]]["appliances"].sort()

        ## Finished appliance bipartite graph.

    def _HardcodeValues(self):
        if ("nwc1000m_light" in self.list_of_appliances):
            self.list_of_appliances["nwc1000m_light"]["value"] = 300
        if ("nwc10hallway_light" in self.list_of_appliances):
            self.list_of_appliances["nwc10hallway_light"]["value"] = 100
        if ("nwc10elevator_light" in self.list_of_appliances):
            self.list_of_appliances["nwc10elevator_light"]["value"] = 150
        if ("nwc8_light" in self.list_of_appliances):
            self.list_of_appliances["nwc8_light"]["value"] = 150
        if ("nwc7_light" in self.list_of_appliances):
            self.list_of_appliances["nwc7_light"]["value"] = 150
        if ("nwc1003b_light" in self.list_of_appliances):
            self.list_of_appliances["nwc1003b_light"]["value"] = 300

    def _GracefulReloadGraph(self):
        print('Reloading values...')
        try:
            latest_snapshot = self.todayCumulativeEnergy.find_one(
                sort=[("_log_timestamp", pymongo.DESCENDING)])
            if (latest_snapshot != None):
                self.cumulativeEnergy = latest_snapshot["value"]
                print("Loaded: " + str(self.cumulativeEnergy))
            else:
                print("Didn't recover cumulative energy")
        except Exception as e:
            print e
        try:
            latest_snapshot = self.snapshots_col_appliances.find_one(
                sort=[("timestamp", pymongo.DESCENDING)])
            if latest_snapshot != None:
                for applianceID in latest_snapshot["data"]:
                    value = latest_snapshot["data"][applianceID]["value"]
                    if value > 0:
                        if applianceID == "nwcM3_fcu" or applianceID == "nwcM4_fcu":
                            self.updateApplianceValue(applianceID, 0)
                            continue
                        elif applianceID == "nwc1000m_light":
                            self.updateApplianceValue(applianceID, 0)
                            continue
                        print('Recovered Appliance:', applianceID, value)
                        self.updateApplianceValue(applianceID, value)
            else:
                print('Appliance latest snapshot not found.')
        except Exception:
            add_log(
                'failed to recover appliance power values during graceful reload.',
                latest_snapshot)

        try:
            latest_snapshot = self.snapshots_col_users.find_one(
                sort=[("timestamp", pymongo.DESCENDING)])
            if latest_snapshot != None:
                for userID in latest_snapshot["data"]:
                    roomID = latest_snapshot["data"][userID]["location"]
                    print('Recovered Location:', userID, roomID)
                    #self.updateUserLocation(userID, roomID, None)
            else:
                print('User location latest snapshot not found.')
        except Exception:
            add_log('failed to recover user locations during graceful reload.',
                    latest_snapshot)

####################################################################
## Room ID and Appliance IDs functions #############################
####################################################################

    def RoomIdToName(self, id):
        return self.list_of_rooms[id]["name"]

    def RoomIDToLab(self, id):
        return self.list_of_rooms[id]["lab"]

    def ApplIdToName(self, id):
        return self.list_of_appliances[id]["name"]

    def ApplIdToVal(self, id):
        return self.list_of_appliances[id]["value"]

    def ApplIdToType(self, id):
        return self.list_of_appliances[id]["type"]

    def ApplIdToRoom(self, id):
        return self.list_of_appliances[id]["room"]
####################################################################

    def _encode(self, data, isPretty):
        return MongoJsonEncoder().encode(data)

    def __init__(self, start_bg_thread=True):
        self.dbc = pymongo.MongoClient()

        self.registration_col1 = self.dbc.db.registration_col1
        self.ranking = self.dbc.db.ranking
        self.config_col = self.dbc.db.config
        self.raw_data = self.dbc.db.raw_data
        self.snapshots_col_rooms = self.dbc.db.snapshots_col_rooms
        self.snapshots_col_appliances = self.dbc.db.snapshots_col_appliances
        self.DRL_recommendations = self.dbc.db.DRL_recommendations
        self.snapshots_col_users = self.dbc.db.snapshots_col_users
        self.web_registration = self.dbc.db.web_registration
        self.web_comfort_feedback = self.dbc.db.web_comfort_feedback
        self.web_rec_feedback = self.dbc.db.web_rec_feedback

        self.snapshots_parameters = self.dbc.db.snapshots_parameters
        self._latestSuccessShot = 0

        self._ReadConfigs()
        self.watchdog = Watchdog(self.WATCHDOG_TIMEOUT_USER,
                                 self.WATCHDOG_TIMEOUT_APPLIANCE)
        ## Data Structure Init: bipartite graph between rooms and appls
        ## TODO: Add a web interface to update config in db, and pull new config into memory.

        self._ConstructInMemoryGraph()
        ## Construct bipartite graph.
        # self._accumulator()
        #self._GracefulReloadGraph()
        ## Read appliance values from database; TODO: occupants location
        #self._HardcodeValues()

        # self.watchdogInit()

        self.recommender = recommenderSystem()
        if start_bg_thread:
            self.startDaemon()

        self.snapshots_col_rooms = self.dbc.db.snapshots_col_rooms
        self.snapshots_col_appliances = self.dbc.db.snapshots_col_appliances
        self.snapshots_col_users = self.dbc.db.snapshots_col_users

        ## Start the snapshot thread if not running "python DBMgr.py"
        ## (perform self-test if it is.)

        ##if __name__ != "__main__":
        ##	self.recover_from_latest_shot()

    def startDaemon(self):
        t = Thread(target=self._backgroundLoop, args=())
        t.setDaemon(True)
        t.start()
        t_rec = Thread(target=self._backgroundRecommender, args=())
        t_rec.setDaemon(True)
        t_rec.start()

    def _now(self):
        return calendar.timegm(datetime.datetime.utcnow().utctimetuple())

    def _toUnix(self, ts):
        return calendar.timegm(ts.utctimetuple())

    def _backgroundLoop(self):
        print("DBMGR _backgroundLoop started...")
        while True:
            time.sleep(self.SAMPLING_TIMEOUT_LONGEST)
            self.SaveShot()
#			self.watchdogCheckUser()
#			self.watchdogCheckAppliance()

    def _backgroundRecommender(self):
        print("Recommender System started...")
        while True:
            time.sleep(10)
            recommendations1, recommendations2, locations = self.recommender._loopRecommendations(
                self.location_of_users, self.list_of_rooms,
                self.list_of_appliances)
            self.save_recommendations(recommendations1, recommendations2,
                                      locations)
            time.sleep(60)

    def save_recommendations(self, recommendations1, recommendations2,
                             locations):
        self.DRL_recommendations.insert({
            "timestamp": datetime.datetime.utcnow(),
            "data1": recommendations1,
            "data2": recommendations2,
            "locations": locations
        })

    def modify_recommendations(self, person, r_type):
        entry = self.DRL_recommendations.find_one(sort=[("timestamp",
                                                         pymongo.DESCENDING)])
        recs1 = entry["data1"]
        recs2 = entry["data2"]
        location = entry["locations"]
        person = self.PID2Name(person)
        if person in recs1 and person in recs2 and person in location:
            if r_type == "move":
                new_recs = []
                for rec in recs1[person]:
                    print(rec)
                    if rec["t"] != "move":
                        new_recs.append(rec)
                recs1[person] = new_recs
                print("New number of recs: " + str(len(recs1[person])))
        self.DRL_recommendations.insert({
            "timestamp": datetime.datetime.utcnow(),
            "data1": recs1,
            "data2": recs2,
            "locations": location
        })

    def get_recommendations(self, person):
        entry = self.DRL_recommendations.find_one(sort=[("timestamp",
                                                         pymongo.DESCENDING)])
        recs1 = entry["data1"]
        recs2 = entry["data2"]
        location = entry["locations"]
        if person in recs1 and person in recs2 and person in location:
            data = {
                "recommendations1": recs1[person],
                "recommendations2": recs2[person],
                "loc": location[person]
            }
            return self._encode(data, True)
        return None

    def PID2Name(self, PID):
        condition = {}
        entry = self.web_registration.find_one({"PID": PID})
        if "name" in entry:
            name = entry["name"]
        elif "Name" in entry:
            name = entry["Name"]
        else:
            name = "Peter"
        return name

    def _getShotRooms(self, concise=True):
        return self.list_of_rooms

    def _getShotAppliances(self, concise=True):
        print("Getting Appliances")
        for appliance in self.list_of_appliances:
            print((appliance, self.list_of_appliances[appliance]["value"]))

        return self.list_of_appliances

    def _getShotPersonal(self, concise=True):
        personal_consumption = {}
        cached_per_room_consumption = {}
        for user_id in self.location_of_users:
            try:
                roomID = self.location_of_users[user_id]
                if roomID == None:
                    continue
                if (roomID not in cached_per_room_consumption):
                    cached_per_room_consumption[
                        roomID] = self.calculateRoomFootprint(roomID)
                personal_consumption[user_id] = cached_per_room_consumption[
                    roomID]
                personal_consumption[user_id]["location"] = roomID
            except:
                add_log("fail to trace person's consumption; id:", user_id)

        return personal_consumption

    def updateApplianceValue(self, applianceID, value):
        self.list_of_appliances[applianceID]["value"] = int(float(value))

    def calculateRoomFootprint(self, roomID):
        app_list = self.list_of_rooms[roomID]["appliances"]
        ret = {"value": 0, "consumptions": []}
        total_con = 0.0
        for applianceID in app_list:
            app = self.list_of_appliances[applianceID]
            app["share"] = app["value"] / (1.0 * app["total_users"])

            total_con += app["share"]
            ret["consumptions"] += [app]
        ret["value"] = total_con
        return ret

    def calculateLocationEnergyFootprint(self, roomID, encoded=True):
        ret = {"value": 0, "HVAC": 0, "Light": 0, "Electrical": 0}
        if (roomID is None):
            return ret
        app_list = self.list_of_rooms[roomID]["appliances"]
        total_con = 0.0
        for applianceID in app_list:
            app = self.list_of_appliances[applianceID]
            appValue = app["value"]
            total_con += appValue
            if (app["type"] == "Electrical"):
                ret["Electrical"] += appValue
                continue
            if (app["type"] == "HVAC"):
                ret["HVAC"] += appValue
                continue
            if (app["type"] == "Light"):
                ret["Light"] += appValue
        ret["value"] = total_con
        if (encoded):
            return self._encode(ret, False)
        return ret

    def calculateEnergyFootprint(self, roomID, encoded=True):
        ret = {"value": 0, "HVAC": 0, "Light": 0, "Electrical": 0}
        if (roomID is None):
            return ret
        app_list = self.list_of_rooms[roomID]["appliances"]
        total_con = 0.0
        print("starting appliances")
        for applianceID in app_list:
            app = self.list_of_appliances[applianceID]
            if app["total_users"] > 0:
                appValue = app["value"] / (1.0 * app["total_users"])
            else:
                appValue = app["value"]
            total_con += appValue
            if (app["type"] == "Electrical"):
                ret["Electrical"] += appValue
                continue
            if (app["type"] == "HVAC"):
                ret["HVAC"] += appValue
                continue
            if (app["type"] == "Light"):
                ret["Light"] += appValue
        ret["value"] = total_con
        if (encoded):
            return self._encode(ret, False)
        return ret

    def ReportPMValue(self, sensorID, PM1, PM25, PM10):
        self.list_of_PM_values[sensorID] = [
            float(PM1), float(PM25), float(PM10)
        ]
        PM2room = {
            "nwc10MPeter": ["nwc1000m_a5", "nwc1000m_a6"],
            "nwc10MJoe": ["nwc1000m_a1", "nwc1000m_a2"],
            "nwc1003B": ["nwc1003b_a", "nwc1003b_b"],
            "nwc1003G": ["nwc1003g", "nwc1003g_a", "nwc1003g_c"],
            "nwc1008_fcu": ["nwc1008"],
            "nwc1003E": ["nwc1003E"]
        }
        if sensorID not in PM2room:
            print("No spaces assigned to sensor " + sensorID)
            return
        for room in PM2room[sensorID]:
            self.list_of_rooms[room]["PM"]["PM1"] = PM1
            self.list_of_rooms[room]["PM"]["PM25"] = PM25
            self.list_of_rooms[room]["PM"]["PM10"] = PM10

    def ReportTempValue(self, applianceID, T, P, H):
        Temp2room = {
            "nwc1003B_parameters": ["nwc1003b_a", "nwc1003b_b"],
            "nwc1008_parameters": ["nwc1008"],
            "nwc1000m_a1_parameters": ["nwc1000m_a1"],
            "nwc1000m_a2_parameters": ["nwc1000m_a2"],
            "nwc1000m_a5_parameters": ["nwc1000m_a5"],
            "nwc1000m_a6_parameters": ["nwc1000m_a6"],
            "nwc1003g_parameters": ["nwc1003g"],
            "nwc1003gA_parameters": ["nwc1003g_a"],
            "nwc1003gB_parameters": ["nwc1003g_c"],
            "nwc1003E_parameters": ["nwc1003E"]
        }
        self.list_of_temp_values[applianceID] = [float(T), float(P), float(H)]
        if applianceID not in Temp2room:
            print("No spaces assigned to sensor " + applianceID)
            return
        for room in Temp2room[applianceID]:
            self.list_of_rooms[room]["Temperature"] = float(T)
            self.list_of_rooms[room]["Pressure"] = float(P)
            self.list_of_rooms[room]["Humidity"] = float(H)

    def ReportLightValue(self, applianceID, raw_value):
        self.list_of_light_values[applianceID] = raw_value

    def ReportEnergyValue(self, applianceID, value, raw_data=None):
        "maintenance tree node's energy consumption item, and update a sum value"
        known_room = None
        defaultValueFilter = {
            "nwc1003t2_vav": 19231,
            "nwc1003o1_vav": 1560,
            "nwc1003gA_vav": 3078,
            "nwc1003gC_vav": 3079,
            "nwc1003g1_vav": 537
        }
        try:
            if (applianceID not in self.list_of_appliances):
                print("applianceID " + applianceID +
                      " not in list of appliances.")
                return
            app = self.list_of_appliances[applianceID]
            if applianceID in defaultValueFilter and int(
                    value) == defaultValueFilter[applianceID]:
                return
            known_room = app["rooms"]
            if value < 0:
                add_log("Negative value found on energy report?", {
                    "deviceID": applianceID,
                    "value": value,
                    "raw": raw_data
                })
                return
            self.updateApplianceValue(app["id"], value)

        except:
            add_log(
                "failed to report energy value on device", {
                    "known_room": known_room,
                    "deviceID": applianceID,
                    "value": value,
                    "raw": raw_data
                })
            return

        self.LogRawData({
            "type": "energy_report",
            "roomID": known_room,
            "applianceID": applianceID,
            "value": value,
            "raw": raw_data
        })
        self.watchdog.watchdogRefresh_Appliance(applianceID)

    def watchdogRefresh_User(self, userID):
        if userID not in self.watchdog.watchdogLastSeen_User:
            self.watchdog.watchdogLastSeen_User[userID] = 0
        self.watchdog.watchdogLastSeen_User[userID] = max(
            self._now(), self.watchdog.watchdogLastSeen_User[userID])

    def updateUserLocation(self, user_id, in_id=None, out_id=None):
        self.location_of_users[user_id] = in_id
        if in_id == out_id:
            return
        ## TODO: optimize, merge In-ops and Out-ops and remove unnecessary update to common appliances
        if in_id != None and self.list_of_rooms[in_id] != None:
            self.list_of_rooms[in_id]["users"] += [user_id]
            for applianceID in self.list_of_rooms[in_id]["appliances"]:
                self.list_of_appliances[applianceID]["total_users"] += 1
        if out_id != None and self.list_of_rooms[out_id] != None:
            if (user_id in self.list_of_rooms[out_id]["users"]):
                self.list_of_rooms[out_id]["users"].remove(user_id)
                for applianceID in self.list_of_rooms[out_id]["appliances"]:
                    self.list_of_appliances[applianceID]["total_users"] -= 1

    def LogRawData(self, obj):
        obj["_log_timestamp"] = datetime.datetime.utcnow()
        self.raw_data.insert(obj)

    def watchdogCheckUser(self):
        outOfRange_List = []
        minTime = self._now() - self.watchdog.WATCHDOG_TIMEOUT_USER

        for userID in self.watchdog.watchdogLastSeen_User:
            if self.watchdog.watchdogLastSeen_User[userID] < minTime:
                outOfRange_List += [userID]
                if userID in location_of_users:
                    oldS = location_of_users[userID]
                    self.updateUserLocation(userID, "outOfLab", oldS)
        for userID in self.location_of_users:
            if userID not in self.watchdog.watchdogLastSeen_User:
                oldS = self.location_of_users[userID]
                self.updateUserLocation(userID, "outOfLab", oldS)

        self.LogRawData({
            "type": "watchdogCheck_User",
            "time": self._now(),
            "minTime": minTime,
            "outOfRange_List": outOfRange_List,
            "raw": self.watchdog.watchdogLastSeen_User,
        })
        for userID in outOfRange_List:
            if (userID in self.watchdog.watchdogLastSeen_User):
                last_seen = self.watchdog.watchdogLastSeen_User[userID]
            else:
                last_seen = None
            #self.ReportLocationAssociation(userID, "outOfLab", {"Note":"Reported by Watchdog","last_seen": last_seen})

    def ReportUserData(self, person, camera, x, y, loc, temperature):
        if camera == "thermal5":
            if person not in ["Peter", "Yanchen"]:
                person = "Peter"
            self.list_of_users[person] = {
                "camera": camera,
                "x": int(x),
                "y": int(y),
                "location": loc,
                "temp": temperature
            }
            self.ReportLocationAssociation(person, loc)

        if person in ["Joe", "Mark"] and camera != "thermal3":
            return
        if person in ["Lei"] and camera != "thermal3" and camera != "thermal2":
            return
        if person in ["Abhi", "Ankur"] and camera != "thermal6":
            return
        if person in [
                "Peter", "Yanchen"
        ] and camera != "thermal5" and camera != "thermal1" and camera != "thermal7":
            return
        if person in ["Fred"] and camera != "thermal4":
            return
        if camera == "thermal4":
            self.list_of_users["Fred"] = {
                "camera": camera,
                "x": int(x),
                "y": int(y),
                "temp": temperature
            }
            self.ReportLocationAssociation("Fred", "nwc1008")
            return
        if person in ["Unknown"]:
            return
        self.list_of_users[person] = {
            "camera": camera,
            "x": int(x),
            "y": int(y),
            "location": loc,
            "temp": temperature
        }
        self.ReportLocationAssociation(person, loc)

    def ReportLocationAssociation(self, personID, roomID, raw_data=None):
        #self.watchdogUserLastSeen()
        print("Reporting Location for user:"******"type": "location_report",
            "roomID": roomID,
            "personID": personID,
            "raw": raw_data,
            "oldS": oldS,
            "newS": newS
        })
        self.watchdogRefresh_User(personID)

        if roomID != None and roomID not in self.list_of_rooms:
            #"if no legitimate roomID, then he's out of tracking."
            newS = None
            # self.recordEvent(personID,"illegitimateLocationReported",roomID)
        # else:
        # self.recordEvent(personID,"locationChange",roomID)

        self.updateUserLocation(personID, newS, oldS)
        #print("\n\n\n\n\n\n\n")
        #print(self.location_of_users)
        #print("\n\n\n\n\n\n\n")

        if newS != None:
            self.list_of_rooms[newS]["phantom_user"] = personID
            self.list_of_rooms[newS]["phantom_time"] = int(
                time.mktime(datetime.datetime.now().timetuple()))

        #"people change; should we update now?"
#		self.OptionalSaveShot();

    def watchdogCheckAppliance(self):
        notWorking_List = []
        minTime = self._now() - self.watchdog.WATCHDOG_TIMEOUT_APPLIANCE
        futureTime = self._now() + 86400

        #for applID in self.watchdogLastSeen_Appliance:
        for applID in self.list_of_appliances:
            if self.list_of_appliances[applID]["value"] > 0:
                # for all working(value>0) appliances
                if applID in self.watchdog.watchdogLastSeen_Appliance:
                    if self.watchdog.watchdogLastSeen_Appliance[
                            applID] < minTime:
                        notWorking_List += [applID]
                else:
                    #start-up issue, maybe the first report haven't arrived yet.
                    self.watchdog.watchdogLastSeen_Appliance[
                        applID] = self._now()

        for applID in notWorking_List:
            last_seen = self.watchdog.watchdogLastSeen_Appliance[applID]
            self.watchdog.watchdogLastSeen_Appliance[applID] = futureTime
            self.ReportEnergyValue(applID, 0, {
                "Note": "Reported by Watchdog",
                "last_seen": last_seen
            })

        title = "Energy Monitoring Appliance Down: " + str(notWorking_List)
        body = "Dear SysAdmin,\nThe following appliance ID has not been reporting to the system for >15 minutes."
        body += "\n\n" + "\n".join([str(x) for x in notWorking_List]) + "\n\n"
        body += "Please debug as appropriate.\nNote: this warning will repeat every 24 hours."
        body += "\n\nSincerely, system watchdog."

        if len(notWorking_List) > 0:
            email_ret = SendEmail(title, body)

    def ShowRealtime(self, person=None, concise=True):
        #save into database, with: timestamp, additional data
        ret = {"timestamp": self._now()}
        if person and person in self.location_of_users:
            roomID = self.location_of_users[person]
            if roomID != None:
                ret["personal"] = self.calculateRoomFootprint(roomID)
                #ret["location"]=roomID
                ret["location"] = self.list_of_rooms[roomID]
        else:
            ret["rooms"] = self._getShotRooms(concise)
            ret["appliances"] = self._getShotAppliances(concise)
            ret["personal"] = self._getShotPersonal(concise)
            ret["locations"] = self.location_of_users
            ret["watchdog_user"] = self.watchdog.watchdogLastSeen_User
            ret["watchdog_appl"] = self.watchdog.watchdogLastSeen_Appliance
        return self._encode(ret, True)

    def DashboardRequest(self, PID):
        ret = {"timestamp": self._now()}
        condition = {"PID": PID}
        recs = self.web_rec_feedback.find(condition)
        titles = {}
        total_energy = 0
        total_comfort = 0
        total_aq = 0
        setpoints = 0
        moves = 0
        unknown = 0
        for rec in recs:
            title = rec["title"]
            if title[0] == "C":
                setpoints += 1
            elif title[0] == "M":
                moves += 1
            else:
                unknown += 1
            energy = rec["energy"]
            energy = int(energy[:-3])
            total_energy += energy
            aq = rec["aq"]
            aq = int(aq[:-1])
            total_aq += aq
            comfort = rec["comfort"]
            comfort = int(comfort[:-1])
            total_comfort += comfort

            if title in titles:
                titles[title] += 1
            else:
                titles[title] = 1

        max_title = ""
        max_title_count = 0
        for title in titles:
            if titles[title] > max_title_count:
                max_title_count = titles[title]
                max_title = title
        ret["max_title"] = max_title
        ret["max_title_count"] = max_title_count
        ret["cum_energy"] = total_energy
        ret["cum_comfort"] = total_comfort
        ret["cum_aq"] = total_aq
        ret["setpoints"] = setpoints
        ret["moves"] = moves
        ret["unknown"] = unknown
        return self._encode(ret, True)

    def ShowRealtimeGraphs(self, single=True, concise=True):
        ret = {"timestamp": self._now()}
        condition = {
            "timestamp": {
                "$gte":
                datetime.datetime.utcnow() - datetime.timedelta(hours=1),
                "$lt": datetime.datetime.utcnow()
            }
        }
        energy = {}
        timestamps = {}
        if single:
            shot = self.snapshots_col_appliances.find_one(
                sort=[("timestamp", pymongo.DESCENDING)])
            appliance_list = shot["data"]
            for appliance in appliance_list:
                if appliance not in energy:
                    assert (appliance not in timestamps)
                    energy[appliance] = []
                    timestamps[appliance] = []
                energy[appliance].append(appliance_list[appliance]["value"])
                timestamps[appliance].append(shot["timestamp"])
        else:
            iterator = self.snapshots_col_appliances.find(condition).sort([
                ("timestamp", pymongo.DESCENDING)
            ])
            for shot in iterator:
                appliance_list = shot["data"]
                for appliance in appliance_list:
                    if appliance not in energy:
                        assert (appliance not in timestamps)
                        energy[appliance] = []
                        timestamps[appliance] = []
                    energy[appliance].append(
                        appliance_list[appliance]["value"])
                    timestamps[appliance].append(shot["timestamp"])
        ret["rooms"] = self._getShotRooms(concise)
        ret["appliances"] = self._getShotAppliances(concise)
        ret["applianceEnergy"] = energy
        ret["applianceTimestamps"] = timestamps
        ret["personal"] = self._getShotPersonal(concise)
        ret["locations"] = self.location_of_users
        ret["watchdog_user"] = self.watchdog.watchdogLastSeen_User
        ret["watchdog_appl"] = self.watchdog.watchdogLastSeen_Appliance

        return self._encode(ret, True)

    def ShowRealtimePMParameters(self):
        ret = {"timestamp": self._now()}
        PM1_dict, PM25_dict, PM10_dict = {}, {}, {}
        for sensorID in self.list_of_PM_values:
            (PM1, PM25, PM10) = self.list_of_PM_values[sensorID]
            PM1_dict[sensorID] = PM1
            PM25_dict[sensorID] = PM25
            PM10_dict[sensorID] = PM10
        ret["PM1"] = PM1_dict
        ret["PM25"] = PM25_dict
        ret["PM10"] = PM10_dict
        return self._encode(ret, True)

    def ShowRealtimeTempParameters(self):
        ret = {"timestamp": self._now()}
        T_dict, P_dict, H_dict = {}, {}, {}
        for applianceID in self.list_of_temp_values:
            (T, P, H) = self.list_of_temp_values[applianceID]
            T_dict[applianceID] = T
            P_dict[applianceID] = P
            H_dict[applianceID] = H
        ret["raw_temp_values"] = T_dict
        ret["raw_pressure_values"] = P_dict
        ret["raw_humidity_values"] = H_dict
        return self._encode(ret, True)

    def ShowRealtimeLights(self):
        ret = {"timestamp": self._now()}
        appliancePower = {}
        ret["raw_light_values"] = self.list_of_light_values
        for applianceID in self.list_of_light_values:
            if applianceID in self.list_of_appliances:
                appliancePower[applianceID] = self.list_of_appliances[
                    applianceID]["value"]
        ret["power"] = appliancePower
        return self._encode(ret, True)

    def ShowRealtimeUsers(self):
        ret = {"timestamp": self._now()}
        ret["locations"] = self.location_of_users
        ret["watchdog_user"] = self.watchdog.watchdogLastSeen_User
        ret["energy"] = {}
        for user in self.location_of_users:
            location = self.location_of_users[user]
            energy = self.calculateEnergyFootprint(location, encoded=False)
            ret["energy"][user] = [
                energy["value"], energy["HVAC"], energy["Light"],
                energy["Electrical"]
            ]
        return self._encode(ret, True)

    def ShowRealtimeDashboard(self, locations):
        ret = {"timestamp": self._now()}
        location_data = {}
        for location in locations:
            data = {}
            PM1 = 10
            PM25 = 5
            PM10 = 12.5
            if "PM" in self.list_of_rooms[
                    location] and "PM1" in self.list_of_rooms[location]["PM"]:
                PM1 = self.list_of_rooms[location]["PM"]["PM1"]
                PM25 = self.list_of_rooms[location]["PM"]["PM25"]
                PM10 = self.list_of_rooms[location]["PM"]["PM10"]
            if (location not in self.list_of_rooms):
                print(location + " not found")
                continue
            T, P, H = 0, 0, 0
            if "Temperature" in self.list_of_rooms[location]:
                T = self.list_of_rooms[location]["Temperature"]
                P = self.list_of_rooms[location]["Pressure"]
                H = self.list_of_rooms[location]["Humidity"]
            energyDict = self.calculateLocationEnergyFootprint(location,
                                                               encoded=False)
            if location == "nwc1003b_b":
                energyDict = self.calculateLocationEnergyFootprint(
                    "nwc1003b_c", encoded=False)
            data["HVAC"] = energyDict["HVAC"]
            data["Light"] = energyDict["Light"]
            data["Electrical"] = energyDict["Electrical"]
            data["PM1"] = PM1
            data["PM25"] = PM25
            data["PM10"] = PM10
            data["Temperature"] = T
            data["Pressure"] = P
            data["Humidity"] = H
            location_data[location] = data
        if "nwc1000m_a1" in location_data and "nwc1000m_a2" in location_data:
            location_data["nwc1000m_a1"]["Light"] = location_data[
                "nwc1000m_a2"]["Light"]
        ret["location_data"] = location_data
        #		users = {"Peter": {"x":125, "y":60, "floor":"10", "temp":94}}
        users = self.list_of_users
        print(users)
        ret["occupants"] = users
        return self._encode(ret, True)

    def SaveParameters(self, parameters):
        self.snapshots_parameters.insert({
            "timestamp":
            datetime.datetime.utcnow(),
            "data":
            parameters
        })

    def SaveShot(self, any_additional_data=None):
        #save into database, with: timestamp, additional data
        # self.accumulate()
        # obj = {"value":self.cumulativeEnergy}
        # obj["_log_timestamp"]=datetime.datetime.utcnow()
        # self.todayCumulativeEnergy.insert(obj)

        timestamp = datetime.datetime.utcnow()
        self.snapshots_col_rooms.insert({
            "timestamp": datetime.datetime.utcnow(),
            "data": self._getShotRooms()
        })

        self.snapshots_col_appliances.insert({
            "timestamp":
            datetime.datetime.utcnow(),
            "data":
            self._getShotAppliances()
        })

        self.snapshots_col_users.insert({
            "timestamp": datetime.datetime.utcnow(),
            "data": self._getShotPersonal()
        })

        self._latestSuccessShot = self._now()
        return True

    def OptionalSaveShot(self):
        #"minimum interval: 10s; in lieu with regular snapshotting"
        if self._latestSuccessShot < self._now() - 10:
            self.SaveShot()

    def recordOccupancy(self, room, occupancy):
        if room in self.list_of_rooms:
            self.list_of_rooms[room]["occupancy"] = occupancy
        else:
            print("No room called " + room + " to assign occupancy " +
                  str(occupancy))

    def addLocationSample(self, label, sample):
        return self.dbc.loc_db.sample_col.insert({
            "label":
            label,
            "sample":
            sample,
            "timestamp":
            datetime.datetime.utcnow()
        })

    def getAllLocationSamples(self):
        return list(self.dbc.loc_db.sample_col.find())

    def DestroyLocationSamples(self):
        self.dbc.loc_db.sample_col.remove({})

    def getAllUsers(self):
        usernames = []
        users = list(self.registration_col1.find())
        #print users
        for user in users:
            if "name" not in user:
                continue
            #print "debug", user
            usernames.append(user["name"])
        nameList = ",".join(usernames)
        ret = {"names": usernames}
        return self._encode(ret, False)
####################################################################
## Login Information, for self.registration_col1 ###################
####################################################################

    def screenNameCheckAvailability(self, screenName):
        return len(
            list(self.registration_col1.find({"screenName": screenName}))) == 0

    def deviceIDCheckAvailability(self, deviceID):
        return len(list(self.registration_col1.find({"userID":
                                                     deviceID}))) == 0

    def screenNameRegister(self, screenName, userID, control=True):
        self.LogRawData({
            "type": "screenNameRegister",
            "time": self._now(),
            "screenName": screenName,
            "userID": userID,
            "rewards": 0,
            "tempRewards": 0
        })
        try:
            self.registration_col1.insert({
                "screenName": screenName,
                "userID": userID,
                "control": control,
                "balance": 0,
                "tempBalance": 0
            })
            return True
        except pymongo.errors.DuplicateKeyError:
            return False

    def userIDRemoveAll(self, userID):
        self.registration_col1.remove({"userID": userID})

    def updateName(self, deviceID, username):
        itm = self.registration_col1.find_one({"screenName": username})
        if (itm is None):
            return False
        self.registration_col1.update({"screenName": username},
                                      {"$set": {
                                          "userID": deviceID
                                      }},
                                      multi=True)
        return True

    def fullRegistration(self, deviceID, name, email, password):
        try:
            self.registration_col1.insert({"userID": deviceID})
            print("successfully inserted new user")
            self.registration_col1.update({"userID": deviceID}, {
                "$set": {
                    "name": name,
                    "email": email,
                    "password": password,
                    "control": True,
                    "balance": 0,
                    "tempBalance": 0,
                    "loggedIn": True
                }
            })
            return True
        except pymongo.errors.DuplicateKeyError:
            return False

    def checkLoginFlow(self, deviceID):
        user = self.registration_col1.find_one({"userID": deviceID})
        if user is not None:
            if user.get("loggedIn"):
                return "0"  #user is logged in
            else:
                return "1"  #user not logged in
        return "404"  #user not registered

    def getNameFromDeviceID(self, deviceID):
        user = self.registration_col1.find_one({"userID": deviceID})
        return user.get("name")

    def login(self, deviceID, email, password):
        user = self.registration_col1.find_one({"userID": deviceID})
        if user is not None:
            if (user.get("email") == email) and (user.get("password")
                                                 == password):
                self.registration_col1.update({"userID": deviceID},
                                              {"$set": {
                                                  "loggedIn": True
                                              }})
                return "0"
            else:
                return "1"
        user = self.registration_col1.find_one({
            "email": email,
            "password": password
        })
        if user is None:
            return "404"
        newInput = {}
        newInput["userID"] = deviceID
        newInput["control"] = user.get("control")
        newInput["password"] = user.get("password")
        newInput["name"] = user.get("name")
        newInput["loggedIn"] = True
        newInput["tempBalance"] = user.get("tempBalance")
        newInput["balance"] = user.get("balance")
        newInput["email"] = user.get("email")
        self.registration_col1.insert(newInput)
        return "0"

    def logout(self, deviceID):
        try:
            self.registration_col1.update({"userID": deviceID},
                                          {"$set": {
                                              "loggedIn": False
                                          }})
            return "0"
        except pymongo.errors.DuplicateKeyError:
            return "1"

    def addPushToken(self, deviceID, token):
        try:
            self.registration_col1.update({"userID": deviceID},
                                          {"$set": {
                                              "token": token
                                          }})
            return "0"
        except pymongo.errors.DuplicateKeyError:
            return "404"
        return "400"

    def addDeviceToken(self, deviceID, deviceToken):
        try:
            user = self.registration_col1.find_one({"userID": deviceID})
            if (user is not None):
                if ("devices" not in user):
                    self.registration_col1.update(
                        {"userID": deviceID},
                        {"$set": {
                            "devices": [deviceToken]
                        }})
                    return "0"
                else:
                    devices = user.get("devices")
                    devices.append(deviceToken)
                    self.registration_col1.update(
                        {"userID": deviceID}, {"$set": {
                            "devices": devices
                        }})
                    return "0"
            return "1"
        except pymongo.errors.DuplicateKeyError:
            return "404"
        return "400"

    def userIDLookup(self, userID):
        ret = list(self.registration_col1.find({"userID": userID}))
        if len(ret) != 1:
            return None
        if "name" in ret[0]:
            return ret[0]["name"]
        return ret[0]["screenName"]

    def getControl(self, userID):
        user = self.registration_col1.find_one({"userID": userID})
        if user != None:
            if "control" in user:
                return user.get("control")
        return True

    def getUserLocation(self, user_id):
        if user_id in self.location_of_users:
            return self.location_of_users[user_id]
        else:
            return None

    def getUserTempBalance(self, deviceID):
        U = list(self.registration_col1.find({"userID": deviceID}))
        if (len(U) == 0):
            return None
        doc = U[0]
        return doc["tempBalance"]

    def getUserBalance(self, deviceID):
        U = list(self.registration_col1.find({"userID": deviceID}))
        if (len(U) == 0):
            return None
        doc = U[0]
        return doc["balance"]

    def getAttributes(self, username, encodeJson=True):
        json_return = {
            "username": "******",
            "frequency": 0,
            "wifi": True,
            "public": True,
            "lab": 0,
            "affiliation": 0
        }
        itm = self.ranking.find_one({"user": username})

        json_return["username"] = username
        if (itm == None):
            #print("username not found: " + username)
            if (encodeJson == True):
                return self._encode(json_return, False)
            else:
                return json_return
        json_return["lab"] = self.labInt(itm.get("lab"))
        json_return["affiliation"] = self.affiliationInt(
            itm.get("affiliation"))
        json_return["frequency"] = itm.get("frequency")
        json_return["wifi"] = itm.get("wifi")
        json_return["public"] = itm.get("public")
        if (encodeJson == True):
            return self._encode(json_return, False)
        else:
            return json_return

    def webLogin(self, PID):
        user = self.web_registration.find_one({"PID": PID})
        registered = (user is not None)
        if registered and "name" in user:
            print("Login: "******"name"])
        return registered

    def webSubmitComfort(self, PID, comfort):
        comfortFeedback = {
            "timestamp": self._now(),
            "PID": PID,
            "comfort": int(comfort)
        }
        return self.web_comfort_feedback.insert(comfortFeedback)

    def SubmitRecommendations(self, PID, recs, location):
        one_selected = False
        two_selected = False
        for rec in recs:
            print((rec, rec["list"], rec["title"][0]))
            print((rec["list"] == 1, rec["title"][0] == "M"))
            if rec["list"] == 1:
                if rec["title"][0] == "M":
                    self.modify_recommendations(PID, "move")
                one_selected = True
            if rec["list"] == 2:
                two_selected = True
            recFeedback = {
                "timestamp": self._now(),
                "location": location,
                "PID": PID,
                "list": rec["list"],
                "energy": rec["energy"],
                "comfort": rec["comfort"],
                "aq": rec["aq"],
                "global": rec["global"],
                "title": rec["title"],
                "desc": rec["desc"],
                "opt": rec["opt"],
                "rank": rec["rank"]
            }
            self.web_rec_feedback.insert(recFeedback)
        if not one_selected:
            recFeedback = {
                "location": location,
                "timestamp": self._now(),
                "PID": PID,
                "list": 1,
                "rank": -1
            }
            self.web_rec_feedback.insert(recFeedback)
        if not two_selected:
            recFeedback = {
                "location": location,
                "timestamp": self._now(),
                "PID": PID,
                "list": 2,
                "rank": -1
            }
            self.web_rec_feedback.insert(recFeedback)
        return True

    def labInt(self, x):
        return {
            'Burke Lab': 1,
            'Teherani Lab': 2,
            'Professor Teherani\'s Lab': 2,
            'Jiang Lab': 3,
            'Sajda Lab': 4,
            'Danino Lab': 5
        }[x]

    def affiliationInt(self, x):
        return {'Student': 1, 'Professor': 2, 'Employee': 1}[x]
                result = False
            else:
                # unknown error occurred or wrong
                result = False

        return make_summary(result, grader_msg, grader_checks, str(error))

    def tidy(self):
        pass


if __name__ == "__main__":
    loglevel = logging.INFO
    if len(sys.argv) > 1:
        if sys.argv[1] == "DEBUG":
            loglevel = logging.DEBUG

    # Initialize logging
    logging.basicConfig(
        filename="poller.log",
        filemode='a',
        format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
        datefmt='%m-%d %H:%M:%S',
        level=loglevel)

    def poll():
        Poller_Isa().run()

    poller = Watchdog(poll)
    poller.watch()
Example #28
0
class TestWatchdogClass(unittest.TestCase):

    def setUp(self):
        def zombiefn():
            pass
        self.watchdog = Watchdog(connection=Mockup())

    def tearDown(self):
        self.watchdog = None

    @patch("watchdog.subprocess")
    def test_put(self, subprocess):
        subprocess.check_output.return_value = True
        subprocess.call.return_value = True
        test_msg = {
            "id": 12345,
            "method": "put",
            "resource": "/system/watchdog"
        }

        # case 1: no data attribute
        def resp1(code=200, data=None):
            self.assertEqual(400, code)
            self.assertEqual(data, {"message": "Invalid Input."})
        message = Message(test_msg)
        self.watchdog.put_root(message, response=resp1, test=True)

        # case 2: data dict is empty or no enable exist
        def resp2(code=200, data=None):
            self.assertEqual(200, code)
        test_msg["data"] = dict()
        message = Message(test_msg)
        self.watchdog.put_root(message, response=resp2, test=True)

        # case 3: data
        def resp3(code=200, data=None):
            self.assertEqual(200, code)
        test_msg["data"] = {"path": "abcde"}
        message = Message(test_msg)
        self.watchdog.put_root(message, response=resp3, test=True)

    def test2_put(self):
        test1_msg = {
            "id": 1,
            "method": "put",
            "resource": "/system/watchdog"
            }

        # case 5: data
        def resp5(code=200, data=None):
            self.assertEqual(200, code)
        test1_msg["data"] = {"path": "somewhere", "process": "hello_world"}
        message = Message(test1_msg)
        self.watchdog.put_root(message, response=resp5, test=True)

        # case 9: data
        def resp9(code=200, data=None):
            self.assertEqual(200, code)
        self.watchdog.get_root(message, response=resp9, test=True)

    def test_delete(self):
        test2_msg = {
            "id": 1,
            "method": "put",
            "resource": "/system/watchdog/1",
            "param": {"id": 1}
            }

        # case 5: data
        def resp10(code=200, data=None):
            self.assertEqual(400, code)
        message = Message(test2_msg)
        self.watchdog.delete_root(message, response=resp10, test=True)

        # case 5: data
        def resp11(code=200, data=None):
            self.assertEqual(200, code)
        self.watchdog.processes = [1, 2, 3]
        message = Message(test2_msg)
        self.watchdog.monit_reload()
        self.watchdog.delete_root(message, response=resp11, test=True)

    def test_monit_reload(self):
        m = mock_open()
        with patch("watchdog.open", m, create=True):
            rc = self.watchdog.monit_reload()
            self.assertEqual(rc, True)

    def test_init(self):
        with patch("watchdog.ModelInitiator") as model:
            model.return_value.db.__getitem__.return_value = 1
            self.watchdog.init()
Example #29
0
    def run(self):
        print("%s in run loop" % self.name)
        hosts = Hosts().getHosts()
        imageHosts = []
        phraseHosts = []
        lastCacheId = 0
        for h in hosts:
            ip = h['ip']
            if Hosts().isLocalHost(ip):
                ImageHandler.clearCache(None)
            else:
                Hosts().sendToHost(ip, {
                    'cmd': 'ClearCache',
                    'args': None
                })
            iAltar = Hosts().getAttr(ip, 'iAltar')
            if iAltar['enabled']:
                if iAltar['image']:
                    Debug().p("%s: display type for %s: image" %
                              (self.name, ip))
                    imageHosts.append(ip)
                if iAltar['phrase']:
                    Debug().p("%s: wants phrase for %s: " % (self.name, ip))
                    phraseHosts.append(ip)
        sleepTime = .001
        while True:
            try:
                Debug().p("%s: sleeping %d" % (self.name, sleepTime))
                msg = self.queue.get(timeout=sleepTime)
                if msg == "__stop__":
                    print("%s: stopping" % self.name)
                break
            except Queue.Empty:
                pass

            Watchdog().feed(self)
            cacheId = random.randint(10000, 20000)
            images = []
            choices = []
            urls = []
            if self.searchType == 'Archive':
                [images, choices] = Archive().getArchive()
                for i in images:
                    Debug().p("%s: image %s" % (self.name, i))
                for c in choices:
                    Debug().p("%s: choice %s" % (self.name, c))
            elif self.searchType == 'Google':
                choices = []
                msg = doGetRecog('')
                if msg != "":
                    test = json.loads(msg)
                    Debug().p("%s got %s" % (self.name, test))
                    if test['recog'] != ["", ""]:
                        choices = test['recog']
                        Debug().p["%s choices from recog %s" %
                                  (self.name, choices)]
                if len(choices) == 0:
                    choices = Words().getWords()
                urls = Search().getUrls(choices)
                if urls == None:
                    Debug().p("%s Google Error switching to Archive" %
                              self.name)
                    self.searchType = "Archive"
                    continue
                if len(urls) == 0:
                    Debug().p("%s Nothing found try again" % self.name)
                    continue
                images = self.urlsToImages(Search().getUrls(choices))
            else:
                Debug().p("%s unimplemented type %s switching to archive" %
                          (self.name, searchType))
                self.searchType = 'Archive'
            if self.searchType != 'Archive':
                Archive().putArchive(choices)

            phraseArgs = {}
            if len(phraseHosts) != 0:
                phraseArgs['phrase'] = choices
                phraseArgs['phraseData'] = ""
                Debug().p("%s sending %s to %s" % (self.name, choices, ip))
                for ip in phraseHosts:
                    phr = Hosts().getAttr(ip, 'phrase')
                    if phr['voice']:
                        lang = random.choice(Specs().s['langList'])
                        phraseArgs['phraseData'] = makeSpeakData(
                            "%s %s" % (choices[0], choices[1]), lang)

                #os.unlink(file.replace("mp3","wav"));

            if len(imageHosts) != 0:
                numImages = len(images)
                imagesPerHost = numImages / len(imageHosts)
                extraImages = numImages % len(imageHosts)
                extra = 0
                count = 0
                Debug().p("%s numImages:%d imagesPerHost:%d extraImages:%d" %
                          (self.name, numImages, imagesPerHost, extraImages))
                for ip in imageHosts:
                    args = {}
                    args['id'] = cacheId
                    args['imgData'] = []
                    for i in range(0, imagesPerHost):
                        fname = images[i + count]
                        args['imgData'].append(self.setImgData(fname))
                    count += imagesPerHost
                    if extra < extraImages:
                        fname = images[count + extra]
                        args['imgData'].append(self.setImgData(fname))
                        extra += 1
                    cmd = {'cmd': "AddImage", 'args': args}
                    if Hosts().isLocalHost(ip):
                        ImageHandler().addImage(args)
                    else:
                        Hosts().sendToHost(ip, cmd)

                for ip in imageHosts:
                    args = [cacheId]
                    if Hosts().isLocalHost(ip):
                        ImageHandler().setImageDir(args)
                    else:
                        Hosts().sendToHost(ip, {
                            'cmd': 'SetImageDir',
                            'args': args
                        })

                if lastCacheId != 0:
                    for ip in imageHosts:
                        args = [lastCacheId]
                        if Hosts().isLocalHost(ip):
                            ImageHandler.rmCacheDir(args)
                        else:
                            Hosts().sendToHost(ip, {
                                'cmd': 'RmCacheDir',
                                'args': args
                            })
                lastCacheId = cacheId

            if len(phraseHosts) != 0:
                for ip in phraseHosts:
                    if Hosts().isLocalHost(ip):
                        PhraseHandler().setPhrase(phraseArgs)
                    else:
                        Hosts().sendToHost(ip, {
                            'cmd': 'Phrase',
                            'args': phraseArgs
                        })

            sleepTime = Specs().s['iAltarSleepInterval']
Example #30
0
 def setUp(self):
     def zombiefn():
         pass
     self.watchdog = Watchdog(connection=Mockup())
Example #31
0
logger = logging.getLogger()
fileConfig('logger.conf', defaults={'logfilename': 'Tri-Zone.log'})

from pinball import Pinball, switch_controller

SOLENOID_CONTROLLER_ADDRESS = 0x20


try:
    watchdog = None
    from watchdog import Watchdog
except:
    logger.warn("[!] Watchdog not imported")
else:
    watchdog = Watchdog(SOLENOID_CONTROLLER_ADDRESS)  # watchdog to make sure the solenoids don't stay on too long
    watchdog.start()

def main():
    pinball_game = Pinball()
    pinball_game.start()


if __name__ == '__main__':
    try:
        main()
    except Exception as ex:
        logger.critical(ex)

    switch_controller.stop_listening()
Example #32
0
    import coloredlogs, verboselogs
    import configparser

    logger = verboselogs.VerboseLogger("crocodile-hunter")
    fmt=f"\b * %(asctime)s crocodile-hunter - %(levelname)s %(message)s"
    coloredlogs.install(level="DEBUG", fmt=fmt, datefmt='%H:%M:%S')

    if not 'CH_PROJ' in os.environ:
        print("Please set the CH_PROJ environment variable")
        sys.exit()
    class Args:
        disable_gps = True
        disable_wigle = False
        debug = False
        project_name = os.environ['CH_PROJ']
        logger = logger
        config_fp = 'config.ini'
        config = configparser.ConfigParser(inline_comment_prefixes=('#', ';'))
        config.read(config_fp)

    w = Watchdog(Args)
    webui = Webui(w)
    SQL_PATH = f"mysql://*****:*****@localhost:3306"
    DB_PATH = f"{SQL_PATH}/{Args.project_name}"
    webui.app.config['SQLALCHEMY_DATABASE_URI'] = DB_PATH

    if 'db' in sys.argv:
        webui.manager.run()
    else:
        webui.start_daemon()
Example #33
0
    def run(self):
        self.logger.debug("run")
        telemetry = {}
        telemCoef = {
            'SatCount': 1,
            'outside_temp': 10,
            'inside_temp': 10,
            'barometer': 1,
            'battery': 100
        }
        exitFlag = False
        self.prev_gps_status = ""
        while not exitFlag:
            try:
                # blink
                self.ledState = 1 - self.ledState
                GPIO.output(self.config['pins']['LED1'], GPIO.HIGH)
                watchdog = Watchdog(60)

                # gps
                self.gps.loop()
                gpsdata = self.gps.get_data()
                self.calc_balloon_state(gpsdata)
                if gpsdata['status'] == "fix" and gpsdata['alt'] > 0:
                    self.sensors.calibrate_alt(gpsdata['alt'])
                if gpsdata['status'] != "fix":
                    gpsdata['alt'] = self.sensors.read_pressure()

                # sensors
                sensordata = self.sensors.get_data()
                sensordata.update(self.cam.status)
                status_bits = self.calc_status_bits(gpsdata, sensordata)
                telemetry[
                    'Satellites'] = gpsdata['SatCount'] * telemCoef['SatCount']
                telemetry['outside_temp'] = sensordata[
                    'outside_temp'] * telemCoef['outside_temp']
                telemetry['inside_temp'] = sensordata[
                    'inside_temp'] * telemCoef['inside_temp']
                telemetry['barometer'] = sensordata['barometer'] * telemCoef[
                    'barometer']
                telemetry[
                    'battery'] = sensordata['battery'] * telemCoef['battery']

                if gpsdata['status'] != self.prev_gps_status:
                    frame = self.aprs.create_telem_data_msg(
                        telemetry, status_bits, gpsdata['alt'])
                    self.modem.encode(frame)
                    self.modem.saveToFile(
                        os.path.join(self.tmp_dir, 'aprs.wav'))
                    self.radio_queue(self.config['frequencies']['APRS'],
                                     os.path.join(self.tmp_dir, 'aprs.wav'))
                    self.prev_gps_status = gpsdata['status']

                # UI
                self.webserver.update(gpsdata, sensordata, self.state)
                self.update_system_datetime(gpsdata)

                if self.timers.expired("APRS"):
                    if gpsdata['status'] == "fix":
                        self.logger.debug("sending location")
                        frame = self.aprs.create_location_msg(
                            gpsdata, telemetry, status_bits)
                    else:
                        self.logger.debug("sending only telemetry")
                        frame = self.aprs.create_telem_data_msg(
                            telemetry, status_bits, gpsdata['alt'])
                    self.modem.encode(frame)
                    self.modem.saveToFile(
                        os.path.join(self.tmp_dir, 'aprs.wav'))
                    self.radio_queue(self.config['frequencies']['APRS'],
                                     os.path.join(self.tmp_dir, 'aprs.wav'))
                    with open(os.path.join(self.tmp_dir, "flight.log"),
                              'a+') as f:
                        merged = dict()
                        merged.update(gpsdata)
                        merged.update(sensordata)
                        merged['datatime'] = datetime.datetime.now().isoformat(
                        )
                        f.write(json.dumps(merged, indent=2))
                        f.write(',\n')

                if self.timers.expired("APRS-META"):
                    frame = self.aprs.create_telem_name_msg(
                        telemetry, self.status_names)
                    self.modem.encode(frame)
                    self.modem.saveToFile(
                        os.path.join(self.tmp_dir, 'aprs.wav'))
                    self.radio_queue(self.config['frequencies']['APRS'],
                                     os.path.join(self.tmp_dir, 'aprs.wav'))

                    frame = self.aprs.create_telem_eqns_msg(telemCoef)
                    self.modem.encode(frame)
                    self.modem.saveToFile(
                        os.path.join(self.tmp_dir, 'coef.wav'))
                    self.radio_queue(self.config['frequencies']['APRS'],
                                     os.path.join(self.tmp_dir, 'coef.wav'))

                if self.timers.expired("Capture"):
                    self.capture_image()

                if self.timers.expired("Snapshot"):
                    self.imaging_counter += 1
                    cam_select = self.imaging_counter % CAMERAS
                    self.capture_image(archive=False)
                    self.prep_image(cam_select, gpsdata, sensordata)
                    self.webserver.snapshot()

                if self.timers.expired("Imaging"):
                    self.imaging_counter += 1
                    cam_select = self.imaging_counter % CAMERAS
                    cam_system = self.imaging_counter % (CAMERAS + 1)
                    self.logger.info("imageing trigger")
                    self.logger.debug("cam %s system %s" %
                                      (cam_select, cam_system))
                    self.capture_image(archive=False)
                    self.prep_image(cam_select, gpsdata, sensordata)
                    self.webserver.snapshot()
                    if cam_system == 0:
                        self.logger.info("->sstv")
                        _thread.start_new_thread(self.process_sstv, ())
                    else:
                        self.logger.info("->ssdv")
                        _thread.start_new_thread(self.process_ssdv, ())

                if self.timers.expired("PLAY-SSDV"):
                    self.logger.debug("sending ssdv")
                    self.radio_queue(self.config['frequencies']['APRS'],
                                     os.path.join("data", 'starting_ssdv.wav'))
                    self.radio_queue(self.config['frequencies']['APRS'],
                                     os.path.join("data", 'habhub.wav'))
                    self.radio_queue(self.config['frequencies']['APRS'],
                                     os.path.join(self.tmp_dir, 'ssdv.wav'))

                if self.timers.expired("PLAY-SSTV"):
                    self.logger.debug("sending sstv")
                    self.radio_queue(
                        self.config['frequencies']['APRS'],
                        os.path.join("data", 'switching_to_sstv.wav'))
                    self.radio_queue(self.config['frequencies']['SSTV'],
                                     os.path.join("data", 'starting_sstv.wav'))
                    self.radio_queue(self.config['frequencies']['SSTV'],
                                     os.path.join(self.tmp_dir, 'sstv.wav'))

                if self.timers.expired("Buzzer"):
                    for i in range(3):
                        GPIO.output(self.config['pins']['BUZZER'], GPIO.HIGH)
                        time.sleep(0.5)
                        GPIO.output(self.config['pins']['BUZZER'], GPIO.LOW)
                        time.sleep(0.5)

                GPIO.output(self.config['pins']['LED1'], GPIO.LOW)
                watchdog.stop()
                time.sleep(1)

            except Watchdog:
                self.logger.error("task timedout!")
            except KeyboardInterrupt:  # If CTRL+C is pressed, exit cleanly
                exitFlag = True
                self.gps.stop()
                break
            except Exception as x:
                self.logger.exception(x)
        self.logger.info("Done.")
    def recv(self, message, ref):
        action = message.get('action', None)

        if action == 'config':
            if self.manager.isrunning:
                self.debug("Reconfiguration process: Shutting down Manager",
                           color='cyan')
                self.manager.shutdown()

            # Initialize manager
            self.debug("Starting up manager", color='blue')
            self.manager.attach(
                WebServer(uuid.uuid4(), 'Local Webserver', self.__commit))
            watchdog_uuid = uuid.uuid4()
            self.manager.attach(Watchdog(watchdog_uuid, 'Watchdog', self))
            self.manager.set_watchdog(watchdog_uuid)

            # Get commit ID
            commit = message.get('commit', None)
            if commit:
                if self.__force_version:
                    self.debug("Using static version: {}".format(
                        self.__force_version),
                               color="cyan")
                else:
                    # Set new commit
                    self.debug("Setting COMMIT to: {}".format(commit),
                               color="cyan")
                    with open("commit.dat", "w") as F:
                        F.write(commit)

                    # Pull changes
                    cmd = "git show --format='%H' --no-patch"  # Long HASH
                    status, output = getstatusoutput(cmd)
                    if status:
                        lastcommit = output
                    else:
                        lastcommit = None

                    if commit != lastcommit:
                        status, output = getstatusoutput("git fetch origin")
                        if status:
                            self.error(
                                "Couldn't fetch changes from REPOSITORY, I won't try any MERGE!"
                            )
                        else:
                            if commit == 'LATEST':
                                extraarg = ''
                            else:
                                extraarg = ' ' + commit
                            status, output = getstatusoutput(
                                "git merge{}".format(extraarg))
                            if status:
                                self.error("Couldn't merge changes!")

            else:
                # Delete commit.dat
                self.warning("This client is not linked to GITHUB")
                if os.path.exists("commit.dat"):
                    os.unlink("commit.dat")

            # Configure hardware
            self.debug("Setting configuration", color='blue')

            error = False
            for hw in message.get('hardware', []):
                # Get details
                uuidtxt = hw.get('uuid', None)
                kind = hw.get('kind', '')
                config = hw.get('config', {})

                if uuidtxt is not None:
                    uid = uuid.UUID(uuidtxt)
                    if not self.manager.exists_worker(uid):
                        self.debug("    > Configuring ",
                                   color='yellow',
                                   tail=False)
                        self.debug(str(uid),
                                   color='purple',
                                   head=False,
                                   tail=False)
                        self.debug(" as ",
                                   color='yellow',
                                   head=False,
                                   tail=False)
                        if kind in self.AVAILABLE_HARDWARE:
                            self.debug(kind, color='white', head=False)
                            try:
                                self.manager.attach(
                                    self.AVAILABLE_HARDWARE.get(kind)(uid,
                                                                      config))
                            except HardwareError as e:
                                self.send_error(
                                    "Device {} as {} is wrong configured: {}".
                                    format(uid, kind, e), ref, uid)
                                error = True
                        else:
                            self.debug(
                                "{}??? - Not setting it up!".format(kind),
                                color='red',
                                head=False)
                else:
                    self.error(
                        "    > I found a hardware configuration without UUID, I will not set it up!"
                    )

            # Make sure all tasks in manager are running
            self.manager.run(self)

            # If some error during startup
            if error:
                self.error(
                    "I have detected some error, I will try to reconfigure system when next message arrives!"
                )
            else:
                # No error happened, we are ready to go
                self.debug("Everything is set up and ready to work",
                           color='green')
                self.__fully_configured = True

        elif action == 'reset':
            self.warning("Got Reset request from Server")
            self.close(reason='Reset requested from Server')
        elif action == 'msg':
            msg = message.get('message', None)
            uid = message.get('uuid', None)
            if msg and uid:
                error = self.manager.recv(msg, ref, uid)
                if error:
                    self.send_error(error, ref)
            elif msg:
                self.send_error("No destination added to your message", ref)
            elif uuid:
                self.send_error("Got message for '{}' with no content", ref)
            else:
                self.send_error(
                    "Missing message and destination for your message", ref)
        elif action == 'error':
            self.error("Got an error from server: {}".format(
                message.get('error', 'No error')))
        elif action == 'ping':
            subref = message.get('ref', '-')
            self.debug("Sending PONG {} (ref:{})".format(subref, ref))
            self.send({'action': 'pong'}, ref)
        elif action != 'config':
            self.send_error("Unknown action '{}'".format(action), ref)

        if action != 'config' and not self.__fully_configured:
            self.debug("Reconfigure system", color='yellow')
            self.send({'action': 'get_config'}, ref)
Example #35
0
    def work(cls, jobs, session, taskconf):

        if taskconf.ssh_identity:
            sshkey = ssh.PrivateKey(taskconf.ssh_identity)
        else:
            sshkey = ssh.TempPrivateKey()

        def status(msg):
            timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
            session.logs.manager.write("%s :: session %d %s\n" %
                                       (timestamp, session.id, msg))

        status("(pid %d)" % os.getpid())
        print >> session.logs.manager

        class CaughtSignal(CloudWorker.Terminated):
            pass

        def terminate(sig, f):
            signal.signal(sig, signal.SIG_IGN)
            sigs = dict([(getattr(signal, attr), attr) for attr in dir(signal)
                         if attr.startswith("SIG")])

            raise CaughtSignal("caught %s termination signal" % sigs[sig], sig)

        exception = None
        while True:
            watchdog = Watchdog(session.logs.manager, session.paths.workers,
                                taskconf)

            signal.signal(signal.SIGINT, terminate)
            signal.signal(signal.SIGTERM, terminate)

            executor = None

            work_started = time.time()
            try:
                executor = CloudExecutor(session.logs, taskconf, sshkey)
                for job in jobs:
                    executor(job)

                executor.join()
                executor_results = list(executor.results)

            except Exception, exception:
                if isinstance(exception, CaughtSignal):
                    print >> session.logs.manager, "# " + str(exception[0])

                elif not isinstance(
                        exception,
                    (CloudWorker.Error, CloudWorker.Terminated)):
                    traceback.print_exc(file=session.logs.manager)

                if executor:
                    executor.stop()
                    executor_results = list(executor.results)
                else:
                    executor_results = []

            watchdog.terminate()
            watchdog.join()

            session.jobs.update(jobs, executor_results)

            if len(session.jobs.pending
                   ) != 0 and executor_results and exception is None:
                print >> session.logs.manager
                status("(auto-resuming) %d pending jobs remaining" %
                       len(session.jobs.pending))
                print >> session.logs.manager

                jobs = list(session.jobs.pending)
                if taskconf.split > len(jobs):
                    taskconf.split = len(jobs)
            else:
                break
Example #36
0
import time
from watchdog import Watchdog

while True:
  dog = Watchdog('watchdog.ini')
  dog.ping()
  time.sleep(10)