Skip to content

Commit

Permalink
support for xml-rpc and remote hm-script authentication, fixes #215,
Browse files Browse the repository at this point in the history
fixes #172
  • Loading branch information
mdzio committed Apr 25, 2020
1 parent a74c199 commit b51184b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
21 changes: 14 additions & 7 deletions ccu-historian/src/mdz/ccuhistorian/ManagerConfigurator.groovy
Expand Up @@ -109,17 +109,20 @@ class ManagerConfigurator {
if (prefix==null) prefix=''
Boolean writeAccess=getOption(cfg, 'writeAccess', Boolean, "Device $idx: ", false)
Integer sysVarDataCycle=getOption(cfg, 'sysVarDataCycle', Integer, "Device $idx: ", false)
Integer timeout=getOption(cfg, 'timeout', Integer, "Device $idx: ", false)
Integer timeout=getOption(cfg, 'timeout', Integer, "Device $idx: ", false)
String username=getOption(cfg, 'username', String, "Device $idx: ", false)
String password=getOption(cfg, 'password', String, "Device $idx: ", false)

HmReinitTask reinitTask=new HmReinitTask(manager.executor)
if (reinitTimeout!=null)
reinitTask.timeout=reinitTimeout
HmScriptClient scriptClient=new HmScriptClient(address)
HmScriptClient scriptClient=new HmScriptClient(address, username, password)

if (type==DeviceTypes.CCU1) {
HmXmlRpcInterface xmlRpcItfWired=new HmXmlRpcInterface(
prefix+INTERFACE_WIRED_NAME, INTERFACE_WIRED_NAME, address, INTERFACE_WIRED_PORT,
manager.xmlRpcServer, scriptClient, reinitTask, manager.executor
manager.xmlRpcServer, scriptClient, reinitTask, manager.executor,
username, password
)
if (writeAccess!=null)
xmlRpcItfWired.writeAccess=writeAccess
Expand All @@ -128,7 +131,8 @@ class ManagerConfigurator {

HmXmlRpcInterface xmlRpcItfRf=new HmXmlRpcInterface(
prefix+INTERFACE_RF_NAME, INTERFACE_RF_NAME, address, INTERFACE_RF_PORT,
manager.xmlRpcServer, scriptClient, reinitTask, manager.executor
manager.xmlRpcServer, scriptClient, reinitTask, manager.executor,
username, password
)
if (writeAccess!=null)
xmlRpcItfRf.writeAccess=writeAccess
Expand All @@ -137,7 +141,8 @@ class ManagerConfigurator {
if (type==DeviceTypes.CCU1) {
HmXmlRpcInterface xmlRpcItfSys=new HmXmlRpcInterface(
prefix+INTERFACE_SYSTEM_NAME, INTERFACE_SYSTEM_NAME, address, INTERFACE_SYSTEM_PORT,
manager.xmlRpcServer, scriptClient, reinitTask, manager.executor
manager.xmlRpcServer, scriptClient, reinitTask, manager.executor,
username, password
)
if (writeAccess!=null)
xmlRpcItfSys.writeAccess=writeAccess
Expand All @@ -147,7 +152,8 @@ class ManagerConfigurator {
if (type==DeviceTypes.CCU2 || type==DeviceTypes.CCU3) {
HmXmlRpcInterface hmIpItf=new HmXmlRpcInterface(
prefix+INTERFACE_HMIP_RF_NAME, INTERFACE_HMIP_RF_NAME, address, INTERFACE_HMIP_RF_PORT,
manager.xmlRpcServer, scriptClient, reinitTask, manager.executor
manager.xmlRpcServer, scriptClient, reinitTask, manager.executor,
username, password
)
if (writeAccess!=null)
hmIpItf.writeAccess=writeAccess
Expand Down Expand Up @@ -200,7 +206,8 @@ class ManagerConfigurator {
} else {
HmXmlRpcInterface xmlRpcItfPi=new HmXmlRpcInterface(
prefix+name, name, address, port,
manager.xmlRpcServer, scriptClient, reinitTask, manager.executor
manager.xmlRpcServer, scriptClient, reinitTask, manager.executor,
username, password
)
if (writeAccess!=null)
xmlRpcItfPi.writeAccess=writeAccess
Expand Down
9 changes: 8 additions & 1 deletion hc-utils/src/mdz/hc/itf/hm/HmScriptClient.groovy
Expand Up @@ -45,9 +45,13 @@ public class HmScriptClient {
private HmModel model
private long modelLastScan
private Object modelMutex=[]
private String auth

public HmScriptClient(String address) {
public HmScriptClient(String address, String username, String password) {
this.address=address
if (username) {
auth='Basic '+(username+':'+password?:'').bytes.encodeBase64()
}
String host="http://$address:8181/tclrega.exe"
log.info "Creating HM script client for $host"
url=[host]
Expand Down Expand Up @@ -349,6 +353,9 @@ public class HmScriptClient {
con.connectTimeout=DEFAULT_CONNECT_TIMEOUT
con.requestMethod='POST'
con.doOutput=true
if (auth) {
con.setRequestProperty("Authorization", auth);
}
con.outputStream.write script.getBytes('ISO-8859-1')
con.outputStream.close()
List<String> response=[]
Expand Down
26 changes: 18 additions & 8 deletions hc-utils/src/mdz/hc/itf/hm/HmXmlRpcClient.groovy
Expand Up @@ -19,16 +19,26 @@ package mdz.hc.itf.hm

import groovy.net.xmlrpc.XMLRPCServerProxy as Proxy
import groovy.util.logging.Log
import groovy.lang.Lazy

@Log
public class HmXmlRpcClient {

String host
int port
String username
String password

@Lazy
private Proxy proxy=new Proxy("http://$host:$port")
private Proxy proxy

public synchronized Proxy getProxy() {
if (proxy==null) {
proxy=new Proxy("http://$host:$port")
if (username) {
proxy.setBasicAuth(username, password?:"")
}
}
return proxy
}

public List<String> systemListMethods() {
log.fine "Calling system.listMethods()"
Expand All @@ -41,17 +51,17 @@ public class HmXmlRpcClient {

public void init(String url, String interfaceId) {
log.fine "Calling init($url, $interfaceId)"
proxy.init url, interfaceId
getProxy().init url, interfaceId
}

public void deinit(String url) {
log.fine "Calling init($url)"
proxy.init url
getProxy().init url
}

public Map<String, Map<String, Object>> getParamsetDescription(String address, String type) {
log.fine "Calling getParamsetDescription($address, $type)"
def result=proxy.getParamsetDescription(address, type)
def result=getProxy().getParamsetDescription(address, type)
if (!(result in Map) ||
!result.every {
it.key in String && it.value in Map &&
Expand All @@ -69,11 +79,11 @@ public class HmXmlRpcClient {

public void setValue(String address, String identifier, value) {
log.fine "Calling setValue($address, $identifier, $value)"
proxy.setValue address, identifier, value
getProxy().setValue address, identifier, value
}

public void event(String interfaceId, String address, String key, value) {
log.fine "Calling event($interfaceId, $address, $key, $value)"
proxy.event interfaceId, address, key, value
getProxy().event interfaceId, address, key, value
}
}
4 changes: 4 additions & 0 deletions hc-utils/src/mdz/hc/itf/hm/HmXmlRpcInterface.groovy
Expand Up @@ -55,6 +55,8 @@ public class HmXmlRpcInterface extends BasicProducer<RawEvent> implements Interf
final HmScriptClient scriptClient
final HmReinitTask reinitTask
final ScheduledExecutorService executor
final String username
final String password

private HmXmlRpcClient client=[]
private Date lastCommTime
Expand All @@ -68,6 +70,8 @@ public class HmXmlRpcInterface extends BasicProducer<RawEvent> implements Interf
server.addConsumer(this)
client.host=host
client.port=port
client.username=username
client.password=password
if (!disableRegistration) {
Exceptions.catchToLog(log) { init() }
reinitTask.add this
Expand Down

0 comments on commit b51184b

Please sign in to comment.