Exemplo n.º 1
0
        private NetworkMessage communicator_MessageReceived(NetworkMessage request)
        {
            NetworkMessage response = null;

            if (request != null)
                switch (request.ID)
                {
                    #region Settings
                    case NetworkMessageID.Settings:
                        if (request.Parameters.Count == 0)
                            response = new NetworkMessage(NetworkMessageID.Settings, WebTheme, UnitSystem);
                        else
                        {
                            WebTheme = request[0];
                            UnitSystem = request[1];

                            communicator.Broadcast(new NetworkMessage(NetworkMessageID.Settings, WebTheme, UnitSystem));
                        }
                        break;
                    #endregion

                    #region Version
                    case NetworkMessageID.Version: response = new NetworkMessage(NetworkMessageID.Version, Version); break;
                    #endregion

                    #region GetNodes
                    case NetworkMessageID.GetNodes: response = new NetworkMessage(NetworkMessageID.GetNodes, JsonConvert.SerializeObject(nodes, Formatting.Indented)); break;
                    #endregion

                    #region DeleteNode
                    case NetworkMessageID.DeleteNode:
                        if (request.Parameters.Count == 1)
                        {
                            Node node = GetNode(byte.Parse(request[0]));
                            dbService.Delete(node);
                            nodes.Remove(node);
                            communicator.Broadcast(new NetworkMessage(NetworkMessageID.DeleteNode, node.ID.ToString()));
                        }
                        break;
                    #endregion

                    #region GetModules
                    case NetworkMessageID.GetModules: response = new NetworkMessage(NetworkMessageID.GetModules, SerializeModules()); break;
                    #endregion

                    #region AddModule
                    case NetworkMessageID.AddModule:
                        AutomationModule newModule = new AutomationModule("Untitled", "", "", "");
                        dbService.Insert(newModule);
                        modules.Add(newModule);
                        response = new NetworkMessage(NetworkMessageID.AddModule, SerializeModules(newModule.ID));
                        break;
                    #endregion

                    #region SetModule
                    case NetworkMessageID.SetModule:
                        if (request.Parameters.Count == 1)
                        {
                            dynamic obj = JsonConvert.DeserializeObject(request[0]);
                            string s;

                            AutomationModule module = GetModule(Guid.Parse(obj.ID.ToString()));
                            module.Name = obj.Name;
                            module.Description = obj.Description;
                            s = obj.Script;
                            module.Script = new string(Encoding.ASCII.GetChars(Convert.FromBase64String(s)));
                            s = obj.View;
                            module.View = new string(Encoding.ASCII.GetChars(Convert.FromBase64String(s)));

                            dbService.Update(module);
                            communicator.Broadcast(new NetworkMessage(NetworkMessageID.SetModule, SerializeModules(module.ID)));

                            string errors = RunAutomationService(module);
                            if (!string.IsNullOrEmpty(errors))
                                response = new NetworkMessage(NetworkMessageID.Message, errors, "Error");
                        }
                        break;
                    #endregion

                    #region DeleteModule
                    case NetworkMessageID.DeleteModule:
                        if (request.Parameters.Count == 1)
                        {
                            AutomationModule module = GetModule(Guid.Parse(request[0]));
                            dbService.Delete(module);
                            modules.Remove(module);
                            communicator.Broadcast(new NetworkMessage(NetworkMessageID.DeleteModule, module.ID.ToString()));
                        }
                        break;
                    #endregion

                    #region Sensor message (client sets actuator value)
                    case NetworkMessageID.SensorMessage: gatewayProxy.Send(SensorMessage.FromRawMessage(request[0].Replace("*", ";"))); break;
                    #endregion

                    default: break;
                }

            return response;
        }
Exemplo n.º 2
0
 public void Broadcast(NetworkMessage msg)
 {
     if (IsStarted && msg != null)
         foreach (WebSocketSession session in wsServer.GetAllSessions())
             Send(session, msg);
 }
 public NetworkMessageEventArgs(NetworkMessage message)
 {
     Message = message;
 }
Exemplo n.º 4
0
 private void Send(WebSocketSession session, NetworkMessage msg)
 {
     try
     {
         if (IsStarted && msg != null)
             session.Send(msg.PackToString());
     }
     catch (Exception) { }
 }