public static AutomationModuleDto FromModel(AutomationModule item)
        {
            if (item == null)
                return null;

            return new AutomationModuleDto()
            {
                ID = item.ID.ToString(),
                Name = item.Name,
                Description = item.Description,
                Script = item.Script,
                View = item.View
            };
        }
        private string RunAutomationService(AutomationModule module)
        {
            string errors = module.StartService(this);
            if (!string.IsNullOrEmpty(errors) && Log != null)
                Log(this, errors, true, LogLevel.Error);

            return errors;
        }
        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;
        }
 public int Update(AutomationModule item)
 {
     return con.Update(AutomationModuleDto.FromModel(item));
 }
 public int Insert(AutomationModule item)
 {
     return con.Insert(AutomationModuleDto.FromModel(item), "OR REPLACE");
 }
 public int Delete(AutomationModule item)
 {
     return con.Delete(AutomationModuleDto.FromModel(item));
 }