# In-Sight Script Helper Functions

The following code snippet A snippet is one or more pre-configured cell that allows you to automate frequently performed spreadsheet tasks. You can create your own snippets, or use snippets provided by Cognex. contains a set of helper functions, including commands to load a job A job is a user-configured program in In-Sight 3D Spreadsheet consisting of functions and designed for specific applications. You can create and edit jobs using the In-Sight 3D Spreadsheet interface, and load them on a vision system for deployment in a live environment., set properties, and set Online state on your vision system:

Copy

    /**
       * Loads a job file and sets the specified properties.
       * @param {string} jobFile
       * @param {Array<{key: string, value: string, type: string}>} properties
       * @returns {Promise<void>}
       */
      async loadJobAndSetProperties(jobFile, properties) {
        // Must be offline before making changes
        const onlineState = await this.goOffline();

        // Load job if specified
        if (jobFile) {
          await this.loadJob(jobFile);
        }

        // Set properties
        for (const prop of properties) {
          await this.setProperty(prop.type, prop.key, prop.value);
        }

        // Restore original online state
        await this.setOnlineState(onlineState);
      }

      /**
       * Puts the device offline and returns the original online state.
       * @returns {Promise<number>} The original online state before going offline
       */
      async goOffline() {
        // Get current online state
        let getOnlineResp = await device.commands["native"].execute(`GO`);
        const onlineState = getOnlineResp.result;

        // Set to offline if currently online
        let setOfflineResp = await device.commands["native"].execute(`SO0`);
        if (setOfflineResp.statusCode !== 1) {
          console.warn(`Failed to set offline state. Response: ${JSON.stringify(setOfflineResp)}`);
        }
        return onlineState;
      }

      /**
       * Restores the online state of the device.
       * @param {number} state The online state to restore (0 = offline, 1 = online)
       * @returns {Promise<object>} The response from the set online state command execution
       */
      async setOnlineState(state) {
        // Restore the original online state
        let restoreOnlineResp = await device.commands["native"].execute(`SO${state}`);
        if (restoreOnlineResp.statusCode !== 1) {
          console.warn(`Failed to restore online state ${state}. Response: ${JSON.stringify(restoreOnlineResp)}`);
        }
        return restoreOnlineResp;
      }

      /**
       * Loads a job file that is already present on the device and sets it as the active job.
       * @param {string} jobFile - The name of the job file to load.
       * @returns {Promise<object>} The response from the load job command execution
       */
      async loadJob(jobFile) {
        // Load the job file
        console.log(`Loading job file: ${jobFile}`);
        let jobLoadResp = await device.commands["native"].execute(`LF${jobFile}`, 10000);
        if (jobLoadResp.statusCode !== 1) {
          console.warn(`Failed to load job file ${jobFile}. Response: ${JSON.stringify(jobLoadResp)}`);
        } else {
          console.log(`Successfully loaded job file: ${jobFile}`);
        }
        return jobLoadResp;
      }

      /**
       * Sets a property on the device.
       * @param {string} type Can be S (string), I (integer), F (float), R (region)
       * @param {string} key The named cell to set
       * @param {*} value The value to set
       * @returns {Promise<object>} The response from the set property command execution
       */
      async setProperty(type, key, value) {
        console.log(`Setting property ${key} of type ${type} to ${value}`);
        let resp = await device.commands["native"].execute(`S${type}${key} ${value}`);
        if (resp.statusCode !== 1) {
          console.warn(`Failed to set property ${key}. Response: ${JSON.stringify(resp)}`);
        }
        return resp;
      }