def init_device_cloud(self): self.count=0 self.app_config={} self.reached=False self.lastDist=0 app_config_path = path.join(self.config_dir, self.app_config_file) # open the file in write mode (may need to update the geoCodes) app_config_file_handle = open(app_config_path, "r+") self.app_config.update(json.load(app_config_file_handle)) # Initialize client self.client = iot.Client(self.app_id) # Use the iot-tracker.cfg iot application config file self.client.config.config_file = self.config_file self.client.config.config_dir = self.config_dir self.client.initialize() self.client.action_register_callback("start", self.remote_start) self.client.action_register_callback("stop", self.remote_stop) self.client.action_register_callback("configure", self.remote_configure) # Connect to Device Cloud if self.client.connect(timeout=10) != iot.STATUS_SUCCESS: self.client.error("Failed") # At startup if only home address is configured get the geoCodes if self.app_config["home"].get("address") is not None and \ (self.app_config["home"].get("lat") is None or self.app_config["home"].get("lon") is None): geoCodes = self.getGeoCodes(self.app_config["home"]["address"]) self.app_config["home"]["lat"]=geoCodes["lat"] self.app_config["home"]["lon"]=geoCodes["lng"] json.dump(self.app_config, app_config_file_handle) self.location_home = "{}\nlat:{},lon:{}".format(self.app_config["home"]["address"], self.app_config["home"]["lat"], self.app_config["home"]["lon"]) # Identify the device using the android unique ID self.client.attribute_publish('id', uniqueid.id or "unavailable") # Send an SMS when app starts sms.send(recipient=self.app_config["recipient"], message="Tracking started for "+self.client.config.key)
client_socket.connect(('localhost', 8080)) except: sys.exit(1) # Start proxy username = client_socket.recv(1024).decode() password = client_socket.recv(1024).decode() t = Thread(target=connect, args=( username, password, )) t.start() sleep(2) # Connect to cloud client = device_cloud.Client("qos_app") client.config.config_file = "test.cfg" client.initialize() client.log_level("CRITICAL") # Check connection if client.connect(timeout=10) != device_cloud.STATUS_SUCCESS: client_socket.send(("fail").encode()) sys.exit(1) client_socket.send(("connected").encode()) while client.is_alive(): response = client_socket.recv(1024).decode() if response == "close": break else:
# Parse command line arguments for easy customization parser = argparse.ArgumentParser(description="Demo app for Python HDC " "location APIs") parser.add_argument("-i", "--app_id", help="Custom app id") parser.add_argument("-c", "--config_dir", help="Custom config directory") parser.add_argument("-f", "--config_file", help="Custom config file name " "(in config directory)") args = parser.parse_args(sys.argv[1:]) # Initialize client default called 'python-demo-app' app_id = "iot-simple-location-py" if args.app_id: app_id = args.app_id client = iot.Client(app_id) # Use the demo-connect.cfg file inside the config directory # (Default would be python-demo-app-connect.cfg) config_file = "demo-iot-simple-location.cfg" if args.config_file: config_file = args.config_file client.config.config_file = config_file # Look for device_id and demo-connect.cfg in this directory # (This is already default behaviour) config_dir = "." if args.config_dir: config_dir = args.config_dir client.config.config_dir = config_dir
else: return 18 def fail_action(client, params, user_data): return (19, "fail and such") if __name__ == "__main__": signal.signal(signal.SIGINT, sighandler) if enable_cov == True: cov = coverage.Coverage() cov.start() client = device_cloud.Client("iot-validate-app") client.config.config_file = "validate.cfg" client.initialize() client.action_register_callback("pass_action", pass_action) client.action_register_callback("fail_action", fail_action) if client.connect(timeout=10) != device_cloud.STATUS_SUCCESS: print("Failed to connect") sys.exit(1) client.telemetry_publish("property", 12.34) client.attribute_publish("attribute", "text and such") client.location_publish(45.351603, -75.918713,
file_name = app_name + ".cfg" gen_fake_config(file_name) # action args params = {} params['ota_timeout'] = 0 params['package'] = args.package_name user_data = [ args.runtime_dir, ] request = iot._core.defs.ActionRequest(request_id='unused', name='offline_updater', params=params) # client object client = iot.Client(app_name, offline=True) client.config.config_file = file_name client.initialize() client.log_level("DEBUG") ota = ota_handler.OTAHandler(offline=True) ota._runtime_dir = args.runtime_dir ota._offline = True ret = ota.update_callback(client, params, user_data, request) print("Status of update: {}".format(ret)) try: os.remove(file_name) except: print("Unable to remove config file") else: print("package_name {} does not exist".format(args.package_name))
secure = client.config.validate_cloud_cert is not False try: relay.create_relay(url, host, protocol, secure=secure, log_func=client.log) return (iot.STATUS_SUCCESS, "") except Exception as error: client.error(str(error)) return (iot.STATUS_FAILURE, str(error)) if __name__ == "__main__": signal.signal(signal.SIGINT, sighandler) if osal.POSIX: signal.signal(signal.SIGQUIT, sighandler) # Initialize client called 'device_manager_py' client = iot.Client("device_manager_py") client.config.config_file = default_cfg_dir + "/iot-connect.cfg" client.initialize() config = config_load() runtime_dir = config.runtime_dir upload_dir = os.path.join(runtime_dir, "upload") download_dir = os.path.join(runtime_dir, "download") try: if not os.path.isdir(runtime_dir): os.mkdir(runtime_dir) if not os.path.isdir(upload_dir): os.mkdir(upload_dir)