public int SetCalibrationValue(byte type, byte cmd, short value)
        {
            byte[] data = new byte[4];

            int offset = 0;

            //data.Add(ref offset, BitConverter.GetBytes(type));
            //data.Add(ref offset, BitConverter.GetBytes(cmd));
            //data.Add(ref offset, BitConverter.GetBytes(value));
            data.Add(ref offset, type);
            data.Add(ref offset, cmd);
            data.Add(ref offset, BitConverter.GetBytes(value));
            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdHandleCalibration, data, sender, receiver);

            V45Packet ret = SendMessage(cmdPack);

            return(ret.Payload[0]);
            //SendMessage(cmdPack);
            //if (ret.Payload[0]==0)
            //{
            //    return 0;
            //}
            //else
            //{
            //    return 1;
            //}
            //return BitConverter.ToInt32(ret.Payload, 2);
            //return BitConverter.ToUInt16(ret.Payload, 0);
        }
        private V45Packet HandleFileTransfer(String fileName, Byte option, byte[] tempdata)
        {
            byte[] data;
            if (option < 4)
            {
                data = new byte[1];
            }
            else
            {
                data = new byte[1 + tempdata.Length];
            }
            //byte[] data = new byte[fileName.Length];

            int offset = 0;

            data.Add(ref offset, option);
            if (option >= 4)
            {
                data.Add(ref offset, tempdata);
            }
            //data.Add(ref offset, Convert.ToByte(fileName));


            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdSendFile, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);

            return(packet);
        }
        public SubscriptionResult UnSubscribeToMeasurepoints(List <UInt16> measurePoints)
        {
            try
            {
                byte[] data = new byte[3 + (2 * measurePoints.Count)];

                data[0] = 3;
                data[1] = 1;
                data[2] = Convert.ToByte(measurePoints.Count);

                int i      = 0;
                int offset = 3;
                while (i < 10 && i < measurePoints.Count)
                {
                    data.Add(ref offset, BitConverter.GetBytes(measurePoints[i]));
                    i++;
                }

                V45Packet cmdPack      = new V45Packet(V45Packet.V45Commands.cmdHandleMeasurePointSub, data, this.sender, this.receiver);
                V45Packet responsePack = SendMessage(cmdPack);
                return(new SubscriptionResult(responsePack.Payload));
            }
            catch (DisconnectedException e)
            {
                if (communication.Connect())
                {
                    return(UnSubscribeToMeasurepoints(measurePoints));
                }
                else
                {
                    throw e;
                }
            }
        }
        public UInt16[] GetAvailableMeasurePoints()
        {
            UInt16[] availablemeasurepoints;
            try
            {
                byte[] data = new byte[0];

                V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdGetAvailableMeasPoints, data, this.sender, this.receiver);

                V45Packet packet = SendMessage(cmdPack);

                UInt16 measurePointCount = BitConverter.ToUInt16(packet.Payload, 0);
                availablemeasurepoints = new UInt16[measurePointCount];
                for (int i = 0; i < measurePointCount; i++)
                {
                    availablemeasurepoints[i] = BitConverter.ToUInt16(packet.Payload, i * 2 + 2);
                }

                return(availablemeasurepoints);
            }
            catch (DisconnectedException e)
            {
                if (communication.Connect())
                {
                    return(GetAvailableMeasurePoints());
                }
                else
                {
                    throw e;
                }
            }
        }
        public Boolean deleteCalibrationDB()
        {
            byte[] data = new byte[1];

            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdDeleteCalDatabase, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);

            return(true);// Convert.ToBoolean(packet.Payload[0]);
        }
        public Boolean EraseLog()
        {
            byte[] data = new byte[1];

            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdEraseLogs, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);

            return(Convert.ToBoolean(packet.Payload[0]));
        }
        public Boolean UpgradeFirmware()
        {
            byte[] data = new byte[1];

            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdUpgradeFirmware, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);

            return(true);// Convert.ToBoolean(packet.Payload[0]);
        }
        public GetRtcResult getRTC()
        {
            byte[]    data    = new byte[0];
            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdGetRTC, data, sender, receiver);

            var          ret             = SendMessage(cmdPack);
            GetRtcResult changertcresult = new GetRtcResult(ret.Payload);

            return(changertcresult);
        }
        public Boolean RestartDevice()
        {
            byte[] data = new byte[1];


            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdGotoNormalMode, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);

            return(true);// Convert.ToBoolean(packet.Payload[0]);
        }
        private void CheckStatusByte(V45Packet Status)
        {
            if (Status.AlarmAvailable)
            {
                RaiseAlarmAvailable();
            }

            //CheckEvents

            //CheckNack?
        }
 private void checkSenderandReceiver(V45Packet packet)
 {
     if (this.sender == 0)
     {
         this.sender = packet.Receiver;
     }
     if (this.receiver == 0)
     {
         this.receiver = packet.Sender;
     }
 }
        public byte GetDeviceFWMode()
        {
            byte[] data = new byte[1];

            int offset = 0;
            //data.Add(ref offset, Convert.ToByte(V45Packet.V45SystemStateRequests.pwrSTART));


            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdGetDeviceFWMode, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);

            return(packet.Payload[0]);// Convert.ToBoolean(packet.Payload[0]);
        }
        public int GetMeasurePointValue(ushort id)
        {
            byte[] data = new byte[2];

            int offset = 0;

            data.Add(ref offset, BitConverter.GetBytes(id));

            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdGetMeasPointValue, data, this.sender, this.receiver);
            V45Packet ret     = SendMessage(cmdPack);

            return(BitConverter.ToInt32(ret.Payload, 2));
        }
        public bool StartUpload(String fileName, byte option)
        {
            bool rtn = false;

            V45Packet cmdResponse = HandleFileTransfer(fileName, option, null);

            if (cmdResponse.Status == 0x80)
            {
                rtn = true;
            }

            return(rtn);
        }
        public Boolean BottonPress(UInt16 buttonNumber)
        {
            byte[] data   = new byte[3];
            int    offset = 0;

            data.Add(ref offset, BitConverter.GetBytes(buttonNumber));
            data.Add(ref offset, 1);//Pressed

            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdButtonPress, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);

            return(Convert.ToBoolean(packet.Payload[0]));
        }
        public Boolean SetAlarm(byte alarm, byte active)
        {
            byte[] data   = new byte[3];
            int    offset = 0;

            data.Add(ref offset, alarm);
            data.Add(ref offset, active);//Pressed

            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdSetAlarm, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);

            return(Convert.ToBoolean(packet.Payload[0]));
        }
        public Boolean StopTreatment()
        {
            byte[] data = new byte[1];

            int offset = 0;

            data.Add(ref offset, Convert.ToByte(V45Packet.V45SystemStateRequests.pwrSTOP));


            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdSetSystemState, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);

            return(Convert.ToBoolean(packet.Payload[0]));
        }
        public bool SetEncryptionKey(byte[] key)
        {
            byte[] data = new byte[key.Length + sizeof(byte)];

            int offset = 0;

            data.Add(ref offset, Convert.ToByte(key.Length));
            data.Add(ref offset, key);

            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdSetEncryptionKey, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);

            return(packet.Payload.Length == data.Length);
        }
        public Boolean StopTempCompensation()
        {
            byte[] data = new byte[1];

            int offset = 0;

            data.Add(ref offset, Convert.ToByte(3));


            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdHandleTempCompensation, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);

            return(Convert.ToBoolean(packet.Payload[0]));
        }
        public bool VerifyChallengeString(byte[] challengeString)
        {
            byte[] data = new byte[challengeString.Length + sizeof(UInt16)];

            int offset = 0;

            data.Add(ref offset, BitConverter.GetBytes((UInt16)challengeString.Length));
            data.Add(ref offset, challengeString);

            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdVerifyChallengeString, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);

            return(Convert.ToBoolean(packet.Payload[0]));
        }
        public byte[] GetChallengeString()
        {
            byte[] data = new byte[0];

            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdGetChallengeString, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);

            UInt16 count = BitConverter.ToUInt16(packet.Payload, 0);

            byte[] challengeString = new byte[count];
            Array.Copy(packet.Payload, sizeof(UInt16), challengeString, 0, challengeString.Length);

            return(challengeString);
        }
        public int GetRTC(byte target)
        {
            byte[] data = new byte[0];

            int offset = 0;

            data.Add(ref offset, target);


            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdGetRTC, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);

            return(packet.Payload[0]);
        }
        public ushort GetSettingValue(byte profile, byte type, ushort settingId)
        {
            byte[] data = new byte[4];

            int offset = 0;

            data.Add(ref offset, profile);
            data.Add(ref offset, BitConverter.GetBytes(settingId));
            data.Add(ref offset, type);
            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdGetSetting, data, sender, receiver);
            var       ret     = SendMessage(cmdPack);

            return(BitConverter.ToUInt16(ret.Payload, 4));
        }
        public bool StartLogDownload(Vivo45LogPacket.Vivo45LogLevel level, DateTime startTimestamp, DateTime endTimestamp, bool storeSync)
        {
            bool rtn = false;

            V45Packet cmdResponse = HandleLogData(Vivo45LogPacket.Vivo45LogAction.sendLog, level, startTimestamp, endTimestamp, storeSync);

            if ((Vivo45LogPacket.Vivo45LogAction)cmdResponse.Payload[0] == Vivo45LogPacket.Vivo45LogAction.sendLog &&
                (Vivo45LogPacket.Vivo45LogLevel)cmdResponse.Payload[1] == level)
            {
                rtn = true;
            }

            return(rtn);
        }
        public string GetDeviceInfoValue(UInt16 id)
        {
            byte[] data = new byte[2];

            int offset = 0;

            data.Add(ref offset, BitConverter.GetBytes(id));

            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdGetDeviceValue, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);
            var       rtnId   = BitConverter.ToUInt16(packet.Payload, 0);
            var       str     = System.Text.Encoding.UTF8.GetString(packet.Payload, 2, packet.Payload.Skip(2).TakeWhile(s => s != 0).ToArray().Length);

            return(str);
        }
        public Vivo45DeviceInfo GetDeviceInfoInformation(UInt16 id)
        {
            byte[] data = new byte[2];

            int offset = 0;

            data.Add(ref offset, BitConverter.GetBytes(id));

            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdGetDeviceInfo, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);

            Vivo45DeviceInfo v45i = new Vivo45DeviceInfo(packet.Payload);

            return(v45i);
        }
        public bool CopyProfile(byte sourceProfile, byte destinationProfile)
        {
            byte[] data = new byte[2];

            int offset = 0;

            data.Add(ref offset, sourceProfile);
            data.Add(ref offset, destinationProfile);

            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdCopyProfile, data, sender, receiver);

            var ret = SendMessage(cmdPack);


            return(Convert.ToBoolean(ret.Payload[0]));
        }
        public bool ChangeProfileName(byte ProfileId, string profileName)
        {
            byte[] data = new byte[13];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = 0;
            }
            int offset = 0;

            data.Add(ref offset, ProfileId);
            data.Add(ref offset, System.Text.Encoding.UTF8.GetBytes(profileName));
            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdSetProfileName, data, sender, receiver);
            var       ret     = SendMessage(cmdPack);

            return(Convert.ToBoolean(ret.Payload[0]));
        }
        public string SendWifiCommand(string cmd)
        {
            byte[] data = new byte[System.Text.Encoding.UTF8.GetByteCount(cmd) + 1]; //+1 for null termination

            int offset = 0;

            data.Add(ref offset, System.Text.Encoding.UTF8.GetBytes(cmd));
            data.Add(ref offset, 0); //null termination



            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdHandleWifi, data, sender, receiver);
            var       packet  = SendMessage(cmdPack);

            return(System.Text.Encoding.UTF8.GetString(packet.Payload, 0, packet.PayloadLength));
        }
        public int SetMeasurePointValue(ushort id, uint value)
        {
            byte[] data = new byte[6];

            int offset = 0;

            data.Add(ref offset, BitConverter.GetBytes(id));
            data.Add(ref offset, BitConverter.GetBytes(value));

            V45Packet cmdPack = new V45Packet(V45Packet.V45Commands.cmdSetMeasPointValue, data, sender, receiver);

            V45Packet ret = SendMessage(cmdPack);

            //return BitConverter.ToInt32(ret.Payload, 2);
            return(1);
        }