Ejemplo n.º 1
0
        public async void ChangeDTempCommand()
        {
            Views.Busy.SetBusy(true, _BusyText);

            string speed    = SlaveModel.GetInstance().Speed;
            double dtemp    = SliderValueDTemp;
            double ctemp    = SlaveModel.GetInstance().CTemp;
            string workmode = SlaveModel.GetInstance().WorkMode;


            string message = JsonOperation.GetInstance().PraseJson(2, "req", dtemp, ctemp, speed, workmode);
            //向服务器请求链接
            await TcpClient.GetInstance().SendToTcpServer(message);

            //等待Ack
            while (true)
            {
                string JsonString = await TcpClient.GetInstance().WaitTcpServer();

                if (JsonOperation.GetInstance().DeserializeJson("ack", JsonString))
                {
                    SlaveModel.GetInstance().Cost += double.Parse(JsonOperation.GetInstance().DeserializeJson(JsonString));
                    SlaveModel.GetInstance().DTemp = SliderValueDTemp;
                    break;
                }
            }
            Views.Busy.SetBusy(false);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Updates ViewModel with received slaveModel data
 /// </summary>
 private void UpdateViewModel(SlaveModel slaveModel)
 {
     uint mask = 0x01;
     for (int i = 0; i < DigitalIoCollection.Count; i++)
     {
         DigitalIoCollection[i].State = Convert.ToBoolean((slaveModel.DigitalValue>>i) & mask);
     }
     AnalogValue = slaveModel.AnalogValue;
     SlaveId = slaveModel.SlaveId;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Processes message with slave data
 /// </summary>
 /// <param name="message">Slave model</param>
 private void HandleSlaveModelMessage(SlaveDataMessage message)
 {
     _inputSlaveModel = message.SlaveModel;
     UpdateViewModel(message.SlaveModel);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Creates new slave model with user data
 /// </summary>
 private SlaveModel CreateSlaveModel()
 {
     uint digitalValue = 0x00;
     SlaveModel slaveModel = new SlaveModel();
     for (int i = 0; i < DigitalIoCollection.Count; i++)
     {
         digitalValue = digitalValue | (Convert.ToUInt32(DigitalIoCollection[i].State)<<i);
     }
     slaveModel.DigitalValue = (ushort)digitalValue;
     slaveModel.AnalogValue =(short)Convert.ToInt32(AnalogValue);
     slaveModel.SlaveId = SlaveId;
     return slaveModel;
 }
Ejemplo n.º 5
0
        public static void UpdateDuLieuSlave(string tenNhaMay, string tenThietBi, string oldNameSlave, SlaveModel newSlave)
        {
            var path = GetPathJson.getPathConfig("DeviceAndData.json");
            Dictionary <string, NhaMayModel> dicNhaMay = GetDicNhaMay();

            ThietBiModel thietBi = dicNhaMay[tenNhaMay].dsThietBi[tenThietBi];

            if (thietBi.dsSlave.ContainsKey(oldNameSlave))
            {
                thietBi.dsSlave.Remove(oldNameSlave);
                thietBi.dsSlave.Add(newSlave.Name, newSlave);
            }

            string jsonString = (new JavaScriptSerializer()).Serialize((object)dicNhaMay);

            File.WriteAllText(path, jsonString);
        }
        public static void CreateTimer(int d)
        {
            //定时器
            TimeSpan delay = TimeSpan.FromSeconds(5);

            DelayTimer = ThreadPoolTimer.CreateTimer(
                (source) =>
            {
                Debug.WriteLine("启动定时器任务");
                //从控机进入待机状态
                SlaveModel.GetInstance().IsWorking = false;
                if (asyncSelfChange == null)
                {
                    asyncSelfChange = ThreadPool.RunAsync(
                        async(selfChange) =>
                    {
                        while (true)
                        {
                            //如果工作项还没有开始就调用了 workItem.Cancel 方法,工作项
                            //就会被终止。工作项如果开始工作了,那么它就会一直运行到结束,除非它
                            //支持“取消”操作。
                            //如果让工作项支持“取消”操作,这个工作项应该检查 IAsyncAction.Status
                            //是否为取消操作,并且当取消的时候执行退出操作
                            Debug.WriteLine("selfChange Status:" + selfChange.Status);
                            Debug.WriteLine("asyncSelfChange Status:" + asyncSelfChange.Status);

                            if (SlaveModel.GetInstance().IsWorking == false)
                            {
                                if (SlaveModel.GetInstance().WorkMode == "Cooling")
                                {
                                    if (SlaveModel.GetInstance().CTemp + 0.5 > SlaveModel.GetInstance().InitDTemp)
                                    {
                                        SlaveModel.GetInstance().CTemp = SlaveModel.GetInstance().InitDTemp;
                                    }
                                    else
                                    {
                                        SlaveModel.GetInstance().CTemp += 0.5;
                                    }
                                }
                                else
                                {
                                    if (SlaveModel.GetInstance().CTemp - 0.5 < 18)
                                    {
                                        SlaveModel.GetInstance().CTemp = 18.0;
                                    }
                                    else
                                    {
                                        SlaveModel.GetInstance().CTemp -= 0.5;
                                    }
                                }
                            }
                            await Task.Delay(d);
                        }
                    });
                }
            }, delay,
                (source) =>
            {
                Debug.WriteLine("定时器完成");
            });
        }
        public static void CreatePeriodicRequest(int d)
        {
            //workItem.Status == AsyncStatus.Canceled
            asyncRequest = ThreadPool.RunAsync(
                async(workItem) =>
            {
                while (true)
                {
                    //延迟时间
                    await Task.Delay(d);

                    Debug.WriteLine("进入正常工作线程");
                    double ctemp    = SlaveModel.GetInstance().CTemp;
                    double dtemp    = SlaveModel.GetInstance().DTemp;
                    string workMode = SlaveModel.GetInstance().WorkMode;
                    string speed    = SlaveModel.GetInstance().Speed;



                    if (workMode == "Cooling")
                    {
                        ctemp -= 1.0;
                        if (ctemp < dtemp)
                        {
                            ctemp = dtemp;
                        }
                    }
                    else
                    {
                        ctemp += 1.0;
                        if (ctemp > dtemp)
                        {
                            ctemp = dtemp;
                        }
                    }
                    string message = JsonOperation.GetInstance().PraseJson(2, "req", dtemp, ctemp, speed, workMode);
                    await TcpClient.GetInstance().SendToTcpServer(message);

                    //启动5秒定时器
                    CreateTimer(5000);
                    //等待Ack
                    string JsonString = await TcpClient.GetInstance().WaitTcpServer();
                    Debug.WriteLine(JsonOperation.GetInstance().DeserializeJson("ack", JsonString));
                    //如果收到的消息是ack
                    if (JsonOperation.GetInstance().DeserializeJson("ack", JsonString))
                    {
                        //取消定时器
                        DelayTimer.Cancel();

                        //停止自己变化线程
                        if (asyncSelfChange != null)
                        {
                            asyncSelfChange.Cancel();
                            Debug.WriteLine("停止自己变化");
                        }

                        //累加消费金额
                        SlaveModel.GetInstance().Cost += double.Parse(JsonOperation.GetInstance().DeserializeJson(JsonString));
                        //如果当前正在工作,就更新温度,否则就设置为开始工作
                        if (SlaveModel.GetInstance().IsWorking == true)
                        {
                            SlaveModel.GetInstance().CTemp = ctemp;
                            SlaveModel.GetInstance().DTemp = dtemp;
                        }
                        else
                        {
                            SlaveModel.GetInstance().IsWorking = true;
                        }
                    }
                }
            });
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="frame"></param>
        private void UpdateSlave(ProtocolFrame frame)
        {
            int slaveId = frame.Id;
            if (!Slaves.ContainsKey(slaveId))
                Slaves[slaveId] = new SlaveModel() { SlaveId = slaveId };

            var cmd = new string(frame.Command);
                switch(cmd)
                {
                    case "SA":
                        Slaves[slaveId].AnalogValue = BitConverter.ToInt16(frame.Data,0);
                        break;
                    case "SD":
                        Slaves[slaveId].DigitalValue = BitConverter.ToUInt16(frame.Data,0);
                        break;
                    case "AS":
                    case "ID":
                        Slaves[slaveId].AnalogValue = BitConverter.ToInt16(frame.Data, 0);
                        Slaves[slaveId].DigitalValue = BitConverter.ToUInt16(frame.Data, frame.DataLength / 2);
                        break;
                }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="slave"></param>
        /// <param name="command"></param>
        /// <returns></returns>
        private ProtocolFrame ConvertSlaveModelToProtocolFrame(SlaveModel slave, string command )
        {
            ProtocolFrame frame = null;

            switch (command)
            {
                case "SA":
                    frame = new ProtocolFrame(slave.SlaveId, command.ToCharArray(), BitConverter.GetBytes(slave.AnalogValue));
                    break;
                case "SD":
                    frame = new ProtocolFrame(slave.SlaveId, command.ToCharArray(), BitConverter.GetBytes(slave.DigitalValue));
                    break;
                case "AS":
                    var concatedArray = BitConverter.GetBytes(slave.AnalogValue).Concat(BitConverter.GetBytes(slave.DigitalValue)).ToArray();
                    frame = new ProtocolFrame(slave.SlaveId, command.ToCharArray(), concatedArray);
                    break;
                case "ID":
                    frame = new ProtocolFrame(slave.SlaveId, command.ToCharArray(), null);
                    break;
            }
            return frame;
        }