|  |  |  | 
|---|
|  |  |  | package com.genersoft.iot.vmp.storager.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.text.SimpleDateFormat; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.genersoft.iot.vmp.common.StreamInfo; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.bean.*; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.dto.StreamPushItem; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.storager.IRedisCatchStorage; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description:视频设备数据存储-jdbc实现 | 
|---|
|  |  |  | * @description:视频设备数据存储-jdbc实现 | 
|---|
|  |  |  | * @author: swwheihei | 
|---|
|  |  |  | * @date:   2020年5月6日 下午2:31:42 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private VideoStreamSessionManager streamSession; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private MediaServerMapper mediaServerMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 根据设备ID判断设备是否存在 | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public synchronized boolean updateDevice(Device device) { | 
|---|
|  |  |  | String now = this.format.format(System.currentTimeMillis()); | 
|---|
|  |  |  | device.setUpdateTime(now); | 
|---|
|  |  |  | Device deviceByDeviceId = deviceMapper.getDeviceByDeviceId(device.getDeviceId()); | 
|---|
|  |  |  | if (deviceByDeviceId == null) { | 
|---|
|  |  |  | device.setCreateTime(now); | 
|---|
|  |  |  | return deviceMapper.add(device) > 0; | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | return deviceMapper.update(device) > 0; | 
|---|
|  |  |  | 
|---|
|  |  |  | String channelId = channel.getChannelId(); | 
|---|
|  |  |  | channel.setDeviceId(deviceId); | 
|---|
|  |  |  | channel.setStreamId(streamSession.getStreamId(deviceId, channel.getChannelId())); | 
|---|
|  |  |  | String now = this.format.format(System.currentTimeMillis()); | 
|---|
|  |  |  | channel.setUpdateTime(now); | 
|---|
|  |  |  | DeviceChannel deviceChannel = deviceChannelMapper.queryChannel(deviceId, channelId); | 
|---|
|  |  |  | if (deviceChannel == null) { | 
|---|
|  |  |  | channel.setCreateTime(now); | 
|---|
|  |  |  | deviceChannelMapper.add(channel); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | deviceChannelMapper.update(channel); | 
|---|
|  |  |  | 
|---|
|  |  |  | if (device == null) return false; | 
|---|
|  |  |  | device.setOnline(0); | 
|---|
|  |  |  | return deviceMapper.update(device) > 0; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 更新所有设备离线 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @return true:更新成功  false:更新失败 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public synchronized boolean outlineForAll() { | 
|---|
|  |  |  | logger.info("更新所有设备离线"); | 
|---|
|  |  |  | int result = deviceMapper.outlineForAll(); | 
|---|
|  |  |  | return result > 0; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | boolean result = false; | 
|---|
|  |  |  | streamProxyItem.setStreamType("proxy"); | 
|---|
|  |  |  | streamProxyItem.setStatus(true); | 
|---|
|  |  |  | String now = this.format.format(System.currentTimeMillis()); | 
|---|
|  |  |  | streamProxyItem.setCreateTime(now); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (gbStreamMapper.add(streamProxyItem)<0 || streamProxyMapper.add(streamProxyItem) < 0) { | 
|---|
|  |  |  | //事务回滚 | 
|---|
|  |  |  | 
|---|
|  |  |  | result = true; | 
|---|
|  |  |  | dataSourceTransactionManager.commit(transactionStatus);     //手动提交 | 
|---|
|  |  |  | }catch (Exception e) { | 
|---|
|  |  |  | logger.error("向数据库添加流代理失败:", e); | 
|---|
|  |  |  | dataSourceTransactionManager.rollback(transactionStatus); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | 
|---|
|  |  |  | gbStreamMapper.setStatus(app, streamId, false); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void updateParentPlatformStatus(String platformGbID, boolean online) { | 
|---|
|  |  |  | platformMapper.updateParentPlatformStatus(platformGbID, online); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void updateMediaServer(MediaServerItem mediaServerItem) { | 
|---|
|  |  |  | String now = this.format.format(System.currentTimeMillis()); | 
|---|
|  |  |  | mediaServerItem.setUpdateTime(now); | 
|---|
|  |  |  | if (mediaServerMapper.queryOne(mediaServerItem.getId()) != null) { | 
|---|
|  |  |  | mediaServerMapper.update(mediaServerItem); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | mediaServerItem.setCreateTime(now); | 
|---|
|  |  |  | mediaServerMapper.add(mediaServerItem); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<StreamProxyItem> getStreamProxyListForEnableInMediaServer(String id, boolean enable) { | 
|---|
|  |  |  | return streamProxyMapper.selectForEnableInMediaServer(id, enable); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Device queryVideoDeviceByChannelId(String channelId) { | 
|---|
|  |  |  | Device result = null; | 
|---|
|  |  |  | List<DeviceChannel> channelList = deviceChannelMapper.queryChannelByChannelId(channelId); | 
|---|
|  |  |  | if (channelList.size() == 1) { | 
|---|
|  |  |  | result = deviceMapper.getDeviceByDeviceId(channelList.get(0).getDeviceId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|