diff --git a/src/main.py b/src/main.py index a0f1c68..8326063 100644 --- a/src/main.py +++ b/src/main.py @@ -3,6 +3,7 @@ import logging import os,sys import publisher +import trigger parser = argparse.ArgumentParser(description='Smoke Detector Plugin', formatter_class=argparse.ArgumentDefaultsHelpFormatter) @@ -131,6 +132,15 @@ execute = configure.ExecuteBase(model_abs_path,model_type,camera_device,smokeynet_delay) execute.run(smoke_threshold) +logging.info('Trigger if smoke is detected') +lat_lon = [32.848132, -116.805901] +logging.warning(f'Using dummy lat_lon: {lat_lon}') +trig = trigger.Trigger() +if trig.predictSmoke(model_type, execute): + trig.trigger(lat_lon) + for status in trig.statuses: + logging.info(f"Trigger status: {status}") + logging.info('Publish') publisher_waggle = publisher.PublisherWaggle(model_type,execute) publisher_waggle.publish(sage_data_topic,smoke_threshold,camera_src) diff --git a/src/trigger.py b/src/trigger.py new file mode 100644 index 0000000..32598e8 --- /dev/null +++ b/src/trigger.py @@ -0,0 +1,108 @@ +import os +import requests + +_AUTHURL = 'https://hotshot.sdsc.edu/pylaski/auth' +_PROXYURL = 'https://wifire-api-proxy.nrp-nautilus.io' +_HOOKURL = 'https://wifire-webhook.nrp-nautilus.io/webhook' + +class Trigger: + + def __init__(self, authURL=_AUTHURL, proxyURL=_PROXYURL): + self.authURL = authURL + self.proxyURL = proxyURL + self.statuses = [] + + + def predictSmoke(self, + model_type, + execute, + ): + do_trigger = False + + if model_type == 'smokeynet': + image_preds, _, _ = execute.inference_results + do_trigger = any(image_preds) + + elif model_type == 'binary-classifier': + raise NotImplementedError("Binary Classifier not implemented yet") + + else: + raise ValueError(f"Invalid model type {model_type}") + + return do_trigger + + + def trigger(self, site_lat_long): + authToken = self.getAuthToken() + headers = { + 'content-type': "application/json", + 'authorization': 'Bearer ' + authToken['token'], + } + for counter, latLong in enumerate(self.getEnsembleLatLongList(site_lat_long)): + params = self.getDefaultParams() + params['ignition']['point'] = latLong + params['webhook']['request_id'] = str(counter) + status = self.launchFarsiteModel(headers, params) + self.statuses.append(status) + + + def getAuthToken(self): + userName = os.getenv('AUTHUSERNAME') + password = os.getenv('AUTHPASSWORD') + + if not userName: + raise EnvironmentError(f"Failed because AUTHUSERNAME is not set.") + if not password: + raise EnvironmentError(f"Failed because AUTHPASSWORD is not set.") + + authURL1 = self.authURL + '?user=' + userName + '&password=' + password + + r = requests.request('GET', authURL1) + authToken = r.json() + return authToken + + + def getEnsembleLatLongList(self, site_lat_long, count=1, dx=0.01): + returnList = [] + for i in range(count): + returnList.append([ + site_lat_long[0] + dx*i, + site_lat_long[1] + dx*i, + ]) + return returnList + + + def launchFarsiteModel(self, headers, params): + r = requests.request('POST', self.proxyURL, headers=headers, json=params) + return r.text + + + def getDefaultParams(self): + defaultParams = { + "hours": 2, + "ember": 80, + "ignition": { + "point": [32.848132, -116.805901] + }, + "weather": { + "repeat_values": { + "wind_direction": 180, + "wind_speed": 5, + "relative_humidity": 5, + "temperature": 70 + } + }, + "fuel_moisture": { + "one_hr": 3, + "ten_hr": 4, + "hundred_hr": 6, + "live_herb": 5, + "live_woody": 60 + }, + "webhook": { + "url": _HOOKURL, + "request_id": "0" + } + } + return defaultParams +