def PythonScripts(ctx): #print ("I am in PythonScript Api") #call project Demo_Rest/Rest_Python api0 api = RestApi("Demo_Rest/Rest_Python", "api0", ctx) result = api.runApi() if result == True: print("call api success") else: print("call api failed") return result
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) ticker = dynamodb.Attribute( name='Ticker', type=dynamodb.AttributeType.STRING, ) date = dynamodb.Attribute( name='Date', type=dynamodb.AttributeType.STRING, ) table = dynamodb.Table( self, 'StockHistory', partition_key=ticker, sort_key=date, billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST, removal_policy=core.RemovalPolicy.DESTROY, point_in_time_recovery=True, ) index_name = 'Date-index' table.add_global_secondary_index( index_name=index_name, partition_key=date, sort_key=ticker, projection_type=dynamodb.ProjectionType.INCLUDE, non_key_attributes=['Name']) Importer(self, 'Importer', table=table) restapi = RestApi(self, 'Api', table=table, index_name=index_name) Website(self, 'Website', api=restapi.api)
def main(args): """This program will use the GPIO capabilities of the raspberry pi to toggle LED lights based on a schedule """ driver = None try: logger = logging.getLogger(__name__) driver = AlarmDriver(int(args.light1),int(args.light2)) driver.build_schedule(args.onTime,args.toggleTime,args.offTime) if hasWeb: from restapi import RestApi api = RestApi(int(args.port),driver) api.start() logger.info("Starting event loop") driver.run_event_loop(int(args.resolution)) finally: if driver is not None: driver.shutdown()
def main(args): """This program will use the GPIO capabilities of the raspberry pi to toggle LED lights based on a schedule """ driver = None try: logger = logging.getLogger(__name__) driver = AlarmDriver(int(args.light1), int(args.light2)) driver.build_schedule(args.onTime, args.toggleTime, args.offTime) if hasWeb: from restapi import RestApi api = RestApi(int(args.port), driver) api.start() logger.info("Starting event loop") driver.run_event_loop(int(args.resolution)) finally: if driver is not None: driver.shutdown()
def post(self, request, data, *args): if not type(data) == type(str()): data = json.dumps(data) res = RestApi.post(self, request, data, args) if res: res = json.loads(res["data"]) return res
def main(): if app_config['restapi']: restapi = RestApi() restapi.run() # Retrieve previously stored baselines, if any (helps the compensation algorithm). has_baseline = False try: f_co2 = open('co2eq_baseline.txt', 'r') f_tvoc = open('tvoc_baseline.txt', 'r') co2_baseline = int(f_co2.read()) tvoc_baseline = int(f_tvoc.read()) #Use them to calibrate the sensor sgp30.set_indoor_air_quality_baseline(co2_baseline, tvoc_baseline) f_co2.close() f_tvoc.close() has_baseline = True except: print('Impossible to read SGP30 baselines!') #Store the time at which last baseline has been saved baseline_time = utime.time() if app_config['gcp']: # cloud connection jwt = create_jwt(google_cloud_config['project_id'], jwt_config['private_key'], jwt_config['algorithm'], jwt_config['token_ttl']) client = get_mqtt_client(google_cloud_config['project_id'], google_cloud_config['cloud_region'], google_cloud_config['registry_id'], config.google_cloud_config['device_id'], jwt) gc.collect() ts_send_gcp = 0 ts_send_restapi = 0 # acquiring and sending data while True: # acquiring data tvoc, eco2 = sgp30.indoor_air_quality timestamp = utime.time() + epoch_offset if tvoc > app_config['danger']: np[0] = (255, 0, 0) np.write() if app_config['audio']: warning_sound() elif tvoc > app_config['warning']: np[0] = (255, 255, 0) np.write() if app_config['audio']: warning_sound() else: np[0] = (0, 255, 0) np.write() #sending data to restapi if app_config['restapi']: if utime.time() - ts_send_restapi > app_config['ts_restapi']: restapi.tvoc = tvoc restapi.eco2 = eco2 restapi.timestamp = timestamp ts_send_restapi = utime.time() #sending data to gcp if app_config['gcp']: if utime.time() - ts_send_gcp > app_config['ts_gcp']: message = { "device_id": google_cloud_config['device_id'], "tvoc": tvoc, "eco2": eco2, "timestamp": timestamp } print("Publishing message " + str(ujson.dumps(message))) mqtt_topic = '/devices/{}/{}'.format( google_cloud_config['device_id'], 'events') client.publish(mqtt_topic.encode('utf-8'), ujson.dumps(message).encode('utf-8')) gc.collect() client.check_msg() ts_send_gcp = utime.time() gc.collect() # Baselines should be saved after 12 hour the first # timen then every hour according to the doc. if (has_baseline and (utime.time() - baseline_time >= 3600)) \ or ((not has_baseline) and (utime.time() - baseline_time >= 43200)): print('Saving baseline!') baseline_time = utime.time() try: f_co2 = open('co2eq_baseline.txt', 'w') f_tvoc = open('tvoc_baseline.txt', 'w') bl_co2, bl_tvoc = sgp30.indoor_air_quality_baseline f_co2.write(str(bl_co2)) f_tvoc.write(str(bl_tvoc)) f_co2.close() f_tvoc.close() has_baseline = True except: print('Impossible to write SGP30 baselines!') gc.collect() print("Going to sleep for about %s milliseconds!" % app_config["deepsleepms"]) if app_config['restapi']: utime.sleep_ms(app_config["deepsleepms"]) else: deepsleep(app_config["deepsleepms"])
def get(self, request, *args): res = RestApi.get(self, request, args) if res: res = self.__decode__(res["data"]) return res
# encoding: utf-8 """ write any your Python scripts here """ import sys from os.path import os,join sys.path.append(os.path.os.path.dirname(__file__)) import setting sys.path.append(join(setting.local, "libraryPy")) import requests from restapi import RestApi from restBirdCtx import RestBirdCtx #Call Demo_Rest/Rest_Python api0 with env Demo_Rest_Env sess = requests.Session() ctx = RestBirdCtx(setting.local, "Demo_Rest_Env", sess, "") api = RestApi("Demo_Rest/Rest_Python", "api0", ctx) api.runApi() #Get/Set global env ctx.setGlobal("hello", "world") v = ctx.getGlobal("hello") print (v) #Get Env print(ctx.loadEnvVaribles("Demo_Rest_Env")) print(ctx.vars['counter'])