Example #1
0
        // Threaded home IO update
        private static void HomeIOUpdate(object parameter)
        {
            MemoryMap homeio = parameter as MemoryMap;

            try {
                while (true)
                {
                    homeio.Update();
                    Thread.Sleep(100);
                }
            } catch (ThreadAbortException) { }
        }
Example #2
0
        public HomeIOWSModule()
        {
            // register routes
            Get("/", args => string.Join("\n", topicToMemory.Keys.Select(k => "/home/" + k)));
            Get("/home/{devid*}", args => getValue(topicToMemory[args.devid]));
            Post("/home/{devid*}", args => setValue(args));

            if (!init)
            {
                init = true;
                // init HomeIO
                Console.WriteLine("Initializing Home IO...");
                MemoryBit[]      bitI, bitO, bitIO;
                MemoryByte[]     byteI, byteO, byteIO;
                MemoryShort[]    shortI, shortO, shortIO;
                MemoryInt[]      intI, intO, intIO;
                MemoryLong[]     longI, longO, longIO;
                MemoryFloat[]    floatI, floatO, floatIO;
                MemoryDouble[]   doubleI, doubleO, doubleIO;
                MemoryString[]   stringI, stringO, stringIO;
                MemoryDateTime[] dtI, dtO, dtIO;
                MemoryTimeSpan[] tsI, tsO, tsIO;

                MemoryMap homeio = MemoryMap.Instance;

                bitI  = homeio.GetBitMemories(MemoryType.Input);
                bitO  = homeio.GetBitMemories(MemoryType.Output);
                bitIO = homeio.GetBitMemories(MemoryType.Memory);

                byteI  = homeio.GetByteMemories(MemoryType.Input);
                byteO  = homeio.GetByteMemories(MemoryType.Output);
                byteIO = homeio.GetByteMemories(MemoryType.Memory);

                shortI  = homeio.GetShortMemories(MemoryType.Input);
                shortO  = homeio.GetShortMemories(MemoryType.Output);
                shortIO = homeio.GetShortMemories(MemoryType.Memory);

                intI  = homeio.GetIntMemories(MemoryType.Input);
                intO  = homeio.GetIntMemories(MemoryType.Output);
                intIO = homeio.GetIntMemories(MemoryType.Memory);

                longI  = homeio.GetLongMemories(MemoryType.Input);
                longO  = homeio.GetLongMemories(MemoryType.Output);
                longIO = homeio.GetLongMemories(MemoryType.Memory);

                floatI  = homeio.GetFloatMemories(MemoryType.Input);
                floatO  = homeio.GetFloatMemories(MemoryType.Output);
                floatIO = homeio.GetFloatMemories(MemoryType.Memory);

                doubleI  = homeio.GetDoubleMemories(MemoryType.Input);
                doubleO  = homeio.GetDoubleMemories(MemoryType.Output);
                doubleIO = homeio.GetDoubleMemories(MemoryType.Memory);

                stringI  = homeio.GetStringMemories(MemoryType.Input);
                stringO  = homeio.GetStringMemories(MemoryType.Output);
                stringIO = homeio.GetStringMemories(MemoryType.Memory);

                dtI  = homeio.GetDateTimeMemories(MemoryType.Input);
                dtO  = homeio.GetDateTimeMemories(MemoryType.Output);
                dtIO = homeio.GetDateTimeMemories(MemoryType.Memory);

                tsI  = homeio.GetTimeSpanMemories(MemoryType.Input);
                tsO  = homeio.GetTimeSpanMemories(MemoryType.Output);
                tsIO = homeio.GetTimeSpanMemories(MemoryType.Memory);

                homeio.Update();

                List <string> topics    = new List <string>();
                List <byte>   qosLevels = new List <byte>();

                topicToMemory = new Dictionary <string, Memory>();
                memoryToTopic = new Dictionary <Memory, string>();

                Regex pattern = new Regex(@"[A-Z]\ -\ *");

                Memory[][] allMemories = new Memory[][] {
                    bitI, byteI, shortI, intI, longI, floatI, doubleI, stringI, dtI, tsI,
                    bitO, byteO, shortO, intO, longO, floatO, doubleO, stringO, dtO, tsO,
                    bitIO, byteIO, shortIO, intIO, longIO, floatIO, doubleIO, stringIO, dtIO, tsIO
                };
                foreach (var memoryArray in allMemories)
                {
                    foreach (var memory in memoryArray)
                    {
                        if (memory.HasName)
                        {
                            string room  = "general";
                            string topic = memory.Name;
                            if (pattern.Match(topic).Success)
                            {
                                room  = topic.Substring(0, 1);
                                topic = topic.Substring(4);
                            }
                            topicToMemory.Add(room + "/" + memory.MemoryType + "/" + GetTypeForMemory(memory) + "/" + Clean(topic), memory);
                            memoryToTopic.Add(memory, room + "/" + memory.MemoryType + "/" + GetTypeForMemory(memory) + "/" + Clean(topic));

                            Console.WriteLine(memory.MemoryType + "," + memory.Address + "," + memory.Name + "," + room + "," + GetTypeForMemory(memory) + "," + "/home/" + room + "/" + memory.MemoryType + "/" + GetTypeForMemory(memory) + "/" + Clean(topic));

                            // Listen to memory changes

                            /*if (memory.MemoryType == MemoryType.Input || memory.MemoryType == MemoryType.Memory) {
                             *  memory.PropertyChanged += Memory_PropertyChanged;
                             * }
                             *
                             * // Subscribe to the topic
                             * if (memory.MemoryType == MemoryType.Output) {
                             *  topics.Add("/home/" + room + "/" + memory.MemoryType + "/" + GetTypeForMemory(memory) + "/" + Clean(topic));
                             *  qosLevels.Add(MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE);
                             * }*/
                        }
                    }
                }
                Console.WriteLine(topicToMemory.Count);
                if (/*topics.Count != qosLevels.Count ||*/ topicToMemory.Count == 0 || memoryToTopic.Count == 0)
                {
                    Console.WriteLine("! Home IO init FAILED\n\nMake sure Home IO is running and that at least one device has its DeviceMode set to External\n");
                    Console.WriteLine("! Restart this application");
                }

                Thread t = new Thread(new ParameterizedThreadStart(HomeIOUpdate));
                t.Start(homeio);
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Initializing Home IO...");
            MemoryBit[]      bitI, bitO, bitIO;
            MemoryByte[]     byteI, byteO, byteIO;
            MemoryShort[]    shortI, shortO, shortIO;
            MemoryInt[]      intI, intO, intIO;
            MemoryLong[]     longI, longO, longIO;
            MemoryFloat[]    floatI, floatO, floatIO;
            MemoryDouble[]   doubleI, doubleO, doubleIO;
            MemoryString[]   stringI, stringO, stringIO;
            MemoryDateTime[] dtI, dtO, dtIO;
            MemoryTimeSpan[] tsI, tsO, tsIO;

            MemoryMap homeio = MemoryMap.Instance;

            bitI  = homeio.GetBitMemories(MemoryType.Input);
            bitO  = homeio.GetBitMemories(MemoryType.Output);
            bitIO = homeio.GetBitMemories(MemoryType.Memory);

            byteI  = homeio.GetByteMemories(MemoryType.Input);
            byteO  = homeio.GetByteMemories(MemoryType.Output);
            byteIO = homeio.GetByteMemories(MemoryType.Memory);

            shortI  = homeio.GetShortMemories(MemoryType.Input);
            shortO  = homeio.GetShortMemories(MemoryType.Output);
            shortIO = homeio.GetShortMemories(MemoryType.Memory);

            intI  = homeio.GetIntMemories(MemoryType.Input);
            intO  = homeio.GetIntMemories(MemoryType.Output);
            intIO = homeio.GetIntMemories(MemoryType.Memory);

            longI  = homeio.GetLongMemories(MemoryType.Input);
            longO  = homeio.GetLongMemories(MemoryType.Output);
            longIO = homeio.GetLongMemories(MemoryType.Memory);

            floatI  = homeio.GetFloatMemories(MemoryType.Input);
            floatO  = homeio.GetFloatMemories(MemoryType.Output);
            floatIO = homeio.GetFloatMemories(MemoryType.Memory);

            doubleI  = homeio.GetDoubleMemories(MemoryType.Input);
            doubleO  = homeio.GetDoubleMemories(MemoryType.Output);
            doubleIO = homeio.GetDoubleMemories(MemoryType.Memory);

            stringI  = homeio.GetStringMemories(MemoryType.Input);
            stringO  = homeio.GetStringMemories(MemoryType.Output);
            stringIO = homeio.GetStringMemories(MemoryType.Memory);

            dtI  = homeio.GetDateTimeMemories(MemoryType.Input);
            dtO  = homeio.GetDateTimeMemories(MemoryType.Output);
            dtIO = homeio.GetDateTimeMemories(MemoryType.Memory);

            tsI  = homeio.GetTimeSpanMemories(MemoryType.Input);
            tsO  = homeio.GetTimeSpanMemories(MemoryType.Output);
            tsIO = homeio.GetTimeSpanMemories(MemoryType.Memory);

            homeio.Update();

            List <string> topics    = new List <string>();
            List <byte>   qosLevels = new List <byte>();

            topicToMemory = new Dictionary <string, Memory>();
            memoryToTopic = new Dictionary <Memory, string>();

            Regex pattern = new Regex(@"[A-Z]\ -\ *");

            Memory[][] allMemories = new Memory[][] {
                bitI, byteI, shortI, intI, longI, floatI, doubleI, stringI, dtI, tsI,
                bitO, byteO, shortO, intO, longO, floatO, doubleO, stringO, dtO, tsO,
                bitIO, byteIO, shortIO, intIO, longIO, floatIO, doubleIO, stringIO, dtIO, tsIO
            };
            foreach (var memoryArray in allMemories)
            {
                foreach (var memory in memoryArray)
                {
                    if (memory.HasName)
                    {
                        string room  = "general";
                        string topic = memory.Name;
                        if (pattern.Match(topic).Success)
                        {
                            room  = topic.Substring(0, 1);
                            topic = topic.Substring(4);
                        }
                        topicToMemory.Add("/home/" + room + "/" + memory.MemoryType + "/" + GetTypeForMemory(memory) + "/" + Clean(topic), memory);
                        memoryToTopic.Add(memory, "/home/" + room + "/" + memory.MemoryType + "/" + GetTypeForMemory(memory) + "/" + Clean(topic));

                        Console.WriteLine(memory.MemoryType + "," + memory.Address + "," + memory.Name + "," + room + "," + GetTypeForMemory(memory) + "," + "/home/" + room + "/" + memory.MemoryType + "/" + GetTypeForMemory(memory) + "/" + Clean(topic));

                        // Listen to memory changes
                        if (memory.MemoryType == MemoryType.Input || memory.MemoryType == MemoryType.Memory)
                        {
                            memory.PropertyChanged += Memory_PropertyChanged;
                        }

                        // Subscribe to the topic
                        if (memory.MemoryType == MemoryType.Output)
                        {
                            topics.Add("/home/" + room + "/" + memory.MemoryType + "/" + GetTypeForMemory(memory) + "/" + Clean(topic));
                            qosLevels.Add(MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE);
                        }
                    }
                }
            }

            if (topics.Count != qosLevels.Count || topics.Count == 0 || qosLevels.Count == 0)
            {
                Console.WriteLine("Home IO init FAILED\n\nMake sure Home IO is running\n");
                Console.WriteLine("Press any key to exit");
                Console.ReadLine();
                return;
            }


            // parametrized
            foreach (string param in args)
            {
                try {
                    MqttClient client = new MqttClient(param);
                    client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
                    client.Connect(Guid.NewGuid().ToString());
                    client.Subscribe(topics.ToArray(), qosLevels.ToArray());
                    clients.Add(client);
                    Console.WriteLine("\nConnected to broker at " + param + "\n");
                } catch (Exception e) {
                    Console.WriteLine("Unable to connect to broker " + param + "\n->" + e);
                }
            }
            // default
            if (args.Length == 0)
            {
                try {
                    MqttClient client = new MqttClient("localhost");
                    client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
                    client.Connect(Guid.NewGuid().ToString());
                    client.Subscribe(topics.ToArray(), qosLevels.ToArray());
                    clients.Add(client);
                    Console.WriteLine("\nConnected to broker at localhost\n");
                } catch (Exception e) {
                    Console.WriteLine("Unable to connect to broker localhost\n->" + e);
                }
            }

            Thread t = new Thread(new ParameterizedThreadStart(HomeIOUpdate));

            t.Start(homeio);

            Console.WriteLine("\nConnected\n\nRunning...");
            Console.WriteLine("Press any key to exit");
            Console.ReadLine();

            t.Abort();
            foreach (MqttClient client in clients)
            {
                client.Disconnect();
            }
            homeio.Dispose();
        }