using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.ComponentModel;
using FObjBase;
using Misc;
using System.Collections.ObjectModel;
namespace FlyADBase
{
    /// <summary>
    /// AD盒提供的功能
    /// </summary>
    public interface IFlyAD : INotifyPropertyChanged
    {
        /// <summary>
        /// AD盒地址
        /// </summary>
        string Addr { get; set; }
        
        /// <summary>
        /// 从flyad7 获取 的systick 转换的时间
        /// </summary>
        DateTime Now { get; }

        /// <summary>
        /// AD盒版本号 版本1 没有systick;版本2 有systick;
        /// 通过通讯读取
        /// </summary>
        int Version { get; }

        /// <summary>
        /// 硬件版本，设置的！！！
        /// </summary>
        int HardwareVersion { get; set; }

        /// <summary>
        /// 脉冲
        /// </summary>
        int Position { get; }

        /// <summary>
        /// 速度 pps
        /// </summary>
        int Speed { get; }

        /// <summary>
        /// 1000ms从AD卡获取(或推送)1个AD值。此AD值只用于调试，定点
        /// </summary>
        int AD { get; }

        /// <summary>
        /// AD值最大值 ，恒定返回65535
        /// </summary>
        int ADMax { get; }
        
        /// <summary>
        /// 输入口状态,只有12位有效
        /// </summary>
        UInt16 IStatus { get; }

        /// <summary>
        /// 输出口状态,只有4位有效
        /// </summary>
        UInt16 OStatus { get; }

        /// <summary>
        /// Out0 = 0, SetOutput(0x0001,0x0000);
        /// Out0 = 1, Out1 = 0, SetOutput(0x0003,0x0001);
        /// </summary>
        /// <param name="mask"></param>
        /// <param name="enable"></param>
        void SetOutput(UInt16 mask, UInt16 enable); 

        /// <summary>
        /// 读取参数, 全部！！！！
        /// </summary>
        void UpdateParam();

        #region 运动参数
        /// <summary>
        /// 设置 运动参数
        /// </summary>
        /// <param name="posOfGrid">脉冲/grid</param>
        /// <param name="motortype">电机类型</param>
        /// <param name="ratio01">脉冲比例 分母(电机脉冲)</param>
        /// <param name="ratio02">脉冲比例 分子(编码器脉冲)</param>
        void SetSysParam(UInt16 posOfGrid, MOTORTYPE motortype, UInt16 ratio01, UInt16 ratio02);
        /// <summary>
        /// 电机类型
        /// </summary>
        MOTORTYPE MotorType { set; get; }
        /// <summary>
        /// 脉冲/grid, 最大1000个grid
        /// </summary>
        UInt16 PosOfGrid { set; get; }
        /// <summary>
        /// 脉冲比例 分母(电机脉冲)
        /// </summary>
        UInt16 Ratio01 { get; set; }
        /// <summary>
        /// 脉冲比例 分子(编码器脉冲)
        /// </summary>
        UInt16 Ratio02 { get; set; }

        #endregion

        /// <summary>
        /// 脉冲平移(0位处脉冲值)
        /// </summary>
        Int16 PosOffset { set; get; }
        /// <summary>
        /// 手动速度 pps
        /// </summary>
        UInt32 JogVelocity { set; get; }

        #region 速度参数
        /// <summary>
        /// 设置速度参数
        /// </summary>
        /// <param name="velocity">runto 速度 pps</param>
        /// <param name="sv">开始速度 pps</param>
        /// <param name="atime">加速时间 ms</param>
        /// <param name="dtime">减速时间 ms</param>
        /// <param name="hspeed1">归0 第1段速 pps</param>
        /// <param name="hspeed2">归0 第2段速 pps</param>
        void SetPosParam(UInt32 velocity, UInt32 sv, UInt32 atime, UInt32 dtime, UInt32 hspeed1, UInt32 hspeed2);
        /// <summary>
        /// 设置速度
        /// </summary>
        /// <param name="velocity"></param>
        void SetVelocity(UInt32 velocity);
        /// <summary>
        /// runto 速度 pps
        /// </summary>
        UInt32 Velocity { get; }
        /// <summary>
        /// 开始速度 pps
        /// </summary>
        UInt32 SVelocity { get; }
        /// <summary>
        /// 加速时间 ms
        /// </summary>
        UInt32 ATime { get; }
        /// <summary>
        /// 减速时间 ms
        /// </summary>
        UInt32 DTime { get; }
        /// <summary>
        /// 归0 第1段速 pps
        /// </summary>
        UInt32 HVelocity1 { get; }
        /// <summary>
        /// 归0 第2段速 pps
        /// </summary>
        UInt32 HVelocity2 { get; }
        
        #endregion

        #region 密码
        /// <summary>
        /// 使用码区状态
        /// </summary>
        AREA_STATUS AreaStatus { get; }
        /// <summary>
        /// 使用码区出错码
        /// </summary>
        AREA_ERR AreaRet { get; }
        /// <summary>
        /// 序列码byte[6]
        /// </summary>
        byte[] Code { get; }
        /// <summary>
        /// 系统剩余时间
        /// </summary>
        int Surplus { get; }
        /// <summary>
        /// 系统授权码access[8]
        /// </summary>
        byte[] Access { get; }
        /// <summary>
        /// 输入系统授权码
        /// </summary>
        /// <param name="access">系统授权码access[8]</param>
        void SetAccess(byte[] access);
        /// <summary>
        /// 初始化系统信息区
        /// </summary>
        void InitArea();

        #endregion


        /// <summary>
        /// timegrid 事件; 版本1: 1.28s 一次; 版本2: 0.2s一次， 
        /// </summary>
        event TimeGridEventHandler TimeGridEvent;

        /// <summary>
        /// runto 中，每经过1个grid 推送数据
        /// </summary>
        event MiniGridEventHandler MiniGridEvent;

        /// <summary>
        /// runto 的 grid事件
        /// </summary>
        event MiniGridEventHandler GridEvent;

        /// <summary>
        /// 输入状态改变事件
        /// </summary>
        event IStatusChangedEventHandler IStatusChangedEvent;

        /// <summary>
        /// 指令状态改变事件
        /// </summary>
        event DriveStatusChangedEventHandler DriveStatusChangedEvent;

        /// <summary>
        /// 脉冲1改变事件
        /// </summary>
        event PositionChangedEventHandler PositionChangedEvent;

        /// <summary>
        /// 脉冲2改变事件
        /// </summary>
        event PositionChangedEventHandler Position2ChangedEvent;


        #region 运行动作
        /// <summary>
        /// 运行至指定位置
        /// </summary>
        /// <param name="to">脉冲</param>
        void Runto(int to);
        /// <summary>
        /// 归原点
        /// </summary>
        void Origin();
        /// <summary>
        /// 停止
        /// </summary>
        void Stop();
        /// <summary>
        /// 反向，直到撞了限位
        /// </summary>
        void Backward();
        /// <summary>
        /// 正行，直到撞了限位
        /// </summary>
        void Forward();

        /// <summary>
        /// 当前运行指令
        /// </summary>
        DRIVE_MAN_ORDER DriveOrder { get; }
        /// <summary>
        /// 之前运行动作的状态
        /// </summary>
        DRIVE_MAN_STATUS DriveStatus { get; }
        #endregion


        #region 同步
        /// <summary>
        /// 同步用标示
        /// </summary>
        Int32 Marker { get; }
        #region 横向脉冲
        /// <summary>
        /// 逻辑横向脉冲偏移  Pos1 + Pos1LCShift = Pos1LC
        /// </summary>
        int Pos1LCShift { get; set; }
        #endregion
        #region 主轴脉冲
        /// <summary>
        /// 纵向脉冲，也叫主轴脉冲
        /// </summary>
        int Position2 { get; }
        /// <summary>
        /// 纵向速度
        /// </summary>
        int Speed2 { get; }

        /// <summary>
        /// 设置纵向位置
        /// </summary>
        /// <param name="postion2"></param>
        [Obsolete("pos2 不应该被修改")]
        void SetPos2(int postion2);

        /// <summary>
        /// 纵向偏移,这个是相对量
        /// </summary>
        [Obsolete("pos2 不应该被修改")]
        void SetPos2Offset(int offset);

        /// <summary>
        /// 纵向同步事件，0-1事件
        /// </summary>
        /// <param name="pos2"></param>
        /// <param name="immediately">只要是 01，就立刻修正</param>
        [Obsolete("pos2 不应该被修改")]
        void SetPos2At01(int pos2, bool immediately);

        #endregion
        #region 同步控制 同步状态转换规则
        /// <summary>
        /// 进入同步状态
        /// </summary>
        /// <param name="pos2"></param>
        [Obsolete("pos2 不应该被修改")]
        void SyncBegin(int pos2);
        /// <summary>
        /// 进入同步状态
        /// </summary>
        void SyncBegin();
        /// <summary>
        /// 退出同步状态
        /// </summary>
        void SyncEnd();
        /// <summary>
        /// 清空同步指令
        /// </summary>
        void SyncClear();
        /// <summary>
        /// 清空POS2同步列表
        /// </summary>
        [Obsolete("pos2 不应该被修改")]
        void SyncPos2Clear();
        #endregion
        #region 同步扫描 脚本指令

        /// <summary>
        /// 同步扫描至;
        /// D+0xE0+开始主轴位置+结束主轴位置+结束横向脉冲位置（逻辑位置）+脉冲开关（1B）+命令识标号（4B）
        /// </summary>
        /// <param name="pos2_begin"></param>
        /// <param name="pos2_end"></param>
        /// <param name="pos1lc"></param>
        /// <param name="hasDataGrid"></param>
        /// <param name="marker"></param>
        void SyncRunAtLC(int pos2_begin, int pos2_end, int pos1lc, bool hasDataGrid, Int32 marker);


        /// <summary>
        /// 位于队列头时运行，归零；
        /// D+0xE1+命令识标号（4B）
        /// </summary>
        /// <param name="marker"></param>
        void SyncOrigin(Int32 marker);


        /// <summary>
        /// 位于队列头时运行，以速度运行至物理位置；
        /// D+0xE2+横向脉冲位置（4B：int32，物理位置）+速度（4B：int32）+脉冲开关（1B）+命令识标号（4B）
        /// </summary>
        /// <param name="pos1"></param>
        /// <param name="velocity"></param>
        /// <param name="hasDataGrid"></param>
        /// <param name="marker"></param>
        void SyncRunTo(int pos1, UInt32 velocity, bool hasDataGrid, Int32 marker);


        /// <summary>
        /// 位于队列头时运行，以速度运行至逻辑位置；
        /// D+0xE3+横向脉冲位置（4B：int32，逻辑位置）+速度（4B：int32）+脉冲开关（1B）+命令识标号（4B）
        /// </summary>
        /// <param name="pos1lc"></param>
        /// <param name="velocity"></param>
        /// <param name="hasDataGrid"></param>
        /// <param name="marker"></param>
        void SyncRunToLC(int pos1lc, UInt32 velocity, bool hasDataGrid, Int32 marker);


        /// <summary>
        /// 等待，ms；
        /// D+0xE4+毫秒数（4B：int32）+命令识标号（4B）
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="marker"></param>
        void SyncWait(int ms, Int32 marker);

        #endregion
        #endregion
    }



    /// <summary>
    /// 电机类型
    /// </summary>
    public enum MOTORTYPE
    {
        /// <summary>
        /// 没电机
        /// </summary>
        NULL = 0,
        /// <summary>
        /// 异步电机，通过I1~I3控制 正反转
        /// </summary>
        VF0 = 1,
        /// <summary>
        /// 伺服 或 步进电机，通过 S,P, 控制驱动器
        /// </summary>
        SERVO = 2
    }
    /// <summary>
    /// 使用码区状态
    /// </summary>
    public enum AREA_STATUS
    {
        /// <summary>
        /// 没有读过密码区
        /// </summary>
        NOTLOAD = 0,
        /// <summary>
        /// 读出错
        /// </summary>
        RDERR = 1,
        /// <summary>
        /// 非有效密码区 没有0xAA55 或 0x5A5A 标志
        /// </summary>
        NOTVALID = 2,
        /// <summary>
        /// 写出错
        /// </summary>
        WRERR = 3,
        /// <summary>
        /// 有效密码区
        /// </summary>
        VALID = 4,
        /// <summary>
        /// 有效密码区，应回写EEPROM
        /// </summary>
        VALID_DIRTY = 5,
        /// <summary>
        /// 有效密码区，但回写EEPROM失败
        /// </summary>
        VALID_WRERR = 6
    }
    /// <summary>
    /// 使用码区操作结果
    /// </summary>

    public enum AREA_ERR
    {
        /// <summary>
        /// 没有错误
        /// </summary>
        NO_ERR = 0,
        /// <summary>
        /// 重复使用码
        /// </summary>
        DUP_ACCESS = 1,
        /// <summary>
        /// 无效使用码
        /// </summary>
        ERR_ACCESS = 2
    }
    /// <summary>
    /// 运行状态
    /// </summary>
    public enum DRIVE_MAN_STATUS
    {
        /// <summary>
        /// 正常运行结束
        /// </summary>
        STOP = 0,
        /// <summary>
        /// 正在运行
        /// </summary>
        RUNNING = 1,
        /// <summary>
        /// 指令结束（收到停止指令）
        /// </summary>
        STOP_MANUAL = 2,
        /// <summary>
        /// 运行结束，限位开关闭合
        /// </summary>
        LIMIT = 3
    }
    /// <summary>
    /// 运行动作
    /// </summary>
    public enum DRIVE_MAN_ORDER
    {
        /// <summary>
        /// 闲置
        /// </summary>
        IDLE = 'I',
        /// <summary>
        /// 运行到
        /// </summary>
        RUNTO = 'R',
        /// <summary>
        /// 归原点
        /// </summary>
        ORIGIN = 'O',
        /// <summary>
        /// 后退
        /// </summary>
        BACKWORD = 'B',
        /// <summary>
        /// 前进
        /// </summary>
        FORWORD = 'F',
        /// <summary>
        /// 停止
        /// </summary>
        STOP = 'S',
        /// <summary>
        /// 同步
        /// </summary>
        SYNC = 'D',

        /// <summary>
        /// 未知状态
        /// </summary>
        SYNC2 = 'P'
    }

    /// <summary>
    /// 同步状态
    /// </summary>
    public enum SYNC_STATUS
    {
        /// <summary>
        /// 
        /// </summary>
        SYNCST_NO = 0,
        /// <summary>
        /// 
        /// </summary>
        SYNCST_PENDING1 = 1,
        /// <summary>
        /// 
        /// </summary>
        SYNCST_PENDING2 = 2,
        /// <summary>
        /// 
        /// </summary>
        SYNCST_PENDING3 = 3,
        /// <summary>
        /// 
        /// </summary>
        SYNCST_TEMP_OUT = 4,
        /// <summary>
        /// 
        /// </summary>
        SYNCST_WAIT_TO_PENDING = 5,
        /// <summary>
        /// 
        /// </summary>
        SYNCST_RESUME = 6,
        /// <summary>
        /// 
        /// </summary>
        SYNCST_YES = 10,

    }










}
