private void RemoteNotify(Notify notify, UpDevice device) { if ( device == null || notify == null || string.IsNullOrEmpty(notify.driver) || string.IsNullOrEmpty(notify.eventKey)) { throw new System.ArgumentException("Either the device or notification is invalid."); } string message = Json.Serialize(notify.ToJSON()); SendMessage(message, device, false); }
private void HandleMessage(string message, ClientConnection connection) { if ((message == null) || ((message = message.Trim()).Length == 0) || (connection == null) || (!connection.connected)) return; NetworkDevice clientDevice = connection.clientDevice; Message response = null; try { logger.Log("Handling incoming message:\n" + message); object json = Json.Deserialize(message); string type = Util.JsonOptString(json as IDictionary<string, object>, "type"); if (type != null) { Message.Type messageType = (Message.Type)System.Enum.Parse(typeof(Message.Type), type, true); switch (messageType) { case Message.Type.SERVICE_CALL_REQUEST: logger.Log("Incoming Service Call"); CallContext messageContext = new CallContext(); messageContext.callerNetworkDevice = clientDevice; response = HandleServiceCall(message, messageContext); break; case Message.Type.NOTIFY: logger.Log("Incoming Notify"); HandleNotify(message, clientDevice); break; default: break; } } } catch (System.Exception ex) { PushLog("Failure to handle the incoming message. ", ex); response = new Notify(); response.error = "Failure to handle the incoming message. "; } if (response != null) { string msg = Json.Serialize(response.ToJSON()) + "\n"; byte[] bytes = Encoding.UTF8.GetBytes(msg); try { connection.Write(bytes, 0, bytes.Length); PushLog("Responded successfully."); } catch (System.Exception e) { PushLog("Error while responding. ", e); } } }
private void RemoteNotify(Notify notify, UpDevice device) { if ( device == null || notify == null || string.IsNullOrEmpty(notify.driver) || string.IsNullOrEmpty(notify.eventKey)) throw new System.ArgumentException("Either the device or notification is invalid."); string message = Json.Serialize(notify.ToJSON()); SendMessage(message, device, false); }
private void HandleMessage(string message, ClientConnection connection) { if ((message == null) || ((message = message.Trim()).Length == 0) || (connection == null) || (!connection.connected)) { return; } NetworkDevice clientDevice = connection.clientDevice; Message response = null; try { logger.Log("Handling incoming message:\n" + message); object json = Json.Deserialize(message); string type = Util.JsonOptString(json as IDictionary <string, object>, "type"); if (type != null) { Message.Type messageType = (Message.Type)System.Enum.Parse(typeof(Message.Type), type, true); switch (messageType) { case Message.Type.SERVICE_CALL_REQUEST: logger.Log("Incoming Service Call"); CallContext messageContext = new CallContext(); messageContext.callerNetworkDevice = clientDevice; response = HandleServiceCall(message, messageContext); break; case Message.Type.NOTIFY: logger.Log("Incoming Notify"); HandleNotify(message, clientDevice); break; default: break; } } } catch (System.Exception ex) { PushLog("Failure to handle the incoming message. ", ex); response = new Notify(); response.error = "Failure to handle the incoming message. "; } if (response != null) { string msg = Json.Serialize(response.ToJSON()) + "\n"; byte[] bytes = Encoding.UTF8.GetBytes(msg); try { connection.Write(bytes, 0, bytes.Length); PushLog("Responded successfully."); } catch (System.Exception e) { PushLog("Error while responding. ", e); } } }