I am trying to fetch a value from a database table from client scope but I am currently unable to build it, can you suggest the correct way

#1

Attaching code from common scope-

package com.inductiveautomation.examples.helperfunction;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public interface ModuleRPC {
    String getDatabase(String DatabaseName);
}
    
public class ModuleRPCImpl implements ModuleRPC 
{
public String getDatabase(String Databasename)
{
SRConnection con; int maxVal;
try{
    con=context.getDatasourceManager().getConnection(Databasename); 
    
    maxVal= (Integer) con.runScalarQuery("SELECT max(accumulatorLevel) FROM History");

return "MAX_VALUE, " + maxVal;
}
finally
{
    if(con!=null){
        con.close();
    }
}
    }
}  

code from clienthook-

package com.inductiveautomation.examples.helperfunction.client;

import com.inductiveautomation.ignition.client.model.ClientContext;
import com.inductiveautomation.ignition.common.licensing.LicenseState;
import com.inductiveautomation.ignition.common.script.ScriptManager;
import com.inductiveautomation.vision.api.client.AbstractClientModuleHook;
import com.inductiveautomation.examples.helperfunction.HelperFunctions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ClientHook extends AbstractClientModuleHook {

    private final Logger logger = LoggerFactory.getLogger(getClass());
	
ModuleRPC rpc = ModuleRPCFactory.create(ModuleMeta.MODULE_ID, ModuleRPC.class); 
System.out.println( rpc.getDatabase("DB"));


    @Override
    public void startup(ClientContext context, LicenseState activationState) throws Exception {
        super.startup(context, activationState);
    }

    @Override
    public void shutdown() {
        super.shutdown();
    }

    @Override
    public void initializeScriptManager(ScriptManager manager) {
        super.initializeScriptManager(manager);
        
	manager.addScriptModule("system.getDatabase",ModuleRPCImpl.class);

    }
}
0 Likes