public static Response FromJSON(object json) { Response r = new Response(); Message.FromJSON(r, json); r.responseData = Util.JsonOptField(json as IDictionary<string, object>, "responseData") as IDictionary<string, object>; return r; }
public void Handshake(Call serviceCall, Response serviceResponse, CallContext messageContext) { string deviceParameter = serviceCall.GetParameterString(DEVICE_KEY); if (deviceParameter == null) { serviceResponse.error = "No 'device' parameter informed."; return; } try { UpDevice device = UpDevice.FromJSON(Json.Deserialize(deviceParameter)); gateway.deviceManager.RegisterDevice(device); serviceResponse.AddParameter(DEVICE_KEY, Json.Serialize(gateway.currentDevice.ToJSON())); //TODO: actually implement the driver register for other devices... //Response driversResponse = gateway.CallService(device, new Call("uos.DeviceDriver", "listDrivers")); //object driverList = driversResponse.GetResponseData("driverList"); //if (driverList != null) //{ // var driverMap = (IDictionary<string, object>)Json.Deserialize(driverList.ToString()); // // TODO: this is duplicated with DeviceManager.registerRemoteDriverInstances // foreach (string id in driverMap.Keys) // { // UpDriver upDriver = UpDriver.FromJSON(Json.Deserialize(driverMap[id].ToString())); // DriverModel driverModel = new DriverModel(id, upDriver, device.name); // gateway.driverManager.Insert(driverModel); // } //} } catch (System.Exception e) { serviceResponse.error = e.Message; logger.LogError("Problems on handshake: " + e.Message + "," + e.StackTrace); } }
public Response CallService(object instance, Call call, CallContext context) { MethodInfo method = FindMethod(call, instance); if (method != null) { logger.Log( "Calling service '" + call.service + "' of driver '" + call.driver + "' on instance '" + call.instanceId + "'"); HandleStreamCall(call, context); Response response = new Response(); method.Invoke(instance, new object[] { call, response, context }); logger.Log("Finished service call."); return response; } else throw new System.Exception( "No Service Implementation found for service '" + call.service + "' on driver '" + call.driver + "' with id '" + call.instanceId + "'."); }
public void Goodbye(Call serviceCall, Response serviceResponse, CallContext messageContext) { gateway.deviceManager.DeviceLeft(messageContext.callerNetworkDevice); }
/// <summary> /// This method is responsible for informing the unknown equivalent driverss. /// </summary> /// <param name="serviceCall"></param> /// <param name="serviceResponse"></param> /// <param name="messageContext"></param> public void TellEquivalentDrivers(Call serviceCall, Response serviceResponse, CallContext messageContext) { try { string equivalentDrivers = serviceCall.GetParameterString(DRIVERS_NAME_KEY); IList<object> equivalentDriversJson = Json.Deserialize(equivalentDrivers) as IList<object>; List<object> jsonList = new List<object>(); IDictionary<string, object> responseData = new Dictionary<string, object>(); for (int i = 0; i < equivalentDriversJson.Count; i++) { string equivalentDriver = equivalentDriversJson[i] as string; UpDriver driver = gateway.driverManager.GetDriverFromEquivalanceTree(equivalentDriver); if (driver != null) AddToEquivalanceList(jsonList, driver); } responseData[INTERFACES_KEY] = Json.Serialize(jsonList); serviceResponse.responseData = responseData; } catch (System.Exception e) { logger.LogError("Problems on equivalent drivers. " + e.StackTrace); } }
public void ListDrivers(Call serviceCall, Response serviceResponse, CallContext messageContext) { logger.Log("Handling DeviceDriverImpl#listDrivers service"); try { IDictionary<string, object> parameters = serviceCall.parameters; DriverManager driverManager = gateway.driverManager; // Handles parameters to filter message... IList<DriverData> listDrivers = driverManager.ListDrivers( ((parameters != null) ? (parameters[DRIVER_NAME_KEY] as string) : null), gateway.currentDevice.name ); IDictionary<string, object> driversList = new Dictionary<string, object>(); if ((listDrivers != null) && (listDrivers.Count > 0)) { foreach (var driver in listDrivers) driversList[driver.instanceID] = driver.driver.ToJSON(); } IDictionary<string, object> responseData = new Dictionary<string, object>(); responseData[DRIVER_LIST_KEY] = driversList; serviceResponse.responseData = responseData; } catch (System.Exception e) { serviceResponse.error = e.Message; logger.LogError("Problem on ListDrivers service: " + e.Message + "," + e.StackTrace); } }
private Response HandleServiceCall(string message, CallContext messageContext) { try { Call serviceCall = Call.FromJSON(Json.Deserialize(message)); Response response = gateway.HandleServiceCall(serviceCall, messageContext); logger.Log("Returning service response"); return response; } catch (System.Exception e) { PushLog("Internal Failure: ", e); Response errorResponse = new Response(); errorResponse.error = e.Message == null ? "Internal Error" : e.Message; return errorResponse; } }