예제 #1
0
파일: Manager.cs 프로젝트: robrhce/Tulip
        public void PostCommand(Point p, Command unatt_c)
        {
            OutstationWrapper ow = Outstations.Where(x => x.Model.Id == p.OutstationID).SingleOrDefault();

            if (ow != null)
            {
                if (ow.state == StackState.COMMS_UP)
                {
                    unatt_c.TimestampSent = DateTime.UtcNow;

                    if (p.Type == BasicType.ANALOG_CONTROL)
                    {
                        AnalogOutputFloat32 aof = unatt_c.GetAnalogOutput();

                        var future = ow.Master.GetCommandProcessor().DirectOperate(aof, Convert.ToUInt32(p.PointIndex));
                        // Use a lambda to curry the command object into the callback as well
                        future.Listen((cr) => CommandComplete(unatt_c, cr));

                    }
                    else if (p.Type == BasicType.DIGITAL_CONTROL)
                    {
                        ControlRelayOutputBlock crob = unatt_c.GetCROB();

                        var future = ow.Master.GetCommandProcessor().DirectOperate(crob, Convert.ToUInt32(p.PointIndex));
                        // Use a lambda to curry the command object into the callback as well
                        future.Listen((cr) => CommandComplete(unatt_c, cr));
                    }
                    else
                    {
                        throw new ArgumentException("c.Point.Type != POINT_TYPE.{DIGITAL_CONTROL,ANALOG_CONTROL}");
                    }

                    // TODO: how to ensure this makes it into the DB before the callback gets fired?
                    // or does it not matter because the first line pushes it into the model?

                    p.Commands.Add(unatt_c);
                    TulipContext.SaveChanges();
                }
                else
                {
                    throw new InvalidOperationException("Cannot issue command, communications with outstation down (COMMS_DOWN)");
                }
            }
        }