def test_app_knownpools(self): app = ApplicationService(component='test') app.startup() pools = app.knownpools() self.assertTrue(len(pools) > 0) for pool in pools: self.assertTrue(isinstance(pool, AvailablePool))
def test_message_inapp(self): app = ApplicationService(component='test') values = '{"version":"1.1","sender":"fullcyclereact","type":"configuration","timestamp":"2018-09-16T07:18:34.431Z","body":"{\\"command\\":\\"save\\",\\"parameter\\":\\"\\",\\"id\\":\\"unknown\\",\\"entity\\":\\"miner\\",\\"values\\":[{\\"name\\":\\"S9102\\"},{\\"ipaddress\\":\\"test.com\\"},{\\"port\\":\\"4102\\"},{\\"location\\":\\"222\\"},{\\"in_service_date\\":null}]}"}' msg = app.messagedecode_configuration(values) self.assertTrue( isinstance(msg, messaging.messages.ConfigurationMessage)) self.assertTrue(msg.entity == 'miner') miner = Miner.create(msg.values) self.assertTrue(miner.name == "S9102")
def main(): mainapp = ApplicationService(component='discover', option='') mainapp.cache.purge() #this is the discovery process hosts_list = networkmap(mainapp.configuration.get('discover.dns'), \ mainapp.configuration.get('discover.minerport'), \ mainapp.configuration.get('discover.sshport')) discoveredentries = findminers(hosts_list, mainapp.knownminers()) dispatchmessages(mainapp, discoveredentries) print('done')
def test_app_json_serialize(self): app = ApplicationService(component='test') pool = AvailablePool('S9', None, 'url', 'user', 'x', 0) strpool = app.jsonserialize(AvailablePoolSchema(), pool) self.assertTrue(isinstance(strpool, str)) self.assertFalse(strpool.startswith('['))
'''a component that gathers stats and hearbeats from other components evaluate if this is going to be used or not ''' from helpers.queuehelper import QueueName from backend.fcmapp import ApplicationService APP = ApplicationService(component='component') def when_component(channel, method, properties, body): '''when the event happens''' try: print("[{0}] Received component message".format(APP.now())) docomponent(body.decode()) except Exception as ex: APP.logexception(ex) def docomponent(msg): '''do this''' APP.telegram.sendmessage(msg) print("Sent telegram {0}".format(msg)) Q = APP.subscribe(QueueName.Q_COMPONENT, when_component) APP.bus.listen(Q)
'''test telegram api''' from backend.fcmapp import ApplicationService, ComponentName APP = ApplicationService(ComponentName.fullcycle) MESSAGE = 'Full Cycle Mining test {}'.format(APP.now()) APP.sendtelegrammessage(MESSAGE) FILEOUT = APP.take_picture('fullcycle_camera.png') APP.sendtelegramphoto(FILEOUT) APP.shutdown()
'''test broadcast''' from helpers.queuehelper import BroadcastSender, QueueName #from messaging.messages import * #from domain import mining, rep from backend.fcmapp import ApplicationService, ServiceName APP = ApplicationService() QBROAD = BroadcastSender(QueueName.Q_LOG, APP.getservice(ServiceName.messagebus)) QBROAD.broadcast('{0} WORKING! test_broadcast {1}'.format( APP.now(), QBROAD.queue_name)) QBROAD.close()
'''#runs tasks on schedule #should read tasks from configuration #examples: when to discover. when to monitor. when to ... #will run tasks by sending command (raising event) ''' import datetime import time from helpers.queuehelper import QueueName, QueueEntry from helpers.taskschedule import TaskSchedule from backend.fcmapp import ApplicationService #one-time schedule provision when app starts up APP = ApplicationService(component='schedule') APP.startup() APP.send(QueueName.Q_POOLCONFIGURATIONCHANGED, '') SLEEP_SECONDS = APP.configuration.get('schedule.sleep.seconds') HEARTBEAT = TaskSchedule(run_on_init=True) HEARTBEAT.start = datetime.datetime.now().replace( microsecond=0, second=0, minute=0) + datetime.timedelta(hours=1) HEARTBEAT.interval = 60 * APP.configuration.get('schedule.hearbeat.minutes') MONITOR = TaskSchedule(run_on_init=True) MONITOR.interval = APP.configuration.get('schedule.monitor.seconds') DISCOVER = TaskSchedule(run_on_init=True) DISCOVER.interval = 60 * APP.configuration.get('schedule.discover.minutes') CAMERA = TaskSchedule(run_on_init=True) CAMERA.start = datetime.datetime.now().replace( microsecond=0, second=0, minute=0) + datetime.timedelta(hours=1)
'''purge all data. go back to clean slate''' from backend.fcmapp import ApplicationService print('Starting application...') APP = ApplicationService() print('started', APP.component) #purge cache APP.cache.purge() #purge rabbit APP.shutdown()
'''http://emmanuel-klinger.net/distributed-task-queues-for-machine-learning-in-python-celery-rabbitmq-redis.html''' import pika from backend.fcmapp import ApplicationService DURABLE = False APP = ApplicationService() CONNECTION = APP.bus.connection() CHANNEL = CONNECTION.channel() CHANNEL.queue_declare(queue='task_queue', durable=DURABLE) CHANNELR = CONNECTION.channel() CHANNELR.queue_declare(queue='result_queue', durable=DURABLE) def callback(chan, method, properties, body): print(body) vara, varb = list(map(int, body.decode()[1:-1].split(","))) res = str(vara + varb) CHANNELR.basic_publish(exchange='', routing_key='result_queue', body=res, properties=pika.BasicProperties( delivery_mode=2, )) chan.basic_ack(delivery_tag=method.delivery_tag) CHANNEL.basic_qos(prefetch_count=1) CHANNEL.basic_consume(callback, queue='task_queue') CHANNEL.start_consuming()
''' A test program that pushes monitoring messages onto the queue A stress tester for monitoring when_monitorminer.py should be running to process the messages ''' from helpers.queuehelper import QueueName, QueueEntries from backend.fcmapp import ApplicationService, ComponentName APP = ApplicationService(ComponentName.fullcycle) MINERS = APP.miners() cnt = 1 while cnt < 1000: entries = QueueEntries() for miner in MINERS: entries.add(QueueName.Q_MONITORMINER, APP.messageencode(miner)) APP.enqueue(entries) print('sent {} messages'.format(len(entries.entries))) cnt += 1
'''test connectivity with database''' #currently using MariaDB (mysql) #mysql Ver 15.1 Distrib 10.1.23-MariaDB, for debian-linux-gnueabihf (armv7l) using readline 5.2 import datetime from domain.logging import MinerLog from domain.loggingrepository import getminerlog from backend.fcmapp import ApplicationService, ComponentName APP = ApplicationService(ComponentName.fullcycle) LOG = MinerLog() LOG.createdate = datetime.datetime.utcnow() LOG.minerid = 'unit test' LOG.minername = 'unit test' LOG.action = 'unit test' APP.log_mineractivity(LOG) for log in getminerlog(APP.getsession()): print(log.__dict__)
'''purge all data. go back to clean slate''' from backend.fcmapp import ApplicationService print('Starting application...') APP = ApplicationService() print('started', APP.component) #purge cache APP.cacheclear() #purge rabbit APP.shutdown()
'''gets stats from a miner and serializes to disk''' import asyncio from concurrent.futures import ThreadPoolExecutor from colorama import Fore from backend.fcmapp import ApplicationService from domain.mining import MinerApiCall from helpers import antminerhelper print('Starting...') APP = ApplicationService(component='fullcycle') APP.print('started app. getting known miners') WORKER_THREADS = 1 MINER_MULTIPLIER = 1 #async def getstats_async(miner): # minerstats, minerinfo, statspolling, minerpool = await antminerhelper.stats(miner) # return minerstats, minerinfo, statspolling, minerpool def getstats(miner): '''poll miner''' minerstats, minerinfo, statspolling, minerpool = antminerhelper.stats( miner) return miner, minerstats, minerinfo, statspolling, minerpool def process_results(results): '''process all results''' totaltime = 0 for miner, minerstats, minerinfo, statspolling, minerpool in results: totaltime += statspolling.elapsed() * 1000
'''Full Cycle Mining Systems Check''' import datetime from colorama import Fore from backend.fcmapp import ApplicationService from domain.loggingrepository import getminerlog from domain.logging import MinerLog print('Starting application...') APP = ApplicationService() print('started', APP.component) APP.tryputcache(key='test', value='value') CACHE_VALUE = APP.safestring(APP.trygetvaluefromcache('test')) if CACHE_VALUE == 'value': print(Fore.GREEN + 'cache is working') else: print(Fore.RED + 'cache is broken') try: SUCCESS = APP.alert('Full Cycle diagnostics test') if SUCCESS: print(Fore.GREEN + 'message bus is working') else: print(Fore.RED + 'message bus is broken') except BaseException as ex: print(Fore.RED + 'message bus is broken') print(APP.exceptionmessage(ex)) try: LOG = MinerLog() LOG.createdate = datetime.datetime.utcnow()
'''Full Cycle Mining Systems Check''' import datetime from colorama import Fore import backend.fcmutils as utils from backend.fcmapp import ApplicationService from domain.loggingrepository import getminerlog from domain.logging import MinerLog print('Starting application...') APP = ApplicationService() print('started', APP.component) APP.cache.tryputcache(key='test', value='value') CACHE_VALUE = utils.safestring(APP.cache.trygetvaluefromcache('test')) if CACHE_VALUE == 'value': print(Fore.GREEN+'cache is working') else: print(Fore.RED+'cache is broken') try: SUCCESS = APP.alert('Full Cycle diagnostics test') if SUCCESS: print(Fore.GREEN+'message bus is working') else: print(Fore.RED+'message bus is broken') except BaseException as ex: print(Fore.RED+'message bus is broken') print(APP.exceptionmessage(ex)) try: LOG = MinerLog()
'''test telegram api''' from backend.fcmapp import ApplicationService, ComponentName APP = ApplicationService(ComponentName.fullcycle) MESSAGE = 'Full Cycle Mining test {}'.format(APP.now()) if APP.is_enabled_configuration('telegram'): APP.sendtelegrammessage(MESSAGE) else: print('telegram is not enabled in configuration') if APP.is_enabled_configuration('camera'): FILEOUT = APP.take_picture('fullcycle_camera.png') APP.sendtelegramphoto(FILEOUT) else: print('camera is not enabled in configuration') APP.shutdown()
def main(): '''main''' mainapp = ApplicationService(component='monitor', option='') entries = domonitor() dispatchmessages(mainapp, entries) print('done')
'''what to do when a sensor is read''' #import paho.mqtt.client as mqtt import sys #import jsonpickle from helpers import mydeviceshelper from helpers.queuehelper import QueueName, BroadcastListener from backend.fcmapp import ApplicationService APP = ApplicationService(component='sensor') def shutdown(): '''#TODO: make this get call on app shutdown''' print('Shutting down...') for miner in MINERS: DEVICES[miner.clientid].disconnect() QUEUE.close() sys.exit() MINERS = APP.miners() print("{0} miners configured".format(len(MINERS))) DEVICES = dict() for MINER in MINERS: DEVICES[MINER.clientid] = mydeviceshelper.startdevice( MINER.clientid, MINER.username, MINER.password) def when_miner_stats_updated(channel, method, properties, body): '''when miner statistics have been read'''
'''send command to miner''' from backend.fcmcomponent import ComponentName from backend.fcmapp import ApplicationService from helpers.antminerhelper import getminerconfig, print_response APP = ApplicationService(ComponentName.fullcycle) MINER = APP.getminerbyname('TEST') MINER.ftpport = '22' RESPONSE = getminerconfig(MINER, APP.sshlogin()) print_response(RESPONSE) ACCESS_ORIGINAL = APP.antminer.getaccesslevel(MINER) print('original', ACCESS_ORIGINAL) if ACCESS_ORIGINAL != 'restricted': APP.antminer.set_restricted(MINER) APP.antminer.waitforonline(MINER) ACCESS_RESTRICTED = APP.antminer.getaccesslevel(MINER) print('restricted', ACCESS_RESTRICTED) APP.antminer.set_privileged(MINER) APP.antminer.waitforonline(MINER) ACCESS_PRIVILEGED = APP.antminer.getaccesslevel(MINER) print('privileged', ACCESS_PRIVILEGED) #APP.antminer.restartssh(MINER) #APP.antminer.waitforonline(MINER) ACCESS_AFTERRESET = APP.antminer.getaccesslevel(MINER)
'''Test saving application data''' ''' Requires services to be running. Redis ''' from domain.mining import Miner from backend.fcmapp import ApplicationService, ComponentName APP = ApplicationService(ComponentName.fullcycle) KNOWN = APP.allminers() CNT = len(KNOWN) MINER = Miner('New Miner2', '', '', '192.168.1.2', '4028', '', '') APP.save_miner(MINER) KNOWN2 = APP.allminers() CNT2 = len(KNOWN2) print(CNT2) APP.save_miner(MINER) KNOWN3 = APP.allminers() CNT3 = len(KNOWN3) print(CNT3)
'''test telegram api''' from backend.fcmapp import ApplicationService, ComponentName APP = ApplicationService(ComponentName.fullcycle) MESSAGE = 'Full Cycle Mining test {}'.format(APP.now()) if APP.configuration.is_enabled('telegram'): APP.telegram.sendmessage(MESSAGE) else: print('telegram is not enabled in configuration') if APP.configuration.is_enabled('camera'): FILEOUT = APP.camera.take_picture('fullcycle_camera.png') APP.telegram.sendphoto(FILEOUT) else: print('camera is not enabled in configuration') APP.shutdown()
'''test telegram api''' from backend.fcmapp import ApplicationService, ComponentName APP = ApplicationService(ComponentName.fullcycle) HUMID, TEMPER = APP.readtemperature() MESSAGE = '{0}: Temp={1:0.1f}* Humidity={2:0.1f}%'.format(APP.now(), TEMPER.value or 0, HUMID.value or 0) APP.logdebug(MESSAGE) APP.shutdown()
'''test logging''' from backend.fcmapp import ApplicationService, ComponentName APP = ApplicationService(ComponentName.fullcycle) APP.loginfo('Unit Test Info') APP.logdebug('Unit Test Debug') APP.logerror('Unit Test Error')
'''test telegram api''' from backend.fcmapp import ApplicationService, ComponentName APP = ApplicationService(ComponentName.fullcycle) HUMID, TEMPER = APP.readtemperature() MESSAGE = '{0}: Temp={1:0.1f}* Humidity={2:0.1f}%'.format( APP.now(), TEMPER, HUMID) APP.sendtelegrammessage(MESSAGE) APP.shutdown()
'''test miner summary''' from backend.fcmapp import ApplicationService FCM = ApplicationService('test') print(FCM.minersummary()) FCM.shutdown()