hwh
2024-09-10 eff7a7ff6ee1851094dfe1f94dd40a98535865d8
设备通讯
3个文件已修改
53 ■■■■ 已修改文件
Admin.NET/WCS.Application/Hub/PlcDeviceHub.cs 20 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Web/src/views/device/deviceMonitor/index.vue 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Web/src/views/device/deviceMonitor/signalR.ts 16 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Admin.NET/WCS.Application/Hub/PlcDeviceHub.cs
@@ -88,7 +88,8 @@
                        {
                            throw new OperationCanceledException();
                        }
                        var modPlc = listPlc.Where(s => s.Id == modDevice.Id).FirstOrDefault();
                        var modPlc = listPlc.Where(s => s.Id == modDevice.PlcId).FirstOrDefault();
                        modDevice.Type = modPlc.Type;
                        //读取plc的值
                        var modConn = PLCTaskAction.listPlcConn.FirstOrDefault(s => s != null && s.PlcId == modDevice.PlcId);
                        if (modConn == null)
@@ -155,6 +156,15 @@
                                modDevice.PickStorey = Convert.ToInt32(pickStorey);
                            }
                        }
                        //else
                        //{
                        //    //测试
                        //    modDevice.TaskNo = "TK00001";
                        //    modDevice.TaskType = TaskTypeEnum.In;
                        //    modDevice.Wcs = new Random().Next(1000).ToString();
                        //    modDevice.Plc = new Random().Next(1000).ToString();
                        //    modDevice.Status = true;
                        //}
                        // 比较之前的状态
                        if (initialStates.TryGetValue(modDevice.Id, out var initialState))
                        {
@@ -178,17 +188,17 @@
                                _plcDeviceHubContext.Clients.All.PublicPlcDevice(modDevice);
                            }
                        }
                        initialStates[modDevice.Id] = modDevice.Adapt<WcsDeviceOutput>();
                    }
                    Thread.Sleep(2000);
                }
                catch (OperationCanceledException)
                {
                    break;
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }, cts.Token);
Web/src/views/device/deviceMonitor/index.vue
@@ -114,13 +114,13 @@
</template>
<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { ref, onMounted  } from 'vue';
import { listWcsDevice, writeValue } from '/@/api/wcs/wcsDevice';
import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
import { listWcsPlc } from '/@/api/wcs/wcsPlc';
import { ElMessageBox, ElMessage } from 'element-plus';
import setting from '/@/views/device/deviceMonitor/component/setting.vue'
import { signalR } from './signalR';
import { signalR,stopConnection } from './signalR';
//连接signalR 监听变更
onMounted(async () => {
    signalR.off('PublicPlcDevice');
@@ -133,12 +133,15 @@
            }
        }
        else if (data.type == 1) {
            stations.value.forEach(list => {
                var index = list.findIndex(s => s.id == data.id);
            lineOptions.value.forEach(s => {
                var index = stations.value[s.id].findIndex(s => s.id == data.id);
                if (index !== -1) {
                    list.splice(index, 1, data);
                    stations.value[s.id].splice(index, 1, data);
                }
            });
            if (stationValue.value.id == data.id) {
                stationValue.value = data;
            }
        }
    });
});
@@ -169,10 +172,10 @@
}
// 查询操作
const handleQuery = async () => {
    var listplc = await listWcsPlc({ type: 1 });
    const listplc = await listWcsPlc({ type: 1 });
    lineOptions.value = listplc.data.result;
    lineValue.value = listplc.data.result[0].id;
    var res = await listWcsDevice();
    const res = await listWcsDevice();
    listStackingMachineData.value = res.data.result.filter(s => s.type == 0);
    const listConveyorLineData = res.data.result.filter(s => s.type == 1);
    listplc.data.result.forEach(s => {
Web/src/views/device/deviceMonitor/signalR.ts
@@ -17,21 +17,33 @@
// 启动连接
connection.start().then(() => {
    debugger;
    console.log('启动连接plc');
});
// 断开连接
connection.onclose(async () => {
    debugger;
    console.log('断开连接plc');
});
// 重连中
connection.onreconnecting(() => {
    debugger;
    console.log('服务器已断线plc');
});
// 重连成功
connection.onreconnected(() => {
    debugger;
    console.log('重连成功plc');
});
// 关闭连接的方法
async function stopConnection() {
    try {
      await connection.stop();
      console.log('连接已关闭');
    } catch (error) {
      console.error('关闭连接时发生错误:', error);
    }
  }
// connection.on('PublicPlcConn', () => {});
export { connection as signalR };
export { connection as signalR,stopConnection };