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')
'''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 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)
'''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'''
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)
def main(): '''main''' mainapp = ApplicationService(component='monitor', option='') entries = domonitor() dispatchmessages(mainapp, entries) print('done')
'''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
'''test miner summary''' from backend.fcmapp import ApplicationService FCM = ApplicationService('test') print(FCM.minersummary()) FCM.shutdown()