Exemplo n.º 1
0
 public AssignReturnRouteResult AssignPriorityReturnRoute(byte source, byte destination,
                                                          byte priorityRoute0, byte priorityRoute1, byte priorityRoute2, byte priorityRoute3, byte routespeed, out ActionToken token)
 {
     token = ExecuteAsync(new AssignPriorityReturnRouteOperation(source, destination,
                                                                 priorityRoute0, priorityRoute1, priorityRoute2, priorityRoute3, routespeed), null);
     return((AssignReturnRouteResult)token.WaitCompletedSignal());
 }
Exemplo n.º 2
0
        //private ActionToken controllerListener;

        //public void AddToQueue(Action action)
        //{
        //    queueItems.Add(action);
        //}

        public override bool Start()
        {
            Common.logger.Info("-----------");
            Common.logger.Info("Listening mode");
            Common.logger.Info("-----------");

            Common.logger.Info("Loading available command classes...");
            var assembly       = typeof(COMMAND_CLASS_BASIC).GetTypeInfo().Assembly;
            var commandClasses = Common.GetAllCommandClasses(assembly, "CommandClasses");

            Common.logger.Info("Got {0} command classes", commandClasses.Count);
            var nestedCommandClasses = Common.GetAllNestedCommandClasses(commandClasses.Values);

            Common.logger.Info("Got all inner command classes for {0} command classes", commandClasses.Count);
            Common.logger.Info("Listening...");

            //byte[] numArray = File.ReadAllBytes(@"C:\Users\james\Desktop\tmp\MultiSensor 6_OTA_EU_A_V1_13.exe");
            //int length = (int)numArray[numArray.Length - 4] << 24 | (int)numArray[numArray.Length - 3] << 16 | (int)numArray[numArray.Length - 2] << 8 | (int)numArray[numArray.Length - 1];
            //byte[] flashDataB = new byte[length];
            //Array.Copy((Array)numArray, numArray.Length - length - 4 - 4 - 256, (Array)flashDataB, 0, length);
            //List<byte> flashData = new List<byte>(flashDataB);

            dataListener = controller.ListenData((x) =>
            {
                lock (lockObject)
                {
                    var filterActive = Filter != 0 && Filter != x.SrcNodeId;

                    var _commandClass = commandClasses.TryGetValue(x.Command[0], out var commandClass);
                    if (!_commandClass)
                    {
                        Common.logger.Error("node id: {1} - command class {0} not found!", x.Command[0], x.SrcNodeId);
                        return;
                    }
                    var _nestedDict = nestedCommandClasses.TryGetValue(commandClass, out var nestedDict);
                    if (!_nestedDict)
                    {
                        Common.logger.Error("node id: {1} - nested command classes for command class {0} not found!", commandClass.Name, x.SrcNodeId);
                        return;
                    }
                    var _nestedType = nestedDict.TryGetValue(x.Command[1], out Type nestedType);
                    if (!_nestedType)
                    {
                        Common.logger.Error("node id: {2} - nested command class {0} for command class {1} not found!", x.Command[1], commandClass.Name, x.SrcNodeId);
                        return;
                    }

                    if (!filterActive)
                    {
                        Common.logger.Info("{0}: {2}:{3} from node {1}", x.TimeStamp, x.SrcNodeId, _commandClass ? commandClass.Name : string.Format("unknown(id:{0})", x.Command[0]), _nestedType ? nestedType.Name : string.Format("unknown(id:{0})", x.Command[1]));
                    }

                    if (commandClass == null)
                    {
                        Common.logger.Error("command class is null!");
                        return;
                    }
                    if (nestedType == null)
                    {
                        Common.logger.Error("nested type is null!");
                        return;
                    }

                    //     var dummyInstance = Activator.CreateInstance(nestedType);

                    var implicitCastMethod =
                        nestedType.GetMethod("op_Implicit",
                                             new[] { x.Command.GetType() });

                    if (implicitCastMethod == null)
                    {
                        Common.logger.Warn("byteArray to {0} not possible!", nestedType.Name);
                        return;
                    }
                    var report = implicitCastMethod.Invoke(null, new[] { x.Command });
                    report.GetKeyValue(out Enums.EventKey eventKey, out float eventValue);
                    Common.logger.Info($"key: {eventKey} - value: {eventValue}");

                    if (!filterActive && Debug)
                    {
                        Common.logger.Info(Util.ObjToJson(report));
                    }

                    if (!(report is COMMAND_CLASS_FIRMWARE_UPDATE_MD_V5.FIRMWARE_UPDATE_MD_GET))
                    {
                        OutputManager.HandleCommand(report, x.SrcNodeId, x.DestNodeId);
                    }

                    //    Handle(dummyInstance, x.SrcNodeId, x.Command);
                    if (!Active)
                    {
                        return;
                    }

                    //switch (report)
                    //{
                    //    case COMMAND_CLASS_WAKE_UP_V2.WAKE_UP_NOTIFICATION _:
                    //        // TODO EVENT, last Battery
                    //        var lastDate = eventDao.GetLastEvent(typeof(COMMAND_CLASS_BATTERY.BATTERY_REPORT).Name, x.SrcNodeId);
                    //        if ((DateTime.Now - lastDate).TotalHours >= 6)
                    //        {
                    //            queueItems.Add(() => Common.RequestBatteryReport(controller, x.SrcNodeId));
                    //        }

                    //        break;

                    //    //case COMMAND_CLASS_FIRMWARE_UPDATE_MD_V5.FIRMWARE_UPDATE_MD_GET fupdateReport:

                    //    //    var rep1 = fupdateReport.properties1.reportNumber1;
                    //    //    var rep2 = fupdateReport.reportNumber2;
                    //    //    var count = fupdateReport.numberOfReports;

                    //    //    queueItems.Add(() =>
                    //    //    {
                    //    //        var take = 40;
                    //    //        var cmd = new COMMAND_CLASS_FIRMWARE_UPDATE_MD_V2.FIRMWARE_UPDATE_MD_REPORT();
                    //    //        cmd.properties1.last = 0;
                    //    //        cmd.properties1.reportNumber1 = rep1;
                    //    //        cmd.reportNumber2 = rep2;
                    //    //        short repNumber = BitConverter.ToInt16(new byte[] { rep2, rep1 });
                    //    //        //Console.WriteLine($"Repnumber: {repNumber}");
                    //    //        var offset = (repNumber - 1) * 40;
                    //    //        Common.logger.Info("Progress: {0}", (offset / (float)flashData.Count).ToString("0.00%"));

                    //    //        if ((flashData.Count - offset) < 40)
                    //    //        {
                    //    //            // Console.WriteLine("ima full!");
                    //    //            Console.WriteLine(offset);
                    //    //            Console.WriteLine(flashData.Count);
                    //    //            take = flashData.Count - offset;
                    //    //            Console.WriteLine(take);
                    //    //            cmd.properties1.last = 1;
                    //    //            Common.logger.Info("Progress: {0}", (1).ToString("0.00%"));
                    //    //        }
                    //    //        var data = flashData.Skip(offset).Take(take).ToArray();
                    //    //        cmd.data = data;
                    //    //        //7A 97
                    //    //        // Common.logger.Info(Util.ObjToJson((byte[])cmd));
                    //    //        // Common.logger.Info(Util.ObjToJson(new byte[] { COMMAND_CLASS_FIRMWARE_UPDATE_MD_V2.ID, COMMAND_CLASS_FIRMWARE_UPDATE_MD_V2.FIRMWARE_UPDATE_MD_REPORT.ID }.Concat(new byte[] { cmd.properties1, cmd.reportNumber2 }).Concat(data).ToArray()));
                    //    //        cmd.checksum = Tools.CalculateCrc16Array((byte[])cmd, 0, ((byte[])cmd).Length - 2);
                    //    //        //cmd.checksum =  Tools.CalculateCrc16Array(new byte[] { COMMAND_CLASS_FIRMWARE_UPDATE_MD_V2.ID, COMMAND_CLASS_FIRMWARE_UPDATE_MD_V2.FIRMWARE_UPDATE_MD_REPORT .ID}.Concat(new byte[] { cmd.properties1, cmd.reportNumber2 }).Concat(data));
                    //    //        controller.SendData(x.SrcNodeId, cmd, Common.txOptions);
                    //    //    });

                    //    //    break;

                    //    default:
                    //        break;
                    //}
                }
            });

            //controllerListener = controller.HandleControllerUpdate((r) =>
            //{
            //    if (!Active)
            //    {
            //        return;
            //    }
            //    Common.logger.Info("{0}: Got {2} for node {1}", DateTime.Now, r.NodeId, r.Status);
            //    var lastDate = eventDao.GetLastEvent(typeof(COMMAND_CLASS_BATTERY.BATTERY_REPORT).Name, r.NodeId);
            //    if ((DateTime.Now - lastDate).TotalHours >= 6)
            //    {
            //        queueItems.Add(() => Common.RequestBatteryReport(controller, r.NodeId));
            //    }
            //});

            //Active = true;
            //while (!queueItems.IsCompleted)
            //{
            //    try
            //    {
            //        var action = queueItems.Take();
            //        if (Active)
            //            action();
            //    }
            //    catch (InvalidOperationException) { }
            //}
            dataListener.WaitCompletedSignal();

            //   Active = false;
            Common.logger.Info("Listening done!");
            return(true);
        }
Exemplo n.º 3
0
 public RequestNodeNeighborUpdateResult RequestNodeNeighborUpdate(byte nodeId, int timeoutMs, out ActionToken token)
 {
     token = RequestNodeNeighborUpdate(nodeId, timeoutMs, null);
     return((RequestNodeNeighborUpdateResult)token.WaitCompletedSignal());
 }
Exemplo n.º 4
0
 public ActionResult DeleteSucReturnRoute(byte nodeId, out ActionToken token)
 {
     token = ExecuteAsync(new DeleteSucReturnRouteOperation(nodeId), null);
     return(token.WaitCompletedSignal());
 }
Exemplo n.º 5
0
 public ActionResult AssignPrioritySucReturnRoute(byte source, byte repeater0, byte repeater1, byte repeater2, byte repeater3, byte routespeed, out ActionToken token)
 {
     token = ExecuteAsync(new AssignPrioritySucReturnRouteOperation(source, repeater0, repeater1, repeater2, repeater3, routespeed), null);
     return(token.WaitCompletedSignal());
 }
Exemplo n.º 6
0
 public AssignReturnRouteResult AssignReturnRoute(byte nodeId1, byte nodeId2, out ActionToken token)
 {
     token = ExecuteAsync(new AssignReturnRouteOperation(nodeId1, nodeId2), null);
     return((AssignReturnRouteResult)token.WaitCompletedSignal());
 }
Exemplo n.º 7
0
        //public void AddToQueue(Action action)
        //{
        //    queueItems.Add(action);
        //}

        public override bool Start()
        {
            Common.logger.Info("-----------");
            Common.logger.Info("Listening mode");
            Common.logger.Info("-----------");

            Common.logger.Info("Loading available command classes...");
            var assembly       = typeof(COMMAND_CLASS_BASIC).GetTypeInfo().Assembly;
            var commandClasses = Common.GetAllCommandClasses(assembly, "CommandClasses");

            Common.logger.Info("Got {0} command classes", commandClasses.Count);
            var nestedCommandClasses = Common.GetAllNestedCommandClasses(commandClasses.Values);

            Common.logger.Info("Got all inner command classes for {0} command classes", commandClasses.Count);
            Common.logger.Info("Listening...");

            //byte[] numArray = File.ReadAllBytes(@"C:\Users\james\Desktop\tmp\MultiSensor 6_OTA_EU_A_V1_13.exe");
            //int length = (int)numArray[numArray.Length - 4] << 24 | (int)numArray[numArray.Length - 3] << 16 | (int)numArray[numArray.Length - 2] << 8 | (int)numArray[numArray.Length - 1];
            //byte[] flashDataB = new byte[length];
            //Array.Copy((Array)numArray, numArray.Length - length - 4 - 4 - 256, (Array)flashDataB, 0, length);
            //List<byte> flashData = new List<byte>(flashDataB);

            dataListener = controller.ListenData((x) =>
            {
                lock (lockObject)
                {
                    var _commandClass = commandClasses.TryGetValue(x.Command[0], out var commandClass);
                    if (!_commandClass)
                    {
                        Common.logger.Error("node id: {1} - command class {0} not found!", x.Command[0], x.SrcNodeId);
                        return;
                    }
                    var _nestedDict = nestedCommandClasses.TryGetValue(commandClass, out var nestedDict);
                    if (!_nestedDict)
                    {
                        Common.logger.Error("node id: {1} - nested command classes for command class {0} not found!", commandClass.Name, x.SrcNodeId);
                        return;
                    }
                    var _nestedType = nestedDict.TryGetValue(x.Command[1], out Type nestedType);
                    if (!_nestedType)
                    {
                        Common.logger.Error("node id: {2} - nested command class {0} for command class {1} not found!", x.Command[1], commandClass.Name, x.SrcNodeId);
                        return;
                    }

                    //  Common.logger.Info("{0}: {2}:{3} from node {1}", x.TimeStamp, x.SrcNodeId, _commandClass ? commandClass.Name : string.Format("unknown(id:{0})", x.Command[0]), _nestedType ? nestedType.Name : string.Format("unknown(id:{0})", x.Command[1]));

                    if (commandClass == null)
                    {
                        Common.logger.Error("command class is null!");
                        return;
                    }
                    if (nestedType == null)
                    {
                        Common.logger.Error("nested type is null!");
                        return;
                    }

                    //     var dummyInstance = Activator.CreateInstance(nestedType);

                    var implicitCastMethod =
                        nestedType.GetMethod("op_Implicit",
                                             new[] { x.Command.GetType() });

                    if (implicitCastMethod == null)
                    {
                        Common.logger.Warn("byteArray to {0} not possible!", nestedType.Name);
                        return;
                    }
                    var report = implicitCastMethod.Invoke(null, new[] { x.Command });

                    switch (report)
                    {
                    case COMMAND_CLASS_WAKE_UP_V2.WAKE_UP_NOTIFICATION _:

                        var commandsPresent = nodeToCommandMap.TryGetValue(x.SrcNodeId, out SortedSet <string> commands);
                        if (!commandsPresent)
                        {
                            //Common.logger.Warn($"no commands for {x.SrcNodeId}; check battery if needed");
                            //var lastDate = eventDao.GetLastEvent(typeof(COMMAND_CLASS_BATTERY.BATTERY_REPORT).Name, x.SrcNodeId);
                            //if ((DateTime.Now - lastDate).TotalHours >= 1)
                            {
                                inputManager.InjectCommand($"battery {x.SrcNodeId}");
                            }
                            return;
                        }

                        var command = commands.First();
                        if (command == "config")
                        {
                            command = command + " " + x.SrcNodeId + "!";
                        }
                        Common.logger.Warn($"injecting {command}");
                        inputManager.InjectCommand(command);
                        commands.Remove(commands.First());
                        if (commands.Count == 0)
                        {
                            nodeToCommandMap.Remove(x.SrcNodeId);
                        }

                        break;

                    default:
                        break;
                    }
                }
            });

            controllerListener = controller.HandleControllerUpdate((r) =>
            {
                var commandsPresent = nodeToCommandMap.TryGetValue(r.NodeId, out SortedSet <string> commands);
                if (!commandsPresent)
                {
                    //Common.logger.Warn($"no commands for {r.NodeId}; check battery if needed");
                    var lastDate = eventDao.GetLastEvent(typeof(COMMAND_CLASS_BATTERY.BATTERY_REPORT).Name, r.NodeId);
                    if ((DateTime.Now - lastDate).TotalHours >= 1)
                    {
                        inputManager.InjectCommand($"battery {r.NodeId}");
                    }
                    return;
                }

                var command = commands.First();
                if (command == "config")
                {
                    command = command + " " + r.NodeId + "!";
                }
                Common.logger.Warn($"injecting {command}");
                inputManager.InjectCommand(command);
                commands.Remove(commands.First());
                if (commands.Count == 0)
                {
                    nodeToCommandMap.Remove(r.NodeId);
                }
            });

            //Active = true;
            //while (!queueItems.IsCompleted)
            //{
            //    try
            //    {
            //        var action = queueItems.Take();
            //        if (Active)
            //            action();
            //    }
            //    catch (InvalidOperationException) { }
            //}
            dataListener.WaitCompletedSignal();
            controllerListener.WaitCompletedSignal();
            //Active = false;
            Common.logger.Info("Listening done!");
            return(true);
        }
Exemplo n.º 8
0
 public TransmitResult SendDataMultiBridge(byte srcNodeId, byte[] receiverNodeIds, byte[] data, TransmitOptions txOptions, SubstituteSettings substituteSettings, out ActionToken token)
 {
     token = SendDataMultiBridge(srcNodeId, receiverNodeIds, data, txOptions, substituteSettings, null);
     return((TransmitResult)token.WaitCompletedSignal());
 }