Skip to content

Commit 8413f86

Browse files
committed
Update FhemDemo.java
1 parent 0f3254b commit 8413f86

File tree

1 file changed

+74
-10
lines changed

1 file changed

+74
-10
lines changed

src/main/java/net/b07z/sepia/sdk/services/uid1007/FhemDemo.java

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.net.URL;
44
import java.net.URLConnection;
55
import java.util.HashMap;
6+
import java.util.List;
67
import java.util.Map;
78
import java.util.TreeSet;
89

@@ -22,6 +23,7 @@
2223
import net.b07z.sepia.server.assist.services.ServiceInfo;
2324
import net.b07z.sepia.server.assist.services.ServiceInterface;
2425
import net.b07z.sepia.server.assist.services.ServiceResult;
26+
import net.b07z.sepia.server.assist.services.SmartOpenHAB;
2527
import net.b07z.sepia.server.assist.services.ServiceInfo.Content;
2628
import net.b07z.sepia.server.assist.services.ServiceInfo.Type;
2729
import net.b07z.sepia.server.assist.smarthome.SmartHomeDevice;
@@ -89,7 +91,8 @@ public ServiceAnswers getAnswersPool(String language) {
8991
answerPool
9092
.addAnswer(successAnswer, 0, "Test erfolgreich.")
9193
.addAnswer(okAnswer, 0, "Die Anfrage ist angekommen aber ich kann sie nicht bearbeiten.")
92-
.addAnswer(customAnswer, 0, "Das Gerät mit dem Namen <1> wurde auf <2> gesetzt.")
94+
.addAnswer(deviceStateShow, 0, "Das Gerät mit dem Namen <1> hat den Status <2>.")
95+
.addAnswer(deviceStateSet, 0, "Das Gerät mit dem Namen <1> wurde auf <2> gesetzt.")
9396
;
9497
return answerPool;
9598

@@ -98,15 +101,17 @@ public ServiceAnswers getAnswersPool(String language) {
98101
answerPool
99102
.addAnswer(successAnswer, 0, "Test successful.")
100103
.addAnswer(okAnswer, 0, "Message received but I could not fulfill your request.")
101-
.addAnswer(customAnswer, 0, "The device with the name <1> has been set to <2>.")
104+
.addAnswer(deviceStateShow, 0, "The device with the name <1> has the status <2>.")
105+
.addAnswer(deviceStateSet, 0, "The device with the name <1> has been set to <2>.")
102106
;
103107
return answerPool;
104108
}
105109
}
106110
//We keep a reference here for easy access in getResult - Note that custom answers start with a specific prefix, the system answers don't.
107111
private static final String successAnswer = ServiceAnswers.ANS_PREFIX + CMD_NAME + "_success_0a";
108112
private static final String okAnswer = ServiceAnswers.ANS_PREFIX + CMD_NAME + "_still_ok_0a";
109-
private static final String customAnswer = ServiceAnswers.ANS_PREFIX + CMD_NAME + "_state_set_0a";
113+
private static final String deviceStateSet = ServiceAnswers.ANS_PREFIX + CMD_NAME + "_state_set_0a";
114+
private static final String deviceStateShow = ServiceAnswers.ANS_PREFIX + CMD_NAME + "_state_show_0a";
110115
private static final String failAnswer = "error_0a";
111116
private static final String notAllowed = "smartdevice_0d";
112117

@@ -161,7 +166,8 @@ public ServiceInfo getInfo(String language) {
161166
info.addSuccessAnswer(successAnswer)
162167
.addFailAnswer(failAnswer)
163168
.addOkayAnswer(okAnswer)
164-
.addCustomAnswer("setState", customAnswer) //adding these answers here is optional and used just as info
169+
.addCustomAnswer("setState", deviceStateSet) //adding these answers here is optional and used just as info
170+
.addCustomAnswer("showState", deviceStateShow)
165171
.addCustomAnswer("notAllowed", notAllowed)
166172
;
167173

@@ -206,6 +212,7 @@ public ServiceResult getResult(NluResult nluResult) {
206212

207213
//Device is FHEM server itself?
208214
if (typeIsEqual(deviceType, SmartDevice.Types.device) && deviceName.equals("FHEM")){
215+
//SHOW FHEM status
209216
if (typeIsEqual(actionType, Action.Type.show)){
210217
//check if SEPIA is registered - if not try to register
211218
if (!smartHomeHUB.registerSepiaFramework()){
@@ -227,13 +234,57 @@ public ServiceResult getResult(NluResult nluResult) {
227234
//all good
228235
service.setStatusSuccess();
229236
}
237+
//Other FHEM actions?
238+
}else{
239+
//OK but no result
240+
service.setStatusOkay();
241+
}
242+
243+
//Any LIGHT?
244+
}else if (typeIsEqual(deviceType, SmartDevice.Types.light)){
245+
//SHOW lights status
246+
if (typeIsEqual(actionType, Action.Type.show)){
247+
//find device - we always load a fresh list
248+
Map<String, SmartHomeDevice> devices = smartHomeHUB.getDevices();
249+
if (devices == null){
250+
//FAIL
251+
service.setStatusFail(); //"hard"-fail (probably connection or token error)
252+
return service.buildResult();
253+
}else{
254+
//get all devices with right type and optionally right room
255+
List<SmartHomeDevice> matchingDevices = SmartOpenHAB.getMatchingDevices(devices, deviceType, roomType, -1);
256+
//have found any?
257+
if (matchingDevices.isEmpty()){
258+
service.setStatusOkay();
259+
//service.setCustomAnswer(noDeviceMatchesFound); //TODO: improve answer
260+
return service.buildResult();
261+
}
262+
//keep only the first match for now - TODO: improve
263+
SmartHomeDevice selectedDevice = matchingDevices.get(0);
264+
String state = selectedDevice.getState();
265+
String name = selectedDevice.getName();
266+
267+
//response info
268+
service.resultInfoPut("device_name", name);
269+
service.resultInfoPut("device_value", SmartOpenHAB.getStateLocal(state, service.language));
270+
271+
//all good
272+
service.setStatusSuccess();
273+
service.setCustomAnswer(deviceStateShow);
274+
275+
//DEBUG
276+
//System.out.println(selectedDevice.getDeviceAsJson());
277+
}
278+
279+
//Other light actions?
230280
}else{
231281
//OK but no result
232282
service.setStatusOkay();
233283
}
234284

235-
//OK but no result
285+
//Any other DEVICE type?
236286
}else{
287+
//OK but no result yet
237288
service.setStatusOkay();
238289
}
239290

@@ -397,9 +448,20 @@ public Map<String, SmartHomeDevice> getDevices(){
397448
}
398449

399450
@Override
400-
public SmartHomeDevice loadDeviceData(SmartHomeDevice arg0){
401-
// TODO Auto-generated method stub
402-
return null;
451+
public SmartHomeDevice loadDeviceData(SmartHomeDevice device){
452+
String deviceURL = ""; //TODO - build this: http://localhost:8083/fhem?cmd=jsonlist2%20MyHueBridge_HUEDevice1&XHR=1&fwcsrf=csrf_680293651159739
453+
if (Is.nullOrEmpty(deviceURL)){
454+
return null;
455+
}else{
456+
JSONObject response = Connectors.httpGET(deviceURL);
457+
if (Connectors.httpSuccess(response)){
458+
//build shd from response
459+
SmartHomeDevice shd = buildDeviceFromResponse(response);
460+
return shd;
461+
}else{
462+
return null;
463+
}
464+
}
403465
}
404466

405467
@Override
@@ -476,14 +538,16 @@ private SmartHomeDevice buildDeviceFromResponse(JSONObject hubDevice){
476538
}
477539
//create common object
478540
String fhemObjName = JSON.getStringOrDefault(hubDevice, "Name", null);
479-
Object stateObj = JSON.getObject(hubDevice, new String[]{"Readings", "state"});
541+
//JSONObject stateObj = JSON.getJObject(hubDevice, new String[]{"Readings", "state"});
542+
//String state = (stateObj != null)? JSON.getString(stateObj, "Value") : null;
543+
String state = JSON.getStringOrDefault(internals, "STATE", null);
480544
Object linkObj = (fhemObjName != null)? (this.host + "?cmd." + fhemObjName) : null;
481545
JSONObject meta = JSON.make(
482546
"fhem-id", fhemObjName
483547
);
484548
//note: we need fhem-id for commands although it is basically already in 'link'
485549
SmartHomeDevice shd = new SmartHomeDevice(name, type, room,
486-
(stateObj != null)? stateObj.toString() : null, memoryState,
550+
state, memoryState,
487551
(linkObj != null)? linkObj.toString() : null, meta);
488552
return shd;
489553
}

0 commit comments

Comments
 (0)